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