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