MediaWiki
master
|
00001 <?php 00032 class ApiQueryDeletedrevs extends ApiQueryBase { 00033 00034 public function __construct( $query, $moduleName ) { 00035 parent::__construct( $query, $moduleName, 'dr' ); 00036 } 00037 00038 public function execute() { 00039 $user = $this->getUser(); 00040 // Before doing anything at all, let's check permissions 00041 if ( !$user->isAllowed( 'deletedhistory' ) ) { 00042 $this->dieUsage( 'You don\'t have permission to view deleted revision information', 'permissiondenied' ); 00043 } 00044 00045 $db = $this->getDB(); 00046 $params = $this->extractRequestParams( false ); 00047 $prop = array_flip( $params['prop'] ); 00048 $fld_parentid = isset( $prop['parentid'] ); 00049 $fld_revid = isset( $prop['revid'] ); 00050 $fld_user = isset( $prop['user'] ); 00051 $fld_userid = isset( $prop['userid'] ); 00052 $fld_comment = isset( $prop['comment'] ); 00053 $fld_parsedcomment = isset ( $prop['parsedcomment'] ); 00054 $fld_minor = isset( $prop['minor'] ); 00055 $fld_len = isset( $prop['len'] ); 00056 $fld_sha1 = isset( $prop['sha1'] ); 00057 $fld_content = isset( $prop['content'] ); 00058 $fld_token = isset( $prop['token'] ); 00059 00060 $result = $this->getResult(); 00061 $pageSet = $this->getPageSet(); 00062 $titles = $pageSet->getTitles(); 00063 00064 // This module operates in three modes: 00065 // 'revs': List deleted revs for certain titles (1) 00066 // 'user': List deleted revs by a certain user (2) 00067 // 'all': List all deleted revs in NS (3) 00068 $mode = 'all'; 00069 if ( count( $titles ) > 0 ) { 00070 $mode = 'revs'; 00071 } elseif ( !is_null( $params['user'] ) ) { 00072 $mode = 'user'; 00073 } 00074 00075 if ( $mode == 'revs' || $mode == 'user' ) { 00076 // Ignore namespace and unique due to inability to know whether they were purposely set 00077 foreach( array( 'from', 'to', 'prefix', /*'namespace',*/ 'continue', /*'unique'*/ ) as $p ) { 00078 if ( !is_null( $params[$p] ) ) { 00079 $this->dieUsage( "The '{$p}' parameter cannot be used in modes 1 or 2", 'badparams'); 00080 } 00081 } 00082 } else { 00083 foreach( array( 'start', 'end' ) as $p ) { 00084 if ( !is_null( $params[$p] ) ) { 00085 $this->dieUsage( "The {$p} parameter cannot be used in mode 3", 'badparams'); 00086 } 00087 } 00088 } 00089 00090 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) { 00091 $this->dieUsage( 'user and excludeuser cannot be used together', 'badparams' ); 00092 } 00093 00094 $this->addTables( 'archive' ); 00095 $this->addWhere( 'ar_deleted = 0' ); 00096 $this->addFields( array( 'ar_title', 'ar_namespace', 'ar_timestamp' ) ); 00097 00098 $this->addFieldsIf( 'ar_parent_id', $fld_parentid ); 00099 $this->addFieldsIf( 'ar_rev_id', $fld_revid ); 00100 $this->addFieldsIf( 'ar_user_text', $fld_user ); 00101 $this->addFieldsIf( 'ar_user', $fld_userid ); 00102 $this->addFieldsIf( 'ar_comment', $fld_comment || $fld_parsedcomment ); 00103 $this->addFieldsIf( 'ar_minor_edit', $fld_minor ); 00104 $this->addFieldsIf( 'ar_len', $fld_len ); 00105 $this->addFieldsIf( 'ar_sha1', $fld_sha1 ); 00106 00107 if ( $fld_content ) { 00108 $this->addTables( 'text' ); 00109 $this->addFields( array( 'ar_text', 'ar_text_id', 'old_text', 'old_flags' ) ); 00110 $this->addWhere( 'ar_text_id = old_id' ); 00111 00112 // This also means stricter restrictions 00113 if ( !$user->isAllowed( 'undelete' ) ) { 00114 $this->dieUsage( 'You don\'t have permission to view deleted revision content', 'permissiondenied' ); 00115 } 00116 } 00117 // Check limits 00118 $userMax = $fld_content ? ApiBase::LIMIT_SML1 : ApiBase::LIMIT_BIG1; 00119 $botMax = $fld_content ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_BIG2; 00120 00121 $limit = $params['limit']; 00122 00123 if ( $limit == 'max' ) { 00124 $limit = $this->getMain()->canApiHighLimits() ? $botMax : $userMax; 00125 $this->getResult()->setParsedLimit( $this->getModuleName(), $limit ); 00126 } 00127 00128 $this->validateLimit( 'limit', $limit, 1, $userMax, $botMax ); 00129 00130 if ( $fld_token ) { 00131 // Undelete tokens are identical for all pages, so we cache one here 00132 $token = $user->getEditToken( '', $this->getMain()->getRequest() ); 00133 } 00134 00135 $dir = $params['dir']; 00136 00137 // We need a custom WHERE clause that matches all titles. 00138 if ( $mode == 'revs' ) { 00139 $lb = new LinkBatch( $titles ); 00140 $where = $lb->constructSet( 'ar', $db ); 00141 $this->addWhere( $where ); 00142 } elseif ( $mode == 'all' ) { 00143 $this->addWhereFld( 'ar_namespace', $params['namespace'] ); 00144 00145 $from = is_null( $params['from'] ) ? null : $this->titleToKey( $params['from'] ); 00146 $to = is_null( $params['to'] ) ? null : $this->titleToKey( $params['to'] ); 00147 $this->addWhereRange( 'ar_title', $dir, $from, $to ); 00148 00149 if ( isset( $params['prefix'] ) ) { 00150 $this->addWhere( 'ar_title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) ); 00151 } 00152 } 00153 00154 if ( !is_null( $params['user'] ) ) { 00155 $this->addWhereFld( 'ar_user_text', $params['user'] ); 00156 } elseif ( !is_null( $params['excludeuser'] ) ) { 00157 $this->addWhere( 'ar_user_text != ' . 00158 $db->addQuotes( $params['excludeuser'] ) ); 00159 } 00160 00161 if ( !is_null( $params['continue'] ) && ( $mode == 'all' || $mode == 'revs' ) ) { 00162 $cont = explode( '|', $params['continue'] ); 00163 if ( count( $cont ) != 3 ) { 00164 $this->dieUsage( 'Invalid continue param. You should pass the original value returned by the previous query', 'badcontinue' ); 00165 } 00166 $ns = intval( $cont[0] ); 00167 $title = $db->addQuotes( $cont[1] ); 00168 $ts = $db->addQuotes( $db->timestamp( $cont[2] ) ); 00169 $op = ( $dir == 'newer' ? '>' : '<' ); 00170 $this->addWhere( "ar_namespace $op $ns OR " . 00171 "(ar_namespace = $ns AND " . 00172 "(ar_title $op $title OR " . 00173 "(ar_title = $title AND " . 00174 "ar_timestamp $op= $ts)))" ); 00175 } 00176 00177 $this->addOption( 'LIMIT', $limit + 1 ); 00178 $this->addOption( 'USE INDEX', array( 'archive' => ( $mode == 'user' ? 'usertext_timestamp' : 'name_title_timestamp' ) ) ); 00179 if ( $mode == 'all' ) { 00180 if ( $params['unique'] ) { 00181 $this->addOption( 'GROUP BY', 'ar_title' ); 00182 } else { 00183 $sort = ( $dir == 'newer' ? '' : ' DESC' ); 00184 $this->addOption( 'ORDER BY', array( 00185 'ar_title' . $sort, 00186 'ar_timestamp' . $sort 00187 )); 00188 } 00189 } else { 00190 if ( $mode == 'revs' ) { 00191 // Sort by ns and title in the same order as timestamp for efficiency 00192 $this->addWhereRange( 'ar_namespace', $dir, null, null ); 00193 $this->addWhereRange( 'ar_title', $dir, null, null ); 00194 } 00195 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] ); 00196 } 00197 $res = $this->select( __METHOD__ ); 00198 $pageMap = array(); // Maps ns&title to (fake) pageid 00199 $count = 0; 00200 $newPageID = 0; 00201 foreach ( $res as $row ) { 00202 if ( ++$count > $limit ) { 00203 // We've had enough 00204 if ( $mode == 'all' || $mode == 'revs' ) { 00205 $this->setContinueEnumParameter( 'continue', intval( $row->ar_namespace ) . '|' . 00206 $row->ar_title . '|' . $row->ar_timestamp ); 00207 } else { 00208 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ar_timestamp ) ); 00209 } 00210 break; 00211 } 00212 00213 $rev = array(); 00214 $rev['timestamp'] = wfTimestamp( TS_ISO_8601, $row->ar_timestamp ); 00215 if ( $fld_revid ) { 00216 $rev['revid'] = intval( $row->ar_rev_id ); 00217 } 00218 if ( $fld_parentid && !is_null( $row->ar_parent_id ) ) { 00219 $rev['parentid'] = intval( $row->ar_parent_id ); 00220 } 00221 if ( $fld_user ) { 00222 $rev['user'] = $row->ar_user_text; 00223 } 00224 if ( $fld_userid ) { 00225 $rev['userid'] = $row->ar_user; 00226 } 00227 if ( $fld_comment ) { 00228 $rev['comment'] = $row->ar_comment; 00229 } 00230 00231 $title = Title::makeTitle( $row->ar_namespace, $row->ar_title ); 00232 00233 if ( $fld_parsedcomment ) { 00234 $rev['parsedcomment'] = Linker::formatComment( $row->ar_comment, $title ); 00235 } 00236 if ( $fld_minor && $row->ar_minor_edit == 1 ) { 00237 $rev['minor'] = ''; 00238 } 00239 if ( $fld_len ) { 00240 $rev['len'] = $row->ar_len; 00241 } 00242 if ( $fld_sha1 ) { 00243 if ( $row->ar_sha1 != '' ) { 00244 $rev['sha1'] = wfBaseConvert( $row->ar_sha1, 36, 16, 40 ); 00245 } else { 00246 $rev['sha1'] = ''; 00247 } 00248 } 00249 if ( $fld_content ) { 00250 ApiResult::setContent( $rev, Revision::getRevisionText( $row ) ); 00251 } 00252 00253 if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) { 00254 $pageID = $newPageID++; 00255 $pageMap[$row->ar_namespace][$row->ar_title] = $pageID; 00256 $a['revisions'] = array( $rev ); 00257 $result->setIndexedTagName( $a['revisions'], 'rev' ); 00258 ApiQueryBase::addTitleInfo( $a, $title ); 00259 if ( $fld_token ) { 00260 $a['token'] = $token; 00261 } 00262 $fit = $result->addValue( array( 'query', $this->getModuleName() ), $pageID, $a ); 00263 } else { 00264 $pageID = $pageMap[$row->ar_namespace][$row->ar_title]; 00265 $fit = $result->addValue( 00266 array( 'query', $this->getModuleName(), $pageID, 'revisions' ), 00267 null, $rev ); 00268 } 00269 if ( !$fit ) { 00270 if ( $mode == 'all' || $mode == 'revs' ) { 00271 $this->setContinueEnumParameter( 'continue', intval( $row->ar_namespace ) . '|' . 00272 $row->ar_title . '|' . $row->ar_timestamp ); 00273 } else { 00274 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ar_timestamp ) ); 00275 } 00276 break; 00277 } 00278 } 00279 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'page' ); 00280 } 00281 00282 public function getAllowedParams() { 00283 return array( 00284 'start' => array( 00285 ApiBase::PARAM_TYPE => 'timestamp' 00286 ), 00287 'end' => array( 00288 ApiBase::PARAM_TYPE => 'timestamp', 00289 ), 00290 'dir' => array( 00291 ApiBase::PARAM_TYPE => array( 00292 'newer', 00293 'older' 00294 ), 00295 ApiBase::PARAM_DFLT => 'older' 00296 ), 00297 'from' => null, 00298 'to' => null, 00299 'prefix' => null, 00300 'continue' => null, 00301 'unique' => false, 00302 'user' => array( 00303 ApiBase::PARAM_TYPE => 'user' 00304 ), 00305 'excludeuser' => array( 00306 ApiBase::PARAM_TYPE => 'user' 00307 ), 00308 'namespace' => array( 00309 ApiBase::PARAM_TYPE => 'namespace', 00310 ApiBase::PARAM_DFLT => 0, 00311 ), 00312 'limit' => array( 00313 ApiBase::PARAM_DFLT => 10, 00314 ApiBase::PARAM_TYPE => 'limit', 00315 ApiBase::PARAM_MIN => 1, 00316 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1, 00317 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2 00318 ), 00319 'prop' => array( 00320 ApiBase::PARAM_DFLT => 'user|comment', 00321 ApiBase::PARAM_TYPE => array( 00322 'revid', 00323 'parentid', 00324 'user', 00325 'userid', 00326 'comment', 00327 'parsedcomment', 00328 'minor', 00329 'len', 00330 'sha1', 00331 'content', 00332 'token' 00333 ), 00334 ApiBase::PARAM_ISMULTI => true 00335 ), 00336 ); 00337 } 00338 00339 public function getParamDescription() { 00340 return array( 00341 'start' => 'The timestamp to start enumerating from (1, 2)', 00342 'end' => 'The timestamp to stop enumerating at (1, 2)', 00343 'dir' => $this->getDirectionDescription( $this->getModulePrefix(), ' (1, 3)' ), 00344 'from' => 'Start listing at this title (3)', 00345 'to' => 'Stop listing at this title (3)', 00346 'prefix' => 'Search for all page titles that begin with this value (3)', 00347 'limit' => 'The maximum amount of revisions to list', 00348 'prop' => array( 00349 'Which properties to get', 00350 ' revid - Adds the revision ID of the deleted revision', 00351 ' parentid - Adds the revision ID of the previous revision to the page', 00352 ' user - Adds the user who made the revision', 00353 ' userid - Adds the user ID whom made the revision', 00354 ' comment - Adds the comment of the revision', 00355 ' parsedcomment - Adds the parsed comment of the revision', 00356 ' minor - Tags if the revision is minor', 00357 ' len - Adds the length (bytes) of the revision', 00358 ' sha1 - Adds the SHA-1 (base 16) of the revision', 00359 ' content - Adds the content of the revision', 00360 ' token - Gives the edit token', 00361 ), 00362 'namespace' => 'Only list pages in this namespace (3)', 00363 'user' => 'Only list revisions by this user', 00364 'excludeuser' => 'Don\'t list revisions by this user', 00365 'continue' => 'When more results are available, use this to continue (3)', 00366 'unique' => 'List only one revision for each page (3)', 00367 ); 00368 } 00369 00370 public function getResultProperties() { 00371 return array( 00372 '' => array( 00373 'ns' => 'namespace', 00374 'title' => 'string' 00375 ), 00376 'token' => array( 00377 'token' => 'string' 00378 ) 00379 ); 00380 } 00381 00382 public function getDescription() { 00383 $p = $this->getModulePrefix(); 00384 return array( 00385 'List deleted revisions.', 00386 'Operates in three modes:', 00387 ' 1) List deleted revisions for the given title(s), sorted by timestamp', 00388 ' 2) List deleted contributions for the given user, sorted by timestamp (no titles specified)', 00389 " 3) List all deleted revisions in the given namespace, sorted by title and timestamp (no titles specified, {$p}user not set)", 00390 'Certain parameters only apply to some modes and are ignored in others.', 00391 'For instance, a parameter marked (1) only applies to mode 1 and is ignored in modes 2 and 3', 00392 ); 00393 } 00394 00395 public function getPossibleErrors() { 00396 return array_merge( parent::getPossibleErrors(), array( 00397 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted revision information' ), 00398 array( 'code' => 'badparams', 'info' => 'user and excludeuser cannot be used together' ), 00399 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted revision content' ), 00400 array( 'code' => 'badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ), 00401 array( 'code' => 'badparams', 'info' => "The 'from' parameter cannot be used in modes 1 or 2" ), 00402 array( 'code' => 'badparams', 'info' => "The 'to' parameter cannot be used in modes 1 or 2" ), 00403 array( 'code' => 'badparams', 'info' => "The 'prefix' parameter cannot be used in modes 1 or 2" ), 00404 array( 'code' => 'badparams', 'info' => "The 'continue' parameter cannot be used in modes 1 or 2" ), 00405 array( 'code' => 'badparams', 'info' => "The 'start' parameter cannot be used in mode 3" ), 00406 array( 'code' => 'badparams', 'info' => "The 'end' parameter cannot be used in mode 3" ), 00407 ) ); 00408 } 00409 00410 public function getExamples() { 00411 return array( 00412 'api.php?action=query&list=deletedrevs&titles=Main%20Page|Talk:Main%20Page&drprop=user|comment|content' 00413 => 'List the last deleted revisions of Main Page and Talk:Main Page, with content (mode 1)', 00414 'api.php?action=query&list=deletedrevs&druser=Bob&drlimit=50' 00415 => 'List the last 50 deleted contributions by Bob (mode 2)', 00416 'api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50' 00417 => 'List the first 50 deleted revisions in the main namespace (mode 3)', 00418 'api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50&drnamespace=1&drunique=' 00419 => 'List the first 50 deleted pages in the Talk namespace (mode 3):', 00420 ); 00421 } 00422 00423 public function getHelpUrls() { 00424 return 'https://www.mediawiki.org/wiki/API:Deletedrevs'; 00425 } 00426 00427 public function getVersion() { 00428 return __CLASS__ . ': $Id$'; 00429 } 00430 }