MediaWiki  REL1_21
WatchAction.php
Go to the documentation of this file.
00001 <?php
00028 class WatchAction extends FormAction {
00029 
00030         public function getName() {
00031                 return 'watch';
00032         }
00033 
00034         public function requiresUnblock() {
00035                 return false;
00036         }
00037 
00038         protected function getDescription() {
00039                 return $this->msg( 'addwatch' )->escaped();
00040         }
00041 
00046         protected function getFormFields() {
00047                 return array();
00048         }
00049 
00050         public function onSubmit( $data ) {
00051                 wfProfileIn( __METHOD__ );
00052                 self::doWatch( $this->getTitle(), $this->getUser() );
00053                 wfProfileOut( __METHOD__ );
00054                 return true;
00055         }
00056 
00060         public function show() {
00061                 $this->setHeaders();
00062 
00063                 $user = $this->getUser();
00064                 // This will throw exceptions if there's a problem
00065                 $this->checkCanExecute( $user );
00066 
00067                 // Must have valid token for this action/title
00068                 $salt = array( $this->getName(), $this->getTitle()->getDBkey() );
00069 
00070                 if ( $user->matchEditToken( $this->getRequest()->getVal( 'token' ), $salt ) ) {
00071                         $this->onSubmit( array() );
00072                         $this->onSuccess();
00073                 } else {
00074                         $form = $this->getForm();
00075                         if ( $form->show() ) {
00076                                 $this->onSuccess();
00077                         }
00078                 }
00079         }
00080 
00081         protected function checkCanExecute( User $user ) {
00082                 // Must be logged in
00083                 if ( $user->isAnon() ) {
00084                         throw new ErrorPageError( 'watchnologin', 'watchnologintext' );
00085                 }
00086 
00087                 return parent::checkCanExecute( $user );
00088         }
00089 
00090         public static function doWatch( Title $title, User $user  ) {
00091                 $page = WikiPage::factory( $title );
00092 
00093                 if ( wfRunHooks( 'WatchArticle', array( &$user, &$page ) ) ) {
00094                         $user->addWatch( $title );
00095                         wfRunHooks( 'WatchArticleComplete', array( &$user, &$page ) );
00096                 }
00097                 return true;
00098         }
00099 
00100         public static function doUnwatch( Title $title, User $user  ) {
00101                 $page = WikiPage::factory( $title );
00102 
00103                 if ( wfRunHooks( 'UnwatchArticle', array( &$user, &$page ) ) ) {
00104                         $user->removeWatch( $title );
00105                         wfRunHooks( 'UnwatchArticleComplete', array( &$user, &$page ) );
00106                 }
00107                 return true;
00108         }
00109 
00119         public static function getWatchToken( Title $title, User $user, $action = 'watch' ) {
00120                 if ( $action != 'unwatch' ) {
00121                         $action = 'watch';
00122                 }
00123                 $salt = array( $action, $title->getDBkey() );
00124 
00125                 // This token stronger salted and not compatible with ApiWatch
00126                 // It's title/action specific because index.php is GET and API is POST
00127                 return $user->getEditToken( $salt );
00128         }
00129 
00139         public static function getUnwatchToken( Title $title, User $user, $action = 'unwatch' ) {
00140                 return self::getWatchToken( $title, $user, $action );
00141         }
00142 
00143         protected function alterForm( HTMLForm $form ) {
00144                 $form->setSubmitTextMsg( 'confirm-watch-button' );
00145         }
00146 
00147         protected function preText() {
00148                 return $this->msg( 'confirm-watch-top' )->parse();
00149         }
00150 
00151         public function onSuccess() {
00152                 $this->getOutput()->addWikiMsg( 'addedwatchtext', $this->getTitle()->getPrefixedText() );
00153         }
00154 }
00155 
00161 class UnwatchAction extends WatchAction {
00162 
00163         public function getName() {
00164                 return 'unwatch';
00165         }
00166 
00167         protected function getDescription() {
00168                 return $this->msg( 'removewatch' )->escaped();
00169         }
00170 
00171         public function onSubmit( $data ) {
00172                 wfProfileIn( __METHOD__ );
00173                 self::doUnwatch( $this->getTitle(), $this->getUser() );
00174                 wfProfileOut( __METHOD__ );
00175                 return true;
00176         }
00177 
00178         protected function alterForm( HTMLForm $form ) {
00179                 $form->setSubmitTextMsg( 'confirm-unwatch-button' );
00180         }
00181 
00182         protected function preText() {
00183                 return $this->msg( 'confirm-unwatch-top' )->parse();
00184         }
00185 
00186         public function onSuccess() {
00187                 $this->getOutput()->addWikiMsg( 'removedwatchtext', $this->getTitle()->getPrefixedText() );
00188         }
00189 }