* API: Listing (semi-)deleted revisions and log entries (with rev_/log_deleted !...
[lhc/web/wiklou.git] / includes / api / ApiQueryRevisions.php
index 80cfb45..258f460 100644 (file)
@@ -33,7 +33,7 @@ if (!defined('MEDIAWIKI')) {
  * Various pieces of information may be shown - flags, comments, and the actual wiki markup of the rev.
  * In the enumeration mode, ranges of revisions may be requested and filtered.
  *
- * @addtogroup API
+ * @ingroup API
  */
 class ApiQueryRevisions extends ApiQueryBase {
 
@@ -44,15 +44,46 @@ class ApiQueryRevisions extends ApiQueryBase {
        private $fld_ids = false, $fld_flags = false, $fld_timestamp = false, $fld_size = false,
                        $fld_comment = false, $fld_user = false, $fld_content = false;
 
+       protected function getTokenFunctions() {
+               // tokenname => function
+               // function prototype is func($pageid, $title, $rev)
+               // should return token or false
+
+               // Don't call the hooks twice
+               if(isset($this->tokenFunctions))
+                       return $this->tokenFunctions;
+
+               // If we're in JSON callback mode, no tokens can be obtained
+               if(!is_null($this->getMain()->getRequest()->getVal('callback')))
+                       return array();
+
+               $this->tokenFunctions = array(
+                       'rollback' => array( 'ApiQueryRevisions', 'getRollbackToken' )
+               );
+               wfRunHooks('APIQueryRevisionsTokens', array(&$this->tokenFunctions));
+               return $this->tokenFunctions;
+       }
+
+       public static function getRollbackToken($pageid, $title, $rev)
+       {
+               global $wgUser;
+               if(!$wgUser->isAllowed('rollback'))
+                       return false;
+               return $wgUser->editToken(array($title->getPrefixedText(),
+                                               $rev->getUserText()));
+       }
+
        public function execute() {
-               $limit = $startid = $endid = $start = $end = $dir = $prop = $user = $excludeuser = $expandtemplates = $section = $token = null;
-               extract($this->extractRequestParams(false));
+               $params = $this->extractRequestParams(false);
 
                // If any of those parameters are used, work in 'enumeration' mode.
                // Enum mode can only be used when exactly one page is provided.
                // Enumerating revisions on multiple pages make it extremely
                // difficult to manage continuations and require additional SQL indexes
-               $enumRevMode = (!is_null($user) || !is_null($excludeuser) || !is_null($limit) || !is_null($startid) || !is_null($endid) || $dir === 'newer' || !is_null($start) || !is_null($end));
+               $enumRevMode = (!is_null($params['user']) || !is_null($params['excludeuser']) ||
+                               !is_null($params['limit']) || !is_null($params['startid']) ||
+                               !is_null($params['endid']) || $params['dir'] === 'newer' ||
+                               !is_null($params['start']) || !is_null($params['end']));
 
 
                $pageSet = $this->getPageSet();
@@ -70,9 +101,11 @@ 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->addTables('revision');
-               $this->addFields( Revision::selectFields() );
+               $this->addFields(Revision::selectFields());
+               $this->addTables('page');
+               $this->addWhere('page_id = rev_page');
 
-               $prop = array_flip($prop);
+               $prop = array_flip($params['prop']);
 
                // Optional fields
                $this->fld_ids = isset ($prop['ids']);
@@ -81,13 +114,10 @@ class ApiQueryRevisions extends ApiQueryBase {
                $this->fld_timestamp = isset ($prop['timestamp']);
                $this->fld_comment = isset ($prop['comment']);
                $this->fld_size = isset ($prop['size']);
-               $this->tok_rollback = false; // Prevent PHP undefined property notice
-               if(!is_null($token))
-                       $this->tok_rollback = $this->getTokenFlag($token, 'rollback');
                $this->fld_user = isset ($prop['user']);
+               $this->token = $params['token'];
 
-               if ( $this->tok_rollback || ( $this->fld_content && $this->expandTemplates ) || $pageCount > 0) {
-                       $this->addTables( 'page' );
+               if ( !is_null($this->token) || $pageCount > 0) {
                        $this->addFields( Revision::selectPageFields() );
                }
 
@@ -104,19 +134,21 @@ class ApiQueryRevisions extends ApiQueryBase {
                        $this->addTables('text');
                        $this->addWhere('rev_text_id=old_id');
                        $this->addFields('old_id');
-                       $this->addFields( Revision::selectTextFields() );
+                       $this->addFields(Revision::selectTextFields());
 
                        $this->fld_content = true;
 
-                       $this->expandTemplates = $expandtemplates;
-                       if(isset($section))
-                               $this->section = $section;
+                       $this->expandTemplates = $params['expandtemplates'];
+                       $this->generateXML = $params['generatexml'];
+                       if(isset($params['section']))
+                               $this->section = $params['section'];
                        else
                                $this->section = false;
                }
 
                $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 );
@@ -125,13 +157,13 @@ class ApiQueryRevisions extends ApiQueryBase {
                if ($enumRevMode) {
 
                        // This is mostly to prevent parameter errors (and optimize SQL?)
-                       if (!is_null($startid) && !is_null($start))
+                       if (!is_null($params['startid']) && !is_null($params['start']))
                                $this->dieUsage('start and startid cannot be used together', 'badparams');
 
-                       if (!is_null($endid) && !is_null($end))
+                       if (!is_null($params['endid']) && !is_null($params['end']))
                                $this->dieUsage('end and endid cannot be used together', 'badparams');
 
-                       if(!is_null($user) && !is_null( $excludeuser))
+                       if(!is_null($params['user']) && !is_null($params['excludeuser']))
                                $this->dieUsage('user and excludeuser cannot be used together', 'badparams');
 
                        // This code makes an assumption that sorting by rev_id and rev_timestamp produces
@@ -141,10 +173,12 @@ 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($startid) && is_null($endid))
-                               $this->addWhereRange('rev_timestamp', $dir, $start, $end);
+                       if (is_null($params['startid']) && is_null($params['endid']))
+                               $this->addWhereRange('rev_timestamp', $params['dir'],
+                                       $params['start'], $params['end']);
                        else
-                               $this->addWhereRange('rev_id', $dir, $startid, $endid);
+                               $this->addWhereRange('rev_id', $params['dir'],
+                                       $params['startid'], $params['endid']);
 
                        // must manually initialize unset limit
                        if (is_null($limit))
@@ -154,30 +188,42 @@ class ApiQueryRevisions extends ApiQueryBase {
                        // There is only one ID, use it
                        $this->addWhereFld('rev_page', current(array_keys($pageSet->getGoodTitles())));
 
-                       if(!is_null($user)) {
-                               $this->addWhereFld('rev_user_text', $user);
-                       } elseif (!is_null( $excludeuser)) {
-                               $this->addWhere('rev_user_text != ' . $this->getDB()->addQuotes($excludeuser));
+                       if(!is_null($params['user'])) {
+                               $this->addWhereFld('rev_user_text', $params['user']);
+                       } elseif (!is_null($params['excludeuser'])) {
+                               $this->addWhere('rev_user_text != ' .
+                                       $this->getDB()->addQuotes($params['excludeuser']));
+                       }
+                       if(!is_null($params['user']) || !is_null($params['excludeuser'])) {
+                               // Paranoia: avoid brute force searches (bug 17342)
+                               $this->addWhere('rev_deleted & ' . Revision::DELETED_USER . ' = 0');
                        }
                }
                elseif ($revCount > 0) {
-                       $this->validateLimit('rev_count', $revCount, 1, $userMax, $botMax);
+                       $max = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
+                       $revs = $pageSet->getRevisionIDs();
+                       if(self::truncateArray($revs, $max))
+                               $this->setWarning("Too many values supplied for parameter 'revids': the limit is $max"); 
 
                        // Get all revision IDs
-                       $this->addWhereFld('rev_id', array_keys($pageSet->getRevisionIDs()));
+                       $this->addWhereFld('rev_id', array_keys($revs));
 
                        // assumption testing -- we should never get more then $revCount rows.
                        $limit = $revCount;
                }
                elseif ($pageCount > 0) {
+                       $max = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
+                       $titles = $pageSet->getGoodTitles();
+                       if(self::truncateArray($titles, $max))
+                               $this->setWarning("Too many values supplied for parameter 'titles': the limit is $max");
+                       
                        // When working in multi-page non-enumeration mode,
                        // limit to the latest revision only
                        $this->addWhere('page_id=rev_page');
                        $this->addWhere('page_latest=rev_id');
-                       $this->validateLimit('page_count', $pageCount, 1, $userMax, $botMax);
-
+                       
                        // Get all page IDs
-                       $this->addWhereFld('page_id', array_keys($pageSet->getGoodTitles()));
+                       $this->addWhereFld('page_id', array_keys($titles));
 
                        // assumption testing -- we should never get more then $pageCount rows.
                        $limit = $pageCount;
@@ -238,9 +284,13 @@ class ApiQueryRevisions extends ApiQueryBase {
                        $vals['minor'] = '';
 
                if ($this->fld_user) {
-                       $vals['user'] = $revision->getUserText();
-                       if (!$revision->getUser())
-                               $vals['anon'] = '';
+                       if ($revision->isDeleted(Revision::DELETED_USER)) {
+                               $vals['userhidden'] = '';
+                       } else {
+                               $vals['user'] = $revision->getUserText();
+                               if (!$revision->getUser())
+                                       $vals['anon'] = '';
+                       }
                }
 
                if ($this->fld_timestamp) {
@@ -252,24 +302,32 @@ class ApiQueryRevisions extends ApiQueryBase {
                }
 
                if ($this->fld_comment) {
-                       $comment = $revision->getComment();
-                       if (!empty($comment))           
-                               $vals['comment'] = $comment;
+                       if ($revision->isDeleted(Revision::DELETED_COMMENT)) {
+                               $vals['commenthidden'] = '';
+                       } else {
+                               $comment = $revision->getComment();
+                               if (strval($comment) !== '')
+                                       $vals['comment'] = $comment;
+                       }
                }
 
-               if($this->tok_rollback || ($this->fld_content && $this->expandTemplates))
+               if(!is_null($this->token) || ($this->fld_content && $this->expandTemplates))
                        $title = $revision->getTitle();
 
-               if($this->tok_rollback) {
-                       global $wgUser;
-                       $vals['rollbacktoken'] = $wgUser->editToken( array(
-                                       $title->getPrefixedText(), 
-                                       $revision->getUserText(),
-                       ) );
+               if(!is_null($this->token))
+               {
+                       $tokenFunctions = $this->getTokenFunctions();
+                       foreach($this->token as $t)
+                       {
+                               $val = call_user_func($tokenFunctions[$t], $title->getArticleID(), $title, $revision);
+                               if($val === false)
+                                       $this->setWarning("Action '$t' is not allowed for the current user");
+                               else
+                                       $vals[$t . 'token'] = $val;
+                       }
                }
-
-
-               if ($this->fld_content) {
+               
+               if ($this->fld_content && !$revision->isDeleted(Revision::DELETED_TEXT)) {
                        global $wgParser;
                        $text = $revision->getText();
                        # Expand templates after getting section content because
@@ -280,10 +338,23 @@ class ApiQueryRevisions extends ApiQueryBase {
                                if($text === false)
                                        $this->dieUsage("There is no section {$this->section} in r".$revision->getId(), 'nosuchsection');
                        }
+                       if ($this->generateXML) {
+                               $wgParser->startExternalParse( $title, new ParserOptions(), OT_PREPROCESS );
+                               $dom = $wgParser->preprocessToDom( $text );
+                               if ( is_callable( array( $dom, 'saveXML' ) ) ) {
+                                       $xml = $dom->saveXML();
+                               } else {
+                                       $xml = $dom->__toString();
+                               }
+                               $vals['parsetree'] = $xml;
+                               
+                       }
                        if ($this->expandTemplates) {
                                $text = $wgParser->preprocess( $text, $title, new ParserOptions() );
                        }
                        ApiResult :: setContent($vals, $text);
+               } else if ($this->fld_content) {
+                       $vals['texthidden'] = '';
                }
                return $vals;
        }
@@ -334,15 +405,11 @@ class ApiQueryRevisions extends ApiQueryBase {
                        'excludeuser' => array(
                                ApiBase :: PARAM_TYPE => 'user'
                        ),
-
                        'expandtemplates' => false,
-                       'section' => array(
-                               ApiBase :: PARAM_TYPE => 'integer'
-                       ),
+                       'generatexml' => false,
+                       'section' => null,
                        'token' => array(
-                               ApiBase :: PARAM_TYPE => array(
-                                       'rollback'
-                               ),
+                               ApiBase :: PARAM_TYPE => array_keys($this->getTokenFunctions()),
                                ApiBase :: PARAM_ISMULTI => true
                        ),
                );
@@ -360,6 +427,7 @@ class ApiQueryRevisions extends ApiQueryBase {
                        'user' => 'only include revisions made by user',
                        'excludeuser' => 'exclude revisions made by user',
                        'expandtemplates' => 'expand templates in revision content',
+                       'generatexml' => 'generate XML parse tree for revision content',
                        'section' => 'only retrieve the content of this section',
                        'token' => 'Which tokens to obtain for each revision',
                );