MediaWiki  REL1_19
WatchAction.php
Go to the documentation of this file.
00001 <?php
00023 class WatchAction extends FormAction {
00024 
00025         public function getName() {
00026                 return 'watch';
00027         }
00028 
00029         public function requiresUnblock() {
00030                 return false;
00031         }
00032 
00033         protected function getDescription() {
00034                 return wfMsgHtml( 'addwatch' );
00035         }
00036 
00041         protected function getFormFields() {
00042                 return array();
00043         }
00044 
00045         public function onSubmit( $data ) {
00046                 wfProfileIn( __METHOD__ );
00047                 self::doWatch( $this->getTitle(), $this->getUser() );
00048                 wfProfileOut( __METHOD__ );
00049                 return true;
00050         }
00051 
00055         public function show() {
00056                 $this->setHeaders();
00057 
00058                 $user = $this->getUser();
00059                 // This will throw exceptions if there's a problem
00060                 $this->checkCanExecute( $user );
00061 
00062                 // Must have valid token for this action/title
00063                 $salt = array( $this->getName(), $this->getTitle()->getDBkey() );
00064 
00065                 if ( $user->matchEditToken( $this->getRequest()->getVal( 'token' ), $salt ) ) {
00066                         $this->onSubmit( array() );
00067                         $this->onSuccess();
00068                 } else {
00069                         $form = $this->getForm();
00070                         if ( $form->show() ) {
00071                                 $this->onSuccess();
00072                         }
00073                 }
00074         }
00075 
00076         protected function checkCanExecute( User $user ) {
00077                 // Must be logged in
00078                 if ( $user->isAnon() ) {
00079                         throw new ErrorPageError( 'watchnologin', 'watchnologintext' );
00080                 }
00081 
00082                 return parent::checkCanExecute( $user );
00083         }
00084 
00085         public static function doWatch( Title $title, User $user  ) {
00086                 $page = WikiPage::factory( $title );
00087 
00088                 if ( wfRunHooks( 'WatchArticle', array( &$user, &$page ) ) ) {
00089                         $user->addWatch( $title );
00090                         wfRunHooks( 'WatchArticleComplete', array( &$user, &$page ) );
00091                 }
00092                 return true;
00093         }
00094 
00095         public static function doUnwatch( Title $title, User $user  ) {
00096                 $page = WikiPage::factory( $title );
00097 
00098                 if ( wfRunHooks( 'UnwatchArticle', array( &$user, &$page ) ) ) {
00099                         $user->removeWatch( $title );
00100                         wfRunHooks( 'UnwatchArticleComplete', array( &$user, &$page ) );
00101                 }
00102                 return true;
00103         }
00104 
00114         public static function getWatchToken( Title $title, User $user, $action = 'watch' ) {
00115                 if ( $action != 'unwatch' ) {
00116                         $action = 'watch';
00117                 }
00118                 $salt = array( $action, $title->getDBkey() );
00119 
00120                 // This token stronger salted and not compatible with ApiWatch
00121                 // It's title/action specific because index.php is GET and API is POST
00122                 return $user->getEditToken( $salt );
00123         }
00124 
00134         public static function getUnwatchToken( Title $title, User $user, $action = 'unwatch' ) {
00135                 return self::getWatchToken( $title, $user, $action );
00136         }
00137 
00138         protected function alterForm( HTMLForm $form ) {
00139                 $form->setSubmitText( wfMsg( 'confirm-watch-button' ) );
00140         }
00141 
00142         protected function preText() {
00143                 return wfMessage( 'confirm-watch-top' )->parse();
00144         }
00145 
00146         public function onSuccess() {
00147                 $this->getOutput()->addWikiMsg( 'addedwatchtext', $this->getTitle()->getPrefixedText() );
00148         }
00149 }
00150 
00151 class UnwatchAction extends WatchAction {
00152 
00153         public function getName() {
00154                 return 'unwatch';
00155         }
00156 
00157         protected function getDescription() {
00158                 return wfMsg( 'removewatch' );
00159         }
00160 
00161         public function onSubmit( $data ) {
00162                 wfProfileIn( __METHOD__ );
00163                 self::doUnwatch( $this->getTitle(), $this->getUser() );
00164                 wfProfileOut( __METHOD__ );
00165                 return true;
00166         }
00167 
00168         protected function alterForm( HTMLForm $form ) {
00169                 $form->setSubmitText( wfMsg( 'confirm-unwatch-button' ) );
00170         }
00171 
00172         protected function preText() {
00173                 return wfMessage( 'confirm-unwatch-top' )->parse();
00174         }
00175 
00176         public function onSuccess() {
00177                 $this->getOutput()->addWikiMsg( 'removedwatchtext', $this->getTitle()->getPrefixedText() );
00178         }
00179 }