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 $mResolveRedirs;
36 private $mGoodTitles, $mMissingTitles, $mRedirectTitles, $mNormalizedTitles;
38 public function __construct($query, $resolveRedirs) {
39 parent
:: __construct($query, __CLASS__
);
40 $this->mResolveRedirs
= $resolveRedirs;
42 $this->mAllPages
= array ();
43 $this->mGoodTitles
= array ();
44 $this->mMissingTitles
= array ();
45 $this->mRedirectTitles
= array ();
46 $this->mNormalizedTitles
= array ();
50 * Title objects that were found in the database.
51 * @return array page_id (int) => Title (obj)
53 public function getGoodTitles() {
54 return $this->mGoodTitles
;
58 * Title objects that were NOT found in the database.
59 * @return array of Title objects
61 public function getMissingTitles() {
62 return $this->mMissingTitles
;
66 * Get a list of redirects when doing redirect resolution
67 * @return array prefixed_title (string) => prefixed_title (string)
69 public function getRedirectTitles() {
70 return $this->mRedirectTitles
;
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)
78 public function getNormalizedTitles() {
79 return $this->mNormalizedTitles
;
83 * Returns the number of unique pages (not revisions) in the set.
85 public function getPageCount() {
86 return count($this->getGoodTitles());
90 * This method populates internal variables with page information
91 * based on the given array of title strings.
94 * #1 For each title, get data from `page` table
95 * #2 If page was not found in the DB, store it as missing
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
103 public function populateTitles($titles) {
110 if ($this->mResolveRedirs
) {
111 $pageFlds[] = 'page_is_redirect';
114 // Get validated and normalized title objects
115 $linkBatch = $this->processTitlesStrings($titles);
117 $db = $this->getDB();
120 // Repeat until all redirects have been resolved
122 while (false !== ($set = $linkBatch->constructSet('page', $db))) {
124 // Hack: get the ns:titles stored in array(ns => array(titles)) format
125 $remaining = $linkBatch->data
;
127 $redirectIds = array ();
130 // Get data about $linkBatch from `page` table
132 $this->profileDBIn();
133 $res = $db->select('page', $pageFlds, $set, __CLASS__
. '::' . __FUNCTION__
);
134 $this->profileDBOut();
135 while ($row = $db->fetchObject($res)) {
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
;
141 if ($this->mResolveRedirs
&& $row->page_is_redirect
== '1') {
142 $redirectIds[$row->page_id
] = $title;
144 $this->mGoodTitles
[$row->page_id
] = $title;
147 $db->freeResult($res);
150 // The remaining titles in $remaining are non-existant pages
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;
159 if (!$this->mResolveRedirs ||
empty ($redirectIds))
163 // Resolve redirects by querying the pagelinks table, and repeat the process
166 // Create a new linkBatch object for the next pass
167 $linkBatch = new LinkBatch();
169 // find redirect targets for all redirect pages
170 $this->profileDBIn();
171 $res = $db->select('pagelinks', array (
176 'pl_from' => array_keys($redirectIds
177 )), __CLASS__
. '::' . __FUNCTION__
);
178 $this->profileDBOut();
180 while ($row = $db->fetchObject($res)) {
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
188 $titleStrFrom = $redirectIds[$row->pl_from
]->getPrefixedText();
189 $titleStrTo = Title
:: makeTitle($row->pl_namespace
, $row->pl_title
)->getPrefixedText();
190 $this->mRedirectTitles
[$titleStrFrom] = $titleStrTo;
192 unset ($redirectIds[$row->pl_from
]); // remove line when 7304 is fixed
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
);
200 $db->freeResult($res);
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.
210 * @return LinkBatch of title objects.
212 private function processTitlesStrings($titles) {
214 $linkBatch = new LinkBatch();
216 foreach ($titles as $titleString) {
217 $titleObj = Title
:: newFromText($titleString);
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');
227 $linkBatch->addObj($titleObj);
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();
240 public function populatePageIDs($pageids) {
241 $this->dieUsage(__FUNCTION__
. " is not implemented", 'notimplemented');
244 public function execute() {
245 $this->dieDebug("execute() is not supported on this object");