[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/includes/actions/ -> WatchAction.php (source)

   1  <?php
   2  /**
   3   * Performs the watch actions on a page
   4   *
   5   * This program is free software; you can redistribute it and/or modify
   6   * it under the terms of the GNU General Public License as published by
   7   * the Free Software Foundation; either version 2 of the License, or
   8   * (at your option) any later version.
   9   *
  10   * This program is distributed in the hope that it will be useful,
  11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13   * GNU General Public License for more details.
  14   *
  15   * You should have received a copy of the GNU General Public License
  16   * along with this program; if not, write to the Free Software
  17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  18   *
  19   * @file
  20   * @ingroup Actions
  21   */
  22  
  23  /**
  24   * Page addition to a user's watchlist
  25   *
  26   * @ingroup Actions
  27   */
  28  class WatchAction extends FormAction {
  29  
  30  	public function getName() {
  31          return 'watch';
  32      }
  33  
  34  	public function requiresUnblock() {
  35          return false;
  36      }
  37  
  38  	protected function getDescription() {
  39          return $this->msg( 'addwatch' )->escaped();
  40      }
  41  
  42      /**
  43       * Just get an empty form with a single submit button
  44       * @return array
  45       */
  46  	protected function getFormFields() {
  47          return array();
  48      }
  49  
  50  	public function onSubmit( $data ) {
  51          wfProfileIn( __METHOD__ );
  52          self::doWatch( $this->getTitle(), $this->getUser() );
  53          wfProfileOut( __METHOD__ );
  54  
  55          return true;
  56      }
  57  
  58      /**
  59       * This can be either formed or formless depending on the session token given
  60       */
  61  	public function show() {
  62          $this->setHeaders();
  63  
  64          $user = $this->getUser();
  65          // This will throw exceptions if there's a problem
  66          $this->checkCanExecute( $user );
  67  
  68          // Must have valid token for this action/title
  69          $salt = array( $this->getName(), $this->getTitle()->getDBkey() );
  70  
  71          if ( $user->matchEditToken( $this->getRequest()->getVal( 'token' ), $salt ) ) {
  72              $this->onSubmit( array() );
  73              $this->onSuccess();
  74          } else {
  75              $form = $this->getForm();
  76              if ( $form->show() ) {
  77                  $this->onSuccess();
  78              }
  79          }
  80      }
  81  
  82  	protected function checkCanExecute( User $user ) {
  83          // Must be logged in
  84          if ( $user->isAnon() ) {
  85              throw new UserNotLoggedIn( 'watchlistanontext', 'watchnologin' );
  86          }
  87  
  88          parent::checkCanExecute( $user );
  89      }
  90  
  91      /**
  92       * Watch or unwatch a page
  93       * @since 1.22
  94       * @param bool $watch Whether to watch or unwatch the page
  95       * @param Title $title Page to watch/unwatch
  96       * @param User $user User who is watching/unwatching
  97       * @return Status
  98       */
  99  	public static function doWatchOrUnwatch( $watch, Title $title, User $user ) {
 100          if ( $user->isLoggedIn() &&
 101              $user->isWatched( $title, WatchedItem::IGNORE_USER_RIGHTS ) != $watch
 102          ) {
 103              // If the user doesn't have 'editmywatchlist', we still want to
 104              // allow them to add but not remove items via edits and such.
 105              if ( $watch ) {
 106                  return self::doWatch( $title, $user, WatchedItem::IGNORE_USER_RIGHTS );
 107              } else {
 108                  return self::doUnwatch( $title, $user );
 109              }
 110          }
 111  
 112          return Status::newGood();
 113      }
 114  
 115      /**
 116       * Watch a page
 117       * @since 1.22 Returns Status, $checkRights parameter added
 118       * @param Title $title Page to watch/unwatch
 119       * @param User $user User who is watching/unwatching
 120       * @param int $checkRights Passed through to $user->addWatch()
 121       * @return Status
 122       */
 123  	public static function doWatch( Title $title, User $user,
 124          $checkRights = WatchedItem::CHECK_USER_RIGHTS
 125      ) {
 126          if ( $checkRights !== WatchedItem::IGNORE_USER_RIGHTS &&
 127              !$user->isAllowed( 'editmywatchlist' )
 128          ) {
 129              return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
 130          }
 131  
 132          $page = WikiPage::factory( $title );
 133  
 134          $status = Status::newFatal( 'hookaborted' );
 135          if ( wfRunHooks( 'WatchArticle', array( &$user, &$page, &$status ) ) ) {
 136              $status = Status::newGood();
 137              $user->addWatch( $title, $checkRights );
 138              wfRunHooks( 'WatchArticleComplete', array( &$user, &$page ) );
 139          }
 140  
 141          return $status;
 142      }
 143  
 144      /**
 145       * Unwatch a page
 146       * @since 1.22 Returns Status
 147       * @param Title $title Page to watch/unwatch
 148       * @param User $user User who is watching/unwatching
 149       * @return Status
 150       */
 151  	public static function doUnwatch( Title $title, User $user ) {
 152          if ( !$user->isAllowed( 'editmywatchlist' ) ) {
 153              return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
 154          }
 155  
 156          $page = WikiPage::factory( $title );
 157  
 158          $status = Status::newFatal( 'hookaborted' );
 159          if ( wfRunHooks( 'UnwatchArticle', array( &$user, &$page, &$status ) ) ) {
 160              $status = Status::newGood();
 161              $user->removeWatch( $title );
 162              wfRunHooks( 'UnwatchArticleComplete', array( &$user, &$page ) );
 163          }
 164  
 165          return $status;
 166      }
 167  
 168      /**
 169       * Get token to watch (or unwatch) a page for a user
 170       *
 171       * @param Title $title Title object of page to watch
 172       * @param User $user User for whom the action is going to be performed
 173       * @param string $action Optionally override the action to 'unwatch'
 174       * @return string Token
 175       * @since 1.18
 176       */
 177  	public static function getWatchToken( Title $title, User $user, $action = 'watch' ) {
 178          if ( $action != 'unwatch' ) {
 179              $action = 'watch';
 180          }
 181          $salt = array( $action, $title->getPrefixedDBkey() );
 182  
 183          // This token stronger salted and not compatible with ApiWatch
 184          // It's title/action specific because index.php is GET and API is POST
 185          return $user->getEditToken( $salt );
 186      }
 187  
 188      /**
 189       * Get token to unwatch (or watch) a page for a user
 190       *
 191       * @param Title $title Title object of page to unwatch
 192       * @param User $user User for whom the action is going to be performed
 193       * @param string $action Optionally override the action to 'watch'
 194       * @return string Token
 195       * @since 1.18
 196       */
 197  	public static function getUnwatchToken( Title $title, User $user, $action = 'unwatch' ) {
 198          return self::getWatchToken( $title, $user, $action );
 199      }
 200  
 201  	protected function alterForm( HTMLForm $form ) {
 202          $form->setSubmitTextMsg( 'confirm-watch-button' );
 203      }
 204  
 205  	protected function preText() {
 206          return $this->msg( 'confirm-watch-top' )->parse();
 207      }
 208  
 209  	public function onSuccess() {
 210          $this->getOutput()->addWikiMsg( 'addedwatchtext', $this->getTitle()->getPrefixedText() );
 211      }
 212  }


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1