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