* Code cleanup per TimStarling's suggestions
[lhc/web/wiklou.git] / includes / api / ApiPageSet.php
1 <?php
2
3
4 /*
5 * Created on Sep 24, 2006
6 *
7 * API for MediaWiki 1.8+
8 *
9 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@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 class ApiPageSet extends ApiQueryBase {
33
34 private $mAllPages; // [ns][dbkey] => page_id or 0 when missing
35 private $mGoodTitles, $mMissingTitles, $mRedirectTitles, $mNormalizedTitles;
36
37 private $mRequestedFields;
38
39 public function __construct($query) {
40 parent :: __construct($query, __CLASS__);
41
42 $this->mAllPages = array ();
43 $this->mGoodTitles = array ();
44 $this->mMissingTitles = array ();
45 $this->mRedirectTitles = array ();
46 $this->mNormalizedTitles = array ();
47
48 $this->mRequestedFields = array ();
49 }
50
51 public function requestField($fieldName) {
52 $this->mRequestedFields[$fieldName] = null;
53 }
54
55 /**
56 * Title objects that were found in the database.
57 * @return array page_id (int) => Title (obj)
58 */
59 public function getGoodTitles() {
60 return $this->mGoodTitles;
61 }
62
63 /**
64 * Title objects that were NOT found in the database.
65 * @return array of Title objects
66 */
67 public function getMissingTitles() {
68 return $this->mMissingTitles;
69 }
70
71 /**
72 * Get a list of redirects when doing redirect resolution
73 * @return array prefixed_title (string) => prefixed_title (string)
74 */
75 public function getRedirectTitles() {
76 return $this->mRedirectTitles;
77 }
78
79 /**
80 * Get a list of title normalizations - maps the title given
81 * with its normalized version.
82 * @return array raw_prefixed_title (string) => prefixed_title (string)
83 */
84 public function getNormalizedTitles() {
85 return $this->mNormalizedTitles;
86 }
87
88 /**
89 * Returns the number of unique pages (not revisions) in the set.
90 */
91 public function getGoodTitleCount() {
92 return count($this->getGoodTitles());
93 }
94
95 /**
96 * Get the list of revision IDs (requested with revids= parameter)
97 */
98 public function getRevisionIDs() {
99 $this->dieUsage(__METHOD__ . ' is not implemented', 'notimplemented');
100 }
101
102 /**
103 * Returns the number of revisions (requested with revids= parameter)
104 */
105 public function getRevisionCount() {
106 return 0; // TODO: implement
107 }
108
109 /**
110 * This method populates internal variables with page information
111 * based on the given array of title strings.
112 *
113 * Steps:
114 * #1 For each title, get data from `page` table
115 * #2 If page was not found in the DB, store it as missing
116 *
117 * Additionally, when resolving redirects:
118 * #3 If no more redirects left, stop.
119 * #4 For each redirect, get its links from `pagelinks` table.
120 * #5 Substitute the original LinkBatch object with the new list
121 * #6 Repeat from step #1
122 */
123 private function populateTitles($titles, $redirects) {
124
125 // Ensure we get minimum required fields
126 $pageFlds = array (
127 'page_id' => null,
128 'page_namespace' => null,
129 'page_title' => null
130 );
131
132 // only store non-default fields
133 $this->mRequestedFields = array_diff_key($this->mRequestedFields, $pageFlds);
134
135 if ($redirects)
136 $pageFlds['page_is_redirect'] = null;
137
138 $pageFlds = array_keys(array_merge($this->mRequestedFields, $pageFlds));
139
140 // Get validated and normalized title objects
141 $linkBatch = $this->processTitlesStrArray($titles);
142
143 $db = $this->getDB();
144
145 //
146 // Repeat until all redirects have been resolved
147 //
148 while (false !== ($set = $linkBatch->constructSet('page', $db))) {
149
150 // Hack: get the ns:titles stored in array(ns => array(titles)) format
151 $remaining = $linkBatch->data;
152
153 $redirectIds = array ();
154
155 //
156 // Get data about $linkBatch from `page` table
157 //
158 $this->profileDBIn();
159 $res = $db->select('page', $pageFlds, $set, __METHOD__);
160 $this->profileDBOut();
161 while ($row = $db->fetchObject($res)) {
162
163 unset ($remaining[$row->page_namespace][$row->page_title]);
164 $title = Title :: makeTitle($row->page_namespace, $row->page_title);
165 $this->mAllPages[$row->page_namespace][$row->page_title] = $row->page_id;
166
167 if ($redirects && $row->page_is_redirect == '1') {
168 $redirectIds[$row->page_id] = $title;
169 } else {
170 $this->mGoodTitles[$row->page_id] = $title;
171 }
172 }
173 $db->freeResult($res);
174
175 //
176 // The remaining titles in $remaining are non-existant pages
177 //
178 foreach ($remaining as $ns => $dbkeys) {
179 foreach ($dbkeys as $dbkey => $nothing) {
180 $this->mMissingTitles[] = Title :: makeTitle($ns, $dbkey);
181 $this->mAllPages[$ns][$dbkey] = 0;
182 }
183 }
184
185 if (!$redirects || empty ($redirectIds))
186 break;
187
188 //
189 // Resolve redirects by querying the pagelinks table, and repeat the process
190 //
191
192 // Create a new linkBatch object for the next pass
193 $linkBatch = new LinkBatch();
194
195 // find redirect targets for all redirect pages
196 $this->profileDBIn();
197 $res = $db->select('pagelinks', array (
198 'pl_from',
199 'pl_namespace',
200 'pl_title'
201 ), array (
202 'pl_from' => array_keys($redirectIds
203 )), __METHOD__);
204 $this->profileDBOut();
205
206 while ($row = $db->fetchObject($res)) {
207
208 // Bug 7304 workaround
209 // ( http://bugzilla.wikipedia.org/show_bug.cgi?id=7304 )
210 // A redirect page may have more than one link.
211 // This code will only use the first link returned.
212 if (isset ($redirectIds[$row->pl_from])) { // remove line when 7304 is fixed
213
214 $titleStrFrom = $redirectIds[$row->pl_from]->getPrefixedText();
215 $titleStrTo = Title :: makeTitle($row->pl_namespace, $row->pl_title)->getPrefixedText();
216 $this->mRedirectTitles[$titleStrFrom] = $titleStrTo;
217
218 unset ($redirectIds[$row->pl_from]); // remove line when 7304 is fixed
219
220 // Avoid an infinite loop by checking if we have already processed this target
221 if (!isset ($this->mAllPages[$row->pl_namespace][$row->pl_title])) {
222 $linkBatch->add($row->pl_namespace, $row->pl_title);
223 }
224 }
225 }
226 $db->freeResult($res);
227 }
228 }
229
230 /**
231 * Given an array of title strings, convert them into Title objects.
232 * This method validates access rights for the title,
233 * and appends normalization values to the output.
234 *
235 * @return LinkBatch of title objects.
236 */
237 private function processTitlesStrArray($titles) {
238
239 $linkBatch = new LinkBatch();
240
241 foreach ($titles as $titleString) {
242 $titleObj = Title :: newFromText($titleString);
243
244 // Validation
245 if (!$titleObj)
246 $this->dieUsage("bad title $titleString", 'invalidtitle');
247 if ($titleObj->getNamespace() < 0)
248 $this->dieUsage("No support for special page $titleString has been implemented", 'unsupportednamespace');
249 if (!$titleObj->userCanRead())
250 $this->dieUsage("No read permission for $titleString", 'titleaccessdenied');
251
252 $linkBatch->addObj($titleObj);
253
254 // Make sure we remember the original title that was given to us
255 // This way the caller can correlate new titles with the originally requested,
256 // i.e. namespace is localized or capitalization is different
257 if ($titleString !== $titleObj->getPrefixedText()) {
258 $this->mNormalizedTitles[$titleString] = $titleObj->getPrefixedText();
259 }
260 }
261
262 return $linkBatch;
263 }
264
265 private function populatePageIDs($pageids) {
266 $this->dieUsage(__METHOD__ . ' is not implemented', 'notimplemented');
267 }
268
269 public function execute() {
270 $titles = $pageids = $revids = $redirects = null;
271 extract($this->extractRequestParams());
272
273 // Only one of the titles/pageids/revids is allowed at the same time
274 $dataSource = null;
275 if (isset ($titles))
276 $dataSource = 'titles';
277 if (isset ($pageids)) {
278 if (isset ($dataSource))
279 $this->dieUsage("Cannot use 'pageids' at the same time as '$dataSource'", 'multisource');
280 $dataSource = 'pageids';
281 }
282 if (isset ($revids)) {
283 if (isset ($dataSource))
284 $this->dieUsage("Cannot use 'revids' at the same time as '$dataSource'", 'multisource');
285 $dataSource = 'revids';
286 }
287
288 switch ($dataSource) {
289 case 'titles' :
290 $this->populateTitles($titles, $redirects);
291 break;
292 case 'pageids' :
293 $this->populatePageIDs($pageids, $redirects);
294 break;
295 case 'revids' :
296 $this->populateRevIDs($revids);
297 break;
298 default :
299 // Do nothing - some queries do not need any of the data sources.
300 break;
301 }
302 }
303
304 protected function getAllowedParams() {
305 return array (
306 'titles' => array (
307 ApiBase::PARAM_ISMULTI => true
308 ),
309 'pageids' => array (
310 ApiBase::PARAM_TYPE => 'integer',
311 ApiBase::PARAM_ISMULTI => true
312 ),
313 'revids' => array (
314 ApiBase::PARAM_TYPE => 'integer',
315 ApiBase::PARAM_ISMULTI => true
316 ),
317 'redirects' => false
318 );
319 }
320
321 protected function getParamDescription() {
322 return array (
323 'titles' => 'A list of titles to work on',
324 'pageids' => 'A list of page IDs to work on',
325 'revids' => 'A list of revision IDs to work on',
326 'redirects' => 'Automatically resolve redirects'
327 );
328 }
329 }
330 ?>