MediaWiki  REL1_24
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             throw new UserNotLoggedIn( 'watchlistanontext', 'watchnologin' );
00086         }
00087 
00088         parent::checkCanExecute( $user );
00089     }
00090 
00099     public static function doWatchOrUnwatch( $watch, Title $title, User $user ) {
00100         if ( $user->isLoggedIn() &&
00101             $user->isWatched( $title, WatchedItem::IGNORE_USER_RIGHTS ) != $watch
00102         ) {
00103             // If the user doesn't have 'editmywatchlist', we still want to
00104             // allow them to add but not remove items via edits and such.
00105             if ( $watch ) {
00106                 return self::doWatch( $title, $user, WatchedItem::IGNORE_USER_RIGHTS );
00107             } else {
00108                 return self::doUnwatch( $title, $user );
00109             }
00110         }
00111 
00112         return Status::newGood();
00113     }
00114 
00123     public static function doWatch( Title $title, User $user,
00124         $checkRights = WatchedItem::CHECK_USER_RIGHTS
00125     ) {
00126         if ( $checkRights !== WatchedItem::IGNORE_USER_RIGHTS &&
00127             !$user->isAllowed( 'editmywatchlist' )
00128         ) {
00129             return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
00130         }
00131 
00132         $page = WikiPage::factory( $title );
00133 
00134         $status = Status::newFatal( 'hookaborted' );
00135         if ( wfRunHooks( 'WatchArticle', array( &$user, &$page, &$status ) ) ) {
00136             $status = Status::newGood();
00137             $user->addWatch( $title, $checkRights );
00138             wfRunHooks( 'WatchArticleComplete', array( &$user, &$page ) );
00139         }
00140 
00141         return $status;
00142     }
00143 
00151     public static function doUnwatch( Title $title, User $user ) {
00152         if ( !$user->isAllowed( 'editmywatchlist' ) ) {
00153             return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
00154         }
00155 
00156         $page = WikiPage::factory( $title );
00157 
00158         $status = Status::newFatal( 'hookaborted' );
00159         if ( wfRunHooks( 'UnwatchArticle', array( &$user, &$page, &$status ) ) ) {
00160             $status = Status::newGood();
00161             $user->removeWatch( $title );
00162             wfRunHooks( 'UnwatchArticleComplete', array( &$user, &$page ) );
00163         }
00164 
00165         return $status;
00166     }
00167 
00177     public static function getWatchToken( Title $title, User $user, $action = 'watch' ) {
00178         if ( $action != 'unwatch' ) {
00179             $action = 'watch';
00180         }
00181         $salt = array( $action, $title->getPrefixedDBkey() );
00182 
00183         // This token stronger salted and not compatible with ApiWatch
00184         // It's title/action specific because index.php is GET and API is POST
00185         return $user->getEditToken( $salt );
00186     }
00187 
00197     public static function getUnwatchToken( Title $title, User $user, $action = 'unwatch' ) {
00198         return self::getWatchToken( $title, $user, $action );
00199     }
00200 
00201     protected function alterForm( HTMLForm $form ) {
00202         $form->setSubmitTextMsg( 'confirm-watch-button' );
00203     }
00204 
00205     protected function preText() {
00206         return $this->msg( 'confirm-watch-top' )->parse();
00207     }
00208 
00209     public function onSuccess() {
00210         $this->getOutput()->addWikiMsg( 'addedwatchtext', $this->getTitle()->getPrefixedText() );
00211     }
00212 }