Merge "(bug 33222) Add parentid to revision in export xml"
[lhc/web/wiklou.git] / includes / api / ApiQueryRevisions.php
index 3591e29..1a5ad17 100644 (file)
@@ -131,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;
                                }
@@ -154,6 +154,7 @@ 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'];
@@ -191,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' );
@@ -223,6 +224,13 @@ class ApiQueryRevisions extends ApiQueryBase {
                        }
                }
 
+               // add user name, if needed
+               if ( $this->fld_user ) {
+                       $this->addTables( 'user' );
+                       $this->addJoinConds( array( 'user' => Revision::userJoinCond() ) );
+                       $this->addFields( Revision::selectUserFields() );
+               }
+
                // Bug 24166 - API error when using rvprop=tags
                $this->addTables( 'revision' );
 
@@ -289,7 +297,7 @@ class ApiQueryRevisions extends ApiQueryBase {
                        $this->addWhereFld( 'rev_id', array_keys( $revs ) );
 
                        if ( !is_null( $params['continue'] ) ) {
-                               $this->addWhere( "rev_id >= '" . intval( $params['continue'] ) . "'" );
+                               $this->addWhere( 'rev_id >= ' . intval( $params['continue'] ) );
                        }
                        $this->addOption( 'ORDER BY', 'rev_id' );
 
@@ -321,12 +329,15 @@ class ApiQueryRevisions extends ApiQueryBase {
                                $pageid = intval( $cont[0] );
                                $revid = intval( $cont[1] );
                                $this->addWhere(
-                                       "rev_page > '$pageid' OR " .
-                                       "(rev_page = '$pageid' AND " .
-                                       "rev_id >= '$revid')"
+                                       "rev_page > $pageid OR " .
+                                       "(rev_page = $pageid AND " .
+                                       "rev_id >= $revid)"
                                );
                        }
-                       $this->addOption( 'ORDER BY', 'rev_page, rev_id' );
+                       $this->addOption( 'ORDER BY', array(
+                               'rev_page',
+                               'rev_id'
+                       ));
 
                        // assumption testing -- we should never get more then $pageCount rows.
                        $limit = $pageCount;
@@ -404,8 +415,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 ) {
@@ -462,7 +485,7 @@ class ApiQueryRevisions extends ApiQueryBase {
                }
                if ( $this->fld_content && !$revision->isDeleted( Revision::DELETED_TEXT ) ) {
                        if ( $this->generateXML ) {
-                               $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();
@@ -473,10 +496,10 @@ class ApiQueryRevisions extends ApiQueryBase {
 
                        }
                        if ( $this->expandTemplates && !$this->parseContent ) {
-                               $text = $wgParser->preprocess( $text, $title, new ParserOptions() );
+                               $text = $wgParser->preprocess( $text, $title, ParserOptions::newFromContext( $this->getContext() ) );
                        }
                        if ( $this->parseContent ) {
-                               $text = $wgParser->parse( $text, $title, new ParserOptions() )->getText();
+                               $text = $wgParser->parse( $text, $title, ParserOptions::newFromContext( $this->getContext() ) )->getText();
                        }
                        ApiResult::setContent( $vals, $text );
                } elseif ( $this->fld_content ) {
@@ -533,6 +556,7 @@ class ApiQueryRevisions extends ApiQueryBase {
                                        'user',
                                        'userid',
                                        'size',
+                                       'sha1',
                                        'comment',
                                        'parsedcomment',
                                        'content',
@@ -595,7 +619,8 @@ class ApiQueryRevisions extends ApiQueryBase {
                                ' timestamp      - The timestamp of the revision',
                                ' user           - User that made the revision',
                                ' userid         - User id of revision creator',
-                               ' size           - Length of the revision',
+                               ' 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',
@@ -623,6 +648,66 @@ class ApiQueryRevisions extends ApiQueryBase {
                );
        }
 
+       public function getResultProperties() {
+               $props = array(
+                       '' => array(),
+                       'ids' => array(
+                               'revid' => 'integer',
+                               'parentid' => array(
+                                       ApiBase::PROP_TYPE => 'integer',
+                                       ApiBase::PROP_NULLABLE => true
+                               )
+                       ),
+                       'flags' => array(
+                               'minor' => 'boolean'
+                       ),
+                       'user' => array(
+                               'userhidden' => 'boolean',
+                               'user' => 'string',
+                               'anon' => 'boolean'
+                       ),
+                       'userid' => array(
+                               'userhidden' => 'boolean',
+                               'userid' => 'integer',
+                               'anon' => 'boolean'
+                       ),
+                       'timestamp' => array(
+                               'timestamp' => 'timestamp'
+                       ),
+                       'size' => array(
+                               'size' => 'integer'
+                       ),
+                       'sha1' => array(
+                               'sha1' => 'string'
+                       ),
+                       'comment' => array(
+                               'commenthidden' => 'boolean',
+                               'comment' => array(
+                                       ApiBase::PROP_TYPE => 'string',
+                                       ApiBase::PROP_NULLABLE => true
+                               )
+                       ),
+                       'parsedcomment' => array(
+                               'commenthidden' => 'boolean',
+                               'parsedcomment' => array(
+                                       ApiBase::PROP_TYPE => 'string',
+                                       ApiBase::PROP_NULLABLE => true
+                               )
+                       ),
+                       'content' => array(
+                               '*' => array(
+                                       ApiBase::PROP_TYPE => 'string',
+                                       ApiBase::PROP_NULLABLE => true
+                               ),
+                               'texthidden' => 'boolean'
+                       )
+               );
+
+               self::addTokenProperties( $props, $this->getTokenFunctions() );
+
+               return $props;
+       }
+
        public function getDescription() {
                return array(
                        'Get revision information',
@@ -649,13 +734,13 @@ class ApiQueryRevisions extends ApiQueryBase {
 
        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',
@@ -665,7 +750,7 @@ class ApiQueryRevisions extends ApiQueryBase {
        }
 
        public function getHelpUrls() {
-               return 'http://www.mediawiki.org/wiki/API:Properties#revisions_.2F_rv';
+               return 'https://www.mediawiki.org/wiki/API:Properties#revisions_.2F_rv';
        }
 
        public function getVersion() {