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