Update documentation:
[lhc/web/wiklou.git] / includes / api / ApiQueryRecentChanges.php
index fb40724..13e7375 100644 (file)
@@ -32,7 +32,7 @@ if (!defined('MEDIAWIKI')) {
  * A query action to enumerate the recent changes that were done to the wiki.
  * Various filters are supported.
  *
- * @addtogroup API
+ * @ingroup API
  */
 class ApiQueryRecentChanges extends ApiQueryBase {
 
@@ -43,61 +43,83 @@ class ApiQueryRecentChanges extends ApiQueryBase {
        private $fld_comment = false, $fld_user = false, $fld_flags = false,
                        $fld_timestamp = false, $fld_title = false, $fld_ids = false,
                        $fld_sizes = false;
+       /**
+        * Get an array mapping token names to their handler functions.
+        * The prototype for a token function is func($pageid, $title, $rc)
+        * it should return a token or false (permission denied)
+        * @return array(tokenname => function)
+        */
+       protected function getTokenFunctions() {
+               // 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(
+                       'patrol' => array( 'ApiQueryRecentChanges', 'getPatrolToken' )
+               );
+               wfRunHooks('APIQueryRecentChangesTokens', array(&$this->tokenFunctions));
+               return $this->tokenFunctions;
+       }
+       
+       public static function getPatrolToken($pageid, $title, $rc)
+       {
+               global $wgUser;
+               if(!$wgUser->useRCPatrol() && (!$wgUser->useNPPatrol() ||
+                                $rc->getAttribute('rc_type') != RC_NEW))
+                       return false;
+               
+               // The patrol token is always the same, let's exploit that
+               static $cachedPatrolToken = null;
+               if(!is_null($cachedPatrolToken))
+                       return $cachedPatrolToken;
+
+               $cachedPatrolToken = $wgUser->editToken();
+               return $cachedPatrolToken;
+       }
 
        /**
         * Generates and outputs the result of this query based upon the provided parameters.
         */
        public function execute() {
-               /* Initialize vars */
-               $limit = $prop = $namespace = $titles = $show = $type = $dir = $start = $end = null;
-
                /* Get the parameters of the request. */
-               extract($this->extractRequestParams());
+               $params = $this->extractRequestParams();
 
                /* Build our basic query. Namely, something along the lines of:
-                * SELECT * from recentchanges WHERE rc_timestamp > $start
+                * SELECT * FROM recentchanges WHERE rc_timestamp > $start
                 *              AND rc_timestamp < $end AND rc_namespace = $namespace
                 *              AND rc_deleted = '0'
                 */
                $db = $this->getDB();
-               $rc = $db->tableName('recentchanges');
-               $this->addWhereRange('rc_timestamp', $dir, $start, $end);
-               $this->addWhereFld('rc_namespace', $namespace);
+               $this->addTables('recentchanges');
+               $this->addOption('USE INDEX', array('recentchanges' => 'rc_timestamp'));
+               $this->addWhereRange('rc_timestamp', $params['dir'], $params['start'], $params['end']);
+               $this->addWhereFld('rc_namespace', $params['namespace']);
                $this->addWhereFld('rc_deleted', 0);
-               if(!empty($titles))
-               {
-                       $lb = new LinkBatch;
-                       foreach($titles as $t)
-                       {
-                               $obj = Title::newFromText($t);
-                               $lb->addObj($obj);
-                               if($obj->getNamespace() < 0)
-                               {
-                                       // LinkBatch refuses these, but we need them anyway
-                                       if(!array_key_exists($obj->getNamespace(), $lb->data))
-                                               $lb->data[$obj->getNamespace()] = array();
-                                       $lb->data[$obj->getNamespace()][$obj->getDbKey()] = 1;
-                               }
-                       }
-                       $where = $lb->constructSet('rc', $this->getDb());
-                       if($where != '')
-                               $this->addWhere($where);
-               }
 
-               if(!is_null($type))
-                               $this->addWhereFld('rc_type', $this->parseRCType($type));
+               if(!is_null($params['type']))
+                               $this->addWhereFld('rc_type', $this->parseRCType($params['type']));
 
-               if (!is_null($show)) {
-                       $show = array_flip($show);
+               if (!is_null($params['show'])) {
+                       $show = array_flip($params['show']);
 
                        /* Check for conflicting parameters. */
                        if ((isset ($show['minor']) && isset ($show['!minor']))
                                        || (isset ($show['bot']) && isset ($show['!bot']))
                                        || (isset ($show['anon']) && isset ($show['!anon']))
-                                       || (isset ($show['redirect']) && isset ($show['!redirect']))) {
+                                       || (isset ($show['redirect']) && isset ($show['!redirect']))
+                                       || (isset ($show['patrolled']) && isset ($show['!patrolled']))) {
 
                                $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
                        }
+                       
+                       // Check permissions
+                       global $wgUser;
+                       if((isset($show['patrolled']) || isset($show['!patrolled'])) && !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol())
+                               $this->dieUsage("You need the patrol right to request the patrolled flag", 'permissiondenied');
 
                        /* Add additional conditions to query depending upon parameters. */
                        $this->addWhereIf('rc_minor = 0', isset ($show['!minor']));
@@ -106,6 +128,8 @@ class ApiQueryRecentChanges extends ApiQueryBase {
                        $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->addWhereIf('rc_patrolled = 0', isset($show['!patrolled']));
+                       $this->addWhereIf('rc_patrolled != 0', isset($show['patrolled']));
                        $this->addWhereIf('page_is_redirect = 1', isset ($show['redirect']));
                        // Don't throw log entries out the window here
                        $this->addWhereIf('page_is_redirect = 0 OR page_is_redirect IS NULL', isset ($show['!redirect']));
@@ -116,14 +140,15 @@ class ApiQueryRecentChanges extends ApiQueryBase {
                        'rc_timestamp',
                        'rc_namespace',
                        'rc_title',
+                       'rc_cur_id',
                        'rc_type',
                        'rc_moved_to_ns',
                        'rc_moved_to_title'
                ));
 
                /* Determine what properties we need to display. */
-               if (!is_null($prop)) {
-                       $prop = array_flip($prop);
+               if (!is_null($params['prop'])) {
+                       $prop = array_flip($params['prop']);
 
                        /* Set up internal members based upon params. */
                        $this->fld_comment = isset ($prop['comment']);
@@ -135,14 +160,14 @@ class ApiQueryRecentChanges extends ApiQueryBase {
                        $this->fld_sizes = isset ($prop['sizes']);
                        $this->fld_redirect = isset($prop['redirect']);
                        $this->fld_patrolled = isset($prop['patrolled']);
+                       $this->fld_loginfo = isset($prop['loginfo']);
 
                        global $wgUser;
-                       if($this->fld_patrolled && !$wgUser->isAllowed('patrol'))
+                       if($this->fld_patrolled && !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol())
                                $this->dieUsage("You need the patrol right to request the patrolled flag", 'permissiondenied');
 
                        /* Add fields to our query if they are specified as a needed parameter. */
                        $this->addFieldsIf('rc_id', $this->fld_ids);
-                       $this->addFieldsIf('rc_cur_id', $this->fld_ids);
                        $this->addFieldsIf('rc_this_oldid', $this->fld_ids);
                        $this->addFieldsIf('rc_last_oldid', $this->fld_ids);
                        $this->addFieldsIf('rc_comment', $this->fld_comment);
@@ -154,30 +179,28 @@ class ApiQueryRecentChanges extends ApiQueryBase {
                        $this->addFieldsIf('rc_old_len', $this->fld_sizes);
                        $this->addFieldsIf('rc_new_len', $this->fld_sizes);
                        $this->addFieldsIf('rc_patrolled', $this->fld_patrolled);
+                       $this->addFieldsIf('rc_logid', $this->fld_loginfo);
+                       $this->addFieldsIf('rc_log_type', $this->fld_loginfo);
+                       $this->addFieldsIf('rc_log_action', $this->fld_loginfo);
+                       $this->addFieldsIf('rc_params', $this->fld_loginfo);
                        if($this->fld_redirect || isset($show['redirect']) || isset($show['!redirect']))
                        {
-                               $page = $db->tableName('page');
-                               $tables = "$page RIGHT JOIN $rc FORCE INDEX(rc_timestamp) ON page_namespace=rc_namespace AND page_title=rc_title";
+                               $this->addTables('page');
+                               $this->addJoinConds(array('page' => array('LEFT JOIN', array('rc_namespace=page_namespace', 'rc_title=page_title'))));
                                $this->addFields('page_is_redirect');
                        }
                }
-               if(!isset($tables))
-                       $tables = "$rc FORCE INDEX(rc_timestamp)";
-               $this->addTables($tables);
-               /* Specify the limit for our query. It's $limit+1 because we (possibly) need to
-                * generate a "continue" parameter, to allow paging. */
-               $this->addOption('LIMIT', $limit +1);
-
-               $data = array ();
-               $count = 0;
+               $this->token = $params['token'];
+               $this->addOption('LIMIT', $params['limit'] +1);
 
+               $count = 0;
                /* Perform the actual query. */
                $db = $this->getDB();
                $res = $this->select(__METHOD__);
 
                /* Iterate through the rows, adding data extracted from them to our query result. */
                while ($row = $db->fetchObject($res)) {
-                       if (++ $count > $limit) {
+                       if (++ $count > $params['limit']) {
                                // We've reached the one extra which shows that there are additional pages to be had. Stop here...
                                $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->rc_timestamp));
                                break;
@@ -187,16 +210,20 @@ class ApiQueryRecentChanges extends ApiQueryBase {
                        $vals = $this->extractRowInfo($row);
 
                        /* Add that row's data to our final output. */
-                       if($vals)
-                               $data[] = $vals;
+                       if(!$vals)
+                               continue;
+                       $fit = $this->getResult()->addValue(array('query', $this->getModuleName()), null, $vals);
+                       if(!$fit)
+                       {
+                               $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->rc_timestamp));
+                               break;
+                       }
                }
 
                $db->freeResult($res);
 
                /* Format the result */
-               $result = $this->getResult();
-               $result->setIndexedTagName($data, 'rc');
-               $result->addValue('query', $this->getModuleName(), $data);
+               $this->getResult()->setIndexedTagName_internal(array('query', $this->getModuleName()), 'rc');
        }
 
        /**
@@ -209,7 +236,7 @@ class ApiQueryRecentChanges extends ApiQueryBase {
        private function extractRowInfo($row) {
                /* If page was moved somewhere, get the title of the move target. */
                $movedToTitle = false;
-               if (!empty($row->rc_moved_to_title))
+               if (isset($row->rc_moved_to_title) && $row->rc_moved_to_title !== '')
                        $movedToTitle = Title :: makeTitle($row->rc_moved_to_ns, $row->rc_moved_to_title);
 
                /* Determine the title of the page that has been changed. */
@@ -222,11 +249,11 @@ class ApiQueryRecentChanges extends ApiQueryBase {
 
                /* Determine what kind of change this was. */
                switch ( $type ) {
-               case RC_EDIT:  $vals['type'] = 'edit'; break;
-               case RC_NEW:   $vals['type'] = 'new'; break;
-               case RC_MOVE:  $vals['type'] = 'move'; break;
-               case RC_LOG:   $vals['type'] = 'log'; break;
-               case RC_MOVE_OVER_REDIRECT: $vals['type'] = 'move over redirect'; break;
+                       case RC_EDIT:  $vals['type'] = 'edit'; break;
+                       case RC_NEW:   $vals['type'] = 'new'; break;
+                       case RC_MOVE:  $vals['type'] = 'move'; break;
+                       case RC_LOG:   $vals['type'] = 'log'; break;
+                       case RC_MOVE_OVER_REDIRECT: $vals['type'] = 'move over redirect'; break;
                default: $vals['type'] = $type;
                }
 
@@ -273,7 +300,7 @@ class ApiQueryRecentChanges extends ApiQueryBase {
                        $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->rc_timestamp);
 
                /* Add edit summary / log summary. */
-               if ($this->fld_comment && !empty ($row->rc_comment)) {
+               if ($this->fld_comment && isset($row->rc_comment)) {
                        $vals['comment'] = $row->rc_comment;
                }
 
@@ -284,6 +311,29 @@ class ApiQueryRecentChanges extends ApiQueryBase {
                /* Add the patrolled flag */
                if ($this->fld_patrolled && $row->rc_patrolled == 1)
                        $vals['patrolled'] = '';
+                       
+               if ($this->fld_loginfo && $row->rc_type == RC_LOG) {
+                       $vals['logid'] = intval($row->rc_logid);
+                       $vals['logtype'] = $row->rc_log_type;
+                       $vals['logaction'] = $row->rc_log_action;
+                       ApiQueryLogEvents::addLogParams($this->getResult(),
+                               $vals, $row->rc_params,
+                               $row->rc_log_type, $row->rc_timestamp);
+               }
+               
+               if(!is_null($this->token))
+               {
+                       $tokenFunctions = $this->getTokenFunctions();
+                       foreach($this->token as $t)
+                       {
+                               $val = call_user_func($tokenFunctions[$t], $row->rc_cur_id,
+                                       $title, RecentChange::newFromRow($row));
+                               if($val === false)
+                                       $this->setWarning("Action '$t' is not allowed for the current user");
+                               else
+                                       $vals[$t . 'token'] = $val;
+                       }
+               }
 
                return $vals;
        }
@@ -324,9 +374,6 @@ class ApiQueryRecentChanges extends ApiQueryBase {
                                ApiBase :: PARAM_ISMULTI => true,
                                ApiBase :: PARAM_TYPE => 'namespace'
                        ),
-                       'titles' => array(
-                               ApiBase :: PARAM_ISMULTI => true
-                       ),
                        'prop' => array (
                                ApiBase :: PARAM_ISMULTI => true,
                                ApiBase :: PARAM_DFLT => 'title|timestamp|ids',
@@ -339,9 +386,14 @@ class ApiQueryRecentChanges extends ApiQueryBase {
                                        'ids',
                                        'sizes',
                                        'redirect',
-                                       'patrolled'
+                                       'patrolled',
+                                       'loginfo',
                                )
                        ),
+                       'token' => array(
+                               ApiBase :: PARAM_TYPE => array_keys($this->getTokenFunctions()),
+                               ApiBase :: PARAM_ISMULTI => true
+                       ),
                        'show' => array (
                                ApiBase :: PARAM_ISMULTI => true,
                                ApiBase :: PARAM_TYPE => array (
@@ -352,7 +404,9 @@ class ApiQueryRecentChanges extends ApiQueryBase {
                                        'anon',
                                        '!anon',
                                        'redirect',
-                                       '!redirect'
+                                       '!redirect',
+                                       'patrolled',
+                                       '!patrolled'
                                )
                        ),
                        'limit' => array (
@@ -379,14 +433,14 @@ class ApiQueryRecentChanges extends ApiQueryBase {
                        'end' => 'The timestamp to end enumerating.',
                        'dir' => 'In which direction to enumerate.',
                        'namespace' => 'Filter log entries to only this namespace(s)',
-                       'titles' => 'Filter log entries to only these page titles',
                        'prop' => 'Include additional pieces of information',
+                       'token' => 'Which tokens to obtain for each change',
                        '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'
                        ),
                        'type' => 'Which types of changes to show.',
-                       'limit' => 'How many total pages to return.'
+                       'limit' => 'How many total changes to return.'
                );
        }
 
@@ -403,4 +457,4 @@ class ApiQueryRecentChanges extends ApiQueryBase {
        public function getVersion() {
                return __CLASS__ . ': $Id$';
        }
-}
+}
\ No newline at end of file