wfMsgForContentNoTrans() was removed
[mediawiki.git] / includes / api / ApiQueryAllLinks.php
blob347da89606cffc90aaf9e673ffa366fd64b9838e
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 ApiResult::META_TYPE => 'assoc',
206 if ( $fld_ids ) {
207 $vals['fromid'] = intval( $row->pl_from );
209 if ( $fld_title ) {
210 $title = Title::makeTitle( $namespace, $row->pl_title );
211 ApiQueryBase::addTitleInfo( $vals, $title );
213 foreach ( $this->props as $name => $field ) {
214 if ( isset( $prop[$name] ) && $row->$field !== null && $row->$field !== '' ) {
215 $vals[$name] = $row->$field;
218 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
219 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 );
225 break;
227 } elseif ( $params['unique'] ) {
228 $titles[] = Title::makeTitle( $namespace, $row->pl_title );
229 } else {
230 $pageids[] = $row->pl_from;
234 if ( is_null( $resultPageSet ) ) {
235 $result->addIndexedTagName( array( 'query', $this->getModuleName() ), $this->indexTag );
236 } elseif ( $params['unique'] ) {
237 $resultPageSet->populateFromTitles( $titles );
238 } else {
239 $resultPageSet->populateFromPageIDs( $pageids );
243 public function getAllowedParams() {
244 $allowedParams = array(
245 'continue' => array(
246 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
248 'from' => null,
249 'to' => null,
250 'prefix' => null,
251 'unique' => false,
252 'prop' => array(
253 ApiBase::PARAM_ISMULTI => true,
254 ApiBase::PARAM_DFLT => 'title',
255 ApiBase::PARAM_TYPE => array_merge(
256 array( 'ids', 'title' ), array_keys( $this->props )
258 ApiBase::PARAM_HELP_MSG_PER_VALUE => array(),
260 'namespace' => array(
261 ApiBase::PARAM_DFLT => $this->dfltNamespace,
262 ApiBase::PARAM_TYPE => 'namespace'
264 'limit' => array(
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
271 'dir' => array(
272 ApiBase::PARAM_DFLT => 'ascending',
273 ApiBase::PARAM_TYPE => array(
274 'ascending',
275 'descending'
279 if ( !$this->hasNamespace ) {
280 unset( $allowedParams['namespace'] );
283 return $allowedParams;
286 protected function getExamplesMessages() {
287 $p = $this->getModulePrefix();
288 $name = $this->getModuleName();
289 $path = $this->getModulePath();
291 return array(
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",
303 public function getHelpUrls() {
304 $name = ucfirst( $this->getModuleName() );
306 return "https://www.mediawiki.org/wiki/API:{$name}";