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 ("ApiBase.php");
32 class ApiPageSet
extends ApiBase
{
34 private $allPages; // [ns][dbkey] => page_id or 0 when missing
35 private $db, $resolveRedirs;
36 private $goodTitles, $missingTitles, $redirectTitles, $normalizedTitles;
38 public function __construct($main, $db, $resolveRedirs) {
39 parent
:: __construct($main);
41 $this->resolveRedirs
= $resolveRedirs;
43 $this->allPages
= array ();
44 $this->goodTitles
= array ();
45 $this->missingTitles
= array ();
46 $this->redirectTitles
= array ();
47 $this->normalizedTitles
= array();
51 * Title objects that were found in the database.
52 * @return array page_id (int) => Title (obj)
54 public function GetGoodTitles() {
55 return $this->goodTitles
;
59 * Title objects that were NOT found in the database.
60 * @return array of Title objects
62 public function GetMissingTitles() {
63 return $this->missingTitles
;
67 * Get a list of redirects when doing redirect resolution
68 * @return array prefixed_title (string) => prefixed_title (string)
70 public function GetRedirectTitles() {
71 return $this->redirectTitles
;
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)
79 public function GetNormalizedTitles() {
80 return $this->normalizedTitles
;
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.
88 * @return LinkBatch of title objects.
90 private function ProcessTitlesStrings($titles) {
92 $linkBatch = new LinkBatch();
94 foreach ($titles as $titleString) {
95 $titleObj = Title
:: newFromText($titleString);
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');
105 $linkBatch->addObj($titleObj);
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();
119 * This method populates internal variables with page information
120 * based on the given array of title strings.
123 * #1 For each title, get data from `page` table
124 * #2 If page was not found in the DB, store it as missing
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
132 public function PopulateTitles($titles) {
138 if ($this->resolveRedirs
) {
139 $pageFlds[] = 'page_is_redirect';
142 // Get validated and normalized title objects
143 $linkBatch = $this->ProcessTitlesStrings($titles);
146 // Repeat until all redirects have been resolved
148 while (false !== ($set = $linkBatch->constructSet('page', $this->db
))) {
150 // Hack: Get the ns:titles stored in array(ns => array(titles)) format
151 $remaining = $linkBatch->data
;
153 $redirectIds = array ();
156 // Get data about $linkBatch from `page` table
158 $res = $this->db
->select('page', $pageFlds, $set, __CLASS__
. '::' . __FUNCTION__
);
159 while ($row = $this->db
->fetchObject($res)) {
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
;
165 if ($this->resolveRedirs
&& $row->page_is_redirect
== '1') {
166 $redirectIds[$row->page_id
] = $title;
168 $this->goodTitles
[$row->page_id
] = $title;
171 $this->db
->freeResult($res);
174 // The remaining titles in $remaining are non-existant pages
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;
183 if (!$this->resolveRedirs ||
empty ($redirectIds))
187 // Resolve redirects by querying the pagelinks table, and repeat the process
190 // Create a new linkBatch object for the next pass
191 $linkBatch = new LinkBatch();
193 // find redirect targets for all redirect pages
194 $res = $this->db
->select('pagelinks', array (
199 'pl_from' => array_keys($redirectIds
200 )), __CLASS__
. '::' . __FUNCTION__
);
202 while ($row = $this->db
->fetchObject($res)) {
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
210 $titleStrFrom = $redirectIds[$row->pl_from
]->getPrefixedText();
211 $titleStrTo = Title
:: makeTitle($row->pl_namespace
, $row->pl_title
)->getPrefixedText();
212 $this->redirectTitles
[$titleStrFrom] = $titleStrTo;
214 unset ($redirectIds[$row->pl_from
]); // remove line when 7304 is fixed
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
);
222 $this->db
->freeResult($res);
226 public function Execute() {
227 $this->DieDebug("Execute() is not supported on this object");