Merge "Rewrite pref cleanup script"
[lhc/web/wiklou.git] / includes / api / ApiQueryRevisions.php
index 48f6046..ef0223a 100644 (file)
@@ -1,9 +1,5 @@
 <?php
 /**
- *
- *
- * Created on Sep 7, 2006
- *
  * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
  *
  * This program is free software; you can redistribute it and/or modify
@@ -60,7 +56,7 @@ class ApiQueryRevisions extends ApiQueryRevisionsBase {
                }
 
                $this->tokenFunctions = [
-                       'rollback' => [ 'ApiQueryRevisions', 'getRollbackToken' ]
+                       'rollback' => [ self::class, 'getRollbackToken' ]
                ];
                Hooks::run( 'APIQueryRevisionsTokens', [ &$this->tokenFunctions ] );
 
@@ -129,20 +125,31 @@ class ApiQueryRevisions extends ApiQueryRevisionsBase {
                }
 
                $db = $this->getDB();
-               $this->addTables( [ 'revision', 'page' ] );
-               $this->addJoinConds(
-                       [ 'page' => [ 'INNER JOIN', [ 'page_id = rev_page' ] ] ]
-               );
 
                if ( $resultPageSet === null ) {
                        $this->parseParameters( $params );
                        $this->token = $params['token'];
-                       $this->addFields( Revision::selectFields() );
+                       $opts = [];
                        if ( $this->token !== null || $pageCount > 0 ) {
-                               $this->addFields( Revision::selectPageFields() );
+                               $opts[] = 'page';
+                       }
+                       if ( $this->fetchContent ) {
+                               $opts[] = 'text';
                        }
+                       if ( $this->fld_user ) {
+                               $opts[] = 'user';
+                       }
+                       $revQuery = Revision::getQueryInfo( $opts );
+                       $this->addTables( $revQuery['tables'] );
+                       $this->addFields( $revQuery['fields'] );
+                       $this->addJoinConds( $revQuery['joins'] );
                } else {
                        $this->limit = $this->getParameter( 'limit' ) ?: 10;
+                       // Always join 'page' so orphaned revisions are filtered out
+                       $this->addTables( [ 'revision', 'page' ] );
+                       $this->addJoinConds(
+                               [ 'page' => [ 'INNER JOIN', [ 'page_id = rev_page' ] ] ]
+                       );
                        $this->addFields( [ 'rev_id', 'rev_timestamp', 'rev_page' ] );
                }
 
@@ -162,11 +169,11 @@ class ApiQueryRevisions extends ApiQueryRevisionsBase {
                        $this->addWhereFld( 'ct_tag', $params['tag'] );
                }
 
-               if ( $this->fetchContent ) {
+               if ( $resultPageSet === null && $this->fetchContent ) {
                        // For each page we will request, the user must have read rights for that page
                        $user = $this->getUser();
                        $status = Status::newGood();
-                       /** @var $title Title */
+                       /** @var Title $title */
                        foreach ( $pageSet->getGoodTitles() as $title ) {
                                if ( !$title->userCan( 'read', $user ) ) {
                                        $status->fatal( ApiMessage::create(
@@ -178,20 +185,6 @@ class ApiQueryRevisions extends ApiQueryRevisionsBase {
                        if ( !$status->isGood() ) {
                                $this->dieStatus( $status );
                        }
-
-                       $this->addTables( 'text' );
-                       $this->addJoinConds(
-                               [ 'text' => [ 'INNER JOIN', [ 'rev_text_id=old_id' ] ] ]
-                       );
-                       $this->addFields( 'old_id' );
-                       $this->addFields( Revision::selectTextFields() );
-               }
-
-               // add user name, if needed
-               if ( $this->fld_user ) {
-                       $this->addTables( 'user' );
-                       $this->addJoinConds( [ 'user' => Revision::userJoinCond() ] );
-                       $this->addFields( Revision::selectUserFields() );
                }
 
                if ( $enumRevMode ) {
@@ -218,10 +211,75 @@ class ApiQueryRevisions extends ApiQueryRevisionsBase {
                                );
                        }
 
-                       $this->addTimestampWhereRange( 'rev_timestamp', $params['dir'],
-                               $params['start'], $params['end'] );
-                       $this->addWhereRange( 'rev_id', $params['dir'],
-                               $params['startid'], $params['endid'] );
+                       // Convert startid/endid to timestamps (T163532)
+                       $revids = [];
+                       if ( $params['startid'] !== null ) {
+                               $revids[] = (int)$params['startid'];
+                       }
+                       if ( $params['endid'] !== null ) {
+                               $revids[] = (int)$params['endid'];
+                       }
+                       if ( $revids ) {
+                               $db = $this->getDB();
+                               $sql = $db->unionQueries( [
+                                       $db->selectSQLText(
+                                               'revision',
+                                               [ 'id' => 'rev_id', 'ts' => 'rev_timestamp' ],
+                                               [ 'rev_id' => $revids ],
+                                               __METHOD__
+                                       ),
+                                       $db->selectSQLText(
+                                               'archive',
+                                               [ 'id' => 'ar_rev_id', 'ts' => 'ar_timestamp' ],
+                                               [ 'ar_rev_id' => $revids ],
+                                               __METHOD__
+                                       ),
+                               ], false );
+                               $res = $db->query( $sql, __METHOD__ );
+                               foreach ( $res as $row ) {
+                                       if ( (int)$row->id === (int)$params['startid'] ) {
+                                               $params['start'] = $row->ts;
+                                       }
+                                       if ( (int)$row->id === (int)$params['endid'] ) {
+                                               $params['end'] = $row->ts;
+                                       }
+                               }
+                               if ( $params['startid'] !== null && $params['start'] === null ) {
+                                       $p = $this->encodeParamName( 'startid' );
+                                       $this->dieWithError( [ 'apierror-revisions-badid', $p ], "badid_$p" );
+                               }
+                               if ( $params['endid'] !== null && $params['end'] === null ) {
+                                       $p = $this->encodeParamName( 'endid' );
+                                       $this->dieWithError( [ 'apierror-revisions-badid', $p ], "badid_$p" );
+                               }
+
+                               if ( $params['start'] !== null ) {
+                                       $op = ( $params['dir'] === 'newer' ? '>' : '<' );
+                                       $ts = $db->addQuotes( $db->timestampOrNull( $params['start'] ) );
+                                       if ( $params['startid'] !== null ) {
+                                               $this->addWhere( "rev_timestamp $op $ts OR "
+                                                       . "rev_timestamp = $ts AND rev_id $op= " . intval( $params['startid'] ) );
+                                       } else {
+                                               $this->addWhere( "rev_timestamp $op= $ts" );
+                                       }
+                               }
+                               if ( $params['end'] !== null ) {
+                                       $op = ( $params['dir'] === 'newer' ? '<' : '>' ); // Yes, opposite of the above
+                                       $ts = $db->addQuotes( $db->timestampOrNull( $params['end'] ) );
+                                       if ( $params['endid'] !== null ) {
+                                               $this->addWhere( "rev_timestamp $op $ts OR "
+                                                       . "rev_timestamp = $ts AND rev_id $op= " . intval( $params['endid'] ) );
+                                       } else {
+                                               $this->addWhere( "rev_timestamp $op= $ts" );
+                                       }
+                               }
+                       } else {
+                               $this->addTimestampWhereRange( 'rev_timestamp', $params['dir'],
+                                       $params['start'], $params['end'] );
+                       }
+
+                       $sort = ( $params['dir'] === 'newer' ? '' : 'DESC' );
+                       $this->addOption( 'ORDER BY', [ "rev_timestamp $sort", "rev_id $sort" ] );
 
                        // There is only one ID, use it
                        $ids = array_keys( $pageSet->getGoodTitles() );
@@ -244,7 +302,7 @@ class ApiQueryRevisions extends ApiQueryRevisionsBase {
                                }
                        }
                        if ( $params['user'] !== null || $params['excludeuser'] !== null ) {
-                               // Paranoia: avoid brute force searches (bug 17342)
+                               // Paranoia: avoid brute force searches (T19342)
                                if ( !$this->getUser()->isAllowed( 'deletedhistory' ) ) {
                                        $bitmask = Revision::DELETED_USER;
                                } elseif ( !$this->getUser()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
@@ -447,6 +505,6 @@ class ApiQueryRevisions extends ApiQueryRevisionsBase {
        }
 
        public function getHelpUrls() {
-               return 'https://www.mediawiki.org/wiki/API:Revisions';
+               return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Revisions';
        }
 }