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