20fae3d310e2b0cd61da47eddafca7993fb2d5e3
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");
34 private $allPages; // [ns][dbkey] => page_id or 0 when missing
35 private $db, $resolveRedirs;
36 private $goodTitles, $missingTitles, $redirectTitles;
38 public function __construct($db, $resolveRedirs) {
40 $this->resolveRedirs
= $resolveRedirs;
42 $this->allPages
= array ();
43 $this->goodTitles
= array ();
44 $this->missingTitles
= array ();
46 // only when resolving redirects:
48 $this->redirectTitles
= array ();
53 * Title objects that were found in the database.
54 * @return array page_id (int) => Title (obj)
56 public function GetGoodTitles() {
57 return $this->goodTitles
;
61 * Title objects that were NOT found in the database.
62 * @return array of Title objects
64 public function GetMissingTitles() {
65 return $this->missingTitles
;
69 * Get a list of redirects when doing redirect resolution
70 * @return array prefixed_title (string) => prefixed_title (string)
72 public function GetRedirectTitles() {
73 return $this->redirectTitles
;
77 * This method populates internal variables with page information
78 * based on the list of page titles given as a LinkBatch object.
81 * #1 For each title, get data from `page` table
82 * #2 If page was not found in the DB, store it as missing
84 * Additionally, when resolving redirects:
85 * #3 If no more redirects left, stop.
86 * #4 For each redirect, get its links from `pagelinks` table.
87 * #5 Substitute the original LinkBatch object with the new list
88 * #6 Repeat from step #1
90 public function PopulateTitles($linkBatch) {
96 if ($this->resolveRedirs
) {
97 $pageFlds[] = 'page_is_redirect';
101 // Repeat until all redirects have been resolved
103 while (false !== ($set = $linkBatch->constructSet('page', $this->db
))) {
105 // Hack: Get the ns:titles stored in array(ns => array(titles)) format
106 $remaining = $linkBatch->data
;
108 if ($this->resolveRedirs
)
109 $redirectIds = array ();
112 // Get data about $linkBatch from `page` table
114 $res = $this->db
->select('page', $pageFlds, $set, __CLASS__
. '::' . __FUNCTION__
);
115 while ($row = $this->db
->fetchObject($res)) {
117 unset ($remaining[$row->page_namespace
][$row->page_title
]);
118 $title = Title
:: makeTitle($row->page_namespace
, $row->page_title
);
119 $this->allPages
[$row->page_namespace
][$row->page_title
] = $row->page_id
;
121 if ($this->resolveRedirs
&& $row->page_is_redirect
== '1') {
122 $redirectIds[$row->page_id
] = $title;
124 $this->goodTitles
[$row->page_id
] = $title;
127 $this->db
->freeResult($res);
130 // The remaining titles in $remaining are non-existant pages
132 foreach ($remaining as $ns => $dbkeys) {
133 foreach ($dbkeys as $dbkey => $nothing) {
134 $this->missingTitles
[] = Title
:: makeTitle($ns, $dbkey);
135 $this->allPages
[$ns][$dbkey] = 0;
139 if (!$this->resolveRedirs ||
empty ($redirectIds))
143 // Resolve redirects by querying the pagelinks table, and repeat the process
146 // Create a new linkBatch object for the next pass
147 $linkBatch = new LinkBatch();
149 // find redirect targets for all redirect pages
150 $res = $this->db
->select('pagelinks', array (
155 'pl_from' => array_keys($redirectIds
156 )), __CLASS__
. '::' . __FUNCTION__
);
158 while ($row = $this->db
->fetchObject($res)) {
160 // Bug 7304 workaround
161 // ( http://bugzilla.wikipedia.org/show_bug.cgi?id=7304 )
162 // A redirect page may have more than one link.
163 // This code will only use the first link returned.
164 if (isset ($redirectIds[$row->pl_from
])) { // remove line when 7304 is fixed
166 $titleStrFrom = $redirectIds[$row->pl_from
]->getPrefixedText();
167 $titleStrTo = Title
:: makeTitle($row->pl_namespace
, $row->pl_title
)->getPrefixedText();
168 $this->redirectTitles
[$titleStrFrom] = $titleStrTo;
170 unset ($redirectIds[$row->pl_from
]); // remove line when 7304 is fixed
172 // Avoid an infinite loop by checking if we have already processed this target
173 if (!isset ($this->allPages
[$row->pl_namespace
][$row->pl_title
])) {
174 $linkBatch->add($row->pl_namespace
, $row->pl_title
);
178 $this->db
->freeResult($res);