MediaWiki  REL1_21
ApiWatch.php
Go to the documentation of this file.
00001 <?php
00032 class ApiWatch extends ApiBase {
00033 
00034         public function execute() {
00035                 $user = $this->getUser();
00036                 if ( !$user->isLoggedIn() ) {
00037                         $this->dieUsage( 'You must be logged-in to have a watchlist', 'notloggedin' );
00038                 }
00039 
00040                 $params = $this->extractRequestParams();
00041                 $title = Title::newFromText( $params['title'] );
00042 
00043                 if ( !$title || $title->isExternal() || !$title->canExist() ) {
00044                         $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
00045                 }
00046 
00047                 $res = array( 'title' => $title->getPrefixedText() );
00048 
00049                 // Currently unnecessary, code to act as a safeguard against any change in current behavior of uselang
00050                 // Copy from ApiParse
00051                 $oldLang = null;
00052                 if ( isset( $params['uselang'] ) && $params['uselang'] != $this->getContext()->getLanguage()->getCode() ) {
00053                         $oldLang = $this->getContext()->getLanguage(); // Backup language
00054                         $this->getContext()->setLanguage( Language::factory( $params['uselang'] ) );
00055                 }
00056 
00057                 if ( $params['unwatch'] ) {
00058                         $res['unwatched'] = '';
00059                         $res['message'] = $this->msg( 'removedwatchtext', $title->getPrefixedText() )->title( $title )->parseAsBlock();
00060                         $success = UnwatchAction::doUnwatch( $title, $user );
00061                 } else {
00062                         $res['watched'] = '';
00063                         $res['message'] = $this->msg( 'addedwatchtext', $title->getPrefixedText() )->title( $title )->parseAsBlock();
00064                         $success = WatchAction::doWatch( $title, $user );
00065                 }
00066 
00067                 if ( !is_null( $oldLang ) ) {
00068                         $this->getContext()->setLanguage( $oldLang ); // Reset language to $oldLang
00069                 }
00070 
00071                 if ( !$success ) {
00072                         $this->dieUsageMsg( 'hookaborted' );
00073                 }
00074                 $this->getResult()->addValue( null, $this->getModuleName(), $res );
00075         }
00076 
00077         public function mustBePosted() {
00078                 return true;
00079         }
00080 
00081         public function isWriteMode() {
00082                 return true;
00083         }
00084 
00085         public function needsToken() {
00086                 return true;
00087         }
00088 
00089         public function getTokenSalt() {
00090                 return 'watch';
00091         }
00092 
00093         public function getAllowedParams() {
00094                 return array(
00095                         'title' => array(
00096                                 ApiBase::PARAM_TYPE => 'string',
00097                                 ApiBase::PARAM_REQUIRED => true
00098                         ),
00099                         'unwatch' => false,
00100                         'uselang' => null,
00101                         'token' => array(
00102                                 ApiBase::PARAM_TYPE => 'string',
00103                                 ApiBase::PARAM_REQUIRED => true
00104                         ),
00105                 );
00106         }
00107 
00108         public function getParamDescription() {
00109                 return array(
00110                         'title' => 'The page to (un)watch',
00111                         'unwatch' => 'If set the page will be unwatched rather than watched',
00112                         'uselang' => 'Language to show the message in',
00113                         'token' => 'A token previously acquired via prop=info',
00114                 );
00115         }
00116 
00117         public function getResultProperties() {
00118                 return array(
00119                         '' => array(
00120                                 'title' => 'string',
00121                                 'unwatched' => 'boolean',
00122                                 'watched' => 'boolean',
00123                                 'message' => 'string'
00124                         )
00125                 );
00126         }
00127 
00128         public function getDescription() {
00129                 return 'Add or remove a page from/to the current user\'s watchlist';
00130         }
00131 
00132         public function getPossibleErrors() {
00133                 return array_merge( parent::getPossibleErrors(), array(
00134                         array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
00135                         array( 'invalidtitle', 'title' ),
00136                         array( 'hookaborted' ),
00137                 ) );
00138         }
00139 
00140         public function getExamples() {
00141                 return array(
00142                         'api.php?action=watch&title=Main_Page' => 'Watch the page "Main Page"',
00143                         'api.php?action=watch&title=Main_Page&unwatch=' => 'Unwatch the page "Main Page"',
00144                 );
00145         }
00146 
00147         public function getHelpUrls() {
00148                 return 'https://www.mediawiki.org/wiki/API:Watch';
00149         }
00150 }