MediaWiki
REL1_23
|
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( 00043 'You don\'t have permission to view deleted revision information', 00044 'permissiondenied' 00045 ); 00046 } 00047 00048 $db = $this->getDB(); 00049 $params = $this->extractRequestParams( false ); 00050 $prop = array_flip( $params['prop'] ); 00051 $fld_parentid = isset( $prop['parentid'] ); 00052 $fld_revid = isset( $prop['revid'] ); 00053 $fld_user = isset( $prop['user'] ); 00054 $fld_userid = isset( $prop['userid'] ); 00055 $fld_comment = isset( $prop['comment'] ); 00056 $fld_parsedcomment = isset( $prop['parsedcomment'] ); 00057 $fld_minor = isset( $prop['minor'] ); 00058 $fld_len = isset( $prop['len'] ); 00059 $fld_sha1 = isset( $prop['sha1'] ); 00060 $fld_content = isset( $prop['content'] ); 00061 $fld_token = isset( $prop['token'] ); 00062 $fld_tags = isset( $prop['tags'] ); 00063 00064 // If we're in JSON callback mode, no tokens can be obtained 00065 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) { 00066 $fld_token = false; 00067 } 00068 00069 // If user can't undelete, no tokens 00070 if ( !$user->isAllowed( 'undelete' ) ) { 00071 $fld_token = false; 00072 } 00073 00074 $result = $this->getResult(); 00075 $pageSet = $this->getPageSet(); 00076 $titles = $pageSet->getTitles(); 00077 00078 // This module operates in three modes: 00079 // 'revs': List deleted revs for certain titles (1) 00080 // 'user': List deleted revs by a certain user (2) 00081 // 'all': List all deleted revs in NS (3) 00082 $mode = 'all'; 00083 if ( count( $titles ) > 0 ) { 00084 $mode = 'revs'; 00085 } elseif ( !is_null( $params['user'] ) ) { 00086 $mode = 'user'; 00087 } 00088 00089 if ( $mode == 'revs' || $mode == 'user' ) { 00090 // Ignore namespace and unique due to inability to know whether they were purposely set 00091 foreach ( array( 'from', 'to', 'prefix', /*'namespace', 'unique'*/ ) as $p ) { 00092 if ( !is_null( $params[$p] ) ) { 00093 $this->dieUsage( "The '{$p}' parameter cannot be used in modes 1 or 2", 'badparams' ); 00094 } 00095 } 00096 } else { 00097 foreach ( array( 'start', 'end' ) as $p ) { 00098 if ( !is_null( $params[$p] ) ) { 00099 $this->dieUsage( "The {$p} parameter cannot be used in mode 3", 'badparams' ); 00100 } 00101 } 00102 } 00103 00104 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) { 00105 $this->dieUsage( 'user and excludeuser cannot be used together', 'badparams' ); 00106 } 00107 00108 $this->addTables( 'archive' ); 00109 $this->addFields( array( 'ar_title', 'ar_namespace', 'ar_timestamp', 'ar_deleted', 'ar_id' ) ); 00110 00111 $this->addFieldsIf( 'ar_parent_id', $fld_parentid ); 00112 $this->addFieldsIf( 'ar_rev_id', $fld_revid ); 00113 $this->addFieldsIf( 'ar_user_text', $fld_user ); 00114 $this->addFieldsIf( 'ar_user', $fld_userid ); 00115 $this->addFieldsIf( 'ar_comment', $fld_comment || $fld_parsedcomment ); 00116 $this->addFieldsIf( 'ar_minor_edit', $fld_minor ); 00117 $this->addFieldsIf( 'ar_len', $fld_len ); 00118 $this->addFieldsIf( 'ar_sha1', $fld_sha1 ); 00119 00120 if ( $fld_tags ) { 00121 $this->addTables( 'tag_summary' ); 00122 $this->addJoinConds( 00123 array( 'tag_summary' => array( 'LEFT JOIN', array( 'ar_rev_id=ts_rev_id' ) ) ) 00124 ); 00125 $this->addFields( 'ts_tags' ); 00126 } 00127 00128 if ( !is_null( $params['tag'] ) ) { 00129 $this->addTables( 'change_tag' ); 00130 $this->addJoinConds( 00131 array( 'change_tag' => array( 'INNER JOIN', array( 'ar_rev_id=ct_rev_id' ) ) ) 00132 ); 00133 $this->addWhereFld( 'ct_tag', $params['tag'] ); 00134 } 00135 00136 if ( $fld_content ) { 00137 $this->addTables( 'text' ); 00138 $this->addJoinConds( 00139 array( 'text' => array( 'INNER JOIN', array( 'ar_text_id=old_id' ) ) ) 00140 ); 00141 $this->addFields( array( 'ar_text', 'ar_text_id', 'old_text', 'old_flags' ) ); 00142 00143 // This also means stricter restrictions 00144 if ( !$user->isAllowedAny( 'undelete', 'deletedtext' ) ) { 00145 $this->dieUsage( 00146 'You don\'t have permission to view deleted revision content', 00147 'permissiondenied' 00148 ); 00149 } 00150 } 00151 // Check limits 00152 $userMax = $fld_content ? ApiBase::LIMIT_SML1 : ApiBase::LIMIT_BIG1; 00153 $botMax = $fld_content ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_BIG2; 00154 00155 $limit = $params['limit']; 00156 00157 if ( $limit == 'max' ) { 00158 $limit = $this->getMain()->canApiHighLimits() ? $botMax : $userMax; 00159 $this->getResult()->setParsedLimit( $this->getModuleName(), $limit ); 00160 } 00161 00162 $this->validateLimit( 'limit', $limit, 1, $userMax, $botMax ); 00163 00164 if ( $fld_token ) { 00165 // Undelete tokens are identical for all pages, so we cache one here 00166 $token = $user->getEditToken( '', $this->getMain()->getRequest() ); 00167 } 00168 00169 $dir = $params['dir']; 00170 00171 // We need a custom WHERE clause that matches all titles. 00172 if ( $mode == 'revs' ) { 00173 $lb = new LinkBatch( $titles ); 00174 $where = $lb->constructSet( 'ar', $db ); 00175 $this->addWhere( $where ); 00176 } elseif ( $mode == 'all' ) { 00177 $this->addWhereFld( 'ar_namespace', $params['namespace'] ); 00178 00179 $from = $params['from'] === null 00180 ? null 00181 : $this->titlePartToKey( $params['from'], $params['namespace'] ); 00182 $to = $params['to'] === null 00183 ? null 00184 : $this->titlePartToKey( $params['to'], $params['namespace'] ); 00185 $this->addWhereRange( 'ar_title', $dir, $from, $to ); 00186 00187 if ( isset( $params['prefix'] ) ) { 00188 $this->addWhere( 'ar_title' . $db->buildLike( 00189 $this->titlePartToKey( $params['prefix'], $params['namespace'] ), 00190 $db->anyString() ) ); 00191 } 00192 } 00193 00194 if ( !is_null( $params['user'] ) ) { 00195 $this->addWhereFld( 'ar_user_text', $params['user'] ); 00196 } elseif ( !is_null( $params['excludeuser'] ) ) { 00197 $this->addWhere( 'ar_user_text != ' . 00198 $db->addQuotes( $params['excludeuser'] ) ); 00199 } 00200 00201 if ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ) { 00202 // Paranoia: avoid brute force searches (bug 17342) 00203 // (shouldn't be able to get here without 'deletedhistory', but 00204 // check it again just in case) 00205 if ( !$user->isAllowed( 'deletedhistory' ) ) { 00206 $bitmask = Revision::DELETED_USER; 00207 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) { 00208 $bitmask = Revision::DELETED_USER | Revision::DELETED_RESTRICTED; 00209 } else { 00210 $bitmask = 0; 00211 } 00212 if ( $bitmask ) { 00213 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" ); 00214 } 00215 } 00216 00217 if ( !is_null( $params['continue'] ) ) { 00218 $cont = explode( '|', $params['continue'] ); 00219 $op = ( $dir == 'newer' ? '>' : '<' ); 00220 if ( $mode == 'all' || $mode == 'revs' ) { 00221 $this->dieContinueUsageIf( count( $cont ) != 4 ); 00222 $ns = intval( $cont[0] ); 00223 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] ); 00224 $title = $db->addQuotes( $cont[1] ); 00225 $ts = $db->addQuotes( $db->timestamp( $cont[2] ) ); 00226 $ar_id = (int)$cont[3]; 00227 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] ); 00228 $this->addWhere( "ar_namespace $op $ns OR " . 00229 "(ar_namespace = $ns AND " . 00230 "(ar_title $op $title OR " . 00231 "(ar_title = $title AND " . 00232 "(ar_timestamp $op $ts OR " . 00233 "(ar_timestamp = $ts AND " . 00234 "ar_id $op= $ar_id)))))" ); 00235 } else { 00236 $this->dieContinueUsageIf( count( $cont ) != 2 ); 00237 $ts = $db->addQuotes( $db->timestamp( $cont[0] ) ); 00238 $ar_id = (int)$cont[1]; 00239 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] ); 00240 $this->addWhere( "ar_timestamp $op $ts OR " . 00241 "(ar_timestamp = $ts AND " . 00242 "ar_id $op= $ar_id)" ); 00243 } 00244 } 00245 00246 $this->addOption( 'LIMIT', $limit + 1 ); 00247 $this->addOption( 00248 'USE INDEX', 00249 array( 'archive' => ( $mode == 'user' ? 'usertext_timestamp' : 'name_title_timestamp' ) ) 00250 ); 00251 if ( $mode == 'all' ) { 00252 if ( $params['unique'] ) { 00253 // @todo Does this work on non-MySQL? 00254 $this->addOption( 'GROUP BY', 'ar_title' ); 00255 } else { 00256 $sort = ( $dir == 'newer' ? '' : ' DESC' ); 00257 $this->addOption( 'ORDER BY', array( 00258 'ar_title' . $sort, 00259 'ar_timestamp' . $sort, 00260 'ar_id' . $sort, 00261 ) ); 00262 } 00263 } else { 00264 if ( $mode == 'revs' ) { 00265 // Sort by ns and title in the same order as timestamp for efficiency 00266 $this->addWhereRange( 'ar_namespace', $dir, null, null ); 00267 $this->addWhereRange( 'ar_title', $dir, null, null ); 00268 } 00269 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] ); 00270 // Include in ORDER BY for uniqueness 00271 $this->addWhereRange( 'ar_id', $dir, null, null ); 00272 } 00273 $res = $this->select( __METHOD__ ); 00274 $pageMap = array(); // Maps ns&title to (fake) pageid 00275 $count = 0; 00276 $newPageID = 0; 00277 foreach ( $res as $row ) { 00278 if ( ++$count > $limit ) { 00279 // We've had enough 00280 if ( $mode == 'all' || $mode == 'revs' ) { 00281 $this->setContinueEnumParameter( 'continue', 00282 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id" 00283 ); 00284 } else { 00285 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" ); 00286 } 00287 break; 00288 } 00289 00290 $rev = array(); 00291 $anyHidden = false; 00292 00293 $rev['timestamp'] = wfTimestamp( TS_ISO_8601, $row->ar_timestamp ); 00294 if ( $fld_revid ) { 00295 $rev['revid'] = intval( $row->ar_rev_id ); 00296 } 00297 if ( $fld_parentid && !is_null( $row->ar_parent_id ) ) { 00298 $rev['parentid'] = intval( $row->ar_parent_id ); 00299 } 00300 if ( $fld_user || $fld_userid ) { 00301 if ( $row->ar_deleted & Revision::DELETED_USER ) { 00302 $rev['userhidden'] = ''; 00303 $anyHidden = true; 00304 } 00305 if ( Revision::userCanBitfield( $row->ar_deleted, Revision::DELETED_USER, $user ) ) { 00306 if ( $fld_user ) { 00307 $rev['user'] = $row->ar_user_text; 00308 } 00309 if ( $fld_userid ) { 00310 $rev['userid'] = $row->ar_user; 00311 } 00312 } 00313 } 00314 00315 if ( $fld_comment || $fld_parsedcomment ) { 00316 if ( $row->ar_deleted & Revision::DELETED_COMMENT ) { 00317 $rev['commenthidden'] = ''; 00318 $anyHidden = true; 00319 } 00320 if ( Revision::userCanBitfield( $row->ar_deleted, Revision::DELETED_COMMENT, $user ) ) { 00321 if ( $fld_comment ) { 00322 $rev['comment'] = $row->ar_comment; 00323 } 00324 if ( $fld_parsedcomment ) { 00325 $title = Title::makeTitle( $row->ar_namespace, $row->ar_title ); 00326 $rev['parsedcomment'] = Linker::formatComment( $row->ar_comment, $title ); 00327 } 00328 } 00329 } 00330 00331 if ( $fld_minor && $row->ar_minor_edit == 1 ) { 00332 $rev['minor'] = ''; 00333 } 00334 if ( $fld_len ) { 00335 $rev['len'] = $row->ar_len; 00336 } 00337 if ( $fld_sha1 ) { 00338 if ( $row->ar_deleted & Revision::DELETED_TEXT ) { 00339 $rev['sha1hidden'] = ''; 00340 $anyHidden = true; 00341 } 00342 if ( Revision::userCanBitfield( $row->ar_deleted, Revision::DELETED_TEXT, $user ) ) { 00343 if ( $row->ar_sha1 != '' ) { 00344 $rev['sha1'] = wfBaseConvert( $row->ar_sha1, 36, 16, 40 ); 00345 } else { 00346 $rev['sha1'] = ''; 00347 } 00348 } 00349 } 00350 if ( $fld_content ) { 00351 if ( $row->ar_deleted & Revision::DELETED_TEXT ) { 00352 $rev['texthidden'] = ''; 00353 $anyHidden = true; 00354 } 00355 if ( Revision::userCanBitfield( $row->ar_deleted, Revision::DELETED_TEXT, $user ) ) { 00356 ApiResult::setContent( $rev, Revision::getRevisionText( $row ) ); 00357 } 00358 } 00359 00360 if ( $fld_tags ) { 00361 if ( $row->ts_tags ) { 00362 $tags = explode( ',', $row->ts_tags ); 00363 $this->getResult()->setIndexedTagName( $tags, 'tag' ); 00364 $rev['tags'] = $tags; 00365 } else { 00366 $rev['tags'] = array(); 00367 } 00368 } 00369 00370 if ( $anyHidden && ( $row->ar_deleted & Revision::DELETED_RESTRICTED ) ) { 00371 $rev['suppressed'] = ''; 00372 } 00373 00374 if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) { 00375 $pageID = $newPageID++; 00376 $pageMap[$row->ar_namespace][$row->ar_title] = $pageID; 00377 $a['revisions'] = array( $rev ); 00378 $result->setIndexedTagName( $a['revisions'], 'rev' ); 00379 $title = Title::makeTitle( $row->ar_namespace, $row->ar_title ); 00380 ApiQueryBase::addTitleInfo( $a, $title ); 00381 if ( $fld_token ) { 00382 $a['token'] = $token; 00383 } 00384 $fit = $result->addValue( array( 'query', $this->getModuleName() ), $pageID, $a ); 00385 } else { 00386 $pageID = $pageMap[$row->ar_namespace][$row->ar_title]; 00387 $fit = $result->addValue( 00388 array( 'query', $this->getModuleName(), $pageID, 'revisions' ), 00389 null, $rev ); 00390 } 00391 if ( !$fit ) { 00392 if ( $mode == 'all' || $mode == 'revs' ) { 00393 $this->setContinueEnumParameter( 'continue', 00394 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id" 00395 ); 00396 } else { 00397 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" ); 00398 } 00399 break; 00400 } 00401 } 00402 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'page' ); 00403 } 00404 00405 public function getAllowedParams() { 00406 return array( 00407 'start' => array( 00408 ApiBase::PARAM_TYPE => 'timestamp' 00409 ), 00410 'end' => array( 00411 ApiBase::PARAM_TYPE => 'timestamp', 00412 ), 00413 'dir' => array( 00414 ApiBase::PARAM_TYPE => array( 00415 'newer', 00416 'older' 00417 ), 00418 ApiBase::PARAM_DFLT => 'older' 00419 ), 00420 'from' => null, 00421 'to' => null, 00422 'prefix' => null, 00423 'continue' => null, 00424 'unique' => false, 00425 'tag' => null, 00426 'user' => array( 00427 ApiBase::PARAM_TYPE => 'user' 00428 ), 00429 'excludeuser' => array( 00430 ApiBase::PARAM_TYPE => 'user' 00431 ), 00432 'namespace' => array( 00433 ApiBase::PARAM_TYPE => 'namespace', 00434 ApiBase::PARAM_DFLT => NS_MAIN, 00435 ), 00436 'limit' => array( 00437 ApiBase::PARAM_DFLT => 10, 00438 ApiBase::PARAM_TYPE => 'limit', 00439 ApiBase::PARAM_MIN => 1, 00440 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1, 00441 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2 00442 ), 00443 'prop' => array( 00444 ApiBase::PARAM_DFLT => 'user|comment', 00445 ApiBase::PARAM_TYPE => array( 00446 'revid', 00447 'parentid', 00448 'user', 00449 'userid', 00450 'comment', 00451 'parsedcomment', 00452 'minor', 00453 'len', 00454 'sha1', 00455 'content', 00456 'token', 00457 'tags' 00458 ), 00459 ApiBase::PARAM_ISMULTI => true 00460 ), 00461 ); 00462 } 00463 00464 public function getParamDescription() { 00465 return array( 00466 'start' => 'The timestamp to start enumerating from (1, 2)', 00467 'end' => 'The timestamp to stop enumerating at (1, 2)', 00468 'dir' => $this->getDirectionDescription( $this->getModulePrefix(), ' (1, 3)' ), 00469 'from' => 'Start listing at this title (3)', 00470 'to' => 'Stop listing at this title (3)', 00471 'prefix' => 'Search for all page titles that begin with this value (3)', 00472 'limit' => 'The maximum amount of revisions to list', 00473 'prop' => array( 00474 'Which properties to get', 00475 ' revid - Adds the revision ID of the deleted revision', 00476 ' parentid - Adds the revision ID of the previous revision to the page', 00477 ' user - Adds the user who made the revision', 00478 ' userid - Adds the user ID whom made the revision', 00479 ' comment - Adds the comment of the revision', 00480 ' parsedcomment - Adds the parsed comment of the revision', 00481 ' minor - Tags if the revision is minor', 00482 ' len - Adds the length (bytes) of the revision', 00483 ' sha1 - Adds the SHA-1 (base 16) of the revision', 00484 ' content - Adds the content of the revision', 00485 ' token - Gives the edit token', 00486 ' tags - Tags for the revision', 00487 ), 00488 'namespace' => 'Only list pages in this namespace (3)', 00489 'user' => 'Only list revisions by this user', 00490 'excludeuser' => 'Don\'t list revisions by this user', 00491 'continue' => 'When more results are available, use this to continue', 00492 'unique' => 'List only one revision for each page (3)', 00493 'tag' => 'Only list revisions tagged with this tag', 00494 ); 00495 } 00496 00497 public function getResultProperties() { 00498 return array( 00499 '' => array( 00500 'ns' => 'namespace', 00501 'title' => 'string' 00502 ), 00503 'token' => array( 00504 'token' => 'string' 00505 ) 00506 ); 00507 } 00508 00509 public function getDescription() { 00510 $p = $this->getModulePrefix(); 00511 00512 return array( 00513 'List deleted revisions.', 00514 'Operates in three modes:', 00515 ' 1) List deleted revisions for the given title(s), sorted by timestamp.', 00516 ' 2) List deleted contributions for the given user, sorted by timestamp (no titles specified).', 00517 ' 3) List all deleted revisions in the given namespace, sorted by title and timestamp', 00518 " (no titles specified, {$p}user not set).", 00519 'Certain parameters only apply to some modes and are ignored in others.', 00520 'For instance, a parameter marked (1) only applies to mode 1 and is ignored in modes 2 and 3.', 00521 ); 00522 } 00523 00524 public function getPossibleErrors() { 00525 return array_merge( parent::getPossibleErrors(), array( 00526 array( 00527 'code' => 'permissiondenied', 00528 'info' => 'You don\'t have permission to view deleted revision information' 00529 ), 00530 array( 'code' => 'badparams', 'info' => 'user and excludeuser cannot be used together' 00531 ), 00532 array( 00533 'code' => 'permissiondenied', 00534 'info' => 'You don\'t have permission to view deleted revision content' 00535 ), 00536 array( 'code' => 'badparams', 'info' => "The 'from' parameter cannot be used in modes 1 or 2" ), 00537 array( 'code' => 'badparams', 'info' => "The 'to' parameter cannot be used in modes 1 or 2" ), 00538 array( 00539 'code' => 'badparams', 00540 'info' => "The 'prefix' parameter cannot be used in modes 1 or 2" 00541 ), 00542 array( 'code' => 'badparams', 'info' => "The 'start' parameter cannot be used in mode 3" ), 00543 array( 'code' => 'badparams', 'info' => "The 'end' parameter cannot be used in mode 3" ), 00544 ) ); 00545 } 00546 00547 public function getExamples() { 00548 return array( 00549 'api.php?action=query&list=deletedrevs&titles=Main%20Page|Talk:Main%20Page&' . 00550 'drprop=user|comment|content' 00551 => 'List the last deleted revisions of Main Page and Talk:Main Page, with content (mode 1)', 00552 'api.php?action=query&list=deletedrevs&druser=Bob&drlimit=50' 00553 => 'List the last 50 deleted contributions by Bob (mode 2)', 00554 'api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50' 00555 => 'List the first 50 deleted revisions in the main namespace (mode 3)', 00556 'api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50&drnamespace=1&drunique=' 00557 => 'List the first 50 deleted pages in the Talk namespace (mode 3):', 00558 ); 00559 } 00560 00561 public function getHelpUrls() { 00562 return 'https://www.mediawiki.org/wiki/API:Deletedrevs'; 00563 } 00564 }