5 * Created on Sep 24, 2006
7 * API for MediaWiki 1.8+
9 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
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.
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.
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
27 if (!defined('MEDIAWIKI')) {
28 // Eclipse helper - will be ignored in production
29 require_once ('ApiQueryBase.php');
32 class ApiPageSet
extends ApiQueryBase
{
34 private $mAllPages; // [ns][dbkey] => page_id or 0 when missing
35 private $mGoodTitles, $mMissingTitles, $mRedirectTitles, $mNormalizedTitles;
37 public function __construct($query) {
38 parent
:: __construct($query, __CLASS__
);
40 $this->mAllPages
= array ();
41 $this->mGoodTitles
= array ();
42 $this->mMissingTitles
= array ();
43 $this->mRedirectTitles
= array ();
44 $this->mNormalizedTitles
= array ();
48 * Title objects that were found in the database.
49 * @return array page_id (int) => Title (obj)
51 public function getGoodTitles() {
52 return $this->mGoodTitles
;
56 * Title objects that were NOT found in the database.
57 * @return array of Title objects
59 public function getMissingTitles() {
60 return $this->mMissingTitles
;
64 * Get a list of redirects when doing redirect resolution
65 * @return array prefixed_title (string) => prefixed_title (string)
67 public function getRedirectTitles() {
68 return $this->mRedirectTitles
;
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)
76 public function getNormalizedTitles() {
77 return $this->mNormalizedTitles
;
81 * Returns the number of unique pages (not revisions) in the set.
83 public function getGoodTitleCount() {
84 return count($this->getGoodTitles());
88 * Get the list of revision IDs (requested with revids= parameter)
90 public function getRevisionIDs() {
91 $this->dieUsage(__FUNCTION__
. ' is not implemented', 'notimplemented');
95 * Returns the number of revisions (requested with revids= parameter)
97 public function getRevisionCount() {
98 return 0; // TODO: implement
102 * This method populates internal variables with page information
103 * based on the given array of title strings.
106 * #1 For each title, get data from `page` table
107 * #2 If page was not found in the DB, store it as missing
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
115 private function populateTitles($titles, $redirects) {
123 $pageFlds[] = 'page_is_redirect';
126 // Get validated and normalized title objects
127 $linkBatch = $this->processTitlesStrArray($titles);
129 $db = $this->getDB();
132 // Repeat until all redirects have been resolved
134 while (false !== ($set = $linkBatch->constructSet('page', $db))) {
136 // Hack: get the ns:titles stored in array(ns => array(titles)) format
137 $remaining = $linkBatch->data
;
139 $redirectIds = array ();
142 // Get data about $linkBatch from `page` table
144 $this->profileDBIn();
145 $res = $db->select('page', $pageFlds, $set, __CLASS__
. '::' . __FUNCTION__
);
146 $this->profileDBOut();
147 while ($row = $db->fetchObject($res)) {
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
;
153 if ($redirects && $row->page_is_redirect
== '1') {
154 $redirectIds[$row->page_id
] = $title;
156 $this->mGoodTitles
[$row->page_id
] = $title;
159 $db->freeResult($res);
162 // The remaining titles in $remaining are non-existant pages
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;
171 if (!$redirects ||
empty ($redirectIds))
175 // Resolve redirects by querying the pagelinks table, and repeat the process
178 // Create a new linkBatch object for the next pass
179 $linkBatch = new LinkBatch();
181 // find redirect targets for all redirect pages
182 $this->profileDBIn();
183 $res = $db->select('pagelinks', array (
188 'pl_from' => array_keys($redirectIds
189 )), __CLASS__
. '::' . __FUNCTION__
);
190 $this->profileDBOut();
192 while ($row = $db->fetchObject($res)) {
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
200 $titleStrFrom = $redirectIds[$row->pl_from
]->getPrefixedText();
201 $titleStrTo = Title
:: makeTitle($row->pl_namespace
, $row->pl_title
)->getPrefixedText();
202 $this->mRedirectTitles
[$titleStrFrom] = $titleStrTo;
204 unset ($redirectIds[$row->pl_from
]); // remove line when 7304 is fixed
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
);
212 $db->freeResult($res);
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.
221 * @return LinkBatch of title objects.
223 private function processTitlesStrArray($titles) {
225 $linkBatch = new LinkBatch();
227 foreach ($titles as $titleString) {
228 $titleObj = Title
:: newFromText($titleString);
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');
238 $linkBatch->addObj($titleObj);
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();
251 private function populatePageIDs($pageids) {
252 $this->dieUsage(__FUNCTION__
. ' is not implemented', 'notimplemented');
255 public function execute() {
256 $titles = $pageids = $revids = $redirects = null;
257 extract($this->extractRequestParams());
259 // Only one of the titles/pageids/revids is allowed at the same time
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';
268 if (isset ($revids)) {
269 if (isset ($dataSource))
270 $this->dieUsage("Cannot use 'revids' at the same time as '$dataSource'", 'multisource');
271 $dataSource = 'revids';
274 switch ($dataSource) {
276 $this->populateTitles($titles, $redirects);
279 $this->populatePageIDs($pageids, $redirects);
282 $this->populateRevIDs($revids);
285 // Do nothing - some queries do not need any of the data sources.
290 protected function getAllowedParams() {
293 GN_ENUM_ISMULTI
=> true
296 GN_ENUM_TYPE
=> 'integer',
297 GN_ENUM_ISMULTI
=> true
300 GN_ENUM_TYPE
=> 'integer',
301 GN_ENUM_ISMULTI
=> true
307 protected function getParamDescription() {
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'