public function createRevision()
 {
     $rev = new WikiPageRevision();
     $rev->user_id = Yii::$app->user->id;
     $rev->revision = time();
     $lastRevision = WikiPageRevision::find()->where(array('is_latest' => 1, 'wiki_page_id' => $this->id))->one();
     if ($lastRevision !== null) {
         $rev->content = $lastRevision->content;
     }
     if (!$this->isNewRecord) {
         $rev->wiki_page_id = $this->id;
     }
     return $rev;
 }
 public function up()
 {
     $this->renameClass('WikiPage', WikiPage::className());
     $this->renameClass('WikiPageRevision', WikiPageRevision::className());
 }
 public function actionRevert()
 {
     $this->forcePostRequest();
     $id = (int) Yii::$app->request->get('id');
     $toRevision = (int) Yii::$app->request->get('toRevision');
     $page = WikiPage::find()->contentContainer($this->contentContainer)->readable()->where(['wiki_page.id' => $id])->one();
     if ($page === null) {
         throw new HttpException(404, 'Page not found!');
     }
     if ($page->admin_only && !$page->canAdminister()) {
         throw new HttpException(403, 'Page not editable!');
     }
     $revision = WikiPageRevision::findOne(array('revision' => $toRevision, 'wiki_page_id' => $page->id));
     if ($revision->is_latest) {
         throw new HttpException(404, 'Already latest revision!');
     }
     $revertedRevision = $page->createRevision();
     $revertedRevision->content = $revision->content;
     $revertedRevision->save();
     return $this->redirect($this->contentContainer->createUrl('view', array('title' => $page->title)));
 }
 /**
  * @inheritdoc
  */
 public function afterSave($insert, $changedAttributes)
 {
     WikiPageRevision::updateAll(['is_latest' => 0], 'wiki_page_id=:wikiPageId AND id!=:selfId', [':wikiPageId' => $this->wiki_page_id, ':selfId' => $this->id]);
     return parent::afterSave($insert, $changedAttributes);
 }