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