Merge "Make it show email as required if you choose to email a random password."
[lhc/web/wiklou.git] / includes / api / ApiQueryAllLinks.php
1 <?php
2 /**
3 *
4 *
5 * Created on July 7, 2007
6 *
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 /**
28 * Query module to enumerate links from all pages together.
29 *
30 * @ingroup API
31 */
32 class ApiQueryAllLinks extends ApiQueryGeneratorBase {
33
34 public function __construct( $query, $moduleName ) {
35 switch ( $moduleName ) {
36 case 'alllinks':
37 $prefix = 'al';
38 $this->table = 'pagelinks';
39 $this->tablePrefix = 'pl_';
40 $this->dfltNamespace = NS_MAIN;
41 $this->indexTag = 'l';
42 $this->description = 'Enumerate all links that point to a given namespace';
43 $this->descriptionWhat = 'link';
44 $this->descriptionTargets = 'linked titles';
45 $this->descriptionLinking = 'linking';
46 break;
47 case 'alltransclusions':
48 $prefix = 'at';
49 $this->table = 'templatelinks';
50 $this->tablePrefix = 'tl_';
51 $this->dfltNamespace = NS_TEMPLATE;
52 $this->indexTag = 't';
53 $this->description = 'List all transclusions (pages embedded using {{x}}), including non-existing';
54 $this->descriptionWhat = 'transclusion';
55 $this->descriptionTargets = 'transcluded titles';
56 $this->descriptionLinking = 'transcluding';
57 break;
58 default:
59 ApiBase::dieDebug( __METHOD__, 'Unknown module name' );
60 }
61
62 parent::__construct( $query, $moduleName, $prefix );
63 }
64
65 public function execute() {
66 $this->run();
67 }
68
69 public function getCacheMode( $params ) {
70 return 'public';
71 }
72
73 public function executeGenerator( $resultPageSet ) {
74 $this->run( $resultPageSet );
75 }
76
77 /**
78 * @param $resultPageSet ApiPageSet
79 * @return void
80 */
81 private function run( $resultPageSet = null ) {
82 $db = $this->getDB();
83 $params = $this->extractRequestParams();
84
85 $pfx = $this->tablePrefix;
86 $prop = array_flip( $params['prop'] );
87 $fld_ids = isset( $prop['ids'] );
88 $fld_title = isset( $prop['title'] );
89
90 if ( $params['unique'] ) {
91 if ( $fld_ids ) {
92 $this->dieUsage(
93 "{$this->getModuleName()} cannot return corresponding page ids in unique {$this->descriptionWhat}s mode",
94 'params' );
95 }
96 $this->addOption( 'DISTINCT' );
97 }
98
99 $this->addTables( $this->table );
100 $this->addWhereFld( $pfx . 'namespace', $params['namespace'] );
101
102 $continue = !is_null( $params['continue'] );
103 if ( $continue ) {
104 $continueArr = explode( '|', $params['continue'] );
105 $op = $params['dir'] == 'descending' ? '<' : '>';
106 if ( $params['unique'] ) {
107 $this->dieContinueUsageIf( count( $continueArr ) != 1 );
108 $continueTitle = $db->addQuotes( $continueArr[0] );
109 $this->addWhere( "{$pfx}title $op= $continueTitle" );
110 } else {
111 $this->dieContinueUsageIf( count( $continueArr ) != 2 );
112 $continueTitle = $db->addQuotes( $continueArr[0] );
113 $continueFrom = intval( $continueArr[1] );
114 $this->addWhere(
115 "{$pfx}title $op $continueTitle OR " .
116 "({$pfx}title = $continueTitle AND " .
117 "{$pfx}from $op= $continueFrom)"
118 );
119 }
120 }
121
122 // 'continue' always overrides 'from'
123 $from = ( $continue || is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
124 $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) );
125 $this->addWhereRange( $pfx . 'title', 'newer', $from, $to );
126
127 if ( isset( $params['prefix'] ) ) {
128 $this->addWhere( $pfx . 'title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
129 }
130
131 $this->addFields( array( 'pl_title' => $pfx . 'title' ) );
132 $this->addFieldsIf( array( 'pl_from' => $pfx . 'from' ), !$params['unique'] );
133
134 $this->addOption( 'USE INDEX', $pfx . 'namespace' );
135 $limit = $params['limit'];
136 $this->addOption( 'LIMIT', $limit + 1 );
137
138 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
139 $orderBy = array();
140 $orderBy[] = $pfx . 'title' . $sort;
141 if ( !$params['unique'] ) {
142 $orderBy[] = $pfx . 'from' . $sort;
143 }
144 $this->addOption( 'ORDER BY', $orderBy );
145
146 $res = $this->select( __METHOD__ );
147
148 $pageids = array();
149 $titles = array();
150 $count = 0;
151 $result = $this->getResult();
152 foreach ( $res as $row ) {
153 if ( ++ $count > $limit ) {
154 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
155 if ( $params['unique'] ) {
156 $this->setContinueEnumParameter( 'continue', $row->pl_title );
157 } else {
158 $this->setContinueEnumParameter( 'continue', $row->pl_title . '|' . $row->pl_from );
159 }
160 break;
161 }
162
163 if ( is_null( $resultPageSet ) ) {
164 $vals = array();
165 if ( $fld_ids ) {
166 $vals['fromid'] = intval( $row->pl_from );
167 }
168 if ( $fld_title ) {
169 $title = Title::makeTitle( $params['namespace'], $row->pl_title );
170 ApiQueryBase::addTitleInfo( $vals, $title );
171 }
172 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
173 if ( !$fit ) {
174 if ( $params['unique'] ) {
175 $this->setContinueEnumParameter( 'continue', $row->pl_title );
176 } else {
177 $this->setContinueEnumParameter( 'continue', $row->pl_title . '|' . $row->pl_from );
178 }
179 break;
180 }
181 } elseif ( $params['unique'] ) {
182 $titles[] = Title::makeTitle( $params['namespace'], $row->pl_title );
183 } else {
184 $pageids[] = $row->pl_from;
185 }
186 }
187
188 if ( is_null( $resultPageSet ) ) {
189 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), $this->indexTag );
190 } elseif ( $params['unique'] ) {
191 $resultPageSet->populateFromTitles( $titles );
192 } else {
193 $resultPageSet->populateFromPageIDs( $pageids );
194 }
195 }
196
197 public function getAllowedParams() {
198 return array(
199 'continue' => null,
200 'from' => null,
201 'to' => null,
202 'prefix' => null,
203 'unique' => false,
204 'prop' => array(
205 ApiBase::PARAM_ISMULTI => true,
206 ApiBase::PARAM_DFLT => 'title',
207 ApiBase::PARAM_TYPE => array(
208 'ids',
209 'title'
210 )
211 ),
212 'namespace' => array(
213 ApiBase::PARAM_DFLT => $this->dfltNamespace,
214 ApiBase::PARAM_TYPE => 'namespace'
215 ),
216 'limit' => array(
217 ApiBase::PARAM_DFLT => 10,
218 ApiBase::PARAM_TYPE => 'limit',
219 ApiBase::PARAM_MIN => 1,
220 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
221 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
222 ),
223 'dir' => array(
224 ApiBase::PARAM_DFLT => 'ascending',
225 ApiBase::PARAM_TYPE => array(
226 'ascending',
227 'descending'
228 )
229 ),
230 );
231 }
232
233 public function getParamDescription() {
234 $p = $this->getModulePrefix();
235 $what = $this->descriptionWhat;
236 $targets = $this->descriptionTargets;
237 $linking = $this->descriptionLinking;
238 return array(
239 'from' => "The title of the $what to start enumerating from",
240 'to' => "The title of the $what to stop enumerating at",
241 'prefix' => "Search for all $targets that begin with this value",
242 'unique' => array(
243 "Only show distinct $targets. Cannot be used with {$p}prop=ids.",
244 'When used as a generator, yields target pages instead of source pages.',
245 ),
246 'prop' => array(
247 'What pieces of information to include',
248 " ids - Adds the pageid of the $linking page (Cannot be used with {$p}unique)",
249 " title - Adds the title of the $what",
250 ),
251 'namespace' => 'The namespace to enumerate',
252 'limit' => 'How many total items to return',
253 'continue' => 'When more results are available, use this to continue',
254 'dir' => 'The direction in which to list',
255 );
256 }
257
258 public function getResultProperties() {
259 return array(
260 'ids' => array(
261 'fromid' => 'integer'
262 ),
263 'title' => array(
264 'ns' => 'namespace',
265 'title' => 'string'
266 )
267 );
268 }
269
270 public function getDescription() {
271 return $this->description;
272 }
273
274 public function getPossibleErrors() {
275 $m = $this->getModuleName();
276 $what = $this->descriptionWhat;
277 return array_merge( parent::getPossibleErrors(), array(
278 array( 'code' => 'params', 'info' => "{$m} cannot return corresponding page ids in unique {$what}s mode" ),
279 ) );
280 }
281
282 public function getExamples() {
283 $p = $this->getModulePrefix();
284 $name = $this->getModuleName();
285 $what = $this->descriptionWhat;
286 $targets = $this->descriptionTargets;
287 return array(
288 "api.php?action=query&list={$name}&{$p}from=B&{$p}prop=ids|title"
289 => "List $targets with page ids they are from, including missing ones. Start at B",
290 "api.php?action=query&list={$name}&{$p}unique=&{$p}from=B"
291 => "List unique $targets",
292 "api.php?action=query&generator={$name}&g{$p}unique=&g{$p}from=B"
293 => "Gets all $targets, marking the missing ones",
294 "api.php?action=query&generator={$name}&g{$p}from=B"
295 => "Gets pages containing the {$what}s",
296 );
297 }
298
299 public function getHelpUrls() {
300 $name = ucfirst( $this->getModuleName() );
301 return "https://www.mediawiki.org/wiki/API:{$name}";
302 }
303 }