[FileBackend] Added getScopedLocksForOps() function.
[lhc/web/wiklou.git] / includes / api / ApiQueryRevisions.php
index c2e93f5..fa58bdf 100644 (file)
@@ -1,9 +1,8 @@
 <?php
-
 /**
- * Created on Sep 7, 2006
  *
- * API for MediaWiki 1.8+
+ *
+ * Created on Sep 7, 2006
  *
  * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
  *
  * with this program; if not, write to the Free Software Foundation, Inc.,
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
  */
 
-if ( !defined( 'MEDIAWIKI' ) ) {
-       // Eclipse helper - will be ignored in production
-       require_once( 'ApiQueryBase.php' );
-}
-
 /**
  * A query action to enumerate revisions of a given page, or show top revisions of multiple pages.
  * Various pieces of information may be shown - flags, comments, and the actual wiki markup of the rev.
@@ -37,12 +33,18 @@ if ( !defined( 'MEDIAWIKI' ) ) {
  */
 class ApiQueryRevisions extends ApiQueryBase {
 
+       private $diffto, $difftotext, $expandTemplates, $generateXML, $section,
+               $token, $parseContent;
+
        public function __construct( $query, $moduleName ) {
                parent::__construct( $query, $moduleName, 'rv' );
        }
 
        private $fld_ids = false, $fld_flags = false, $fld_timestamp = false, $fld_size = false,
-                       $fld_comment = false, $fld_parsedcomment = false, $fld_user = false, $fld_content = false, $fld_tags = false;
+                       $fld_comment = false, $fld_parsedcomment = false, $fld_user = false, $fld_userid = false,
+                       $fld_content = false, $fld_tags = false;
+
+       private $tokenFunctions;
 
        protected function getTokenFunctions() {
                // tokenname => function
@@ -66,13 +68,19 @@ class ApiQueryRevisions extends ApiQueryBase {
                return $this->tokenFunctions;
        }
 
+       /**
+        * @param $pageid
+        * @param $title Title
+        * @param $rev Revision
+        * @return bool|String
+        */
        public static function getRollbackToken( $pageid, $title, $rev ) {
                global $wgUser;
                if ( !$wgUser->isAllowed( 'rollback' ) ) {
                        return false;
                }
-               return $wgUser->editToken( array( $title->getPrefixedText(),
-                                               $rev->getUserText() ) );
+               return $wgUser->getEditToken(
+                       array( $title->getPrefixedText(), $rev->getUserText() ) );
        }
 
        public function execute() {
@@ -105,7 +113,6 @@ class ApiQueryRevisions extends ApiQueryBase {
                        $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' );
                }
 
-               $this->diffto = $this->difftotext = null;
                if ( !is_null( $params['difftotext'] ) ) {
                        $this->difftotext = $params['difftotext'];
                } elseif ( !is_null( $params['diffto'] ) ) {
@@ -113,8 +120,7 @@ class ApiQueryRevisions extends ApiQueryBase {
                                $params['diffto'] = 0;
                        }
                        if ( ( !ctype_digit( $params['diffto'] ) || $params['diffto'] < 0 )
-                                       && $params['diffto'] != 'prev' && $params['diffto'] != 'next' )
-                       {
+                                       && $params['diffto'] != 'prev' && $params['diffto'] != 'next' ) {
                                $this->dieUsage( 'rvdiffto must be set to a non-negative number, "prev", "next" or "cur"', 'diffto' );
                        }
                        // Check whether the revision exists and is readable,
@@ -125,7 +131,7 @@ class ApiQueryRevisions extends ApiQueryBase {
                                if ( !$difftoRev ) {
                                        $this->dieUsageMsg( array( 'nosuchrevid', $params['diffto'] ) );
                                }
-                               if ( !$difftoRev->userCan( Revision::DELETED_TEXT ) ) {
+                               if ( $difftoRev->isDeleted( Revision::DELETED_TEXT ) ) {
                                        $this->setWarning( "Couldn't diff to r{$difftoRev->getID()}: content is hidden" );
                                        $params['diffto'] = null;
                                }
@@ -134,7 +140,7 @@ class ApiQueryRevisions extends ApiQueryBase {
                }
 
                $db = $this->getDB();
-               $this->addTables( array( 'page', 'revision' ) );
+               $this->addTables( 'page' );
                $this->addFields( Revision::selectFields() );
                $this->addWhere( 'page_id = rev_page' );
 
@@ -148,12 +154,22 @@ class ApiQueryRevisions extends ApiQueryBase {
                $this->fld_comment = isset ( $prop['comment'] );
                $this->fld_parsedcomment = isset ( $prop['parsedcomment'] );
                $this->fld_size = isset ( $prop['size'] );
+               $this->fld_sha1 = isset ( $prop['sha1'] );
+               $this->fld_userid = isset( $prop['userid'] );
                $this->fld_user = isset ( $prop['user'] );
                $this->token = $params['token'];
 
                // Possible indexes used
                $index = array();
 
+               $userMax = ( $this->fld_content ? ApiBase::LIMIT_SML1 : ApiBase::LIMIT_BIG1 );
+               $botMax  = ( $this->fld_content ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_BIG2 );
+               $limit = $params['limit'];
+               if ( $limit == 'max' ) {
+                       $limit = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
+                       $this->getResult()->setParsedLimit( $this->getModuleName(), $limit );
+               }
+
                if ( !is_null( $this->token ) || $pageCount > 0 ) {
                        $this->addFields( Revision::selectPageFields() );
                }
@@ -176,7 +192,7 @@ class ApiQueryRevisions extends ApiQueryBase {
                if ( isset( $prop['content'] ) || !is_null( $this->difftotext ) ) {
                        // For each page we will request, the user must have read rights for that page
                        foreach ( $pageSet->getGoodTitles() as $title ) {
-                               if ( !$title->userCanRead() ) {
+                               if ( !$title->userCan( 'read' ) ) {
                                        $this->dieUsage(
                                                'The current user is not allowed to read ' . $title->getPrefixedText(),
                                                'accessdenied' );
@@ -192,6 +208,15 @@ class ApiQueryRevisions extends ApiQueryBase {
 
                        $this->expandTemplates = $params['expandtemplates'];
                        $this->generateXML = $params['generatexml'];
+                       $this->parseContent = $params['parse'];
+                       if ( $this->parseContent ) {
+                               // Must manually initialize unset limit
+                               if ( is_null( $limit ) ) {
+                                       $limit = 1;
+                               }
+                               // We are only going to parse 1 revision per request
+                               $this->validateLimit( 'limit', $limit, 1, 1, 1 );
+                       }
                        if ( isset( $params['section'] ) ) {
                                $this->section = $params['section'];
                        } else {
@@ -199,13 +224,8 @@ class ApiQueryRevisions extends ApiQueryBase {
                        }
                }
 
-               $userMax = ( $this->fld_content ? ApiBase::LIMIT_SML1 : ApiBase::LIMIT_BIG1 );
-               $botMax  = ( $this->fld_content ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_BIG2 );
-               $limit = $params['limit'];
-               if ( $limit == 'max' ) {
-                       $limit = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
-                       $this->getResult()->addValue( 'limits', $this->getModuleName(), $limit );
-               }
+               // Bug 24166 - API error when using rvprop=tags
+               $this->addTables( 'revision' );
 
                if ( $enumRevMode ) {
                        // This is mostly to prevent parameter errors (and optimize SQL?)
@@ -228,14 +248,14 @@ class ApiQueryRevisions extends ApiQueryBase {
                        // one row with the same timestamp for the same page.
                        // The order needs to be the same as start parameter to avoid SQL filesort.
                        if ( is_null( $params['startid'] ) && is_null( $params['endid'] ) ) {
-                               $this->addWhereRange( 'rev_timestamp', $params['dir'],
+                               $this->addTimestampWhereRange( 'rev_timestamp', $params['dir'],
                                        $params['start'], $params['end'] );
                        } else {
                                $this->addWhereRange( 'rev_id', $params['dir'],
                                        $params['startid'], $params['endid'] );
                                // One of start and end can be set
                                // If neither is set, this does nothing
-                               $this->addWhereRange( 'rev_timestamp', $params['dir'],
+                               $this->addTimestampWhereRange( 'rev_timestamp', $params['dir'],
                                        $params['start'], $params['end'], false );
                        }
 
@@ -318,7 +338,6 @@ class ApiQueryRevisions extends ApiQueryBase {
                $this->addOption( 'LIMIT', $limit + 1 );
                $this->addOption( 'USE INDEX', $index );
 
-               $data = array();
                $count = 0;
                $res = $this->select( __METHOD__ );
 
@@ -364,14 +383,21 @@ class ApiQueryRevisions extends ApiQueryBase {
                        $vals['minor'] = '';
                }
 
-               if ( $this->fld_user ) {
+               if ( $this->fld_user || $this->fld_userid ) {
                        if ( $revision->isDeleted( Revision::DELETED_USER ) ) {
                                $vals['userhidden'] = '';
                        } else {
-                               $vals['user'] = $revision->getUserText();
-                               if ( !$revision->getUser() ) {
+                               if ( $this->fld_user ) {
+                                       $vals['user'] = $revision->getUserText();
+                               }
+                               $userid = $revision->getUser();
+                               if ( !$userid ) {
                                        $vals['anon'] = '';
                                }
+
+                               if ( $this->fld_userid ) {
+                                       $vals['userid'] = $userid;
+                               }
                        }
                }
 
@@ -379,8 +405,20 @@ class ApiQueryRevisions extends ApiQueryBase {
                        $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $revision->getTimestamp() );
                }
 
-               if ( $this->fld_size && !is_null( $revision->getSize() ) ) {
-                       $vals['size'] = intval( $revision->getSize() );
+               if ( $this->fld_size ) {
+                       if ( !is_null( $revision->getSize() ) ) {
+                               $vals['size'] = intval( $revision->getSize() );
+                       } else {
+                               $vals['size'] = 0;
+                       }
+               }
+
+               if ( $this->fld_sha1 ) {
+                       if ( $revision->getSha1() != '' ) {
+                               $vals['sha1'] = wfBaseConvert( $revision->getSha1(), 36, 16, 40 );
+                       } else {
+                               $vals['sha1'] = '';
+                       }
                }
 
                if ( $this->fld_comment || $this->fld_parsedcomment ) {
@@ -388,14 +426,13 @@ class ApiQueryRevisions extends ApiQueryBase {
                                $vals['commenthidden'] = '';
                        } else {
                                $comment = $revision->getComment();
-                               
+
                                if ( $this->fld_comment ) {
                                        $vals['comment'] = $comment;
                                }
 
                                if ( $this->fld_parsedcomment ) {
-                                       global $wgUser;
-                                       $vals['parsedcomment'] = $wgUser->getSkin()->formatComment( $comment, $title );
+                                       $vals['parsedcomment'] = Linker::formatComment( $comment, $title );
                                }
                        }
                }
@@ -423,14 +460,13 @@ class ApiQueryRevisions extends ApiQueryBase {
                }
 
                $text = null;
+               global $wgParser;
                if ( $this->fld_content || !is_null( $this->difftotext ) ) {
                        $text = $revision->getText();
                        // Expand templates after getting section content because
                        // template-added sections don't count and Parser::preprocess()
                        // will have less input
                        if ( $this->section !== false ) {
-                               global $wgParser;
-
                                $text = $wgParser->getSection( $text, $this->section, false );
                                if ( $text === false ) {
                                        $this->dieUsage( "There is no section {$this->section} in r" . $revision->getId(), 'nosuchsection' );
@@ -439,8 +475,7 @@ class ApiQueryRevisions extends ApiQueryBase {
                }
                if ( $this->fld_content && !$revision->isDeleted( Revision::DELETED_TEXT ) ) {
                        if ( $this->generateXML ) {
-                               global $wgParser;
-                               $wgParser->startExternalParse( $title, new ParserOptions(), OT_PREPROCESS );
+                               $wgParser->startExternalParse( $title, ParserOptions::newFromContext( $this->getContext() ), OT_PREPROCESS );
                                $dom = $wgParser->preprocessToDom( $text );
                                if ( is_callable( array( $dom, 'saveXML' ) ) ) {
                                        $xml = $dom->saveXML();
@@ -450,8 +485,11 @@ class ApiQueryRevisions extends ApiQueryBase {
                                $vals['parsetree'] = $xml;
 
                        }
-                       if ( $this->expandTemplates ) {
-                               $text = $wgParser->preprocess( $text, $title, new ParserOptions() );
+                       if ( $this->expandTemplates && !$this->parseContent ) {
+                               $text = $wgParser->preprocess( $text, $title, ParserOptions::newFromContext( $this->getContext() ) );
+                       }
+                       if ( $this->parseContent ) {
+                               $text = $wgParser->parse( $text, $title, ParserOptions::newFromContext( $this->getContext() ) )->getText();
                        }
                        ApiResult::setContent( $vals, $text );
                } elseif ( $this->fld_content ) {
@@ -463,11 +501,13 @@ class ApiQueryRevisions extends ApiQueryBase {
                        static $n = 0; // Number of uncached diffs we've had
                        if ( $n < $wgAPIMaxUncachedDiffs ) {
                                $vals['diff'] = array();
+                               $context = new DerivativeContext( $this->getContext() );
+                               $context->setTitle( $title );
                                if ( !is_null( $this->difftotext ) ) {
-                                       $engine = new DifferenceEngine( $title );
+                                       $engine = new DifferenceEngine( $context );
                                        $engine->setText( $text, $this->difftotext );
                                } else {
-                                       $engine = new DifferenceEngine( $title, $revision->getID(), $this->diffto );
+                                       $engine = new DifferenceEngine( $context, $revision->getID(), $this->diffto );
                                        $vals['diff']['from'] = $engine->getOldid();
                                        $vals['diff']['to'] = $engine->getNewid();
                                }
@@ -483,6 +523,17 @@ class ApiQueryRevisions extends ApiQueryBase {
                return $vals;
        }
 
+       public function getCacheMode( $params ) {
+               if ( isset( $params['token'] ) ) {
+                       return 'private';
+               }
+               if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
+                       // formatComment() calls wfMsg() among other things
+                       return 'anon-public-user-private';
+               }
+               return 'public';
+       }
+
        public function getAllowedParams() {
                return array(
                        'prop' => array(
@@ -493,7 +544,9 @@ class ApiQueryRevisions extends ApiQueryBase {
                                        'flags',
                                        'timestamp',
                                        'user',
+                                       'userid',
                                        'size',
+                                       'sha1',
                                        'comment',
                                        'parsedcomment',
                                        'content',
@@ -534,6 +587,7 @@ class ApiQueryRevisions extends ApiQueryBase {
                        'tag' => null,
                        'expandtemplates' => false,
                        'generatexml' => false,
+                       'parse' => false,
                        'section' => null,
                        'token' => array(
                                ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
@@ -548,17 +602,31 @@ class ApiQueryRevisions extends ApiQueryBase {
        public function getParamDescription() {
                $p = $this->getModulePrefix();
                return array(
-                       'prop' => 'Which properties to get for each revision',
+                       'prop' => array(
+                               'Which properties to get for each revision:',
+                               ' ids            - The ID of the revision',
+                               ' flags          - Revision flags (minor)',
+                               ' timestamp      - The timestamp of the revision',
+                               ' user           - User that made the revision',
+                               ' userid         - User id of revision creator',
+                               ' size           - Length (bytes) of the revision',
+                               ' sha1           - SHA-1 (base 16) of the revision',
+                               ' comment        - Comment by the user for revision',
+                               ' parsedcomment  - Parsed comment by the user for the revision',
+                               ' content        - Text of the revision',
+                               ' tags           - Tags for the revision',
+                       ),
                        'limit' => 'Limit how many revisions will be returned (enum)',
                        'startid' => 'From which revision id to start enumeration (enum)',
                        'endid' => 'Stop revision enumeration on this revid (enum)',
                        'start' => 'From which revision timestamp to start enumeration (enum)',
                        'end' => 'Enumerate up to this timestamp (enum)',
-                       'dir' => 'Direction of enumeration - towards "newer" or "older" revisions (enum)',
-                       'user' => 'Only include revisions made by user',
-                       'excludeuser' => 'Exclude revisions made by user',
+                       'dir' => $this->getDirectionDescription( $p, ' (enum)' ),
+                       'user' => 'Only include revisions made by user (enum)',
+                       'excludeuser' => 'Exclude revisions made by user (enum)',
                        'expandtemplates' => 'Expand templates in revision content',
                        'generatexml' => 'Generate XML parse tree for revision content',
+                       'parse' => 'Parse revision content. For performance reasons if this option is used, rvlimit is enforced to 1.',
                        'section' => 'Only retrieve the content of this section number',
                        'token' => 'Which tokens to obtain for each revision',
                        'continue' => 'When more results are available, use this to continue',
@@ -573,7 +641,7 @@ class ApiQueryRevisions extends ApiQueryBase {
        public function getDescription() {
                return array(
                        'Get revision information',
-                       'This module may be used in several ways:',
+                       'May be used in several ways:',
                        ' 1) Get data about a set of pages (last revision), by setting titles or pageids parameter',
                        ' 2) Get revisions for one given page, by using titles/pageids with start/end/limit params',
                        ' 3) Get data about a set of revisions by setting their IDs with revids parameter',
@@ -594,15 +662,15 @@ class ApiQueryRevisions extends ApiQueryBase {
                ) );
        }
 
-       protected function getExamples() {
+       public function getExamples() {
                return array(
-                       'Get data with content for the last revision of titles "API" and "Main Page":',
+                       'Get data with content for the last revision of titles "API" and "Main Page"',
                        '  api.php?action=query&prop=revisions&titles=API|Main%20Page&rvprop=timestamp|user|comment|content',
-                       'Get last 5 revisions of the "Main Page":',
+                       'Get last 5 revisions of the "Main Page"',
                        '  api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment',
-                       'Get first 5 revisions of the "Main Page":',
+                       'Get first 5 revisions of the "Main Page"',
                        '  api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer',
-                       'Get first 5 revisions of the "Main Page" made after 2006-05-01:',
+                       'Get first 5 revisions of the "Main Page" made after 2006-05-01',
                        '  api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer&rvstart=20060501000000',
                        'Get first 5 revisions of the "Main Page" that were not made made by anonymous user "127.0.0.1"',
                        '  api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvexcludeuser=127.0.0.1',
@@ -611,6 +679,10 @@ class ApiQueryRevisions extends ApiQueryBase {
                );
        }
 
+       public function getHelpUrls() {
+               return 'https://www.mediawiki.org/wiki/API:Properties#revisions_.2F_rv';
+       }
+
        public function getVersion() {
                return __CLASS__ . ': $Id$';
        }