MediaWiki  REL1_24
ApiWatch.php
Go to the documentation of this file.
00001 <?php
00032 class ApiWatch extends ApiBase {
00033     private $mPageSet = null;
00034 
00035     public function execute() {
00036         $user = $this->getUser();
00037         if ( !$user->isLoggedIn() ) {
00038             $this->dieUsage( 'You must be logged-in to have a watchlist', 'notloggedin' );
00039         }
00040 
00041         if ( !$user->isAllowed( 'editmywatchlist' ) ) {
00042             $this->dieUsage( 'You don\'t have permission to edit your watchlist', 'permissiondenied' );
00043         }
00044 
00045         $params = $this->extractRequestParams();
00046 
00047         $this->getResult()->beginContinuation( $params['continue'], array(), array() );
00048 
00049         $pageSet = $this->getPageSet();
00050         // by default we use pageset to extract the page to work on.
00051         // title is still supported for backward compatibility
00052         if ( !isset( $params['title'] ) ) {
00053             $pageSet->execute();
00054             $res = $pageSet->getInvalidTitlesAndRevisions( array(
00055                 'invalidTitles',
00056                 'special',
00057                 'missingIds',
00058                 'missingRevIds',
00059                 'interwikiTitles'
00060             ) );
00061 
00062             foreach ( $pageSet->getMissingTitles() as $title ) {
00063                 $r = $this->watchTitle( $title, $user, $params );
00064                 $r['missing'] = 1;
00065                 $res[] = $r;
00066             }
00067 
00068             foreach ( $pageSet->getGoodTitles() as $title ) {
00069                 $r = $this->watchTitle( $title, $user, $params );
00070                 $res[] = $r;
00071             }
00072             $this->getResult()->setIndexedTagName( $res, 'w' );
00073         } else {
00074             // dont allow use of old title parameter with new pageset parameters.
00075             $extraParams = array_keys( array_filter( $pageSet->extractRequestParams(), function ( $x ) {
00076                 return $x !== null && $x !== false;
00077             } ) );
00078 
00079             if ( $extraParams ) {
00080                 $p = $this->getModulePrefix();
00081                 $this->dieUsage(
00082                     "The parameter {$p}title can not be used with " . implode( ", ", $extraParams ),
00083                     'invalidparammix'
00084                 );
00085             }
00086 
00087             $this->logFeatureUsage( 'action=watch&title' );
00088             $title = Title::newFromText( $params['title'] );
00089             if ( !$title || !$title->isWatchable() ) {
00090                 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
00091             }
00092             $res = $this->watchTitle( $title, $user, $params, true );
00093         }
00094         $this->getResult()->addValue( null, $this->getModuleName(), $res );
00095         $this->getResult()->endContinuation();
00096     }
00097 
00098     private function watchTitle( Title $title, User $user, array $params,
00099         $compatibilityMode = false
00100     ) {
00101         if ( !$title->isWatchable() ) {
00102             return array( 'title' => $title->getPrefixedText(), 'watchable' => 0 );
00103         }
00104 
00105         $res = array( 'title' => $title->getPrefixedText() );
00106 
00107         // Currently unnecessary, code to act as a safeguard against any change
00108         // in current behavior of uselang.
00109         // Copy from ApiParse
00110         $oldLang = null;
00111         if ( isset( $params['uselang'] ) &&
00112             $params['uselang'] != $this->getContext()->getLanguage()->getCode()
00113         ) {
00114             $oldLang = $this->getContext()->getLanguage(); // Backup language
00115             $this->getContext()->setLanguage( Language::factory( $params['uselang'] ) );
00116         }
00117 
00118         if ( $params['unwatch'] ) {
00119             $status = UnwatchAction::doUnwatch( $title, $user );
00120             if ( $status->isOK() ) {
00121                 $res['unwatched'] = '';
00122                 $res['message'] = $this->msg( 'removedwatchtext', $title->getPrefixedText() )
00123                     ->title( $title )->parseAsBlock();
00124             }
00125         } else {
00126             $status = WatchAction::doWatch( $title, $user );
00127             if ( $status->isOK() ) {
00128                 $res['watched'] = '';
00129                 $res['message'] = $this->msg( 'addedwatchtext', $title->getPrefixedText() )
00130                     ->title( $title )->parseAsBlock();
00131             }
00132         }
00133 
00134         if ( !is_null( $oldLang ) ) {
00135             $this->getContext()->setLanguage( $oldLang ); // Reset language to $oldLang
00136         }
00137 
00138         if ( !$status->isOK() ) {
00139             if ( $compatibilityMode ) {
00140                 $this->dieStatus( $status );
00141             }
00142             $res['error'] = $this->getErrorFromStatus( $status );
00143         }
00144 
00145         return $res;
00146     }
00147 
00152     private function getPageSet() {
00153         if ( $this->mPageSet === null ) {
00154             $this->mPageSet = new ApiPageSet( $this );
00155         }
00156 
00157         return $this->mPageSet;
00158     }
00159 
00160     public function mustBePosted() {
00161         return true;
00162     }
00163 
00164     public function isWriteMode() {
00165         return true;
00166     }
00167 
00168     public function needsToken() {
00169         return 'watch';
00170     }
00171 
00172     public function getAllowedParams( $flags = 0 ) {
00173         $result = array(
00174             'title' => array(
00175                 ApiBase::PARAM_TYPE => 'string',
00176                 ApiBase::PARAM_DEPRECATED => true
00177             ),
00178             'unwatch' => false,
00179             'uselang' => null,
00180             'continue' => '',
00181         );
00182         if ( $flags ) {
00183             $result += $this->getPageSet()->getFinalParams( $flags );
00184         }
00185 
00186         return $result;
00187     }
00188 
00189     public function getParamDescription() {
00190         $psModule = $this->getPageSet();
00191 
00192         return $psModule->getParamDescription() + array(
00193             'title' => 'The page to (un)watch. use titles instead',
00194             'unwatch' => 'If set the page will be unwatched rather than watched',
00195             'uselang' => 'Language to show the message in',
00196             'continue' => 'When more results are available, use this to continue',
00197         );
00198     }
00199 
00200     public function getDescription() {
00201         return 'Add or remove pages from/to the current user\'s watchlist.';
00202     }
00203 
00204     public function getExamples() {
00205         return array(
00206             'api.php?action=watch&titles=Main_Page' => 'Watch the page "Main Page"',
00207             'api.php?action=watch&titles=Main_Page&unwatch=' => 'Unwatch the page "Main Page"',
00208         );
00209     }
00210 
00211     public function getHelpUrls() {
00212         return 'https://www.mediawiki.org/wiki/API:Watch';
00213     }
00214 }