* API: General query modules order of execution
[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 ("ApiBase.php");
30 }
31
32 class ApiPageSet extends ApiBase {
33
34 private $allPages; // [ns][dbkey] => page_id or 0 when missing
35 private $db, $resolveRedirs;
36 private $goodTitles, $missingTitles, $redirectTitles, $normalizedTitles;
37
38 public function __construct($main, $db, $resolveRedirs) {
39 parent :: __construct($main);
40 $this->db = $db;
41 $this->resolveRedirs = $resolveRedirs;
42
43 $this->allPages = array ();
44 $this->goodTitles = array ();
45 $this->missingTitles = array ();
46 $this->redirectTitles = array ();
47 $this->normalizedTitles = array();
48 }
49
50 /**
51 * Title objects that were found in the database.
52 * @return array page_id (int) => Title (obj)
53 */
54 public function GetGoodTitles() {
55 return $this->goodTitles;
56 }
57
58 /**
59 * Title objects that were NOT found in the database.
60 * @return array of Title objects
61 */
62 public function GetMissingTitles() {
63 return $this->missingTitles;
64 }
65
66 /**
67 * Get a list of redirects when doing redirect resolution
68 * @return array prefixed_title (string) => prefixed_title (string)
69 */
70 public function GetRedirectTitles() {
71 return $this->redirectTitles;
72 }
73
74 /**
75 * Get a list of title normalizations - maps the title given
76 * with its normalized version.
77 * @return array raw_prefixed_title (string) => prefixed_title (string)
78 */
79 public function GetNormalizedTitles() {
80 return $this->normalizedTitles;
81 }
82
83 /**
84 * Given an array of title strings, convert them into Title objects.
85 * This method validates access rights for the title,
86 * and appends normalization values to the output.
87 *
88 * @return LinkBatch of title objects.
89 */
90 private function ProcessTitlesStrings($titles) {
91
92 $linkBatch = new LinkBatch();
93
94 foreach ($titles as $titleString) {
95 $titleObj = Title :: newFromText($titleString);
96
97 // Validation
98 if (!$titleObj)
99 $this->dieUsage("bad title $titleString", 'invalidtitle');
100 if ($titleObj->getNamespace() < 0)
101 $this->dieUsage("No support for special page $titleString has been implemented", 'unsupportednamespace');
102 if (!$titleObj->userCanRead())
103 $this->dieUsage("No read permission for $titleString", 'titleaccessdenied');
104
105 $linkBatch->addObj($titleObj);
106
107 // Make sure we remember the original title that was given to us
108 // This way the caller can correlate new titles with the originally requested,
109 // i.e. namespace is localized or capitalization is different
110 if ($titleString !== $titleObj->getPrefixedText()) {
111 $this->normalizedTitles[$titleString] = $titleObj->getPrefixedText();
112 }
113 }
114
115 return $linkBatch;
116 }
117
118 /**
119 * This method populates internal variables with page information
120 * based on the given array of title strings.
121 *
122 * Steps:
123 * #1 For each title, get data from `page` table
124 * #2 If page was not found in the DB, store it as missing
125 *
126 * Additionally, when resolving redirects:
127 * #3 If no more redirects left, stop.
128 * #4 For each redirect, get its links from `pagelinks` table.
129 * #5 Substitute the original LinkBatch object with the new list
130 * #6 Repeat from step #1
131 */
132 public function PopulateTitles($titles) {
133 $pageFlds = array (
134 'page_id',
135 'page_namespace',
136 'page_title'
137 );
138 if ($this->resolveRedirs) {
139 $pageFlds[] = 'page_is_redirect';
140 }
141
142 // Get validated and normalized title objects
143 $linkBatch = $this->ProcessTitlesStrings($titles);
144
145 //
146 // Repeat until all redirects have been resolved
147 //
148 while (false !== ($set = $linkBatch->constructSet('page', $this->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 $res = $this->db->select('page', $pageFlds, $set, __CLASS__ . '::' . __FUNCTION__);
159 while ($row = $this->db->fetchObject($res)) {
160
161 unset ($remaining[$row->page_namespace][$row->page_title]);
162 $title = Title :: makeTitle($row->page_namespace, $row->page_title);
163 $this->allPages[$row->page_namespace][$row->page_title] = $row->page_id;
164
165 if ($this->resolveRedirs && $row->page_is_redirect == '1') {
166 $redirectIds[$row->page_id] = $title;
167 } else {
168 $this->goodTitles[$row->page_id] = $title;
169 }
170 }
171 $this->db->freeResult($res);
172
173 //
174 // The remaining titles in $remaining are non-existant pages
175 //
176 foreach ($remaining as $ns => $dbkeys) {
177 foreach ($dbkeys as $dbkey => $nothing) {
178 $this->missingTitles[] = Title :: makeTitle($ns, $dbkey);
179 $this->allPages[$ns][$dbkey] = 0;
180 }
181 }
182
183 if (!$this->resolveRedirs || empty ($redirectIds))
184 break;
185
186 //
187 // Resolve redirects by querying the pagelinks table, and repeat the process
188 //
189
190 // Create a new linkBatch object for the next pass
191 $linkBatch = new LinkBatch();
192
193 // find redirect targets for all redirect pages
194 $res = $this->db->select('pagelinks', array (
195 'pl_from',
196 'pl_namespace',
197 'pl_title'
198 ), array (
199 'pl_from' => array_keys($redirectIds
200 )), __CLASS__ . '::' . __FUNCTION__);
201
202 while ($row = $this->db->fetchObject($res)) {
203
204 // Bug 7304 workaround
205 // ( http://bugzilla.wikipedia.org/show_bug.cgi?id=7304 )
206 // A redirect page may have more than one link.
207 // This code will only use the first link returned.
208 if (isset ($redirectIds[$row->pl_from])) { // remove line when 7304 is fixed
209
210 $titleStrFrom = $redirectIds[$row->pl_from]->getPrefixedText();
211 $titleStrTo = Title :: makeTitle($row->pl_namespace, $row->pl_title)->getPrefixedText();
212 $this->redirectTitles[$titleStrFrom] = $titleStrTo;
213
214 unset ($redirectIds[$row->pl_from]); // remove line when 7304 is fixed
215
216 // Avoid an infinite loop by checking if we have already processed this target
217 if (!isset ($this->allPages[$row->pl_namespace][$row->pl_title])) {
218 $linkBatch->add($row->pl_namespace, $row->pl_title);
219 }
220 }
221 }
222 $this->db->freeResult($res);
223 }
224 }
225
226 public function Execute() {
227 $this->DieDebug("Execute() is not supported on this object");
228 }
229 }
230 ?>