public function createRevision()
 {
     $rev = new WikiPageRevision();
     $rev->user_id = Yii::app()->user->id;
     $rev->revision = time();
     $lastRevision = WikiPageRevision::model()->findByAttributes(array('is_latest' => 1, 'wiki_page_id' => $this->id));
     if ($lastRevision !== null) {
         $rev->content = $lastRevision->content;
     }
     if (!$this->isNewRecord) {
         $rev->wiki_page_id = $this->id;
     }
     return $rev;
 }
 /**
  * Displays difference between two page revisions
  *
  * @param string $uid unique wiki id
  * @param string $rev1 page revision
  * @param string $rev2 page revision
  */
 public function actionDiff($uid, $rev1, $rev2)
 {
     $r1 = WikiPageRevision::model()->findByPk($rev1);
     if (!$r1) {
         throw new CHttpException(404);
     }
     $r2 = WikiPageRevision::model()->findByPk($rev2);
     if (!$r2) {
         throw new CHttpException(404);
     }
     $this->render('diff', array('diff' => TextDiff::compare($r1->content, $r2->content), 'r1' => $r1, 'r2' => $r2, 'uid' => $uid));
 }
 public function afterSave()
 {
     WikiPageRevision::model()->updateAll(array('is_latest' => 0), 'wiki_page_id=:wikiPageId AND id!=:selfId', array(':wikiPageId' => $this->wiki_page_id, ':selfId' => $this->id));
     return parent::afterSave();
 }
 public function actionRevert()
 {
     $this->forcePostRequest();
     $id = (int) Yii::app()->request->getQuery('id');
     $toRevision = (int) Yii::app()->request->getQuery('toRevision');
     $page = WikiPage::model()->contentContainer($this->contentContainer)->findByPk($id);
     if ($page === null) {
         throw new CHttpException(404, 'Page not found!');
     }
     if ($page->admin_only && !$page->canAdminister()) {
         throw new CHttpException(403, 'Page not editable!');
     }
     $revision = WikiPageRevision::model()->findByAttributes(array('revision' => $toRevision, 'wiki_page_id' => $page->id));
     if ($revision->is_latest) {
         throw new CHttpException(404, 'Already latest revision!');
     }
     $revertedRevision = $page->createRevision();
     $revertedRevision->content = $revision->content;
     $revertedRevision->save();
     $this->redirect($this->createContainerUrl('view', array('title' => $page->title)));
 }