Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / api / ApiQueryAllLinks.php
bloba70d019c75bf4cdb18f695c499ab01c336d69b4e
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 = array();
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 = array(
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 + array( 'ids' => 1 ) );
117 if ( $matches ) {
118 $p = $this->getModulePrefix();
119 $this->dieUsage(
120 "Cannot use {$p}prop=" . join( '|', array_keys( $matches ) ) . " with {$p}unique",
121 'params'
124 $this->addOption( 'DISTINCT' );
127 $this->addTables( $this->table );
128 if ( $this->hasNamespace ) {
129 $this->addWhereFld( $pfx . 'namespace', $namespace );
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)"
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 );
159 if ( isset( $params['prefix'] ) ) {
160 $this->addWhere( $pfx . $fieldTitle . $db->buildLike( $this->titlePartToKey(
161 $params['prefix'], $namespace ), $db->anyString() ) );
164 $this->addFields( array( 'pl_title' => $pfx . $fieldTitle ) );
165 $this->addFieldsIf( array( 'pl_from' => $pfx . 'from' ), !$params['unique'] );
166 foreach ( $this->props as $name => $field ) {
167 $this->addFieldsIf( $field, isset( $prop[$name] ) );
170 if ( $this->useIndex ) {
171 $this->addOption( 'USE INDEX', $this->useIndex );
173 $limit = $params['limit'];
174 $this->addOption( 'LIMIT', $limit + 1 );
176 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
177 $orderBy = array();
178 $orderBy[] = $pfx . $fieldTitle . $sort;
179 if ( !$params['unique'] ) {
180 $orderBy[] = $pfx . 'from' . $sort;
182 $this->addOption( 'ORDER BY', $orderBy );
184 $res = $this->select( __METHOD__ );
186 $pageids = array();
187 $titles = array();
188 $count = 0;
189 $result = $this->getResult();
190 foreach ( $res as $row ) {
191 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 );
199 break;
202 if ( is_null( $resultPageSet ) ) {
203 $vals = array();
204 if ( $fld_ids ) {
205 $vals['fromid'] = intval( $row->pl_from );
207 if ( $fld_title ) {
208 $title = Title::makeTitle( $namespace, $row->pl_title );
209 ApiQueryBase::addTitleInfo( $vals, $title );
211 foreach ( $this->props as $name => $field ) {
212 if ( isset( $prop[$name] ) && $row->$field !== null && $row->$field !== '' ) {
213 $vals[$name] = $row->$field;
216 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
217 if ( !$fit ) {
218 if ( $params['unique'] ) {
219 $this->setContinueEnumParameter( 'continue', $row->pl_title );
220 } else {
221 $this->setContinueEnumParameter( 'continue', $row->pl_title . '|' . $row->pl_from );
223 break;
225 } elseif ( $params['unique'] ) {
226 $titles[] = Title::makeTitle( $namespace, $row->pl_title );
227 } else {
228 $pageids[] = $row->pl_from;
232 if ( is_null( $resultPageSet ) ) {
233 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), $this->indexTag );
234 } elseif ( $params['unique'] ) {
235 $resultPageSet->populateFromTitles( $titles );
236 } else {
237 $resultPageSet->populateFromPageIDs( $pageids );
241 public function getAllowedParams() {
242 $allowedParams = array(
243 'continue' => array(
244 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
246 'from' => null,
247 'to' => null,
248 'prefix' => null,
249 'unique' => false,
250 'prop' => array(
251 ApiBase::PARAM_ISMULTI => true,
252 ApiBase::PARAM_DFLT => 'title',
253 ApiBase::PARAM_TYPE => array_merge(
254 array( 'ids', 'title' ), array_keys( $this->props )
257 'namespace' => array(
258 ApiBase::PARAM_DFLT => $this->dfltNamespace,
259 ApiBase::PARAM_TYPE => 'namespace'
261 'limit' => array(
262 ApiBase::PARAM_DFLT => 10,
263 ApiBase::PARAM_TYPE => 'limit',
264 ApiBase::PARAM_MIN => 1,
265 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
266 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
268 'dir' => array(
269 ApiBase::PARAM_DFLT => 'ascending',
270 ApiBase::PARAM_TYPE => array(
271 'ascending',
272 'descending'
276 if ( !$this->hasNamespace ) {
277 unset( $allowedParams['namespace'] );
280 return $allowedParams;
283 protected function getExamplesMessages() {
284 $p = $this->getModulePrefix();
285 $name = $this->getModuleName();
286 $path = $this->getModulePath();
288 return array(
289 "action=query&list={$name}&{$p}from=B&{$p}prop=ids|title"
290 => "apihelp-$path-example-B",
291 "action=query&list={$name}&{$p}unique=&{$p}from=B"
292 => "apihelp-$path-example-unique",
293 "action=query&generator={$name}&g{$p}unique=&g{$p}from=B"
294 => "apihelp-$path-example-unique-generator",
295 "action=query&generator={$name}&g{$p}from=B"
296 => "apihelp-$path-example-generator",
300 public function getHelpUrls() {
301 $name = ucfirst( $this->getModuleName() );
303 return "https://www.mediawiki.org/wiki/API:{$name}";