MediaWiki  REL1_19
ApiWatch.php
Go to the documentation of this file.
00001 <?php
00032 class ApiWatch extends ApiBase {
00033 
00034         public function __construct( $main, $action ) {
00035                 parent::__construct( $main, $action );
00036         }
00037 
00038         public function execute() {
00039                 $user = $this->getUser();
00040                 if ( !$user->isLoggedIn() ) {
00041                         $this->dieUsage( 'You must be logged-in to have a watchlist', 'notloggedin' );
00042                 }
00043 
00044                 $params = $this->extractRequestParams();
00045                 $title = Title::newFromText( $params['title'] );
00046 
00047                 if ( !$title || $title->getNamespace() < 0 ) {
00048                         $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
00049                 }
00050 
00051                 $res = array( 'title' => $title->getPrefixedText() );
00052 
00053                 if ( $params['unwatch'] ) {
00054                         $res['unwatched'] = '';
00055                         $res['message'] = $this->msg( 'removedwatchtext', $title->getPrefixedText() )->title( $title )->parseAsBlock();
00056                         $success = UnwatchAction::doUnwatch( $title, $user );
00057                 } else {
00058                         $res['watched'] = '';
00059                         $res['message'] = $this->msg( 'addedwatchtext', $title->getPrefixedText() )->title( $title )->parseAsBlock();
00060                         $success = WatchAction::doWatch( $title, $user );
00061                 }
00062                 if ( !$success ) {
00063                         $this->dieUsageMsg( 'hookaborted' );
00064                 }
00065                 $this->getResult()->addValue( null, $this->getModuleName(), $res );
00066         }
00067 
00068         public function mustBePosted() {
00069                 return true;
00070         }
00071 
00072         public function isWriteMode() {
00073                 return true;
00074         }
00075 
00076         public function needsToken() {
00077                 return true;
00078         }
00079 
00080         public function getTokenSalt() {
00081                 return 'watch';
00082         }
00083 
00084         public function getAllowedParams() {
00085                 return array(
00086                         'title' => array(
00087                                 ApiBase::PARAM_TYPE => 'string',
00088                                 ApiBase::PARAM_REQUIRED => true
00089                         ),
00090                         'unwatch' => false,
00091                         'token' => null,
00092                 );
00093         }
00094 
00095         public function getParamDescription() {
00096                 return array(
00097                         'title' => 'The page to (un)watch',
00098                         'unwatch' => 'If set the page will be unwatched rather than watched',
00099                         'token' => 'A token previously acquired via prop=info',
00100                 );
00101         }
00102 
00103         public function getDescription() {
00104                 return 'Add or remove a page from/to the current user\'s watchlist';
00105         }
00106 
00107         public function getPossibleErrors() {
00108                 return array_merge( parent::getPossibleErrors(), array(
00109                         array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
00110                         array( 'invalidtitle', 'title' ),
00111                         array( 'hookaborted' ),
00112                 ) );
00113         }
00114 
00115         public function getExamples() {
00116                 return array(
00117                         'api.php?action=watch&title=Main_Page' => 'Watch the page "Main Page"',
00118                         'api.php?action=watch&title=Main_Page&unwatch=' => 'Unwatch the page "Main Page"',
00119                 );
00120         }
00121 
00122         public function getHelpUrls() {
00123                 return 'https://www.mediawiki.org/wiki/API:Watch';
00124         }
00125 
00126         public function getVersion() {
00127                 return __CLASS__ . ': $Id$';
00128         }
00129 }