MediaWiki  REL1_22
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 
00098     public static function doWatchOrUnwatch( $watch, Title $title, User $user ) {
00099         if ( $user->isLoggedIn() && $user->isWatched( $title, WatchedItem::IGNORE_USER_RIGHTS ) != $watch ) {
00100             // If the user doesn't have 'editmywatchlist', we still want to
00101             // allow them to add but not remove items via edits and such.
00102             if ( $watch ) {
00103                 return self::doWatch( $title, $user, WatchedItem::IGNORE_USER_RIGHTS );
00104             } else {
00105                 return self::doUnwatch( $title, $user );
00106             }
00107         }
00108         return Status::newGood();
00109     }
00110 
00119     public static function doWatch( Title $title, User $user, $checkRights = WatchedItem::CHECK_USER_RIGHTS ) {
00120         if ( $checkRights !== WatchedItem::IGNORE_USER_RIGHTS && !$user->isAllowed( 'editmywatchlist' ) ) {
00121             return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
00122         }
00123 
00124         $page = WikiPage::factory( $title );
00125 
00126         $status = Status::newFatal( 'hookaborted' );
00127         if ( wfRunHooks( 'WatchArticle', array( &$user, &$page, &$status ) ) ) {
00128             $status = Status::newGood();
00129             $user->addWatch( $title, $checkRights );
00130             wfRunHooks( 'WatchArticleComplete', array( &$user, &$page ) );
00131         }
00132         return $status;
00133     }
00134 
00142     public static function doUnwatch( Title $title, User $user ) {
00143         if ( !$user->isAllowed( 'editmywatchlist' ) ) {
00144             return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
00145         }
00146 
00147         $page = WikiPage::factory( $title );
00148 
00149         $status = Status::newFatal( 'hookaborted' );
00150         if ( wfRunHooks( 'UnwatchArticle', array( &$user, &$page, &$status ) ) ) {
00151             $status = Status::newGood();
00152             $user->removeWatch( $title );
00153             wfRunHooks( 'UnwatchArticleComplete', array( &$user, &$page ) );
00154         }
00155         return $status;
00156     }
00157 
00167     public static function getWatchToken( Title $title, User $user, $action = 'watch' ) {
00168         if ( $action != 'unwatch' ) {
00169             $action = 'watch';
00170         }
00171         $salt = array( $action, $title->getDBkey() );
00172 
00173         // This token stronger salted and not compatible with ApiWatch
00174         // It's title/action specific because index.php is GET and API is POST
00175         return $user->getEditToken( $salt );
00176     }
00177 
00187     public static function getUnwatchToken( Title $title, User $user, $action = 'unwatch' ) {
00188         return self::getWatchToken( $title, $user, $action );
00189     }
00190 
00191     protected function alterForm( HTMLForm $form ) {
00192         $form->setSubmitTextMsg( 'confirm-watch-button' );
00193     }
00194 
00195     protected function preText() {
00196         return $this->msg( 'confirm-watch-top' )->parse();
00197     }
00198 
00199     public function onSuccess() {
00200         $this->getOutput()->addWikiMsg( 'addedwatchtext', $this->getTitle()->getPrefixedText() );
00201     }
00202 }
00203 
00209 class UnwatchAction extends WatchAction {
00210 
00211     public function getName() {
00212         return 'unwatch';
00213     }
00214 
00215     protected function getDescription() {
00216         return $this->msg( 'removewatch' )->escaped();
00217     }
00218 
00219     public function onSubmit( $data ) {
00220         wfProfileIn( __METHOD__ );
00221         self::doUnwatch( $this->getTitle(), $this->getUser() );
00222         wfProfileOut( __METHOD__ );
00223         return true;
00224     }
00225 
00226     protected function alterForm( HTMLForm $form ) {
00227         $form->setSubmitTextMsg( 'confirm-unwatch-button' );
00228     }
00229 
00230     protected function preText() {
00231         return $this->msg( 'confirm-unwatch-top' )->parse();
00232     }
00233 
00234     public function onSuccess() {
00235         $this->getOutput()->addWikiMsg( 'removedwatchtext', $this->getTitle()->getPrefixedText() );
00236     }
00237 }