API: implemented prop=imageinfo, minor cleanup
authorYuri Astrakhan <yurik@users.mediawiki.org>
Sat, 7 Jul 2007 03:05:09 +0000 (03:05 +0000)
committerYuri Astrakhan <yurik@users.mediawiki.org>
Sat, 7 Jul 2007 03:05:09 +0000 (03:05 +0000)
13 files changed:
RELEASE-NOTES
includes/AutoLoader.php
includes/api/ApiBase.php
includes/api/ApiMain.php
includes/api/ApiPageSet.php
includes/api/ApiQuery.php
includes/api/ApiQueryBase.php
includes/api/ApiQueryCategories.php
includes/api/ApiQueryExternalLinks.php
includes/api/ApiQueryImageInfo.php [new file with mode: 0644]
includes/api/ApiQueryImages.php
includes/api/ApiQueryLangLinks.php
includes/api/ApiQueryLinks.php

index a55cddb..b8c17b3 100644 (file)
@@ -263,7 +263,7 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN
 
 == API changes since 1.10 ==
 
-(For ongoing development discussion, see http://www.mediawiki.org/wiki/API)
+Full API documentation is available at http://www.mediawiki.org/wiki/API
 
 * New properties: links, templates, images, langlinks, categories, external
   links
@@ -302,6 +302,7 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN
 * (bug 10297) include previous revision ID in prop=revisions
 * backlinks, embeddedin and imageusage lists should use (bl|ei|iu)title parameter
   instead of titles. Titles for these lists is obsolete and might stop working soon.
+* Added prop=imageinfo - gets image properties and upload history
 
 == Maintenance script changes since 1.10 ==
 
index 745c378..ccd2c7a 100644 (file)
@@ -314,6 +314,7 @@ function __autoload($className) {
                'ApiQueryContributions' => 'includes/api/ApiQueryUserContributions.php',
                'ApiQueryExternalLinks' => 'includes/api/ApiQueryExternalLinks.php',
                'ApiQueryImages' => 'includes/api/ApiQueryImages.php',
+               'ApiQueryImageInfo' => 'includes/api/ApiQueryImageInfo.php',
                'ApiQueryInfo' => 'includes/api/ApiQueryInfo.php',
                'ApiQueryLangLinks' => 'includes/api/ApiQueryLangLinks.php',
                'ApiQueryLinks' => 'includes/api/ApiQueryLinks.php',
index 833db59..bd17725 100644 (file)
@@ -52,15 +52,15 @@ abstract class ApiBase {
        const LIMIT_SML1 = 50; // Slow query, std user limit
        const LIMIT_SML2 = 500; // Slow query, bot/sysop limit
 
-       private $mMainModule, $mModuleName, $mParamPrefix;
+       private $mMainModule, $mModuleName, $mModulePrefix;
 
        /**
        * Constructor
        */
-       public function __construct($mainModule, $moduleName, $paramPrefix = '') {
+       public function __construct($mainModule, $moduleName, $modulePrefix = '') {
                $this->mMainModule = $mainModule;
                $this->mModuleName = $moduleName;
-               $this->mParamPrefix = $paramPrefix;
+               $this->mModulePrefix = $modulePrefix;
        }
 
        /**
@@ -78,8 +78,8 @@ abstract class ApiBase {
        /**
         * Get parameter prefix (usually two letters or an empty string). 
         */
-       public function getParamPrefix() {
-               return $this->mParamPrefix;
+       public function getModulePrefix() {
+               return $this->mModulePrefix;
        }       
 
        /**
@@ -296,7 +296,7 @@ abstract class ApiBase {
         * Override this method to change parameter name during runtime 
         */
        public function encodeParamName($paramName) {
-               return $this->mParamPrefix . $paramName;
+               return $this->mModulePrefix . $paramName;
        }
 
        /**
index 7d0df36..6fcef54 100644 (file)
@@ -389,11 +389,11 @@ class ApiMain extends ApiBase {
        }
 
        public static function makeHelpMsgHeader($module, $paramName) {
-               $paramPrefix = $module->getParamPrefix();
-               if (!empty($paramPrefix))
-                       $paramPrefix = "($paramPrefix) "; 
+               $modulePrefix = $module->getModulePrefix();
+               if (!empty($modulePrefix))
+                       $modulePrefix = "($modulePrefix) "; 
                
-               return "* $paramName={$module->getModuleName()} $paramPrefix*";
+               return "* $paramName={$module->getModuleName()} $modulePrefix*";
        } 
 
        private $mIsBot = null;
index 2123b61..b11963d 100644 (file)
@@ -43,9 +43,11 @@ if (!defined('MEDIAWIKI')) {
 class ApiPageSet extends ApiQueryBase {
 
        private $mAllPages; // [ns][dbkey] => page_id or 0 when missing
-       private $mTitles, $mGoodTitles, $mMissingTitles, $mMissingPageIDs, $mRedirectTitles, $mNormalizedTitles, $mInterwikiTitles;
+       private $mTitles, $mGoodTitles, $mMissingTitles, $mMissingPageIDs, $mRedirectTitles;
+       private $mNormalizedTitles, $mInterwikiTitles;
        private $mResolveRedirects, $mPendingRedirectIDs;
        private $mGoodRevIDs, $mMissingRevIDs;
+       private $mFakePageId;
 
        private $mRequestedPageFields;
 
@@ -67,6 +69,8 @@ class ApiPageSet extends ApiQueryBase {
                $this->mResolveRedirects = $resolveRedirects;
                if($resolveRedirects)
                        $this->mPendingRedirectIDs = array();
+                       
+               $this->mFakePageId = -1;
        }
 
        public function isResolvingRedirects() {
@@ -102,6 +106,14 @@ class ApiPageSet extends ApiQueryBase {
                return array_keys($pageFlds);
        }
 
+       /**
+        * Returns an array [ns][dbkey] => page_id for all requested titles
+        * page_id is a unique negative number in case title was not found
+        */
+       public function getAllTitlesByNamespace() {
+               return $this->mAllPages;
+       }
+
        /**
         * All Title objects provided.
         * @return array of Title objects
@@ -134,6 +146,7 @@ class ApiPageSet extends ApiQueryBase {
 
        /**
         * Title objects that were NOT found in the database.
+        * The array's index will be negative for each item
         * @return array of Title objects
         */
        public function getMissingTitles() {
@@ -406,8 +419,9 @@ class ApiPageSet extends ApiQueryBase {
                                foreach ($remaining as $ns => $dbkeys) {
                                        foreach ( $dbkeys as $dbkey => $unused ) {
                                                $title = Title :: makeTitle($ns, $dbkey);
-                                               $this->mMissingTitles[] = $title;
-                                               $this->mAllPages[$ns][$dbkey] = 0;
+                                               $this->mAllPages[$ns][$dbkey] = $this->mFakePageId;
+                                               $this->mMissingTitles[$this->mFakePageId] = $title;
+                                               $this->mFakePageId--;
                                                $this->mTitles[] = $title;
                                        }
                                }
index 8689d53..5168e04 100644 (file)
@@ -51,13 +51,11 @@ class ApiQuery extends ApiBase {
                'links' => 'ApiQueryLinks',
                'langlinks' => 'ApiQueryLangLinks',
                'images' => 'ApiQueryImages',
+               'imageinfo' => 'ApiQueryImageInfo',
                'templates' => 'ApiQueryLinks',
                'categories' => 'ApiQueryCategories',
                'extlinks' => 'ApiQueryExternalLinks',
        );
-       //      'categories' => 'ApiQueryCategories',
-       //      'imageinfo' => 'ApiQueryImageinfo',
-       //      'templates' => 'ApiQueryTemplates',
 
        private $mQueryListModules = array (
                'allpages' => 'ApiQueryAllpages',
@@ -69,15 +67,13 @@ class ApiQuery extends ApiBase {
                'recentchanges' => 'ApiQueryRecentChanges',
                'usercontribs' => 'ApiQueryContributions',
                'watchlist' => 'ApiQueryWatchlist',
-       );
-       //      'recentchanges' => 'ApiQueryRecentchanges',
        //      'users' => 'ApiQueryUsers',
-       //      'watchlist' => 'ApiQueryWatchlist',
+       );
 
        private $mQueryMetaModules = array (
-               'siteinfo' => 'ApiQuerySiteinfo'
-       );
+               'siteinfo' => 'ApiQuerySiteinfo',
        //      'userinfo' => 'ApiQueryUserinfo',
+       );
 
        private $mSlaveDB = null;
        private $mNamedDB = array();
@@ -293,12 +289,11 @@ class ApiQuery extends ApiBase {
                $pages = array ();
 
                // Report any missing titles
-               $fakepageid = -1;
-               foreach ($pageSet->getMissingTitles() as $title) {
+               foreach ($pageSet->getMissingTitles() as $fakeId => $title) {
                        $vals = array();
                        ApiQueryBase :: addTitleInfo($vals, $title, true);
                        $vals['missing'] = '';
-                       $pages[$fakepageid--] = $vals;
+                       $pages[$fakeId] = $vals;
                }
 
                // Report any missing page ids
index a146e68..41c406a 100644 (file)
@@ -151,10 +151,20 @@ abstract class ApiQueryBase extends ApiBase {
                return $this->mQueryModule;
        }
 
+       /**
+        * Add sub-element under the page element with the given pageId. 
+        */
+       protected function addPageSubItems($pageId, $data) {
+               $result = $this->getResult();
+               $result->setIndexedTagName($data, $this->getModulePrefix());
+               $result->addValue(array ('query', 'pages', intval($pageId)),
+                       $this->getModuleName(),
+                       $data);
+       }
+
        protected function setContinueEnumParameter($paramName, $paramValue) {
-               $msg = array (
-                       $this->encodeParamName($paramName
-               ) => $paramValue);
+               
+               $msg = array( $this->encodeParamName($paramName) => $paramValue );
                $this->getResult()->addValue('query-continue', $this->getModuleName(), $msg);
        }
 
index d750843..2da8b20 100644 (file)
@@ -124,14 +124,6 @@ class ApiQueryCategories extends ApiQueryGeneratorBase {
                $db->freeResult($res);
        }
 
-       private function addPageSubItems($pageId, $data) {
-               $result = $this->getResult();
-               $result->setIndexedTagName($data, 'cl');
-               $result->addValue(array ('query', 'pages', intval($pageId)),
-                       'categories',
-                       $data);
-       }
-
        protected function getAllowedParams() {
                return array (
                        'prop' => array (
index 7ea757a..ad4634e 100644 (file)
@@ -75,14 +75,6 @@ class ApiQueryExternalLinks extends ApiQueryBase {
                $db->freeResult($res);
        }
 
-       private function addPageSubItems($pageId, $data) {
-               $result = $this->getResult();
-               $result->setIndexedTagName($data, 'el');
-               $result->addValue(array ('query', 'pages', intval($pageId)),
-                       'extlinks',
-                       $data);
-       }
-
        protected function getDescription() {
                return 'Returns all external urls (not interwikies) from the given page(s)';
        }
diff --git a/includes/api/ApiQueryImageInfo.php b/includes/api/ApiQueryImageInfo.php
new file mode 100644 (file)
index 0000000..b067d87
--- /dev/null
@@ -0,0 +1,143 @@
+<?php
+
+/*
+ * Created on July 6, 2007
+ *
+ * API for MediaWiki 1.8+
+ *
+ * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ */
+
+if (!defined('MEDIAWIKI')) {
+       // Eclipse helper - will be ignored in production
+       require_once ('ApiQueryBase.php');
+}
+
+/**
+ * A query action to get image information and upload history.
+ * 
+ * @addtogroup API
+ */
+class ApiQueryImageInfo extends ApiQueryBase {
+
+       public function __construct($query, $moduleName) {
+               parent :: __construct($query, $moduleName, 'ii');
+       }
+
+       public function execute() {
+               $params = $this->extractRequestParams();
+
+               $history = $params['history'];
+
+               $prop = array_flip($params['prop']);            
+               $fld_timestamp = isset($prop['timestamp']);
+               $fld_user = isset($prop['user']);
+               $fld_comment = isset($prop['comment']);
+               $fld_url = isset($prop['url']);
+               $fld_size = isset($prop['size']);
+
+               $pageIds = $this->getPageSet()->getAllTitlesByNamespace();
+               if (!empty($pageIds[NS_IMAGE])) {
+                       foreach ($pageIds[NS_IMAGE] as $dbKey => $pageId) {
+                                                               
+                               $title = Title :: makeTitle(NS_IMAGE, $dbKey);
+                               $img = wfFindFile($title);
+
+                               $vals = array();
+                               if ( !$img ) {
+                                       $data['missing'] = '';                  
+                               } else {
+                                       
+                                       $data['repository'] = $img->getRepoName();
+                                       
+                                       $isCur = true;
+                                       while($line = $img->nextHistoryLine()) { // assignment
+                                               $vals = array();
+
+                                               if ($fld_timestamp)
+                                                       $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $line->img_timestamp);
+                                               if ($fld_user)
+                                                       $vals['user'] = $line->img_user_text;
+                                               if ($fld_size) {
+                                                       $vals['size'] = $line->img_size;
+                                                       $vals['width'] = $line->img_width;
+                                                       $vals['height'] = $line->img_height;
+                                               }
+                                               if ($fld_url)
+                                                       $vals['url'] = $isCur ? $img->getURL() : $img->getArchiveUrl($line->oi_archive_name);
+                                               if ($fld_comment)
+                                                       $vals['comment'] = $line->img_description;
+
+                                               $data[] = $vals;
+                                               
+                                               if (!$history)  // Stop after the first line.
+                                                       break;
+                                                       
+                                               $isCur = false;
+                                       }
+                                       
+                                       $img->resetHistory();
+                               }
+
+                               $this->addPageSubItems($pageId, $data);
+                       }
+               }
+       }
+
+       protected function getAllowedParams() {
+               return array (
+                       'prop' => array (
+                               ApiBase :: PARAM_ISMULTI => true,
+                               ApiBase :: PARAM_DFLT => 'timestamp|user',
+                               ApiBase :: PARAM_TYPE => array (
+                                       'timestamp',
+                                       'user',
+                                       'comment',
+                                       'url',
+                                       'size',
+                               )
+                       ),
+                       'history' => false,
+               );
+       }
+
+       protected function getParamDescription() {
+               return array (
+                       'prop' => 'What image information to get.',
+                       'history' => 'Include upload history',
+               );
+       }
+
+       protected function getDescription() {
+               return array (
+                       'Returns image information and upload history'
+               );
+       }
+
+       protected function getExamples() {
+               return array (
+                       'api.php?action=query&titles=Image:Albert%20Einstein%20Head.jpg&prop=imageinfo',
+                       'api.php?action=query&titles=Image:Test.jpg&prop=imageinfo&iihistory&iiprop=timestamp|user|url',
+               );
+       }
+
+       public function getVersion() {
+               return __CLASS__ . ': $Id:$';
+       }
+}
+?>
index fb22d04..5739467 100644 (file)
@@ -102,14 +102,6 @@ class ApiQueryImages extends ApiQueryGeneratorBase {
                $db->freeResult($res);
        }
 
-       private function addPageSubItems($pageId, $data) {
-               $result = $this->getResult();
-               $result->setIndexedTagName($data, 'im');
-               $result->addValue(array ('query', 'pages', intval($pageId)),
-                       'images',
-                       $data);
-       }
-
        protected function getDescription() {
                return 'Returns all images contained on the given page(s)';
        }
index fcdc32a..99457ae 100644 (file)
@@ -76,14 +76,6 @@ class ApiQueryLangLinks extends ApiQueryBase {
                $db->freeResult($res);
        }
 
-       private function addPageSubItems($pageId, $data) {
-               $result = $this->getResult();
-               $result->setIndexedTagName($data, 'll');
-               $result->addValue(array ('query', 'pages', intval($pageId)),
-                       'langlinks',
-                       $data);
-       }
-
        protected function getDescription() {
                return 'Returns all interlanguage links from the given page(s)';
        }
index 330fb2f..a8bdebd 100644 (file)
@@ -126,14 +126,6 @@ class ApiQueryLinks extends ApiQueryGeneratorBase {
                $db->freeResult($res);
        }
 
-       private function addPageSubItems($pageId, $data) {
-               $result = $this->getResult();
-               $result->setIndexedTagName($data, $this->prefix);
-               $result->addValue(array ('query', 'pages', intval($pageId)),
-                       $this->getModuleName(),
-                       $data);
-       }
-
        protected function getAllowedParams()
        {
                return array(