Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / api / ApiQueryAllLinks.php
blob3b24e37409a58a0a6ebe76213e310b7dcf6ac2ef
1 <?php
2 /**
5 * Created on July 7, 2007
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
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.
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.
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
24 * @file
27 /**
28 * Query module to enumerate links from all pages together.
30 * @ingroup API
32 class ApiQueryAllLinks extends ApiQueryGeneratorBase {
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 = [];
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 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 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',
76 break;
77 default:
78 ApiBase::dieDebug( __METHOD__, 'Unknown module name' );
81 parent::__construct( $query, $moduleName, $prefix );
84 public function execute() {
85 $this->run();
88 public function getCacheMode( $params ) {
89 return 'public';
92 public function executeGenerator( $resultPageSet ) {
93 $this->run( $resultPageSet );
96 /**
97 * @param ApiPageSet $resultPageSet
98 * @return void
100 private function run( $resultPageSet = null ) {
101 $db = $this->getDB();
102 $params = $this->extractRequestParams();
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;
115 if ( $params['unique'] ) {
116 $matches = array_intersect_key( $prop, $this->props + [ 'ids' => 1 ] );
117 if ( $matches ) {
118 $p = $this->getModulePrefix();
119 $this->dieWithError(
121 'apierror-invalidparammix-cannotusewith',
122 "{$p}prop=" . implode( '|', array_keys( $matches ) ),
123 "{$p}unique"
125 'invalidparammix'
128 $this->addOption( 'DISTINCT' );
131 $this->addTables( $this->table );
132 if ( $this->hasNamespace ) {
133 $this->addWhereFld( $pfx . 'namespace', $namespace );
136 $continue = !is_null( $params['continue'] );
137 if ( $continue ) {
138 $continueArr = explode( '|', $params['continue'] );
139 $op = $params['dir'] == 'descending' ? '<' : '>';
140 if ( $params['unique'] ) {
141 $this->dieContinueUsageIf( count( $continueArr ) != 1 );
142 $continueTitle = $db->addQuotes( $continueArr[0] );
143 $this->addWhere( "{$pfx}{$fieldTitle} $op= $continueTitle" );
144 } else {
145 $this->dieContinueUsageIf( count( $continueArr ) != 2 );
146 $continueTitle = $db->addQuotes( $continueArr[0] );
147 $continueFrom = intval( $continueArr[1] );
148 $this->addWhere(
149 "{$pfx}{$fieldTitle} $op $continueTitle OR " .
150 "({$pfx}{$fieldTitle} = $continueTitle AND " .
151 "{$pfx}from $op= $continueFrom)"
156 // 'continue' always overrides 'from'
157 $from = ( $continue || $params['from'] === null ? null :
158 $this->titlePartToKey( $params['from'], $namespace ) );
159 $to = ( $params['to'] === null ? null :
160 $this->titlePartToKey( $params['to'], $namespace ) );
161 $this->addWhereRange( $pfx . $fieldTitle, 'newer', $from, $to );
163 if ( isset( $params['prefix'] ) ) {
164 $this->addWhere( $pfx . $fieldTitle . $db->buildLike( $this->titlePartToKey(
165 $params['prefix'], $namespace ), $db->anyString() ) );
168 $this->addFields( [ 'pl_title' => $pfx . $fieldTitle ] );
169 $this->addFieldsIf( [ 'pl_from' => $pfx . 'from' ], !$params['unique'] );
170 foreach ( $this->props as $name => $field ) {
171 $this->addFieldsIf( $field, isset( $prop[$name] ) );
174 if ( $this->useIndex ) {
175 $this->addOption( 'USE INDEX', $this->useIndex );
177 $limit = $params['limit'];
178 $this->addOption( 'LIMIT', $limit + 1 );
180 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
181 $orderBy = [];
182 $orderBy[] = $pfx . $fieldTitle . $sort;
183 if ( !$params['unique'] ) {
184 $orderBy[] = $pfx . 'from' . $sort;
186 $this->addOption( 'ORDER BY', $orderBy );
188 $res = $this->select( __METHOD__ );
190 $pageids = [];
191 $titles = [];
192 $count = 0;
193 $result = $this->getResult();
194 foreach ( $res as $row ) {
195 if ( ++$count > $limit ) {
196 // We've reached the one extra which shows that there are
197 // additional pages to be had. Stop here...
198 if ( $params['unique'] ) {
199 $this->setContinueEnumParameter( 'continue', $row->pl_title );
200 } else {
201 $this->setContinueEnumParameter( 'continue', $row->pl_title . '|' . $row->pl_from );
203 break;
206 if ( is_null( $resultPageSet ) ) {
207 $vals = [
208 ApiResult::META_TYPE => 'assoc',
210 if ( $fld_ids ) {
211 $vals['fromid'] = intval( $row->pl_from );
213 if ( $fld_title ) {
214 $title = Title::makeTitle( $namespace, $row->pl_title );
215 ApiQueryBase::addTitleInfo( $vals, $title );
217 foreach ( $this->props as $name => $field ) {
218 if ( isset( $prop[$name] ) && $row->$field !== null && $row->$field !== '' ) {
219 $vals[$name] = $row->$field;
222 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
223 if ( !$fit ) {
224 if ( $params['unique'] ) {
225 $this->setContinueEnumParameter( 'continue', $row->pl_title );
226 } else {
227 $this->setContinueEnumParameter( 'continue', $row->pl_title . '|' . $row->pl_from );
229 break;
231 } elseif ( $params['unique'] ) {
232 $titles[] = Title::makeTitle( $namespace, $row->pl_title );
233 } else {
234 $pageids[] = $row->pl_from;
238 if ( is_null( $resultPageSet ) ) {
239 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], $this->indexTag );
240 } elseif ( $params['unique'] ) {
241 $resultPageSet->populateFromTitles( $titles );
242 } else {
243 $resultPageSet->populateFromPageIDs( $pageids );
247 public function getAllowedParams() {
248 $allowedParams = [
249 'continue' => [
250 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
252 'from' => null,
253 'to' => null,
254 'prefix' => null,
255 'unique' => false,
256 'prop' => [
257 ApiBase::PARAM_ISMULTI => true,
258 ApiBase::PARAM_DFLT => 'title',
259 ApiBase::PARAM_TYPE => array_merge(
260 [ 'ids', 'title' ], array_keys( $this->props )
262 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
264 'namespace' => [
265 ApiBase::PARAM_DFLT => $this->dfltNamespace,
266 ApiBase::PARAM_TYPE => 'namespace',
267 ApiBase::PARAM_EXTRA_NAMESPACES => [ NS_MEDIA, NS_SPECIAL ],
269 'limit' => [
270 ApiBase::PARAM_DFLT => 10,
271 ApiBase::PARAM_TYPE => 'limit',
272 ApiBase::PARAM_MIN => 1,
273 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
274 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
276 'dir' => [
277 ApiBase::PARAM_DFLT => 'ascending',
278 ApiBase::PARAM_TYPE => [
279 'ascending',
280 'descending'
284 if ( !$this->hasNamespace ) {
285 unset( $allowedParams['namespace'] );
288 return $allowedParams;
291 protected function getExamplesMessages() {
292 $p = $this->getModulePrefix();
293 $name = $this->getModuleName();
294 $path = $this->getModulePath();
296 return [
297 "action=query&list={$name}&{$p}from=B&{$p}prop=ids|title"
298 => "apihelp-$path-example-B",
299 "action=query&list={$name}&{$p}unique=&{$p}from=B"
300 => "apihelp-$path-example-unique",
301 "action=query&generator={$name}&g{$p}unique=&g{$p}from=B"
302 => "apihelp-$path-example-unique-generator",
303 "action=query&generator={$name}&g{$p}from=B"
304 => "apihelp-$path-example-generator",
308 public function getHelpUrls() {
309 $name = ucfirst( $this->getModuleName() );
311 return "https://www.mediawiki.org/wiki/API:{$name}";