API:
authorYuri Astrakhan <yurik@users.mediawiki.org>
Sun, 20 May 2007 08:34:47 +0000 (08:34 +0000)
committerYuri Astrakhan <yurik@users.mediawiki.org>
Sun, 20 May 2007 08:34:47 +0000 (08:34 +0000)
* query=images - error when no valid pages
* log - added proper showing of the patrol events
* revisions - code cleanup

includes/api/ApiQueryImages.php
includes/api/ApiQueryLogEvents.php
includes/api/ApiQueryRevisions.php

index 7d0c756..b2aef25 100644 (file)
@@ -47,6 +47,9 @@ class ApiQueryImages extends ApiQueryGeneratorBase {
 
        private function run($resultPageSet = null) {
 
+               if ($this->getPageSet()->getGoodTitleCount() == 0)
+                       return; // nothing to do
+
                $this->addFields(array (
                        'il_from',
                        'il_to'
@@ -73,6 +76,9 @@ class ApiQueryImages extends ApiQueryGeneratorBase {
                                }
                                
                                $title = Title :: makeTitle(NS_IMAGE, $row->il_to);
+                               // do not check userCanRead() -- page content is already accessible,
+                               // and images are listed there.
+
                                $vals = array();
                                ApiQueryBase :: addTitleInfo($vals, $title);
                                $data[] = $vals;
index 69af898..df30c39 100644 (file)
@@ -94,7 +94,7 @@ class ApiQueryLogEvents extends ApiQueryBase {
                                break;
                        }
 
-                       $vals = $this->addRowInfo('log', $row);
+                       $vals = $this->extractRowInfo($row);
                        if($vals)
                                $data[] = $vals;
                }
@@ -104,22 +104,63 @@ class ApiQueryLogEvents extends ApiQueryBase {
                $this->getResult()->addValue('query', $this->getModuleName(), $data);
        }
 
+       private function extractRowInfo($row) {
+               $title = Title :: makeTitle($row->log_namespace, $row->log_title);
+               if (!$title->userCanRead())
+                       return false;
+
+               $vals = array();
+
+               $vals['pageid'] = intval($row->page_id);
+               ApiQueryBase :: addTitleInfo($vals, $title);
+               $vals['type'] = $row->log_type;
+               $vals['action'] = $row->log_action;
+
+               if ($row->log_params !== '') {
+                       $params = explode("\n", $row->log_params);
+                       switch ($row->log_type) {
+                               case 'move': 
+                                       if (isset ($params[0])) {
+                                               $title = Title :: newFromText($params[0]);
+                                               if ($title) {
+                                                       ApiQueryBase :: addTitleInfo($vals, $title, "new_");
+                                                       $params = null;
+                                               }
+                                       }
+                                       break;
+                               case 'patrol':
+                                       list( $cur, $prev, $auto ) = $params;
+                                       $vals['patrol_prev'] = $prev;
+                                       $vals['patrol_cur'] = $cur;
+                                       $vals['patrol_auto'] = $auto;
+                                       $params = null;
+                                       break;
+                       }
+                       
+                       if (!empty ($params)) {
+                               $this->getResult()->setIndexedTagName($params, 'param');
+                               $vals = array_merge($vals, $params);
+                       }
+               }
+
+               $vals['user'] = $row->user_name;
+               if(!$row->log_user)
+                       $vals['anon'] = '';
+               $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->log_timestamp);
+
+               if (!empty ($row->log_comment))
+                       $vals['comment'] = $row->log_comment;
+                       
+               return $vals;
+       }
+
+
        protected function getAllowedParams() {
+               global $wgLogTypes;
                return array (
                        'type' => array (
                                ApiBase :: PARAM_ISMULTI => true,
-                               ApiBase :: PARAM_TYPE => array (
-                                       'block',
-                                       'protect',
-                                       'rights',
-                                       'delete',
-                                       'upload',
-                                       'move',
-                                       'import',
-                                       'renameuser',
-                                       'newusers',
-                                       'makebot'
-                               )
+                               ApiBase :: PARAM_TYPE => $wgLogTypes
                        ),
                        'start' => array (
                                ApiBase :: PARAM_TYPE => 'timestamp'
index 13ae6e5..c542509 100644 (file)
@@ -37,6 +37,8 @@ class ApiQueryRevisions extends ApiQueryBase {
                parent :: __construct($query, $moduleName, 'rv');
        }
 
+       private $fld_timestamp = false, $fld_comment = false, $fld_user = false, $fld_content = false;
+
        public function execute() {
                $limit = $startid = $endid = $start = $end = $dir = $prop = $user = $excludeuser = null;
                extract($this->extractRequestParams());
@@ -70,15 +72,14 @@ class ApiQueryRevisions extends ApiQueryBase {
                ));
                $this->addWhere('rev_deleted=0');
 
-               $showContent = false;
-
                if (!is_null($prop)) {
                        $prop = array_flip($prop);
-                       $this->addFieldsIf('rev_timestamp', isset ($prop['timestamp']));
-                       $this->addFieldsIf('rev_comment', isset ($prop['comment']));
+                       $this->fld_timestamp = $this->addFieldsIf('rev_timestamp', isset ($prop['timestamp']));
+                       $this->fld_comment = $this->addFieldsIf('rev_comment', isset ($prop['comment']));
                        if (isset ($prop['user'])) {
                                $this->addFields('rev_user');
                                $this->addFields('rev_user_text');
+                               $this->fld_user = true;
                        }
                        if (isset ($prop['content'])) {
                                $this->addTables('text');
@@ -86,12 +87,12 @@ class ApiQueryRevisions extends ApiQueryBase {
                                $this->addFields('old_id');
                                $this->addFields('old_text');
                                $this->addFields('old_flags');
-                               $showContent = true;
+                               $this->fld_content = true;
                        }
                }
 
-               $userMax = ($showContent ? 50 : 500);
-               $botMax = ($showContent ? 200 : 10000);
+               $userMax = ($this->fld_content ? 50 : 500);
+               $botMax = ($this->fld_content ? 200 : 10000);
 
                if ($enumRevMode) {
 
@@ -171,19 +172,14 @@ class ApiQueryRevisions extends ApiQueryBase {
                                break;
                        }
 
-                       $vals = $this->addRowInfo('rev', $row);
-                       if ($vals) {
-                               if ($showContent)
-                                       ApiResult :: setContent($vals, Revision :: getRevisionText($row));
-
-                               $this->getResult()->addValue(
-                                       array (
-                                               'query',
-                                               'pages',
-                                               intval($row->rev_page),
-                                               'revisions'),
-                                       null, $vals);
-                       }
+                       $this->getResult()->addValue(
+                               array (
+                                       'query',
+                                       'pages',
+                                       intval($row->rev_page),
+                                       'revisions'),
+                               null,
+                               $this->extractRowInfo($row));
                }
                $db->freeResult($res);
 
@@ -199,6 +195,38 @@ class ApiQueryRevisions extends ApiQueryBase {
                }
        }
 
+       private function extractRowInfo($row) {
+
+               $vals = array ();
+
+               $vals['revid'] = intval( $row->rev_id );
+               $vals['pageid'] = intval($row->rev_page);
+               $vals['oldid'] = intval($row->rev_text_id);
+               
+               if ($row->rev_minor_edit)
+                       $vals['minor'] = '';
+
+               if ($this->fld_user) {
+                       $vals['user'] = $row->rev_user_text;
+                       if (!$row->rev_user)
+                               $vals['anon'] = '';
+               }
+
+               if ($this->fld_timestamp) {
+                       $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->rev_timestamp);
+               }
+               
+               if ($this->fld_comment && !empty ($row->rev_comment)) {
+                       $vals['comment'] = $row->rev_comment;
+               }
+               
+               if ($this->fld_content) {
+                       ApiResult :: setContent($vals, Revision :: getRevisionText($row));
+               }
+               
+               return $vals;
+       }
+
        protected function getAllowedParams() {
                return array (
                        'prop' => array (