API: Add rvdifftotext parameter to prop=revisions to allow diffing to a fixed text...
authorRoan Kattouw <catrope@users.mediawiki.org>
Wed, 9 Dec 2009 18:34:32 +0000 (18:34 +0000)
committerRoan Kattouw <catrope@users.mediawiki.org>
Wed, 9 Dec 2009 18:34:32 +0000 (18:34 +0000)
includes/api/ApiQueryRevisions.php

index e705cea..062ac10 100644 (file)
@@ -100,10 +100,13 @@ class ApiQueryRevisions extends ApiQueryBase {
                if ($pageCount > 1 && $enumRevMode)
                        $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');
 
-               if (!is_null($params['diffto'])) {
+               $this->diffto = $this->difftotext = null;
+               if (!is_null($params['difftotext'])) {
+                       $this->difftotext = $params['difftotext'];
+               } else if (!is_null($params['diffto'])) {
                        if ($params['diffto'] == 'cur')
                                $params['diffto'] = 0;
-                       if ((!ctype_digit($params['diffto']) || $params['diffto'] < 0) 
+                       if ((!ctype_digit($params['diffto']) || $params['diffto'] < 0)
                                        && $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,
@@ -118,6 +121,7 @@ class ApiQueryRevisions extends ApiQueryBase {
                                        $params['diffto'] = null;
                                }
                        }
+                       $this->diffto = $params['diffto'];
                }
 
                $db = $this->getDB();
@@ -136,7 +140,6 @@ class ApiQueryRevisions extends ApiQueryBase {
                $this->fld_size = isset ($prop['size']);
                $this->fld_user = isset ($prop['user']);
                $this->token = $params['token'];
-               $this->diffto = $params['diffto'];
 
                if ( !is_null($this->token) || $pageCount > 0) {
                        $this->addFields( Revision::selectPageFields() );
@@ -155,7 +158,7 @@ class ApiQueryRevisions extends ApiQueryBase {
                        $this->addWhereFld('ct_tag' , $params['tag']);
                }
                
-               if (isset ($prop['content'])) {
+               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) {
@@ -170,7 +173,7 @@ class ApiQueryRevisions extends ApiQueryBase {
                        $this->addFields('old_id');
                        $this->addFields(Revision::selectTextFields());
 
-                       $this->fld_content = true;
+                       $this->fld_content = isset($prop['content']);
 
                        $this->expandTemplates = $params['expandtemplates'];
                        $this->generateXML = $params['generatexml'];
@@ -389,7 +392,8 @@ class ApiQueryRevisions extends ApiQueryBase {
                        }
                }
                
-               if ($this->fld_content && !$revision->isDeleted(Revision::DELETED_TEXT)) {
+               $text = null;
+               if ($this->fld_content || !is_null($this->difftotext)) {
                        global $wgParser;
                        $text = $revision->getText();
                        # Expand templates after getting section content because
@@ -400,6 +404,8 @@ class ApiQueryRevisions extends ApiQueryBase {
                                if($text === false)
                                        $this->dieUsage("There is no section {$this->section} in r".$revision->getId(), 'nosuchsection');
                        }
+               }
+               if ($this->fld_content && !$revision->isDeleted(Revision::DELETED_TEXT)) {
                        if ($this->generateXML) {
                                $wgParser->startExternalParse( $title, new ParserOptions(), OT_PREPROCESS );
                                $dom = $wgParser->preprocessToDom( $text );
@@ -419,14 +425,20 @@ class ApiQueryRevisions extends ApiQueryBase {
                        $vals['texthidden'] = '';
                }
 
-               if (!is_null($this->diffto)) {
+               if (!is_null($this->diffto) || !is_null($this->difftotext)) {
                        global $wgAPIMaxUncachedDiffs;
-                       static $n = 0; // Numer of uncached diffs we've had
-                       if($n< $wgAPIMaxUncachedDiffs) {
-                               $engine = new DifferenceEngine($title, $revision->getID(), $this->diffto);
+                       static $n = 0; // Number of uncached diffs we've had
+                       if($n < $wgAPIMaxUncachedDiffs) {
+                               $vals['diff'] = array();
+                               if(!is_null($this->difftotext)) {
+                                       $engine = new DifferenceEngine($title);
+                                       $engine->setText($text, $this->difftotext);
+                               } else {
+                                       $engine = new DifferenceEngine($title, $revision->getID(), $this->diffto);
+                                       $vals['diff']['from'] = $engine->getOldid();
+                                       $vals['diff']['to'] = $engine->getNewid();
+                               }
                                $difftext = $engine->getDiffBody();
-                               $vals['diff']['from'] = $engine->getOldid();
-                               $vals['diff']['to'] = $engine->getNewid();
                                ApiResult::setContent($vals['diff'], $difftext);
                                if(!$engine->wasCacheHit())
                                        $n++;
@@ -494,6 +506,7 @@ class ApiQueryRevisions extends ApiQueryBase {
                        ),
                        'continue' => null,
                        'diffto' => null,
+                       'difftotext' => null,
                );
        }
 
@@ -515,6 +528,8 @@ class ApiQueryRevisions extends ApiQueryBase {
                        'continue' => 'When more results are available, use this to continue',
                        'diffto' => array('Revision ID to diff each revision to.',
                                'Use "prev", "next" and "cur" for the previous, next and current revision respectively.'),
+                       'difftotext' => array('Text to diff each revision to. Only diffs a limited number of revisions.',
+                               'Overrides diffto. If rvsection is set, only that section will be diffed against this text.'),
                        'tag' => 'Only list revisions tagged with this tag',
                );
        }