MediaWiki  REL1_23
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 
00055         return true;
00056     }
00057 
00061     public function show() {
00062         $this->setHeaders();
00063 
00064         $user = $this->getUser();
00065         // This will throw exceptions if there's a problem
00066         $this->checkCanExecute( $user );
00067 
00068         // Must have valid token for this action/title
00069         $salt = array( $this->getName(), $this->getTitle()->getDBkey() );
00070 
00071         if ( $user->matchEditToken( $this->getRequest()->getVal( 'token' ), $salt ) ) {
00072             $this->onSubmit( array() );
00073             $this->onSuccess();
00074         } else {
00075             $form = $this->getForm();
00076             if ( $form->show() ) {
00077                 $this->onSuccess();
00078             }
00079         }
00080     }
00081 
00082     protected function checkCanExecute( User $user ) {
00083         // Must be logged in
00084         if ( $user->isAnon() ) {
00085             $loginreqlink = Linker::linkKnown(
00086                 SpecialPage::getTitleFor( 'Userlogin' ),
00087                 $this->msg( 'loginreqlink' )->escaped(),
00088                 array(),
00089                 array( 'returnto' => $this->getPageTitle(), 'returntoquery' => 'action=' . $this->getName() )
00090             );
00091             $reasonMsg = $this->msg( 'watchlistanontext' )->rawParams( $loginreqlink );
00092             throw new UserNotLoggedIn( $reasonMsg, 'watchnologin' );
00093         }
00094 
00095         return parent::checkCanExecute( $user );
00096     }
00097 
00106     public static function doWatchOrUnwatch( $watch, Title $title, User $user ) {
00107         if ( $user->isLoggedIn() &&
00108             $user->isWatched( $title, WatchedItem::IGNORE_USER_RIGHTS ) != $watch
00109         ) {
00110             // If the user doesn't have 'editmywatchlist', we still want to
00111             // allow them to add but not remove items via edits and such.
00112             if ( $watch ) {
00113                 return self::doWatch( $title, $user, WatchedItem::IGNORE_USER_RIGHTS );
00114             } else {
00115                 return self::doUnwatch( $title, $user );
00116             }
00117         }
00118 
00119         return Status::newGood();
00120     }
00121 
00130     public static function doWatch( Title $title, User $user,
00131         $checkRights = WatchedItem::CHECK_USER_RIGHTS
00132     ) {
00133         if ( $checkRights !== WatchedItem::IGNORE_USER_RIGHTS &&
00134             !$user->isAllowed( 'editmywatchlist' )
00135         ) {
00136             return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
00137         }
00138 
00139         $page = WikiPage::factory( $title );
00140 
00141         $status = Status::newFatal( 'hookaborted' );
00142         if ( wfRunHooks( 'WatchArticle', array( &$user, &$page, &$status ) ) ) {
00143             $status = Status::newGood();
00144             $user->addWatch( $title, $checkRights );
00145             wfRunHooks( 'WatchArticleComplete', array( &$user, &$page ) );
00146         }
00147 
00148         return $status;
00149     }
00150 
00158     public static function doUnwatch( Title $title, User $user ) {
00159         if ( !$user->isAllowed( 'editmywatchlist' ) ) {
00160             return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
00161         }
00162 
00163         $page = WikiPage::factory( $title );
00164 
00165         $status = Status::newFatal( 'hookaborted' );
00166         if ( wfRunHooks( 'UnwatchArticle', array( &$user, &$page, &$status ) ) ) {
00167             $status = Status::newGood();
00168             $user->removeWatch( $title );
00169             wfRunHooks( 'UnwatchArticleComplete', array( &$user, &$page ) );
00170         }
00171 
00172         return $status;
00173     }
00174 
00184     public static function getWatchToken( Title $title, User $user, $action = 'watch' ) {
00185         if ( $action != 'unwatch' ) {
00186             $action = 'watch';
00187         }
00188         $salt = array( $action, $title->getDBkey() );
00189 
00190         // This token stronger salted and not compatible with ApiWatch
00191         // It's title/action specific because index.php is GET and API is POST
00192         return $user->getEditToken( $salt );
00193     }
00194 
00204     public static function getUnwatchToken( Title $title, User $user, $action = 'unwatch' ) {
00205         return self::getWatchToken( $title, $user, $action );
00206     }
00207 
00208     protected function alterForm( HTMLForm $form ) {
00209         $form->setSubmitTextMsg( 'confirm-watch-button' );
00210     }
00211 
00212     protected function preText() {
00213         return $this->msg( 'confirm-watch-top' )->parse();
00214     }
00215 
00216     public function onSuccess() {
00217         $this->getOutput()->addWikiMsg( 'addedwatchtext', $this->getTitle()->getPrefixedText() );
00218     }
00219 }
00220 
00226 class UnwatchAction extends WatchAction {
00227 
00228     public function getName() {
00229         return 'unwatch';
00230     }
00231 
00232     protected function getDescription() {
00233         return $this->msg( 'removewatch' )->escaped();
00234     }
00235 
00236     public function onSubmit( $data ) {
00237         wfProfileIn( __METHOD__ );
00238         self::doUnwatch( $this->getTitle(), $this->getUser() );
00239         wfProfileOut( __METHOD__ );
00240 
00241         return true;
00242     }
00243 
00244     protected function alterForm( HTMLForm $form ) {
00245         $form->setSubmitTextMsg( 'confirm-unwatch-button' );
00246     }
00247 
00248     protected function preText() {
00249         return $this->msg( 'confirm-unwatch-top' )->parse();
00250     }
00251 
00252     public function onSuccess() {
00253         $this->getOutput()->addWikiMsg( 'removedwatchtext', $this->getTitle()->getPrefixedText() );
00254     }
00255 }