r23819 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r23818‎ | r23819 | r23820 >
Date:03:05, 7 July 2007
Author:yurik
Status:old
Tags:
Comment:
API: implemented prop=imageinfo, minor cleanup
Modified paths:
  • /trunk/phase3/RELEASE-NOTES (modified) (history)
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/api/ApiBase.php (modified) (history)
  • /trunk/phase3/includes/api/ApiMain.php (modified) (history)
  • /trunk/phase3/includes/api/ApiPageSet.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQuery.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryBase.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryCategories.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryExternalLinks.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryImageInfo.php (added) (history)
  • /trunk/phase3/includes/api/ApiQueryImages.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryLangLinks.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryLinks.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiQuery.php
@@ -51,13 +51,11 @@
5252 'links' => 'ApiQueryLinks',
5353 'langlinks' => 'ApiQueryLangLinks',
5454 'images' => 'ApiQueryImages',
 55+ 'imageinfo' => 'ApiQueryImageInfo',
5556 'templates' => 'ApiQueryLinks',
5657 'categories' => 'ApiQueryCategories',
5758 'extlinks' => 'ApiQueryExternalLinks',
5859 );
59 - // 'categories' => 'ApiQueryCategories',
60 - // 'imageinfo' => 'ApiQueryImageinfo',
61 - // 'templates' => 'ApiQueryTemplates',
6260
6361 private $mQueryListModules = array (
6462 'allpages' => 'ApiQueryAllpages',
@@ -69,15 +67,13 @@
7068 'recentchanges' => 'ApiQueryRecentChanges',
7169 'usercontribs' => 'ApiQueryContributions',
7270 'watchlist' => 'ApiQueryWatchlist',
73 - );
74 - // 'recentchanges' => 'ApiQueryRecentchanges',
7571 // 'users' => 'ApiQueryUsers',
76 - // 'watchlist' => 'ApiQueryWatchlist',
 72+ );
7773
7874 private $mQueryMetaModules = array (
79 - 'siteinfo' => 'ApiQuerySiteinfo'
80 - );
 75+ 'siteinfo' => 'ApiQuerySiteinfo',
8176 // 'userinfo' => 'ApiQueryUserinfo',
 77+ );
8278
8379 private $mSlaveDB = null;
8480 private $mNamedDB = array();
@@ -293,12 +289,11 @@
294290 $pages = array ();
295291
296292 // Report any missing titles
297 - $fakepageid = -1;
298 - foreach ($pageSet->getMissingTitles() as $title) {
 293+ foreach ($pageSet->getMissingTitles() as $fakeId => $title) {
299294 $vals = array();
300295 ApiQueryBase :: addTitleInfo($vals, $title, true);
301296 $vals['missing'] = '';
302 - $pages[$fakepageid--] = $vals;
 297+ $pages[$fakeId] = $vals;
303298 }
304299
305300 // Report any missing page ids
Index: trunk/phase3/includes/api/ApiQueryLinks.php
@@ -126,14 +126,6 @@
127127 $db->freeResult($res);
128128 }
129129
130 - private function addPageSubItems($pageId, $data) {
131 - $result = $this->getResult();
132 - $result->setIndexedTagName($data, $this->prefix);
133 - $result->addValue(array ('query', 'pages', intval($pageId)),
134 - $this->getModuleName(),
135 - $data);
136 - }
137 -
138130 protected function getAllowedParams()
139131 {
140132 return array(
Index: trunk/phase3/includes/api/ApiQueryLangLinks.php
@@ -76,14 +76,6 @@
7777 $db->freeResult($res);
7878 }
7979
80 - private function addPageSubItems($pageId, $data) {
81 - $result = $this->getResult();
82 - $result->setIndexedTagName($data, 'll');
83 - $result->addValue(array ('query', 'pages', intval($pageId)),
84 - 'langlinks',
85 - $data);
86 - }
87 -
8880 protected function getDescription() {
8981 return 'Returns all interlanguage links from the given page(s)';
9082 }
Index: trunk/phase3/includes/api/ApiQueryImageInfo.php
@@ -0,0 +1,143 @@
 2+<?php
 3+
 4+/*
 5+ * Created on July 6, 2007
 6+ *
 7+ * API for MediaWiki 1.8+
 8+ *
 9+ * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
 10+ *
 11+ * This program is free software; you can redistribute it and/or modify
 12+ * it under the terms of the GNU General Public License as published by
 13+ * the Free Software Foundation; either version 2 of the License, or
 14+ * (at your option) any later version.
 15+ *
 16+ * This program is distributed in the hope that it will be useful,
 17+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 19+ * GNU General Public License for more details.
 20+ *
 21+ * You should have received a copy of the GNU General Public License along
 22+ * with this program; if not, write to the Free Software Foundation, Inc.,
 23+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 24+ * http://www.gnu.org/copyleft/gpl.html
 25+ */
 26+
 27+if (!defined('MEDIAWIKI')) {
 28+ // Eclipse helper - will be ignored in production
 29+ require_once ('ApiQueryBase.php');
 30+}
 31+
 32+/**
 33+ * A query action to get image information and upload history.
 34+ *
 35+ * @addtogroup API
 36+ */
 37+class ApiQueryImageInfo extends ApiQueryBase {
 38+
 39+ public function __construct($query, $moduleName) {
 40+ parent :: __construct($query, $moduleName, 'ii');
 41+ }
 42+
 43+ public function execute() {
 44+ $params = $this->extractRequestParams();
 45+
 46+ $history = $params['history'];
 47+
 48+ $prop = array_flip($params['prop']);
 49+ $fld_timestamp = isset($prop['timestamp']);
 50+ $fld_user = isset($prop['user']);
 51+ $fld_comment = isset($prop['comment']);
 52+ $fld_url = isset($prop['url']);
 53+ $fld_size = isset($prop['size']);
 54+
 55+ $pageIds = $this->getPageSet()->getAllTitlesByNamespace();
 56+ if (!empty($pageIds[NS_IMAGE])) {
 57+ foreach ($pageIds[NS_IMAGE] as $dbKey => $pageId) {
 58+
 59+ $title = Title :: makeTitle(NS_IMAGE, $dbKey);
 60+ $img = wfFindFile($title);
 61+
 62+ $vals = array();
 63+ if ( !$img ) {
 64+ $data['missing'] = '';
 65+ } else {
 66+
 67+ $data['repository'] = $img->getRepoName();
 68+
 69+ $isCur = true;
 70+ while($line = $img->nextHistoryLine()) { // assignment
 71+ $vals = array();
 72+
 73+ if ($fld_timestamp)
 74+ $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $line->img_timestamp);
 75+ if ($fld_user)
 76+ $vals['user'] = $line->img_user_text;
 77+ if ($fld_size) {
 78+ $vals['size'] = $line->img_size;
 79+ $vals['width'] = $line->img_width;
 80+ $vals['height'] = $line->img_height;
 81+ }
 82+ if ($fld_url)
 83+ $vals['url'] = $isCur ? $img->getURL() : $img->getArchiveUrl($line->oi_archive_name);
 84+ if ($fld_comment)
 85+ $vals['comment'] = $line->img_description;
 86+
 87+ $data[] = $vals;
 88+
 89+ if (!$history) // Stop after the first line.
 90+ break;
 91+
 92+ $isCur = false;
 93+ }
 94+
 95+ $img->resetHistory();
 96+ }
 97+
 98+ $this->addPageSubItems($pageId, $data);
 99+ }
 100+ }
 101+ }
 102+
 103+ protected function getAllowedParams() {
 104+ return array (
 105+ 'prop' => array (
 106+ ApiBase :: PARAM_ISMULTI => true,
 107+ ApiBase :: PARAM_DFLT => 'timestamp|user',
 108+ ApiBase :: PARAM_TYPE => array (
 109+ 'timestamp',
 110+ 'user',
 111+ 'comment',
 112+ 'url',
 113+ 'size',
 114+ )
 115+ ),
 116+ 'history' => false,
 117+ );
 118+ }
 119+
 120+ protected function getParamDescription() {
 121+ return array (
 122+ 'prop' => 'What image information to get.',
 123+ 'history' => 'Include upload history',
 124+ );
 125+ }
 126+
 127+ protected function getDescription() {
 128+ return array (
 129+ 'Returns image information and upload history'
 130+ );
 131+ }
 132+
 133+ protected function getExamples() {
 134+ return array (
 135+ 'api.php?action=query&titles=Image:Albert%20Einstein%20Head.jpg&prop=imageinfo',
 136+ 'api.php?action=query&titles=Image:Test.jpg&prop=imageinfo&iihistory&iiprop=timestamp|user|url',
 137+ );
 138+ }
 139+
 140+ public function getVersion() {
 141+ return __CLASS__ . ': $Id:$';
 142+ }
 143+}
 144+?>
Property changes on: trunk/phase3/includes/api/ApiQueryImageInfo.php
___________________________________________________________________
Name: svn:eol-style
1145 + native
Name: svn:keywords
2146 + Id
Index: trunk/phase3/includes/api/ApiMain.php
@@ -389,11 +389,11 @@
390390 }
391391
392392 public static function makeHelpMsgHeader($module, $paramName) {
393 - $paramPrefix = $module->getParamPrefix();
394 - if (!empty($paramPrefix))
395 - $paramPrefix = "($paramPrefix) ";
 393+ $modulePrefix = $module->getModulePrefix();
 394+ if (!empty($modulePrefix))
 395+ $modulePrefix = "($modulePrefix) ";
396396
397 - return "* $paramName={$module->getModuleName()} $paramPrefix*";
 397+ return "* $paramName={$module->getModuleName()} $modulePrefix*";
398398 }
399399
400400 private $mIsBot = null;
Index: trunk/phase3/includes/api/ApiPageSet.php
@@ -43,9 +43,11 @@
4444 class ApiPageSet extends ApiQueryBase {
4545
4646 private $mAllPages; // [ns][dbkey] => page_id or 0 when missing
47 - private $mTitles, $mGoodTitles, $mMissingTitles, $mMissingPageIDs, $mRedirectTitles, $mNormalizedTitles, $mInterwikiTitles;
 47+ private $mTitles, $mGoodTitles, $mMissingTitles, $mMissingPageIDs, $mRedirectTitles;
 48+ private $mNormalizedTitles, $mInterwikiTitles;
4849 private $mResolveRedirects, $mPendingRedirectIDs;
4950 private $mGoodRevIDs, $mMissingRevIDs;
 51+ private $mFakePageId;
5052
5153 private $mRequestedPageFields;
5254
@@ -67,6 +69,8 @@
6870 $this->mResolveRedirects = $resolveRedirects;
6971 if($resolveRedirects)
7072 $this->mPendingRedirectIDs = array();
 73+
 74+ $this->mFakePageId = -1;
7175 }
7276
7377 public function isResolvingRedirects() {
@@ -103,6 +107,14 @@
104108 }
105109
106110 /**
 111+ * Returns an array [ns][dbkey] => page_id for all requested titles
 112+ * page_id is a unique negative number in case title was not found
 113+ */
 114+ public function getAllTitlesByNamespace() {
 115+ return $this->mAllPages;
 116+ }
 117+
 118+ /**
107119 * All Title objects provided.
108120 * @return array of Title objects
109121 */
@@ -134,6 +146,7 @@
135147
136148 /**
137149 * Title objects that were NOT found in the database.
 150+ * The array's index will be negative for each item
138151 * @return array of Title objects
139152 */
140153 public function getMissingTitles() {
@@ -406,8 +419,9 @@
407420 foreach ($remaining as $ns => $dbkeys) {
408421 foreach ( $dbkeys as $dbkey => $unused ) {
409422 $title = Title :: makeTitle($ns, $dbkey);
410 - $this->mMissingTitles[] = $title;
411 - $this->mAllPages[$ns][$dbkey] = 0;
 423+ $this->mAllPages[$ns][$dbkey] = $this->mFakePageId;
 424+ $this->mMissingTitles[$this->mFakePageId] = $title;
 425+ $this->mFakePageId--;
412426 $this->mTitles[] = $title;
413427 }
414428 }
Index: trunk/phase3/includes/api/ApiQueryBase.php
@@ -151,10 +151,20 @@
152152 return $this->mQueryModule;
153153 }
154154
 155+ /**
 156+ * Add sub-element under the page element with the given pageId.
 157+ */
 158+ protected function addPageSubItems($pageId, $data) {
 159+ $result = $this->getResult();
 160+ $result->setIndexedTagName($data, $this->getModulePrefix());
 161+ $result->addValue(array ('query', 'pages', intval($pageId)),
 162+ $this->getModuleName(),
 163+ $data);
 164+ }
 165+
155166 protected function setContinueEnumParameter($paramName, $paramValue) {
156 - $msg = array (
157 - $this->encodeParamName($paramName
158 - ) => $paramValue);
 167+
 168+ $msg = array( $this->encodeParamName($paramName) => $paramValue );
159169 $this->getResult()->addValue('query-continue', $this->getModuleName(), $msg);
160170 }
161171
Index: trunk/phase3/includes/api/ApiQueryExternalLinks.php
@@ -75,14 +75,6 @@
7676 $db->freeResult($res);
7777 }
7878
79 - private function addPageSubItems($pageId, $data) {
80 - $result = $this->getResult();
81 - $result->setIndexedTagName($data, 'el');
82 - $result->addValue(array ('query', 'pages', intval($pageId)),
83 - 'extlinks',
84 - $data);
85 - }
86 -
8779 protected function getDescription() {
8880 return 'Returns all external urls (not interwikies) from the given page(s)';
8981 }
Index: trunk/phase3/includes/api/ApiBase.php
@@ -52,15 +52,15 @@
5353 const LIMIT_SML1 = 50; // Slow query, std user limit
5454 const LIMIT_SML2 = 500; // Slow query, bot/sysop limit
5555
56 - private $mMainModule, $mModuleName, $mParamPrefix;
 56+ private $mMainModule, $mModuleName, $mModulePrefix;
5757
5858 /**
5959 * Constructor
6060 */
61 - public function __construct($mainModule, $moduleName, $paramPrefix = '') {
 61+ public function __construct($mainModule, $moduleName, $modulePrefix = '') {
6262 $this->mMainModule = $mainModule;
6363 $this->mModuleName = $moduleName;
64 - $this->mParamPrefix = $paramPrefix;
 64+ $this->mModulePrefix = $modulePrefix;
6565 }
6666
6767 /**
@@ -78,8 +78,8 @@
7979 /**
8080 * Get parameter prefix (usually two letters or an empty string).
8181 */
82 - public function getParamPrefix() {
83 - return $this->mParamPrefix;
 82+ public function getModulePrefix() {
 83+ return $this->mModulePrefix;
8484 }
8585
8686 /**
@@ -296,7 +296,7 @@
297297 * Override this method to change parameter name during runtime
298298 */
299299 public function encodeParamName($paramName) {
300 - return $this->mParamPrefix . $paramName;
 300+ return $this->mModulePrefix . $paramName;
301301 }
302302
303303 /**
Index: trunk/phase3/includes/api/ApiQueryCategories.php
@@ -124,14 +124,6 @@
125125 $db->freeResult($res);
126126 }
127127
128 - private function addPageSubItems($pageId, $data) {
129 - $result = $this->getResult();
130 - $result->setIndexedTagName($data, 'cl');
131 - $result->addValue(array ('query', 'pages', intval($pageId)),
132 - 'categories',
133 - $data);
134 - }
135 -
136128 protected function getAllowedParams() {
137129 return array (
138130 'prop' => array (
Index: trunk/phase3/includes/api/ApiQueryImages.php
@@ -102,14 +102,6 @@
103103 $db->freeResult($res);
104104 }
105105
106 - private function addPageSubItems($pageId, $data) {
107 - $result = $this->getResult();
108 - $result->setIndexedTagName($data, 'im');
109 - $result->addValue(array ('query', 'pages', intval($pageId)),
110 - 'images',
111 - $data);
112 - }
113 -
114106 protected function getDescription() {
115107 return 'Returns all images contained on the given page(s)';
116108 }
Index: trunk/phase3/includes/AutoLoader.php
@@ -314,6 +314,7 @@
315315 'ApiQueryContributions' => 'includes/api/ApiQueryUserContributions.php',
316316 'ApiQueryExternalLinks' => 'includes/api/ApiQueryExternalLinks.php',
317317 'ApiQueryImages' => 'includes/api/ApiQueryImages.php',
 318+ 'ApiQueryImageInfo' => 'includes/api/ApiQueryImageInfo.php',
318319 'ApiQueryInfo' => 'includes/api/ApiQueryInfo.php',
319320 'ApiQueryLangLinks' => 'includes/api/ApiQueryLangLinks.php',
320321 'ApiQueryLinks' => 'includes/api/ApiQueryLinks.php',
Index: trunk/phase3/RELEASE-NOTES
@@ -263,7 +263,7 @@
264264
265265 == API changes since 1.10 ==
266266
267 -(For ongoing development discussion, see http://www.mediawiki.org/wiki/API)
 267+Full API documentation is available at http://www.mediawiki.org/wiki/API
268268
269269 * New properties: links, templates, images, langlinks, categories, external
270270 links
@@ -302,6 +302,7 @@
303303 * (bug 10297) include previous revision ID in prop=revisions
304304 * backlinks, embeddedin and imageusage lists should use (bl|ei|iu)title parameter
305305 instead of titles. Titles for these lists is obsolete and might stop working soon.
 306+* Added prop=imageinfo - gets image properties and upload history
306307
307308 == Maintenance script changes since 1.10 ==
308309

Follow-up revisions

RevisionCommit summaryAuthorDate
r23820* Merged r23504:r23819 from trunk...robchurch03:45, 7 July 2007
r23912Merged revisions 23662-23909 via svnmerge from...david18:11, 9 July 2007

Status & tagging log