r29981 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r29980‎ | r29981 | r29982 >
Date:13:09, 20 January 2008
Author:catrope
Status:old
Tags:
Comment:
* Refactoring ApiQueryImageInfo to use new File::loadHistory() interface. No change in user-observed behavior
Modified paths:
  • /trunk/phase3/includes/api/ApiQueryImageInfo.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiQueryImageInfo.php
@@ -42,20 +42,20 @@
4343 public function execute() {
4444 $params = $this->extractRequestParams();
4545
46 - $history = $params['history'];
47 -
4846 $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 - $fld_sha1 = isset($prop['sha1']);
55 - $fld_metadata = isset($prop['metadata']);
 47+ $this->fld_timestamp = isset($prop['timestamp']);
 48+ $this->fld_user = isset($prop['user']);
 49+ $this->fld_comment = isset($prop['comment']);
 50+ $this->fld_url = isset($prop['url']);
 51+ $this->fld_size = isset($prop['size']);
 52+ $this->fld_sha1 = isset($prop['sha1']);
 53+ $this->fld_metadata = isset($prop['metadata']);
5654
5755 if($params['urlheight'] != -1 && $params['urlwidth'] == -1)
5856 $this->dieUsage("iiurlheight cannot be used without iiurlwidth", 'iiurlwidth');
59 - $scale = $params['urlwidth'] != -1;
 57+ $this->scale = $params['urlwidth'] != -1;
 58+ $this->urlwidth = $params['urlwidth'];
 59+ $this->urlheight = $params['urlheight'];
6060
6161 $pageIds = $this->getPageSet()->getAllTitlesByNamespace();
6262 if (!empty($pageIds[NS_IMAGE])) {
@@ -70,59 +70,21 @@
7171 } else {
7272
7373 $repository = $img->getRepoName();
74 -
75 - $isCur = true;
76 - $count = 0;
77 - while($line = $img->nextHistoryLine()) { // assignment
78 - # FIXME: Limiting to 500 because it's unlimited right now
79 - # 500+ image histories are scarce, but this has DoS potential
80 - # FileRepo.php should be fixed
81 - if($count++ == 500)
82 - break;
83 - $row = get_object_vars( $line );
84 - $vals = array();
85 - $prefix = $isCur ? 'img' : 'oi';
86 -
87 - if ($fld_timestamp)
88 - $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row["${prefix}_timestamp"]);
89 - if ($fld_user) {
90 - $vals['user'] = $row["${prefix}_user_text"];
91 - if(!$row["${prefix}_user"])
92 - $vals['anon'] = '';
93 - }
94 - if ($fld_size) {
95 - $vals['size'] = intval($row["{$prefix}_size"]);
96 - $vals['width'] = intval($row["{$prefix}_width"]);
97 - $vals['height'] = intval($row["{$prefix}_height"]);
98 - }
99 - if ($fld_url) {
100 - if($scale && $isCur) {
101 - $vals['url'] = $img->createThumb($params['urlwidth'], $params['urlheight']);
102 - } else {
103 - $vals['url'] = $isCur ? $img->getURL() : $img->getArchiveUrl($row["oi_archive_name"]);
104 - }
105 - }
106 - if ($fld_comment)
107 - $vals['comment'] = $row["{$prefix}_description"];
108 -
109 - if ($fld_sha1)
110 - $vals['sha1'] = wfBaseConvert($row["{$prefix}_sha1"], 36, 16, 40);
111 -
112 - if ($fld_metadata) {
113 - $metadata = unserialize($row["{$prefix}_metadata"]);
114 - $vals['metadata'] = $metadata ? $metadata : null;
115 - $this->getResult()->setIndexedTagName_recursive($vals['metadata'], 'meta');
116 - }
117 -
118 - $data[] = $vals;
119 -
120 - if (!$history) // Stop after the first line.
121 - break;
122 -
123 - $isCur = false;
 74+
 75+ // Get information about the current version first
 76+ // Check that the current version is within the start-end boundaries
 77+ if((is_null($params['start']) || $img->getTimestamp() <= $params['start']) &&
 78+ (is_null($params['end']) || $img->getTimestamp() >= $params['end'])) {
 79+ $data[] = $this->getInfo($img);
12480 }
12581
126 - $img->resetHistory();
 82+ // Now get the old revisions
 83+ if($params['limit'] > 1) {
 84+ $oldies = $img->getHistory($params['limit'] - 1, $params['start'], $params['end']);
 85+ if(!empty($oldies))
 86+ foreach($oldies as $oldie)
 87+ $data[] = $this->getInfo($oldie);
 88+ }
12789 }
12890
12991 $this->getResult()->addValue(array(
@@ -135,6 +97,44 @@
13698 }
13799 }
138100
 101+ /**
 102+ * Get result information for an image revision
 103+ * @param File f The image
 104+ * @return array Result array
 105+ */
 106+ protected function getInfo($f) {
 107+ $vals = array();
 108+ if($this->fld_timestamp)
 109+ $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $f->getTimestamp());
 110+ if($this->fld_user) {
 111+ $vals['user'] = $f->getUser();
 112+ if(!$f->getUser('id'))
 113+ $vals['anon'] = '';
 114+ }
 115+ if($this->fld_size) {
 116+ $vals['size'] = $f->getSize();
 117+ $vals['width'] = $f->getWidth();
 118+ $vals['height'] = $f->getHeight();
 119+ }
 120+ if($this->fld_url) {
 121+ if($this->scale) {
 122+ $vals['url'] = $f->createThumb($this->urlwidth, $this->urlheight);
 123+ } else {
 124+ $vals['url'] = $f->getURL();
 125+ }
 126+ }
 127+ if($this->fld_comment)
 128+ $vals['comment'] = $f->getDescription();
 129+ if($this->fld_sha1)
 130+ $vals['sha1'] = $f->getSha1();
 131+ if($this->fld_metadata) {
 132+ $metadata = unserialize($f->getMetadata());
 133+ $vals['metadata'] = $metadata ? $metadata : null;
 134+ $this->getResult()->setIndexedTagName_recursive($vals['metadata'], 'meta');
 135+ }
 136+ return $vals;
 137+ }
 138+
139139 protected function getAllowedParams() {
140140 return array (
141141 'prop' => array (
@@ -150,7 +150,19 @@
151151 'metadata'
152152 )
153153 ),
154 - 'history' => false,
 154+ 'limit' => array(
 155+ ApiBase :: PARAM_TYPE => 'limit',
 156+ ApiBase :: PARAM_DFLT => 1,
 157+ ApiBase :: PARAM_MIN => 1,
 158+ ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
 159+ ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
 160+ ),
 161+ 'start' => array(
 162+ ApiBase :: PARAM_TYPE => 'timestamp'
 163+ ),
 164+ 'end' => array(
 165+ ApiBase :: PARAM_TYPE => 'timestamp'
 166+ ),
155167 'urlwidth' => array(
156168 ApiBase :: PARAM_TYPE => 'integer',
157169 ApiBase :: PARAM_DFLT => -1
@@ -165,7 +177,9 @@
166178 protected function getParamDescription() {
167179 return array (
168180 'prop' => 'What image information to get.',
169 - 'history' => 'Include upload history',
 181+ 'limit' => 'How many image revisions to return',
 182+ 'start' => 'Timestamp to start listing from',
 183+ 'end' => 'Timestamp to stop listing at',
170184 'urlwidth' => 'If iiprop=url is set, a URL to an image scaled to this width will be returned. Only the current version of the image can be scaled.',
171185 'urlheight' => 'Similar to iiurlwidth. Cannot be used without iiurlwidth',
172186 );
@@ -180,7 +194,7 @@
181195 protected function getExamples() {
182196 return array (
183197 'api.php?action=query&titles=Image:Albert%20Einstein%20Head.jpg&prop=imageinfo',
184 - 'api.php?action=query&titles=Image:Test.jpg&prop=imageinfo&iihistory&iiprop=timestamp|user|url',
 198+ 'api.php?action=query&titles=Image:Test.jpg&prop=imageinfo&iilimit=50&iiend=20071231235959&iiprop=timestamp|user|url',
185199 );
186200 }
187201

Follow-up revisions

RevisionCommit summaryAuthorDate
r29983RELEASE-NOTES update for r29981, which DOES change the interface, contrary to...catrope14:02, 20 January 2008
r30081ApiQueryImageInfo: Convert sha1 to base16 like before r29981btongminh20:32, 23 January 2008
r30432Convert size, width and height to int, as before r29981btongminh16:04, 2 February 2008

Status & tagging log