Don't use public-audience-only function
[lhc/web/wiklou.git] / includes / api / ApiQueryAllLinks.php
1 <?php
2
3 /*
4 * Created on July 7, 2007
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiQueryBase.php');
29 }
30
31 /**
32 * Query module to enumerate links from all pages together.
33 *
34 * @ingroup API
35 */
36 class ApiQueryAllLinks extends ApiQueryGeneratorBase {
37
38 public function __construct($query, $moduleName) {
39 parent :: __construct($query, $moduleName, 'al');
40 }
41
42 public function execute() {
43 $this->run();
44 }
45
46 public function executeGenerator($resultPageSet) {
47 $this->run($resultPageSet);
48 }
49
50 private function run($resultPageSet = null) {
51
52 $db = $this->getDB();
53 $params = $this->extractRequestParams();
54
55 $prop = array_flip($params['prop']);
56 $fld_ids = isset($prop['ids']);
57 $fld_title = isset($prop['title']);
58
59 if ($params['unique']) {
60 if (!is_null($resultPageSet))
61 $this->dieUsage($this->getModuleName() . ' cannot be used as a generator in unique links mode', 'params');
62 if ($fld_ids)
63 $this->dieUsage($this->getModuleName() . ' cannot return corresponding page ids in unique links mode', 'params');
64 $this->addOption('DISTINCT');
65 }
66
67 $this->addTables('pagelinks');
68 $this->addWhereFld('pl_namespace', $params['namespace']);
69
70 if (!is_null($params['from']) && !is_null($params['continue']))
71 $this->dieUsage('alcontinue and alfrom cannot be used together', 'params');
72 if (!is_null($params['continue']))
73 {
74 $arr = explode('|', $params['continue']);
75 if(count($arr) != 2)
76 $this->dieUsage("Invalid continue parameter", 'badcontinue');
77 $params['from'] = $arr[0]; // Handled later
78 $id = intval($arr[1]);
79 $this->addWhere("pl_from >= $id");
80 }
81
82 if (!is_null($params['from']))
83 $this->addWhere('pl_title>=' . $db->addQuotes($this->titlePartToKey($params['from'])));
84 if (isset ($params['prefix']))
85 $this->addWhere("pl_title LIKE '" . $db->escapeLike($this->titlePartToKey($params['prefix'])) . "%'");
86
87 $this->addFields(array (
88 'pl_namespace',
89 'pl_title',
90 'pl_from'
91 ));
92
93 $this->addOption('USE INDEX', 'pl_namespace');
94 $limit = $params['limit'];
95 $this->addOption('LIMIT', $limit+1);
96 # Only order by pl_namespace if it isn't constant in the WHERE clause
97 if(count($params['namespace']) != 1)
98 $this->addOption('ORDER BY', 'pl_namespace, pl_title');
99 else
100 $this->addOption('ORDER BY', 'pl_title');
101
102 $res = $this->select(__METHOD__);
103
104 $data = array ();
105 $count = 0;
106 while ($row = $db->fetchObject($res)) {
107 if (++ $count > $limit) {
108 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
109 // TODO: Security issue - if the user has no right to view next title, it will still be shown
110 $this->setContinueEnumParameter('continue', $this->keyToTitle($row->pl_title) . "|" . $row->pl_from);
111 break;
112 }
113
114 if (is_null($resultPageSet)) {
115 $vals = array();
116 if ($fld_ids)
117 $vals['fromid'] = intval($row->pl_from);
118 if ($fld_title) {
119 $title = Title :: makeTitle($row->pl_namespace, $row->pl_title);
120 $vals['ns'] = intval($title->getNamespace());
121 $vals['title'] = $title->getPrefixedText();
122 }
123 $data[] = $vals;
124 } else {
125 $pageids[] = $row->pl_from;
126 }
127 }
128 $db->freeResult($res);
129
130 if (is_null($resultPageSet)) {
131 $result = $this->getResult();
132 $result->setIndexedTagName($data, 'l');
133 $result->addValue('query', $this->getModuleName(), $data);
134 } else {
135 $resultPageSet->populateFromPageIDs($pageids);
136 }
137 }
138
139 public function getAllowedParams() {
140 return array (
141 'continue' => null,
142 'from' => null,
143 'prefix' => null,
144 'unique' => false,
145 'prop' => array (
146 ApiBase :: PARAM_ISMULTI => true,
147 ApiBase :: PARAM_DFLT => 'title',
148 ApiBase :: PARAM_TYPE => array (
149 'ids',
150 'title'
151 )
152 ),
153 'namespace' => array (
154 ApiBase :: PARAM_DFLT => 0,
155 ApiBase :: PARAM_TYPE => 'namespace'
156 ),
157 'limit' => array (
158 ApiBase :: PARAM_DFLT => 10,
159 ApiBase :: PARAM_TYPE => 'limit',
160 ApiBase :: PARAM_MIN => 1,
161 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
162 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
163 )
164 );
165 }
166
167 public function getParamDescription() {
168 return array (
169 'from' => 'The page title to start enumerating from.',
170 'prefix' => 'Search for all page titles that begin with this value.',
171 'unique' => 'Only show unique links. Cannot be used with generator or prop=ids',
172 'prop' => 'What pieces of information to include',
173 'namespace' => 'The namespace to enumerate.',
174 'limit' => 'How many total links to return.',
175 'continue' => 'When more results are available, use this to continue.',
176 );
177 }
178
179 public function getDescription() {
180 return 'Enumerate all links that point to a given namespace';
181 }
182
183 protected function getExamples() {
184 return array (
185 'api.php?action=query&list=alllinks&alunique&alfrom=B',
186 );
187 }
188
189 public function getVersion() {
190 return __CLASS__ . ': $Id$';
191 }
192 }