Index: trunk/phase3/includes/api/ApiQueryImageInfo.php |
— | — | @@ -42,20 +42,20 @@ |
43 | 43 | public function execute() { |
44 | 44 | $params = $this->extractRequestParams(); |
45 | 45 | |
46 | | - $history = $params['history']; |
47 | | - |
48 | 46 | $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']); |
56 | 54 | |
57 | 55 | if($params['urlheight'] != -1 && $params['urlwidth'] == -1) |
58 | 56 | $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']; |
60 | 60 | |
61 | 61 | $pageIds = $this->getPageSet()->getAllTitlesByNamespace(); |
62 | 62 | if (!empty($pageIds[NS_IMAGE])) { |
— | — | @@ -70,59 +70,21 @@ |
71 | 71 | } else { |
72 | 72 | |
73 | 73 | $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); |
124 | 80 | } |
125 | 81 | |
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 | + } |
127 | 89 | } |
128 | 90 | |
129 | 91 | $this->getResult()->addValue(array( |
— | — | @@ -135,6 +97,44 @@ |
136 | 98 | } |
137 | 99 | } |
138 | 100 | |
| 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 | + |
139 | 139 | protected function getAllowedParams() { |
140 | 140 | return array ( |
141 | 141 | 'prop' => array ( |
— | — | @@ -150,7 +150,19 @@ |
151 | 151 | 'metadata' |
152 | 152 | ) |
153 | 153 | ), |
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 | + ), |
155 | 167 | 'urlwidth' => array( |
156 | 168 | ApiBase :: PARAM_TYPE => 'integer', |
157 | 169 | ApiBase :: PARAM_DFLT => -1 |
— | — | @@ -165,7 +177,9 @@ |
166 | 178 | protected function getParamDescription() { |
167 | 179 | return array ( |
168 | 180 | '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', |
170 | 184 | '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.', |
171 | 185 | 'urlheight' => 'Similar to iiurlwidth. Cannot be used without iiurlwidth', |
172 | 186 | ); |
— | — | @@ -180,7 +194,7 @@ |
181 | 195 | protected function getExamples() { |
182 | 196 | return array ( |
183 | 197 | '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', |
185 | 199 | ); |
186 | 200 | } |
187 | 201 | |