20fae3d310e2b0cd61da47eddafca7993fb2d5e3
[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 ("ApiBase.php");
30 }
31
32 class ApiPageSet {
33
34 private $allPages; // [ns][dbkey] => page_id or 0 when missing
35 private $db, $resolveRedirs;
36 private $goodTitles, $missingTitles, $redirectTitles;
37
38 public function __construct($db, $resolveRedirs) {
39 $this->db = $db;
40 $this->resolveRedirs = $resolveRedirs;
41
42 $this->allPages = array ();
43 $this->goodTitles = array ();
44 $this->missingTitles = array ();
45
46 // only when resolving redirects:
47 if ($resolveRedirs) {
48 $this->redirectTitles = array ();
49 }
50 }
51
52 /**
53 * Title objects that were found in the database.
54 * @return array page_id (int) => Title (obj)
55 */
56 public function GetGoodTitles() {
57 return $this->goodTitles;
58 }
59
60 /**
61 * Title objects that were NOT found in the database.
62 * @return array of Title objects
63 */
64 public function GetMissingTitles() {
65 return $this->missingTitles;
66 }
67
68 /**
69 * Get a list of redirects when doing redirect resolution
70 * @return array prefixed_title (string) => prefixed_title (string)
71 */
72 public function GetRedirectTitles() {
73 return $this->redirectTitles;
74 }
75
76 /**
77 * This method populates internal variables with page information
78 * based on the list of page titles given as a LinkBatch object.
79 *
80 * Steps:
81 * #1 For each title, get data from `page` table
82 * #2 If page was not found in the DB, store it as missing
83 *
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
89 */
90 public function PopulateTitles($linkBatch) {
91 $pageFlds = array (
92 'page_id',
93 'page_namespace',
94 'page_title'
95 );
96 if ($this->resolveRedirs) {
97 $pageFlds[] = 'page_is_redirect';
98 }
99
100 //
101 // Repeat until all redirects have been resolved
102 //
103 while (false !== ($set = $linkBatch->constructSet('page', $this->db))) {
104
105 // Hack: Get the ns:titles stored in array(ns => array(titles)) format
106 $remaining = $linkBatch->data;
107
108 if ($this->resolveRedirs)
109 $redirectIds = array ();
110
111 //
112 // Get data about $linkBatch from `page` table
113 //
114 $res = $this->db->select('page', $pageFlds, $set, __CLASS__ . '::' . __FUNCTION__);
115 while ($row = $this->db->fetchObject($res)) {
116
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;
120
121 if ($this->resolveRedirs && $row->page_is_redirect == '1') {
122 $redirectIds[$row->page_id] = $title;
123 } else {
124 $this->goodTitles[$row->page_id] = $title;
125 }
126 }
127 $this->db->freeResult($res);
128
129 //
130 // The remaining titles in $remaining are non-existant pages
131 //
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;
136 }
137 }
138
139 if (!$this->resolveRedirs || empty ($redirectIds))
140 break;
141
142 //
143 // Resolve redirects by querying the pagelinks table, and repeat the process
144 //
145
146 // Create a new linkBatch object for the next pass
147 $linkBatch = new LinkBatch();
148
149 // find redirect targets for all redirect pages
150 $res = $this->db->select('pagelinks', array (
151 'pl_from',
152 'pl_namespace',
153 'pl_title'
154 ), array (
155 'pl_from' => array_keys($redirectIds
156 )), __CLASS__ . '::' . __FUNCTION__);
157
158 while ($row = $this->db->fetchObject($res)) {
159
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
165
166 $titleStrFrom = $redirectIds[$row->pl_from]->getPrefixedText();
167 $titleStrTo = Title :: makeTitle($row->pl_namespace, $row->pl_title)->getPrefixedText();
168 $this->redirectTitles[$titleStrFrom] = $titleStrTo;
169
170 unset ($redirectIds[$row->pl_from]); // remove line when 7304 is fixed
171
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);
175 }
176 }
177 }
178 $this->db->freeResult($res);
179 }
180 }
181 }
182 ?>