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