|
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
|
|
|
private $table, $tablePrefix, $indexTag; |
|
|
|
|
|
|
35
|
|
|
private $fieldTitle = 'title'; |
|
36
|
|
|
private $dfltNamespace = NS_MAIN; |
|
37
|
|
|
private $hasNamespace = true; |
|
38
|
|
|
private $useIndex = null; |
|
39
|
|
|
private $props = []; |
|
40
|
|
|
|
|
41
|
|
|
public function __construct( ApiQuery $query, $moduleName ) { |
|
42
|
|
|
switch ( $moduleName ) { |
|
43
|
|
|
case 'alllinks': |
|
44
|
|
|
$prefix = 'al'; |
|
45
|
|
|
$this->table = 'pagelinks'; |
|
46
|
|
|
$this->tablePrefix = 'pl_'; |
|
47
|
|
|
$this->useIndex = 'pl_namespace'; |
|
48
|
|
|
$this->indexTag = 'l'; |
|
49
|
|
|
break; |
|
50
|
|
View Code Duplication |
case 'alltransclusions': |
|
51
|
|
|
$prefix = 'at'; |
|
52
|
|
|
$this->table = 'templatelinks'; |
|
53
|
|
|
$this->tablePrefix = 'tl_'; |
|
54
|
|
|
$this->dfltNamespace = NS_TEMPLATE; |
|
55
|
|
|
$this->useIndex = 'tl_namespace'; |
|
56
|
|
|
$this->indexTag = 't'; |
|
57
|
|
|
break; |
|
58
|
|
View Code Duplication |
case 'allfileusages': |
|
59
|
|
|
$prefix = 'af'; |
|
60
|
|
|
$this->table = 'imagelinks'; |
|
61
|
|
|
$this->tablePrefix = 'il_'; |
|
62
|
|
|
$this->fieldTitle = 'to'; |
|
63
|
|
|
$this->dfltNamespace = NS_FILE; |
|
64
|
|
|
$this->hasNamespace = false; |
|
65
|
|
|
$this->indexTag = 'f'; |
|
66
|
|
|
break; |
|
67
|
|
|
case 'allredirects': |
|
68
|
|
|
$prefix = 'ar'; |
|
69
|
|
|
$this->table = 'redirect'; |
|
70
|
|
|
$this->tablePrefix = 'rd_'; |
|
71
|
|
|
$this->indexTag = 'r'; |
|
72
|
|
|
$this->props = [ |
|
73
|
|
|
'fragment' => 'rd_fragment', |
|
74
|
|
|
'interwiki' => 'rd_interwiki', |
|
75
|
|
|
]; |
|
76
|
|
|
break; |
|
77
|
|
|
default: |
|
78
|
|
|
ApiBase::dieDebug( __METHOD__, 'Unknown module name' ); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
parent::__construct( $query, $moduleName, $prefix ); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function execute() { |
|
85
|
|
|
$this->run(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function getCacheMode( $params ) { |
|
89
|
|
|
return 'public'; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function executeGenerator( $resultPageSet ) { |
|
93
|
|
|
$this->run( $resultPageSet ); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param ApiPageSet $resultPageSet |
|
98
|
|
|
* @return void |
|
99
|
|
|
*/ |
|
100
|
|
|
private function run( $resultPageSet = null ) { |
|
101
|
|
|
$db = $this->getDB(); |
|
102
|
|
|
$params = $this->extractRequestParams(); |
|
103
|
|
|
|
|
104
|
|
|
$pfx = $this->tablePrefix; |
|
105
|
|
|
$fieldTitle = $this->fieldTitle; |
|
106
|
|
|
$prop = array_flip( $params['prop'] ); |
|
107
|
|
|
$fld_ids = isset( $prop['ids'] ); |
|
108
|
|
|
$fld_title = isset( $prop['title'] ); |
|
109
|
|
|
if ( $this->hasNamespace ) { |
|
110
|
|
|
$namespace = $params['namespace']; |
|
111
|
|
|
} else { |
|
112
|
|
|
$namespace = $this->dfltNamespace; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if ( $params['unique'] ) { |
|
116
|
|
|
$matches = array_intersect_key( $prop, $this->props + [ 'ids' => 1 ] ); |
|
117
|
|
|
if ( $matches ) { |
|
118
|
|
|
$p = $this->getModulePrefix(); |
|
119
|
|
|
$this->dieUsage( |
|
120
|
|
|
"Cannot use {$p}prop=" . implode( '|', array_keys( $matches ) ) . " with {$p}unique", |
|
121
|
|
|
'params' |
|
122
|
|
|
); |
|
123
|
|
|
} |
|
124
|
|
|
$this->addOption( 'DISTINCT' ); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
$this->addTables( $this->table ); |
|
128
|
|
|
if ( $this->hasNamespace ) { |
|
129
|
|
|
$this->addWhereFld( $pfx . 'namespace', $namespace ); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
$continue = !is_null( $params['continue'] ); |
|
133
|
|
|
if ( $continue ) { |
|
134
|
|
|
$continueArr = explode( '|', $params['continue'] ); |
|
135
|
|
|
$op = $params['dir'] == 'descending' ? '<' : '>'; |
|
136
|
|
|
if ( $params['unique'] ) { |
|
137
|
|
|
$this->dieContinueUsageIf( count( $continueArr ) != 1 ); |
|
138
|
|
|
$continueTitle = $db->addQuotes( $continueArr[0] ); |
|
139
|
|
|
$this->addWhere( "{$pfx}{$fieldTitle} $op= $continueTitle" ); |
|
140
|
|
|
} else { |
|
141
|
|
|
$this->dieContinueUsageIf( count( $continueArr ) != 2 ); |
|
142
|
|
|
$continueTitle = $db->addQuotes( $continueArr[0] ); |
|
143
|
|
|
$continueFrom = intval( $continueArr[1] ); |
|
144
|
|
|
$this->addWhere( |
|
145
|
|
|
"{$pfx}{$fieldTitle} $op $continueTitle OR " . |
|
146
|
|
|
"({$pfx}{$fieldTitle} = $continueTitle AND " . |
|
147
|
|
|
"{$pfx}from $op= $continueFrom)" |
|
148
|
|
|
); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
// 'continue' always overrides 'from' |
|
153
|
|
|
$from = ( $continue || $params['from'] === null ? null : |
|
154
|
|
|
$this->titlePartToKey( $params['from'], $namespace ) ); |
|
155
|
|
|
$to = ( $params['to'] === null ? null : |
|
156
|
|
|
$this->titlePartToKey( $params['to'], $namespace ) ); |
|
157
|
|
|
$this->addWhereRange( $pfx . $fieldTitle, 'newer', $from, $to ); |
|
158
|
|
|
|
|
159
|
|
|
if ( isset( $params['prefix'] ) ) { |
|
160
|
|
|
$this->addWhere( $pfx . $fieldTitle . $db->buildLike( $this->titlePartToKey( |
|
161
|
|
|
$params['prefix'], $namespace ), $db->anyString() ) ); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
$this->addFields( [ 'pl_title' => $pfx . $fieldTitle ] ); |
|
165
|
|
|
$this->addFieldsIf( [ 'pl_from' => $pfx . 'from' ], !$params['unique'] ); |
|
166
|
|
|
foreach ( $this->props as $name => $field ) { |
|
167
|
|
|
$this->addFieldsIf( $field, isset( $prop[$name] ) ); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
if ( $this->useIndex ) { |
|
171
|
|
|
$this->addOption( 'USE INDEX', $this->useIndex ); |
|
172
|
|
|
} |
|
173
|
|
|
$limit = $params['limit']; |
|
174
|
|
|
$this->addOption( 'LIMIT', $limit + 1 ); |
|
175
|
|
|
|
|
176
|
|
|
$sort = ( $params['dir'] == 'descending' ? ' DESC' : '' ); |
|
177
|
|
|
$orderBy = []; |
|
178
|
|
|
$orderBy[] = $pfx . $fieldTitle . $sort; |
|
179
|
|
|
if ( !$params['unique'] ) { |
|
180
|
|
|
$orderBy[] = $pfx . 'from' . $sort; |
|
181
|
|
|
} |
|
182
|
|
|
$this->addOption( 'ORDER BY', $orderBy ); |
|
183
|
|
|
|
|
184
|
|
|
$res = $this->select( __METHOD__ ); |
|
185
|
|
|
|
|
186
|
|
|
$pageids = []; |
|
187
|
|
|
$titles = []; |
|
188
|
|
|
$count = 0; |
|
189
|
|
|
$result = $this->getResult(); |
|
190
|
|
|
foreach ( $res as $row ) { |
|
191
|
|
View Code Duplication |
if ( ++$count > $limit ) { |
|
192
|
|
|
// We've reached the one extra which shows that there are |
|
193
|
|
|
// additional pages to be had. Stop here... |
|
194
|
|
|
if ( $params['unique'] ) { |
|
195
|
|
|
$this->setContinueEnumParameter( 'continue', $row->pl_title ); |
|
196
|
|
|
} else { |
|
197
|
|
|
$this->setContinueEnumParameter( 'continue', $row->pl_title . '|' . $row->pl_from ); |
|
198
|
|
|
} |
|
199
|
|
|
break; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
if ( is_null( $resultPageSet ) ) { |
|
203
|
|
|
$vals = [ |
|
204
|
|
|
ApiResult::META_TYPE => 'assoc', |
|
205
|
|
|
]; |
|
206
|
|
|
if ( $fld_ids ) { |
|
207
|
|
|
$vals['fromid'] = intval( $row->pl_from ); |
|
208
|
|
|
} |
|
209
|
|
|
if ( $fld_title ) { |
|
210
|
|
|
$title = Title::makeTitle( $namespace, $row->pl_title ); |
|
211
|
|
|
ApiQueryBase::addTitleInfo( $vals, $title ); |
|
212
|
|
|
} |
|
213
|
|
|
foreach ( $this->props as $name => $field ) { |
|
214
|
|
|
if ( isset( $prop[$name] ) && $row->$field !== null && $row->$field !== '' ) { |
|
215
|
|
|
$vals[$name] = $row->$field; |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
$fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals ); |
|
219
|
|
View Code Duplication |
if ( !$fit ) { |
|
220
|
|
|
if ( $params['unique'] ) { |
|
221
|
|
|
$this->setContinueEnumParameter( 'continue', $row->pl_title ); |
|
222
|
|
|
} else { |
|
223
|
|
|
$this->setContinueEnumParameter( 'continue', $row->pl_title . '|' . $row->pl_from ); |
|
224
|
|
|
} |
|
225
|
|
|
break; |
|
226
|
|
|
} |
|
227
|
|
|
} elseif ( $params['unique'] ) { |
|
228
|
|
|
$titles[] = Title::makeTitle( $namespace, $row->pl_title ); |
|
229
|
|
|
} else { |
|
230
|
|
|
$pageids[] = $row->pl_from; |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
View Code Duplication |
if ( is_null( $resultPageSet ) ) { |
|
235
|
|
|
$result->addIndexedTagName( [ 'query', $this->getModuleName() ], $this->indexTag ); |
|
236
|
|
|
} elseif ( $params['unique'] ) { |
|
237
|
|
|
$resultPageSet->populateFromTitles( $titles ); |
|
238
|
|
|
} else { |
|
239
|
|
|
$resultPageSet->populateFromPageIDs( $pageids ); |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
public function getAllowedParams() { |
|
244
|
|
|
$allowedParams = [ |
|
245
|
|
|
'continue' => [ |
|
246
|
|
|
ApiBase::PARAM_HELP_MSG => 'api-help-param-continue', |
|
247
|
|
|
], |
|
248
|
|
|
'from' => null, |
|
249
|
|
|
'to' => null, |
|
250
|
|
|
'prefix' => null, |
|
251
|
|
|
'unique' => false, |
|
252
|
|
|
'prop' => [ |
|
253
|
|
|
ApiBase::PARAM_ISMULTI => true, |
|
254
|
|
|
ApiBase::PARAM_DFLT => 'title', |
|
255
|
|
|
ApiBase::PARAM_TYPE => array_merge( |
|
256
|
|
|
[ 'ids', 'title' ], array_keys( $this->props ) |
|
257
|
|
|
), |
|
258
|
|
|
ApiBase::PARAM_HELP_MSG_PER_VALUE => [], |
|
259
|
|
|
], |
|
260
|
|
|
'namespace' => [ |
|
261
|
|
|
ApiBase::PARAM_DFLT => $this->dfltNamespace, |
|
262
|
|
|
ApiBase::PARAM_TYPE => 'namespace' |
|
263
|
|
|
], |
|
264
|
|
|
'limit' => [ |
|
265
|
|
|
ApiBase::PARAM_DFLT => 10, |
|
266
|
|
|
ApiBase::PARAM_TYPE => 'limit', |
|
267
|
|
|
ApiBase::PARAM_MIN => 1, |
|
268
|
|
|
ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1, |
|
269
|
|
|
ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2 |
|
270
|
|
|
], |
|
271
|
|
|
'dir' => [ |
|
272
|
|
|
ApiBase::PARAM_DFLT => 'ascending', |
|
273
|
|
|
ApiBase::PARAM_TYPE => [ |
|
274
|
|
|
'ascending', |
|
275
|
|
|
'descending' |
|
276
|
|
|
] |
|
277
|
|
|
], |
|
278
|
|
|
]; |
|
279
|
|
|
if ( !$this->hasNamespace ) { |
|
280
|
|
|
unset( $allowedParams['namespace'] ); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
return $allowedParams; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
protected function getExamplesMessages() { |
|
287
|
|
|
$p = $this->getModulePrefix(); |
|
288
|
|
|
$name = $this->getModuleName(); |
|
289
|
|
|
$path = $this->getModulePath(); |
|
290
|
|
|
|
|
291
|
|
|
return [ |
|
292
|
|
|
"action=query&list={$name}&{$p}from=B&{$p}prop=ids|title" |
|
293
|
|
|
=> "apihelp-$path-example-B", |
|
294
|
|
|
"action=query&list={$name}&{$p}unique=&{$p}from=B" |
|
295
|
|
|
=> "apihelp-$path-example-unique", |
|
296
|
|
|
"action=query&generator={$name}&g{$p}unique=&g{$p}from=B" |
|
297
|
|
|
=> "apihelp-$path-example-unique-generator", |
|
298
|
|
|
"action=query&generator={$name}&g{$p}from=B" |
|
299
|
|
|
=> "apihelp-$path-example-generator", |
|
300
|
|
|
]; |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
public function getHelpUrls() { |
|
304
|
|
|
$name = ucfirst( $this->getModuleName() ); |
|
305
|
|
|
|
|
306
|
|
|
return "https://www.mediawiki.org/wiki/API:{$name}"; |
|
307
|
|
|
} |
|
308
|
|
|
} |
|
309
|
|
|
|
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.