MediaWiki  REL1_19
ApiQueryDeletedrevs.php
Go to the documentation of this file.
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                                 $this->getDB()->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 = $this->getDB()->strencode( $this->titleToKey( $cont[1] ) );
00173                         $ts = $this->getDB()->strencode( $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                                 $this->addOption( 'ORDER BY', 'ar_title, ar_timestamp' );
00189                         }
00190                 } else {
00191                         if ( $mode == 'revs' ) {
00192                                 // Sort by ns and title in the same order as timestamp for efficiency
00193                                 $this->addWhereRange( 'ar_namespace', $dir, null, null );
00194                                 $this->addWhereRange( 'ar_title', $dir, null, null );
00195                         }
00196                         $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
00197                 }
00198                 $res = $this->select( __METHOD__ );
00199                 $pageMap = array(); // Maps ns&title to (fake) pageid
00200                 $count = 0;
00201                 $newPageID = 0;
00202                 foreach ( $res as $row ) {
00203                         if ( ++$count > $limit ) {
00204                                 // We've had enough
00205                                 if ( $mode == 'all' || $mode == 'revs' ) {
00206                                         $this->setContinueEnumParameter( 'continue', intval( $row->ar_namespace ) . '|' .
00207                                                 $this->keyToTitle( $row->ar_title ) . '|' . $row->ar_timestamp );
00208                                 } else {
00209                                         $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ar_timestamp ) );
00210                                 }
00211                                 break;
00212                         }
00213 
00214                         $rev = array();
00215                         $rev['timestamp'] = wfTimestamp( TS_ISO_8601, $row->ar_timestamp );
00216                         if ( $fld_revid ) {
00217                                 $rev['revid'] = intval( $row->ar_rev_id );
00218                         }
00219                         if ( $fld_parentid && !is_null( $row->ar_parent_id ) ) {
00220                                 $rev['parentid'] = intval( $row->ar_parent_id );
00221                         }
00222                         if ( $fld_user ) {
00223                                 $rev['user'] = $row->ar_user_text;
00224                         }
00225                         if ( $fld_userid ) {
00226                                 $rev['userid'] = $row->ar_user;
00227                         }
00228                         if ( $fld_comment ) {
00229                                 $rev['comment'] = $row->ar_comment;
00230                         }
00231 
00232                         $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
00233 
00234                         if ( $fld_parsedcomment ) {
00235                                 $rev['parsedcomment'] = Linker::formatComment( $row->ar_comment, $title );
00236                         }
00237                         if ( $fld_minor && $row->ar_minor_edit == 1 ) {
00238                                 $rev['minor'] = '';
00239                         }
00240                         if ( $fld_len ) {
00241                                 $rev['len'] = $row->ar_len;
00242                         }
00243                         if ( $fld_sha1 ) {
00244                                 if ( $row->ar_sha1 != '' ) {
00245                                         $rev['sha1'] = wfBaseConvert( $row->ar_sha1, 36, 16, 40 );
00246                                 } else {
00247                                         $rev['sha1'] = '';
00248                                 }
00249                         }
00250                         if ( $fld_content ) {
00251                                 ApiResult::setContent( $rev, Revision::getRevisionText( $row ) );
00252                         }
00253 
00254                         if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
00255                                 $pageID = $newPageID++;
00256                                 $pageMap[$row->ar_namespace][$row->ar_title] = $pageID;
00257                                 $a['revisions'] = array( $rev );
00258                                 $result->setIndexedTagName( $a['revisions'], 'rev' );
00259                                 ApiQueryBase::addTitleInfo( $a, $title );
00260                                 if ( $fld_token ) {
00261                                         $a['token'] = $token;
00262                                 }
00263                                 $fit = $result->addValue( array( 'query', $this->getModuleName() ), $pageID, $a );
00264                         } else {
00265                                 $pageID = $pageMap[$row->ar_namespace][$row->ar_title];
00266                                 $fit = $result->addValue(
00267                                         array( 'query', $this->getModuleName(), $pageID, 'revisions' ),
00268                                         null, $rev );
00269                         }
00270                         if ( !$fit ) {
00271                                 if ( $mode == 'all' || $mode == 'revs' ) {
00272                                         $this->setContinueEnumParameter( 'continue', intval( $row->ar_namespace ) . '|' .
00273                                                 $this->keyToTitle( $row->ar_title ) . '|' . $row->ar_timestamp );
00274                                 } else {
00275                                         $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ar_timestamp ) );
00276                                 }
00277                                 break;
00278                         }
00279                 }
00280                 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'page' );
00281         }
00282 
00283         public function getAllowedParams() {
00284                 return array(
00285                         'start' => array(
00286                                 ApiBase::PARAM_TYPE => 'timestamp'
00287                         ),
00288                         'end' => array(
00289                                 ApiBase::PARAM_TYPE => 'timestamp',
00290                         ),
00291                         'dir' => array(
00292                                 ApiBase::PARAM_TYPE => array(
00293                                         'newer',
00294                                         'older'
00295                                 ),
00296                                 ApiBase::PARAM_DFLT => 'older'
00297                         ),
00298                         'from' => null,
00299                         'to' => null,
00300                         'prefix' => null,
00301                         'continue' => null,
00302                         'unique' => false,
00303                         'user' => array(
00304                                 ApiBase::PARAM_TYPE => 'user'
00305                         ),
00306                         'excludeuser' => array(
00307                                 ApiBase::PARAM_TYPE => 'user'
00308                         ),
00309                         'namespace' => array(
00310                                 ApiBase::PARAM_TYPE => 'namespace',
00311                                 ApiBase::PARAM_DFLT => 0,
00312                         ),
00313                         'limit' => array(
00314                                 ApiBase::PARAM_DFLT => 10,
00315                                 ApiBase::PARAM_TYPE => 'limit',
00316                                 ApiBase::PARAM_MIN => 1,
00317                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00318                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00319                         ),
00320                         'prop' => array(
00321                                 ApiBase::PARAM_DFLT => 'user|comment',
00322                                 ApiBase::PARAM_TYPE => array(
00323                                         'revid',
00324                                         'parentid',
00325                                         'user',
00326                                         'userid',
00327                                         'comment',
00328                                         'parsedcomment',
00329                                         'minor',
00330                                         'len',
00331                                         'sha1',
00332                                         'content',
00333                                         'token'
00334                                 ),
00335                                 ApiBase::PARAM_ISMULTI => true
00336                         ),
00337                 );
00338         }
00339 
00340         public function getParamDescription() {
00341                 return array(
00342                         'start' => 'The timestamp to start enumerating from (1,2)',
00343                         'end' => 'The timestamp to stop enumerating at (1,2)',
00344                         'dir' => $this->getDirectionDescription( $this->getModulePrefix(), ' (1, 3)' ),
00345                         'from' => 'Start listing at this title (3)',
00346                         'to' => 'Stop listing at this title (3)',
00347                         'prefix' => 'Search for all page titles that begin with this value (3)',
00348                         'limit' => 'The maximum amount of revisions to list',
00349                         'prop' => array(
00350                                 'Which properties to get',
00351                                 ' revid          - Adds the revision ID of the deleted revision',
00352                                 ' parentid       - Adds the revision ID of the previous revision to the page',
00353                                 ' user           - Adds the user who made the revision',
00354                                 ' userid         - Adds the user ID whom made the revision',
00355                                 ' comment        - Adds the comment of the revision',
00356                                 ' parsedcomment  - Adds the parsed comment of the revision',
00357                                 ' minor          - Tags if the revision is minor',
00358                                 ' len            - Adds the length (bytes) of the revision',
00359                                 ' sha1           - Adds the SHA-1 (base 16) of the revision',
00360                                 ' content        - Adds the content of the revision',
00361                                 ' token          - Gives the edit token',
00362                         ),
00363                         'namespace' => 'Only list pages in this namespace (3)',
00364                         'user' => 'Only list revisions by this user',
00365                         'excludeuser' => 'Don\'t list revisions by this user',
00366                         'continue' => 'When more results are available, use this to continue (3)',
00367                         'unique' => 'List only one revision for each page (3)',
00368                 );
00369         }
00370 
00371         public function getDescription() {
00372                 $p = $this->getModulePrefix();
00373                 return array(
00374                         'List deleted revisions.',
00375                         'Operates in three modes:',
00376                         ' 1) List deleted revisions for the given title(s), sorted by timestamp',
00377                         ' 2) List deleted contributions for the given user, sorted by timestamp (no titles specified)',
00378                         " 3) List all deleted revisions in the given namespace, sorted by title and timestamp (no titles specified, {$p}user not set)",
00379                         'Certain parameters only apply to some modes and are ignored in others.',
00380                         'For instance, a parameter marked (1) only applies to mode 1 and is ignored in modes 2 and 3',
00381                 );
00382         }
00383 
00384         public function getPossibleErrors() {
00385                 return array_merge( parent::getPossibleErrors(), array(
00386                         array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted revision information' ),
00387                         array( 'code' => 'badparams', 'info' => 'user and excludeuser cannot be used together' ),
00388                         array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted revision content' ),
00389                         array( 'code' => 'badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
00390                         array( 'code' => 'badparams', 'info' => "The 'from' parameter cannot be used in modes 1 or 2" ),
00391                         array( 'code' => 'badparams', 'info' => "The 'to' parameter cannot be used in modes 1 or 2" ),
00392                         array( 'code' => 'badparams', 'info' => "The 'prefix' parameter cannot be used in modes 1 or 2" ),
00393                         array( 'code' => 'badparams', 'info' => "The 'continue' parameter cannot be used in modes 1 or 2" ),
00394                         array( 'code' => 'badparams', 'info' => "The 'start' parameter cannot be used in mode 3" ),
00395                         array( 'code' => 'badparams', 'info' => "The 'end' parameter cannot be used in mode 3" ),
00396                 ) );
00397         }
00398 
00399         public function getExamples() {
00400                 return array(
00401                         'api.php?action=query&list=deletedrevs&titles=Main%20Page|Talk:Main%20Page&drprop=user|comment|content'
00402                                 => 'List the last deleted revisions of Main Page and Talk:Main Page, with content (mode 1)',
00403                         'api.php?action=query&list=deletedrevs&druser=Bob&drlimit=50'
00404                                 => 'List the last 50 deleted contributions by Bob (mode 2)',
00405                         'api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50'
00406                                 => 'List the first 50 deleted revisions in the main namespace (mode 3)',
00407                         'api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50&drnamespace=1&drunique='
00408                                 => 'List the first 50 deleted pages in the Talk namespace (mode 3):',
00409                 );
00410         }
00411 
00412         public function getHelpUrls() {
00413                 return 'https://www.mediawiki.org/wiki/API:Deletedrevs';
00414         }
00415 
00416         public function getVersion() {
00417                 return __CLASS__ . ': $Id$';
00418         }
00419 }