MediaWiki  REL1_24
ApiQueryRevisions.php
Go to the documentation of this file.
00001 <?php
00035 class ApiQueryRevisions extends ApiQueryBase {
00036 
00037     private $diffto, $difftotext, $expandTemplates, $generateXML, $section,
00038         $token, $parseContent, $contentFormat;
00039 
00040     public function __construct( ApiQuery $query, $moduleName ) {
00041         parent::__construct( $query, $moduleName, 'rv' );
00042     }
00043 
00044     private $fld_ids = false, $fld_flags = false, $fld_timestamp = false,
00045         $fld_size = false, $fld_sha1 = false, $fld_comment = false,
00046         $fld_parsedcomment = false, $fld_user = false, $fld_userid = false,
00047         $fld_content = false, $fld_tags = false, $fld_contentmodel = false;
00048 
00049     private $tokenFunctions;
00050 
00052     protected function getTokenFunctions() {
00053         // tokenname => function
00054         // function prototype is func($pageid, $title, $rev)
00055         // should return token or false
00056 
00057         // Don't call the hooks twice
00058         if ( isset( $this->tokenFunctions ) ) {
00059             return $this->tokenFunctions;
00060         }
00061 
00062         // If we're in JSON callback mode, no tokens can be obtained
00063         if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
00064             return array();
00065         }
00066 
00067         $this->tokenFunctions = array(
00068             'rollback' => array( 'ApiQueryRevisions', 'getRollbackToken' )
00069         );
00070         wfRunHooks( 'APIQueryRevisionsTokens', array( &$this->tokenFunctions ) );
00071 
00072         return $this->tokenFunctions;
00073     }
00074 
00082     public static function getRollbackToken( $pageid, $title, $rev ) {
00083         global $wgUser;
00084         if ( !$wgUser->isAllowed( 'rollback' ) ) {
00085             return false;
00086         }
00087 
00088         return $wgUser->getEditToken(
00089             array( $title->getPrefixedText(), $rev->getUserText() ) );
00090     }
00091 
00092     public function execute() {
00093         $params = $this->extractRequestParams( false );
00094 
00095         // If any of those parameters are used, work in 'enumeration' mode.
00096         // Enum mode can only be used when exactly one page is provided.
00097         // Enumerating revisions on multiple pages make it extremely
00098         // difficult to manage continuations and require additional SQL indexes
00099         $enumRevMode = ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ||
00100             !is_null( $params['limit'] ) || !is_null( $params['startid'] ) ||
00101             !is_null( $params['endid'] ) || $params['dir'] === 'newer' ||
00102             !is_null( $params['start'] ) || !is_null( $params['end'] ) );
00103 
00104         $pageSet = $this->getPageSet();
00105         $pageCount = $pageSet->getGoodTitleCount();
00106         $revCount = $pageSet->getRevisionCount();
00107 
00108         // Optimization -- nothing to do
00109         if ( $revCount === 0 && $pageCount === 0 ) {
00110             return;
00111         }
00112 
00113         if ( $revCount > 0 && $enumRevMode ) {
00114             $this->dieUsage(
00115                 'The revids= parameter may not be used with the list options ' .
00116                     '(limit, startid, endid, dirNewer, start, end).',
00117                 'revids'
00118             );
00119         }
00120 
00121         if ( $pageCount > 1 && $enumRevMode ) {
00122             $this->dieUsage(
00123                 'titles, pageids or a generator was used to supply multiple pages, ' .
00124                     'but the limit, startid, endid, dirNewer, user, excludeuser, start ' .
00125                     'and end parameters may only be used on a single page.',
00126                 'multpages'
00127             );
00128         }
00129 
00130         if ( !is_null( $params['difftotext'] ) ) {
00131             $this->difftotext = $params['difftotext'];
00132         } elseif ( !is_null( $params['diffto'] ) ) {
00133             if ( $params['diffto'] == 'cur' ) {
00134                 $params['diffto'] = 0;
00135             }
00136             if ( ( !ctype_digit( $params['diffto'] ) || $params['diffto'] < 0 )
00137                 && $params['diffto'] != 'prev' && $params['diffto'] != 'next'
00138             ) {
00139                 $this->dieUsage(
00140                     'rvdiffto must be set to a non-negative number, "prev", "next" or "cur"',
00141                     'diffto'
00142                 );
00143             }
00144             // Check whether the revision exists and is readable,
00145             // DifferenceEngine returns a rather ambiguous empty
00146             // string if that's not the case
00147             if ( $params['diffto'] != 0 ) {
00148                 $difftoRev = Revision::newFromID( $params['diffto'] );
00149                 if ( !$difftoRev ) {
00150                     $this->dieUsageMsg( array( 'nosuchrevid', $params['diffto'] ) );
00151                 }
00152                 if ( !$difftoRev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
00153                     $this->setWarning( "Couldn't diff to r{$difftoRev->getID()}: content is hidden" );
00154                     $params['diffto'] = null;
00155                 }
00156             }
00157             $this->diffto = $params['diffto'];
00158         }
00159 
00160         $db = $this->getDB();
00161         $this->addTables( 'page' );
00162         $this->addFields( Revision::selectFields() );
00163         $this->addWhere( 'page_id = rev_page' );
00164 
00165         $prop = array_flip( $params['prop'] );
00166 
00167         // Optional fields
00168         $this->fld_ids = isset( $prop['ids'] );
00169         // $this->addFieldsIf('rev_text_id', $this->fld_ids); // should this be exposed?
00170         $this->fld_flags = isset( $prop['flags'] );
00171         $this->fld_timestamp = isset( $prop['timestamp'] );
00172         $this->fld_comment = isset( $prop['comment'] );
00173         $this->fld_parsedcomment = isset( $prop['parsedcomment'] );
00174         $this->fld_size = isset( $prop['size'] );
00175         $this->fld_sha1 = isset( $prop['sha1'] );
00176         $this->fld_contentmodel = isset( $prop['contentmodel'] );
00177         $this->fld_userid = isset( $prop['userid'] );
00178         $this->fld_user = isset( $prop['user'] );
00179         $this->token = $params['token'];
00180 
00181         if ( !empty( $params['contentformat'] ) ) {
00182             $this->contentFormat = $params['contentformat'];
00183         }
00184 
00185         $userMax = ( $this->fld_content ? ApiBase::LIMIT_SML1 : ApiBase::LIMIT_BIG1 );
00186         $botMax = ( $this->fld_content ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_BIG2 );
00187         $limit = $params['limit'];
00188         if ( $limit == 'max' ) {
00189             $limit = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
00190             $this->getResult()->setParsedLimit( $this->getModuleName(), $limit );
00191         }
00192 
00193         if ( !is_null( $this->token ) || $pageCount > 0 ) {
00194             $this->addFields( Revision::selectPageFields() );
00195         }
00196 
00197         if ( isset( $prop['tags'] ) ) {
00198             $this->fld_tags = true;
00199             $this->addTables( 'tag_summary' );
00200             $this->addJoinConds(
00201                 array( 'tag_summary' => array( 'LEFT JOIN', array( 'rev_id=ts_rev_id' ) ) )
00202             );
00203             $this->addFields( 'ts_tags' );
00204         }
00205 
00206         if ( !is_null( $params['tag'] ) ) {
00207             $this->addTables( 'change_tag' );
00208             $this->addJoinConds(
00209                 array( 'change_tag' => array( 'INNER JOIN', array( 'rev_id=ct_rev_id' ) ) )
00210             );
00211             $this->addWhereFld( 'ct_tag', $params['tag'] );
00212         }
00213 
00214         if ( isset( $prop['content'] ) || !is_null( $this->diffto ) || !is_null( $this->difftotext ) ) {
00215             // For each page we will request, the user must have read rights for that page
00216             $user = $this->getUser();
00218             foreach ( $pageSet->getGoodTitles() as $title ) {
00219                 if ( !$title->userCan( 'read', $user ) ) {
00220                     $this->dieUsage(
00221                         'The current user is not allowed to read ' . $title->getPrefixedText(),
00222                         'accessdenied' );
00223                 }
00224             }
00225 
00226             $this->addTables( 'text' );
00227             $this->addWhere( 'rev_text_id=old_id' );
00228             $this->addFields( 'old_id' );
00229             $this->addFields( Revision::selectTextFields() );
00230 
00231             $this->fld_content = isset( $prop['content'] );
00232 
00233             $this->expandTemplates = $params['expandtemplates'];
00234             $this->generateXML = $params['generatexml'];
00235             $this->parseContent = $params['parse'];
00236             if ( $this->parseContent ) {
00237                 // Must manually initialize unset limit
00238                 if ( is_null( $limit ) ) {
00239                     $limit = 1;
00240                 }
00241                 // We are only going to parse 1 revision per request
00242                 $this->validateLimit( 'limit', $limit, 1, 1, 1 );
00243             }
00244             if ( isset( $params['section'] ) ) {
00245                 $this->section = $params['section'];
00246             } else {
00247                 $this->section = false;
00248             }
00249         }
00250 
00251         // add user name, if needed
00252         if ( $this->fld_user ) {
00253             $this->addTables( 'user' );
00254             $this->addJoinConds( array( 'user' => Revision::userJoinCond() ) );
00255             $this->addFields( Revision::selectUserFields() );
00256         }
00257 
00258         // Bug 24166 - API error when using rvprop=tags
00259         $this->addTables( 'revision' );
00260 
00261         if ( $enumRevMode ) {
00262             // This is mostly to prevent parameter errors (and optimize SQL?)
00263             if ( !is_null( $params['startid'] ) && !is_null( $params['start'] ) ) {
00264                 $this->dieUsage( 'start and startid cannot be used together', 'badparams' );
00265             }
00266 
00267             if ( !is_null( $params['endid'] ) && !is_null( $params['end'] ) ) {
00268                 $this->dieUsage( 'end and endid cannot be used together', 'badparams' );
00269             }
00270 
00271             if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
00272                 $this->dieUsage( 'user and excludeuser cannot be used together', 'badparams' );
00273             }
00274 
00275             // Continuing effectively uses startid. But we can't use rvstartid
00276             // directly, because there is no way to tell the client to ''not''
00277             // send rvstart if it sent it in the original query. So instead we
00278             // send the continuation startid as rvcontinue, and ignore both
00279             // rvstart and rvstartid when that is supplied.
00280             if ( !is_null( $params['continue'] ) ) {
00281                 $params['startid'] = $params['continue'];
00282                 $params['start'] = null;
00283             }
00284 
00285             // This code makes an assumption that sorting by rev_id and rev_timestamp produces
00286             // the same result. This way users may request revisions starting at a given time,
00287             // but to page through results use the rev_id returned after each page.
00288             // Switching to rev_id removes the potential problem of having more than
00289             // one row with the same timestamp for the same page.
00290             // The order needs to be the same as start parameter to avoid SQL filesort.
00291             if ( is_null( $params['startid'] ) && is_null( $params['endid'] ) ) {
00292                 $this->addTimestampWhereRange( 'rev_timestamp', $params['dir'],
00293                     $params['start'], $params['end'] );
00294             } else {
00295                 $this->addWhereRange( 'rev_id', $params['dir'],
00296                     $params['startid'], $params['endid'] );
00297                 // One of start and end can be set
00298                 // If neither is set, this does nothing
00299                 $this->addTimestampWhereRange( 'rev_timestamp', $params['dir'],
00300                     $params['start'], $params['end'], false );
00301             }
00302 
00303             // must manually initialize unset limit
00304             if ( is_null( $limit ) ) {
00305                 $limit = 10;
00306             }
00307             $this->validateLimit( 'limit', $limit, 1, $userMax, $botMax );
00308 
00309             // There is only one ID, use it
00310             $ids = array_keys( $pageSet->getGoodTitles() );
00311             $this->addWhereFld( 'rev_page', reset( $ids ) );
00312 
00313             if ( !is_null( $params['user'] ) ) {
00314                 $this->addWhereFld( 'rev_user_text', $params['user'] );
00315             } elseif ( !is_null( $params['excludeuser'] ) ) {
00316                 $this->addWhere( 'rev_user_text != ' .
00317                     $db->addQuotes( $params['excludeuser'] ) );
00318             }
00319             if ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ) {
00320                 // Paranoia: avoid brute force searches (bug 17342)
00321                 if ( !$this->getUser()->isAllowed( 'deletedhistory' ) ) {
00322                     $bitmask = Revision::DELETED_USER;
00323                 } elseif ( !$this->getUser()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
00324                     $bitmask = Revision::DELETED_USER | Revision::DELETED_RESTRICTED;
00325                 } else {
00326                     $bitmask = 0;
00327                 }
00328                 if ( $bitmask ) {
00329                     $this->addWhere( $db->bitAnd( 'rev_deleted', $bitmask ) . " != $bitmask" );
00330                 }
00331             }
00332         } elseif ( $revCount > 0 ) {
00333             $max = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
00334             $revs = $pageSet->getRevisionIDs();
00335             if ( self::truncateArray( $revs, $max ) ) {
00336                 $this->setWarning( "Too many values supplied for parameter 'revids': the limit is $max" );
00337             }
00338 
00339             // Get all revision IDs
00340             $this->addWhereFld( 'rev_id', array_keys( $revs ) );
00341 
00342             if ( !is_null( $params['continue'] ) ) {
00343                 $this->addWhere( 'rev_id >= ' . intval( $params['continue'] ) );
00344             }
00345             $this->addOption( 'ORDER BY', 'rev_id' );
00346 
00347             // assumption testing -- we should never get more then $revCount rows.
00348             $limit = $revCount;
00349         } elseif ( $pageCount > 0 ) {
00350             $max = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
00351             $titles = $pageSet->getGoodTitles();
00352             if ( self::truncateArray( $titles, $max ) ) {
00353                 $this->setWarning( "Too many values supplied for parameter 'titles': the limit is $max" );
00354             }
00355 
00356             // When working in multi-page non-enumeration mode,
00357             // limit to the latest revision only
00358             $this->addWhere( 'page_id=rev_page' );
00359             $this->addWhere( 'page_latest=rev_id' );
00360 
00361             // Get all page IDs
00362             $this->addWhereFld( 'page_id', array_keys( $titles ) );
00363             // Every time someone relies on equality propagation, god kills a kitten :)
00364             $this->addWhereFld( 'rev_page', array_keys( $titles ) );
00365 
00366             if ( !is_null( $params['continue'] ) ) {
00367                 $cont = explode( '|', $params['continue'] );
00368                 $this->dieContinueUsageIf( count( $cont ) != 2 );
00369                 $pageid = intval( $cont[0] );
00370                 $revid = intval( $cont[1] );
00371                 $this->addWhere(
00372                     "rev_page > $pageid OR " .
00373                     "(rev_page = $pageid AND " .
00374                     "rev_id >= $revid)"
00375                 );
00376             }
00377             $this->addOption( 'ORDER BY', array(
00378                 'rev_page',
00379                 'rev_id'
00380             ) );
00381 
00382             // assumption testing -- we should never get more then $pageCount rows.
00383             $limit = $pageCount;
00384         } else {
00385             ApiBase::dieDebug( __METHOD__, 'param validation?' );
00386         }
00387 
00388         $this->addOption( 'LIMIT', $limit + 1 );
00389 
00390         $count = 0;
00391         $res = $this->select( __METHOD__ );
00392 
00393         foreach ( $res as $row ) {
00394             if ( ++$count > $limit ) {
00395                 // We've reached the one extra which shows that there are
00396                 // additional pages to be had. Stop here...
00397                 if ( !$enumRevMode ) {
00398                     ApiBase::dieDebug( __METHOD__, 'Got more rows then expected' ); // bug report
00399                 }
00400                 $this->setContinueEnumParameter( 'continue', intval( $row->rev_id ) );
00401                 break;
00402             }
00403 
00404             $fit = $this->addPageSubItem( $row->rev_page, $this->extractRowInfo( $row ), 'rev' );
00405             if ( !$fit ) {
00406                 if ( $enumRevMode ) {
00407                     $this->setContinueEnumParameter( 'continue', intval( $row->rev_id ) );
00408                 } elseif ( $revCount > 0 ) {
00409                     $this->setContinueEnumParameter( 'continue', intval( $row->rev_id ) );
00410                 } else {
00411                     $this->setContinueEnumParameter( 'continue', intval( $row->rev_page ) .
00412                         '|' . intval( $row->rev_id ) );
00413                 }
00414                 break;
00415             }
00416         }
00417     }
00418 
00419     private function extractRowInfo( $row ) {
00420         $revision = new Revision( $row );
00421         $title = $revision->getTitle();
00422         $user = $this->getUser();
00423         $vals = array();
00424         $anyHidden = false;
00425 
00426         if ( $this->fld_ids ) {
00427             $vals['revid'] = intval( $revision->getId() );
00428             // $vals['oldid'] = intval( $row->rev_text_id ); // todo: should this be exposed?
00429             if ( !is_null( $revision->getParentId() ) ) {
00430                 $vals['parentid'] = intval( $revision->getParentId() );
00431             }
00432         }
00433 
00434         if ( $this->fld_flags && $revision->isMinor() ) {
00435             $vals['minor'] = '';
00436         }
00437 
00438         if ( $this->fld_user || $this->fld_userid ) {
00439             if ( $revision->isDeleted( Revision::DELETED_USER ) ) {
00440                 $vals['userhidden'] = '';
00441                 $anyHidden = true;
00442             }
00443             if ( $revision->userCan( Revision::DELETED_USER, $user ) ) {
00444                 if ( $this->fld_user ) {
00445                     $vals['user'] = $revision->getRawUserText();
00446                 }
00447                 $userid = $revision->getRawUser();
00448                 if ( !$userid ) {
00449                     $vals['anon'] = '';
00450                 }
00451 
00452                 if ( $this->fld_userid ) {
00453                     $vals['userid'] = $userid;
00454                 }
00455             }
00456         }
00457 
00458         if ( $this->fld_timestamp ) {
00459             $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $revision->getTimestamp() );
00460         }
00461 
00462         if ( $this->fld_size ) {
00463             if ( !is_null( $revision->getSize() ) ) {
00464                 $vals['size'] = intval( $revision->getSize() );
00465             } else {
00466                 $vals['size'] = 0;
00467             }
00468         }
00469 
00470         if ( $this->fld_sha1 ) {
00471             if ( $revision->isDeleted( Revision::DELETED_TEXT ) ) {
00472                 $vals['sha1hidden'] = '';
00473                 $anyHidden = true;
00474             }
00475             if ( $revision->userCan( Revision::DELETED_TEXT, $user ) ) {
00476                 if ( $revision->getSha1() != '' ) {
00477                     $vals['sha1'] = wfBaseConvert( $revision->getSha1(), 36, 16, 40 );
00478                 } else {
00479                     $vals['sha1'] = '';
00480                 }
00481             }
00482         }
00483 
00484         if ( $this->fld_contentmodel ) {
00485             $vals['contentmodel'] = $revision->getContentModel();
00486         }
00487 
00488         if ( $this->fld_comment || $this->fld_parsedcomment ) {
00489             if ( $revision->isDeleted( Revision::DELETED_COMMENT ) ) {
00490                 $vals['commenthidden'] = '';
00491                 $anyHidden = true;
00492             }
00493             if ( $revision->userCan( Revision::DELETED_COMMENT, $user ) ) {
00494                 $comment = $revision->getRawComment();
00495 
00496                 if ( $this->fld_comment ) {
00497                     $vals['comment'] = $comment;
00498                 }
00499 
00500                 if ( $this->fld_parsedcomment ) {
00501                     $vals['parsedcomment'] = Linker::formatComment( $comment, $title );
00502                 }
00503             }
00504         }
00505 
00506         if ( $this->fld_tags ) {
00507             if ( $row->ts_tags ) {
00508                 $tags = explode( ',', $row->ts_tags );
00509                 $this->getResult()->setIndexedTagName( $tags, 'tag' );
00510                 $vals['tags'] = $tags;
00511             } else {
00512                 $vals['tags'] = array();
00513             }
00514         }
00515 
00516         if ( !is_null( $this->token ) ) {
00517             $tokenFunctions = $this->getTokenFunctions();
00518             foreach ( $this->token as $t ) {
00519                 $val = call_user_func( $tokenFunctions[$t], $title->getArticleID(), $title, $revision );
00520                 if ( $val === false ) {
00521                     $this->setWarning( "Action '$t' is not allowed for the current user" );
00522                 } else {
00523                     $vals[$t . 'token'] = $val;
00524                 }
00525             }
00526         }
00527 
00528         $content = null;
00529         global $wgParser;
00530         if ( $this->fld_content || !is_null( $this->diffto ) || !is_null( $this->difftotext ) ) {
00531             $content = $revision->getContent( Revision::FOR_THIS_USER, $this->getUser() );
00532             // Expand templates after getting section content because
00533             // template-added sections don't count and Parser::preprocess()
00534             // will have less input
00535             if ( $content && $this->section !== false ) {
00536                 $content = $content->getSection( $this->section, false );
00537                 if ( !$content ) {
00538                     $this->dieUsage(
00539                         "There is no section {$this->section} in r" . $revision->getId(),
00540                         'nosuchsection'
00541                     );
00542                 }
00543             }
00544             if ( $revision->isDeleted( Revision::DELETED_TEXT ) ) {
00545                 $vals['texthidden'] = '';
00546                 $anyHidden = true;
00547             } elseif ( !$content ) {
00548                 $vals['textmissing'] = '';
00549             }
00550         }
00551         if ( $this->fld_content && $content ) {
00552             $text = null;
00553 
00554             if ( $this->generateXML ) {
00555                 if ( $content->getModel() === CONTENT_MODEL_WIKITEXT ) {
00556                     $t = $content->getNativeData(); # note: don't set $text
00557 
00558                     $wgParser->startExternalParse(
00559                         $title,
00560                         ParserOptions::newFromContext( $this->getContext() ),
00561                         OT_PREPROCESS
00562                     );
00563                     $dom = $wgParser->preprocessToDom( $t );
00564                     if ( is_callable( array( $dom, 'saveXML' ) ) ) {
00565                         $xml = $dom->saveXML();
00566                     } else {
00567                         $xml = $dom->__toString();
00568                     }
00569                     $vals['parsetree'] = $xml;
00570                 } else {
00571                     $this->setWarning( "Conversion to XML is supported for wikitext only, " .
00572                         $title->getPrefixedDBkey() .
00573                         " uses content model " . $content->getModel() );
00574                 }
00575             }
00576 
00577             if ( $this->expandTemplates && !$this->parseContent ) {
00578                 #XXX: implement template expansion for all content types in ContentHandler?
00579                 if ( $content->getModel() === CONTENT_MODEL_WIKITEXT ) {
00580                     $text = $content->getNativeData();
00581 
00582                     $text = $wgParser->preprocess(
00583                         $text,
00584                         $title,
00585                         ParserOptions::newFromContext( $this->getContext() )
00586                     );
00587                 } else {
00588                     $this->setWarning( "Template expansion is supported for wikitext only, " .
00589                         $title->getPrefixedDBkey() .
00590                         " uses content model " . $content->getModel() );
00591 
00592                     $text = false;
00593                 }
00594             }
00595             if ( $this->parseContent ) {
00596                 $po = $content->getParserOutput(
00597                     $title,
00598                     $revision->getId(),
00599                     ParserOptions::newFromContext( $this->getContext() )
00600                 );
00601                 $text = $po->getText();
00602             }
00603 
00604             if ( $text === null ) {
00605                 $format = $this->contentFormat ? $this->contentFormat : $content->getDefaultFormat();
00606                 $model = $content->getModel();
00607 
00608                 if ( !$content->isSupportedFormat( $format ) ) {
00609                     $name = $title->getPrefixedDBkey();
00610 
00611                     $this->dieUsage( "The requested format {$this->contentFormat} is not supported " .
00612                         "for content model $model used by $name", 'badformat' );
00613                 }
00614 
00615                 $text = $content->serialize( $format );
00616 
00617                 // always include format and model.
00618                 // Format is needed to deserialize, model is needed to interpret.
00619                 $vals['contentformat'] = $format;
00620                 $vals['contentmodel'] = $model;
00621             }
00622 
00623             if ( $text !== false ) {
00624                 ApiResult::setContent( $vals, $text );
00625             }
00626         }
00627 
00628         if ( $content && ( !is_null( $this->diffto ) || !is_null( $this->difftotext ) ) ) {
00629             static $n = 0; // Number of uncached diffs we've had
00630 
00631             if ( $n < $this->getConfig()->get( 'APIMaxUncachedDiffs' ) ) {
00632                 $vals['diff'] = array();
00633                 $context = new DerivativeContext( $this->getContext() );
00634                 $context->setTitle( $title );
00635                 $handler = $revision->getContentHandler();
00636 
00637                 if ( !is_null( $this->difftotext ) ) {
00638                     $model = $title->getContentModel();
00639 
00640                     if ( $this->contentFormat
00641                         && !ContentHandler::getForModelID( $model )->isSupportedFormat( $this->contentFormat )
00642                     ) {
00643 
00644                         $name = $title->getPrefixedDBkey();
00645 
00646                         $this->dieUsage( "The requested format {$this->contentFormat} is not supported for " .
00647                             "content model $model used by $name", 'badformat' );
00648                     }
00649 
00650                     $difftocontent = ContentHandler::makeContent(
00651                         $this->difftotext,
00652                         $title,
00653                         $model,
00654                         $this->contentFormat
00655                     );
00656 
00657                     $engine = $handler->createDifferenceEngine( $context );
00658                     $engine->setContent( $content, $difftocontent );
00659                 } else {
00660                     $engine = $handler->createDifferenceEngine( $context, $revision->getID(), $this->diffto );
00661                     $vals['diff']['from'] = $engine->getOldid();
00662                     $vals['diff']['to'] = $engine->getNewid();
00663                 }
00664                 $difftext = $engine->getDiffBody();
00665                 ApiResult::setContent( $vals['diff'], $difftext );
00666                 if ( !$engine->wasCacheHit() ) {
00667                     $n++;
00668                 }
00669             } else {
00670                 $vals['diff']['notcached'] = '';
00671             }
00672         }
00673 
00674         if ( $anyHidden && $revision->isDeleted( Revision::DELETED_RESTRICTED ) ) {
00675             $vals['suppressed'] = '';
00676         }
00677 
00678         return $vals;
00679     }
00680 
00681     public function getCacheMode( $params ) {
00682         if ( isset( $params['token'] ) ) {
00683             return 'private';
00684         }
00685         if ( $this->userCanSeeRevDel() ) {
00686             return 'private';
00687         }
00688         if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
00689             // formatComment() calls wfMessage() among other things
00690             return 'anon-public-user-private';
00691         }
00692 
00693         return 'public';
00694     }
00695 
00696     public function getAllowedParams() {
00697         return array(
00698             'prop' => array(
00699                 ApiBase::PARAM_ISMULTI => true,
00700                 ApiBase::PARAM_DFLT => 'ids|timestamp|flags|comment|user',
00701                 ApiBase::PARAM_TYPE => array(
00702                     'ids',
00703                     'flags',
00704                     'timestamp',
00705                     'user',
00706                     'userid',
00707                     'size',
00708                     'sha1',
00709                     'contentmodel',
00710                     'comment',
00711                     'parsedcomment',
00712                     'content',
00713                     'tags'
00714                 )
00715             ),
00716             'limit' => array(
00717                 ApiBase::PARAM_TYPE => 'limit',
00718                 ApiBase::PARAM_MIN => 1,
00719                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00720                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00721             ),
00722             'startid' => array(
00723                 ApiBase::PARAM_TYPE => 'integer'
00724             ),
00725             'endid' => array(
00726                 ApiBase::PARAM_TYPE => 'integer'
00727             ),
00728             'start' => array(
00729                 ApiBase::PARAM_TYPE => 'timestamp'
00730             ),
00731             'end' => array(
00732                 ApiBase::PARAM_TYPE => 'timestamp'
00733             ),
00734             'dir' => array(
00735                 ApiBase::PARAM_DFLT => 'older',
00736                 ApiBase::PARAM_TYPE => array(
00737                     'newer',
00738                     'older'
00739                 )
00740             ),
00741             'user' => array(
00742                 ApiBase::PARAM_TYPE => 'user'
00743             ),
00744             'excludeuser' => array(
00745                 ApiBase::PARAM_TYPE => 'user'
00746             ),
00747             'tag' => null,
00748             'expandtemplates' => false,
00749             'generatexml' => false,
00750             'parse' => false,
00751             'section' => null,
00752             'token' => array(
00753                 ApiBase::PARAM_DEPRECATED => true,
00754                 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
00755                 ApiBase::PARAM_ISMULTI => true
00756             ),
00757             'continue' => null,
00758             'diffto' => null,
00759             'difftotext' => null,
00760             'contentformat' => array(
00761                 ApiBase::PARAM_TYPE => ContentHandler::getAllContentFormats(),
00762                 ApiBase::PARAM_DFLT => null
00763             ),
00764         );
00765     }
00766 
00767     public function getParamDescription() {
00768         $p = $this->getModulePrefix();
00769 
00770         return array(
00771             'prop' => array(
00772                 'Which properties to get for each revision:',
00773                 ' ids            - The ID of the revision',
00774                 ' flags          - Revision flags (minor)',
00775                 ' timestamp      - The timestamp of the revision',
00776                 ' user           - User that made the revision',
00777                 ' userid         - User id of revision creator',
00778                 ' size           - Length (bytes) of the revision',
00779                 ' sha1           - SHA-1 (base 16) of the revision',
00780                 ' contentmodel   - Content model id',
00781                 ' comment        - Comment by the user for revision',
00782                 ' parsedcomment  - Parsed comment by the user for the revision',
00783                 ' content        - Text of the revision',
00784                 ' tags           - Tags for the revision',
00785             ),
00786             'limit' => 'Limit how many revisions will be returned (enum)',
00787             'startid' => 'From which revision id to start enumeration (enum)',
00788             'endid' => 'Stop revision enumeration on this revid (enum)',
00789             'start' => 'From which revision timestamp to start enumeration (enum)',
00790             'end' => 'Enumerate up to this timestamp (enum)',
00791             'dir' => $this->getDirectionDescription( $p, ' (enum)' ),
00792             'user' => 'Only include revisions made by user (enum)',
00793             'excludeuser' => 'Exclude revisions made by user (enum)',
00794             'expandtemplates' => "Expand templates in revision content (requires {$p}prop=content)",
00795             'generatexml' => "Generate XML parse tree for revision content (requires {$p}prop=content)",
00796             'parse' => array( "Parse revision content (requires {$p}prop=content).",
00797                 'For performance reasons if this option is used, rvlimit is enforced to 1.' ),
00798             'section' => 'Only retrieve the content of this section number',
00799             'token' => 'Which tokens to obtain for each revision',
00800             'continue' => 'When more results are available, use this to continue',
00801             'diffto' => array( 'Revision ID to diff each revision to.',
00802                 'Use "prev", "next" and "cur" for the previous, next and current revision respectively' ),
00803             'difftotext' => array(
00804                 'Text to diff each revision to. Only diffs a limited number of revisions.',
00805                 "Overrides {$p}diffto. If {$p}section is set, only that section will be",
00806                 'diffed against this text',
00807             ),
00808             'tag' => 'Only list revisions tagged with this tag',
00809             'contentformat' => 'Serialization format used for difftotext and expected for output of content',
00810         );
00811     }
00812 
00813     public function getDescription() {
00814         return array(
00815             'Get revision information.',
00816             'May be used in several ways:',
00817             ' 1) Get data about a set of pages (last revision), by setting titles or pageids parameter.',
00818             ' 2) Get revisions for one given page, by using titles/pageids with start/end/limit params.',
00819             ' 3) Get data about a set of revisions by setting their IDs with revids parameter.',
00820             'All parameters marked as (enum) may only be used with a single page (#2).'
00821         );
00822     }
00823 
00824     public function getExamples() {
00825         return array(
00826             'Get data with content for the last revision of titles "API" and "Main Page"',
00827             '  api.php?action=query&prop=revisions&titles=API|Main%20Page&' .
00828                 'rvprop=timestamp|user|comment|content',
00829             'Get last 5 revisions of the "Main Page"',
00830             '  api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
00831                 'rvprop=timestamp|user|comment',
00832             'Get first 5 revisions of the "Main Page"',
00833             '  api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
00834                 'rvprop=timestamp|user|comment&rvdir=newer',
00835             'Get first 5 revisions of the "Main Page" made after 2006-05-01',
00836             '  api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
00837                 'rvprop=timestamp|user|comment&rvdir=newer&rvstart=20060501000000',
00838             'Get first 5 revisions of the "Main Page" that were not made made by anonymous user "127.0.0.1"',
00839             '  api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
00840                 'rvprop=timestamp|user|comment&rvexcludeuser=127.0.0.1',
00841             'Get first 5 revisions of the "Main Page" that were made by the user "MediaWiki default"',
00842             '  api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
00843                 'rvprop=timestamp|user|comment&rvuser=MediaWiki%20default',
00844         );
00845     }
00846 
00847     public function getHelpUrls() {
00848         return 'https://www.mediawiki.org/wiki/API:Properties#revisions_.2F_rv';
00849     }
00850 }