Patch from MaxSem re bug 15162 - handle throttling in the login API
[lhc/web/wiklou.git] / includes / api / ApiQueryWatchlist.php
index a101313..17467e0 100644 (file)
@@ -31,8 +31,8 @@ if (!defined('MEDIAWIKI')) {
 /**
  * This query action allows clients to retrieve a list of recently modified pages
  * that are part of the logged-in user's watchlist.
- * 
- * @addtogroup API
+ *
+ * @ingroup API
  */
 class ApiQueryWatchlist extends ApiQueryGeneratorBase {
 
@@ -49,8 +49,8 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase {
        }
 
        private $fld_ids = false, $fld_title = false, $fld_patrol = false, $fld_flags = false,
-                       $fld_timestamp = false, $fld_user = false, $fld_comment = false;
-       
+                       $fld_timestamp = false, $fld_user = false, $fld_comment = false, $fld_sizes = false;
+
        private function run($resultPageSet = null) {
                global $wgUser, $wgDBtype;
 
@@ -59,7 +59,7 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase {
                if (!$wgUser->isLoggedIn())
                        $this->dieUsage('You must be logged-in to have a watchlist', 'notloggedin');
 
-               $allrev = $start = $end = $namespace = $dir = $limit = $prop = null;
+               $allrev = $start = $end = $namespace = $dir = $limit = $prop = $show = null;
                extract($this->extractRequestParams());
 
                if (!is_null($prop) && is_null($resultPageSet)) {
@@ -72,6 +72,7 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase {
                        $this->fld_user = isset($prop['user']);
                        $this->fld_comment = isset($prop['comment']);
                        $this->fld_timestamp = isset($prop['timestamp']);
+                       $this->fld_sizes = isset($prop['sizes']);
                        $this->fld_patrol = isset($prop['patrol']);
 
                        if ($this->fld_patrol) {
@@ -96,6 +97,8 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase {
                        $this->addFieldsIf('rc_user_text', $this->fld_user);
                        $this->addFieldsIf('rc_comment', $this->fld_comment);
                        $this->addFieldsIf('rc_patrolled', $this->fld_patrol);
+                       $this->addFieldsIf('rc_old_len', $this->fld_sizes);
+                       $this->addFieldsIf('rc_new_len', $this->fld_sizes);
                }
                elseif ($allrev) {
                        $this->addFields(array (
@@ -119,7 +122,7 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase {
                        'recentchanges'
                ));
 
-               $userId = $wgUser->getID();
+               $userId = $wgUser->getId();
                $this->addWhere(array (
                        'wl_namespace = rc_namespace',
                        'wl_title = rc_title',
@@ -131,8 +134,29 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase {
                $this->addWhereRange('rc_timestamp', $dir, $start, $end);
                $this->addWhereFld('wl_namespace', $namespace);
                $this->addWhereIf('rc_this_oldid=page_latest', !$allrev);
-               
-               # This is a index optimization for mysql, as done in the Special:Watchlist page
+
+               if (!is_null($show)) {
+                       $show = array_flip($show);
+
+                       /* Check for conflicting parameters. */
+                       if ((isset ($show['minor']) && isset ($show['!minor']))
+                                       || (isset ($show['bot']) && isset ($show['!bot']))
+                                       || (isset ($show['anon']) && isset ($show['!anon']))) {
+
+                               $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
+                       }
+
+                       /* Add additional conditions to query depending upon parameters. */
+                       $this->addWhereIf('rc_minor = 0', isset ($show['!minor']));
+                       $this->addWhereIf('rc_minor != 0', isset ($show['minor']));
+                       $this->addWhereIf('rc_bot = 0', isset ($show['!bot']));
+                       $this->addWhereIf('rc_bot != 0', isset ($show['bot']));
+                       $this->addWhereIf('rc_user = 0', isset ($show['anon']));
+                       $this->addWhereIf('rc_user != 0', isset ($show['!anon']));
+               }
+
+
+               # This is an index optimization for mysql, as done in the Special:Watchlist page
                $this->addWhereIf("rc_timestamp > ''", !isset ($start) && !isset ($end) && $wgDBtype == 'mysql');
 
                $this->addOption('LIMIT', $limit +1);
@@ -145,7 +169,7 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase {
                while ($row = $db->fetchObject($res)) {
                        if (++ $count > $limit) {
                                // We've reached the one extra which shows that there are additional pages to be had. Stop here...
-                               $this->setContinueEnumParameter('start', $row->rc_timestamp);
+                               $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->rc_timestamp));
                                break;
                        }
 
@@ -154,14 +178,10 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase {
                                if ($vals)
                                        $data[] = $vals;
                        } else {
-                               $title = Title :: makeTitle($row->rc_namespace, $row->rc_title);
-                               // skip any pages that user has no rights to read
-                               if ($title->userCanRead()) {
-                                       if ($allrev) {
-                                               $data[] = intval($row->rc_this_oldid);
-                                       } else {
-                                               $data[] = intval($row->rc_cur_id);
-                                       }
+                               if ($allrev) {
+                                       $data[] = intval($row->rc_this_oldid);
+                               } else {
+                                       $data[] = intval($row->rc_cur_id);
                                }
                        }
                }
@@ -181,19 +201,15 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase {
 
        private function extractRowInfo($row) {
 
-               $title = Title :: makeTitle($row->rc_namespace, $row->rc_title);
-               if (!$title->userCanRead())
-                       return false;
-
                $vals = array ();
 
                if ($this->fld_ids) {
                        $vals['pageid'] = intval($row->rc_cur_id);
-                       $vals['revid'] = intval($row->rc_this_oldid); 
+                       $vals['revid'] = intval($row->rc_this_oldid);
                }
-               
+
                if ($this->fld_title)
-                       ApiQueryBase :: addTitleInfo($vals, $title);
+                       ApiQueryBase :: addTitleInfo($vals, Title :: makeTitle($row->rc_namespace, $row->rc_title));
 
                if ($this->fld_user) {
                        $vals['user'] = $row->rc_user_text;
@@ -214,13 +230,20 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase {
                if ($this->fld_timestamp)
                        $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->rc_timestamp);
 
+                       $this->addFieldsIf('rc_new_len', $this->fld_sizes);
+
+               if ($this->fld_sizes) {
+                       $vals['oldlen'] = intval($row->rc_old_len);
+                       $vals['newlen'] = intval($row->rc_new_len);
+               }
+
                if ($this->fld_comment && !empty ($row->rc_comment))
                        $vals['comment'] = $row->rc_comment;
 
                return $vals;
        }
 
-       protected function getAllowedParams() {
+       public function getAllowedParams() {
                return array (
                        'allrev' => false,
                        'start' => array (
@@ -257,26 +280,42 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase {
                                        'user',
                                        'comment',
                                        'timestamp',
-                                       'patrol'
+                                       'patrol',
+                                       'sizes',
+                               )
+                       ),
+                       'show' => array (
+                               ApiBase :: PARAM_ISMULTI => true,
+                               ApiBase :: PARAM_TYPE => array (
+                                       'minor',
+                                       '!minor',
+                                       'bot',
+                                       '!bot',
+                                       'anon',
+                                       '!anon'
                                )
                        )
                );
        }
 
-       protected function getParamDescription() {
+       public function getParamDescription() {
                return array (
                        'allrev' => 'Include multiple revisions of the same page within given timeframe.',
                        'start' => 'The timestamp to start enumerating from.',
                        'end' => 'The timestamp to end enumerating.',
                        'namespace' => 'Filter changes to only the given namespace(s).',
                        'dir' => 'In which direction to enumerate pages.',
-                       'limit' => 'How many total pages to return per request.',
-                       'prop' => 'Which additional items to get (non-generator mode only).'
+                       'limit' => 'How many total results to return per request.',
+                       'prop' => 'Which additional items to get (non-generator mode only).',
+                       'show' => array (
+                               'Show only items that meet this criteria.',
+                               'For example, to see only minor edits done by logged-in users, set show=minor|!anon'
+                       )
                );
        }
 
-       protected function getDescription() {
-               return '';
+       public function getDescription() {
+               return "Get all recent changes to pages in the logged in user's watchlist";
        }
 
        protected function getExamples() {
@@ -293,4 +332,3 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase {
                return __CLASS__ . ': $Id$';
        }
 }
-