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