From f5aa2e80181d1b784626dd141e95e1b8a41a8ae4 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Sat, 7 Jul 2007 03:05:09 +0000 Subject: [PATCH] API: implemented prop=imageinfo, minor cleanup --- RELEASE-NOTES | 3 +- includes/AutoLoader.php | 1 + includes/api/ApiBase.php | 12 +-- includes/api/ApiMain.php | 8 +- includes/api/ApiPageSet.php | 20 +++- includes/api/ApiQuery.php | 17 ++- includes/api/ApiQueryBase.php | 16 ++- includes/api/ApiQueryCategories.php | 8 -- includes/api/ApiQueryExternalLinks.php | 8 -- includes/api/ApiQueryImageInfo.php | 143 +++++++++++++++++++++++++ includes/api/ApiQueryImages.php | 8 -- includes/api/ApiQueryLangLinks.php | 8 -- includes/api/ApiQueryLinks.php | 8 -- 13 files changed, 192 insertions(+), 68 deletions(-) create mode 100644 includes/api/ApiQueryImageInfo.php diff --git a/RELEASE-NOTES b/RELEASE-NOTES index a55cddbd93..b8c17b3097 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -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 == diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 745c378d2b..ccd2c7a08a 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -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', diff --git a/includes/api/ApiBase.php b/includes/api/ApiBase.php index 833db59882..bd17725a59 100644 --- a/includes/api/ApiBase.php +++ b/includes/api/ApiBase.php @@ -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; } /** diff --git a/includes/api/ApiMain.php b/includes/api/ApiMain.php index 7d0df36898..6fcef54933 100644 --- a/includes/api/ApiMain.php +++ b/includes/api/ApiMain.php @@ -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; diff --git a/includes/api/ApiPageSet.php b/includes/api/ApiPageSet.php index 2123b6142b..b11963d655 100644 --- a/includes/api/ApiPageSet.php +++ b/includes/api/ApiPageSet.php @@ -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; } } diff --git a/includes/api/ApiQuery.php b/includes/api/ApiQuery.php index 8689d53a13..5168e04e7f 100644 --- a/includes/api/ApiQuery.php +++ b/includes/api/ApiQuery.php @@ -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 diff --git a/includes/api/ApiQueryBase.php b/includes/api/ApiQueryBase.php index a146e681dc..41c406ad02 100644 --- a/includes/api/ApiQueryBase.php +++ b/includes/api/ApiQueryBase.php @@ -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); } diff --git a/includes/api/ApiQueryCategories.php b/includes/api/ApiQueryCategories.php index d750843039..2da8b20d67 100644 --- a/includes/api/ApiQueryCategories.php +++ b/includes/api/ApiQueryCategories.php @@ -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 ( diff --git a/includes/api/ApiQueryExternalLinks.php b/includes/api/ApiQueryExternalLinks.php index 7ea757ad89..ad4634e14b 100644 --- a/includes/api/ApiQueryExternalLinks.php +++ b/includes/api/ApiQueryExternalLinks.php @@ -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 index 0000000000..b067d8741e --- /dev/null +++ b/includes/api/ApiQueryImageInfo.php @@ -0,0 +1,143 @@ +@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:$'; + } +} +?> diff --git a/includes/api/ApiQueryImages.php b/includes/api/ApiQueryImages.php index fb22d04ef7..57394673c1 100644 --- a/includes/api/ApiQueryImages.php +++ b/includes/api/ApiQueryImages.php @@ -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)'; } diff --git a/includes/api/ApiQueryLangLinks.php b/includes/api/ApiQueryLangLinks.php index fcdc32aca8..99457aee9c 100644 --- a/includes/api/ApiQueryLangLinks.php +++ b/includes/api/ApiQueryLangLinks.php @@ -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)'; } diff --git a/includes/api/ApiQueryLinks.php b/includes/api/ApiQueryLinks.php index 330fb2fdbd..a8bdebdf1a 100644 --- a/includes/api/ApiQueryLinks.php +++ b/includes/api/ApiQueryLinks.php @@ -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( -- 2.20.1