0420838a924580003c2e2f12db836f370d080f7c
[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 parent::__construct( $query, $moduleName, 'al' );
36 }
37
38 public function execute() {
39 $this->run();
40 }
41
42 public function getCacheMode( $params ) {
43 return 'public';
44 }
45
46 public function executeGenerator( $resultPageSet ) {
47 $this->run( $resultPageSet );
48 }
49
50 /**
51 * @param $resultPageSet ApiPageSet
52 * @return void
53 */
54 private function run( $resultPageSet = null ) {
55 $db = $this->getDB();
56 $params = $this->extractRequestParams();
57
58 $prop = array_flip( $params['prop'] );
59 $fld_ids = isset( $prop['ids'] );
60 $fld_title = isset( $prop['title'] );
61
62 if ( $params['unique'] ) {
63 if ( !is_null( $resultPageSet ) ) {
64 $this->dieUsage( $this->getModuleName() . ' cannot be used as a generator in unique links mode', 'params' );
65 }
66 if ( $fld_ids ) {
67 $this->dieUsage( $this->getModuleName() . ' cannot return corresponding page ids in unique links mode', 'params' );
68 }
69 $this->addOption( 'DISTINCT' );
70 }
71
72 $this->addTables( 'pagelinks' );
73 $this->addWhereFld( 'pl_namespace', $params['namespace'] );
74
75 if ( !is_null( $params['from'] ) && !is_null( $params['continue'] ) ) {
76 $this->dieUsage( 'alcontinue and alfrom cannot be used together', 'params' );
77 }
78 if ( !is_null( $params['continue'] ) ) {
79 $continueArr = explode( '|', $params['continue'] );
80 $op = $params['dir'] == 'descending' ? '<' : '>';
81 if ( $params['unique'] ) {
82 if ( count( $continueArr ) != 1 ) {
83 $this->dieUsage( 'Invalid continue parameter', 'badcontinue' );
84 }
85 $continueTitle = $db->addQuotes( $continueArr[0] );
86 $this->addWhere( "pl_title $op= $continueTitle" );
87 } else {
88 if ( count( $continueArr ) != 2 ) {
89 $this->dieUsage( 'Invalid continue parameter', 'badcontinue' );
90 }
91 $continueTitle = $db->addQuotes( $continueArr[0] );
92 $continueFrom = intval( $continueArr[1] );
93 $this->addWhere(
94 "pl_title $op $continueTitle OR " .
95 "(pl_title = $continueTitle AND " .
96 "pl_from $op= $continueFrom)"
97 );
98 }
99 }
100
101 $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
102 $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) );
103 $this->addWhereRange( 'pl_title', 'newer', $from, $to );
104
105 if ( isset( $params['prefix'] ) ) {
106 $this->addWhere( 'pl_title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
107 }
108
109 $this->addFields( 'pl_title' );
110 $this->addFieldsIf( 'pl_from', !$params['unique'] );
111
112 $this->addOption( 'USE INDEX', 'pl_namespace' );
113 $limit = $params['limit'];
114 $this->addOption( 'LIMIT', $limit + 1 );
115
116 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
117 $orderBy = array();
118 $orderBy[] = 'pl_title' . $sort;
119 if ( !$params['unique'] ) {
120 $orderBy[] = 'pl_from' . $sort;
121 }
122 $this->addOption( 'ORDER BY', $orderBy );
123
124 $res = $this->select( __METHOD__ );
125
126 $pageids = array();
127 $count = 0;
128 $result = $this->getResult();
129 foreach ( $res as $row ) {
130 if ( ++ $count > $limit ) {
131 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
132 if ( $params['unique'] ) {
133 $this->setContinueEnumParameter( 'continue', $row->pl_title );
134 } else {
135 $this->setContinueEnumParameter( 'continue', $row->pl_title . "|" . $row->pl_from );
136 }
137 break;
138 }
139
140 if ( is_null( $resultPageSet ) ) {
141 $vals = array();
142 if ( $fld_ids ) {
143 $vals['fromid'] = intval( $row->pl_from );
144 }
145 if ( $fld_title ) {
146 $title = Title::makeTitle( $params['namespace'], $row->pl_title );
147 ApiQueryBase::addTitleInfo( $vals, $title );
148 }
149 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
150 if ( !$fit ) {
151 if ( $params['unique'] ) {
152 $this->setContinueEnumParameter( 'continue', $row->pl_title );
153 } else {
154 $this->setContinueEnumParameter( 'continue', $row->pl_title . "|" . $row->pl_from );
155 }
156 break;
157 }
158 } else {
159 $pageids[] = $row->pl_from;
160 }
161 }
162
163 if ( is_null( $resultPageSet ) ) {
164 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'l' );
165 } else {
166 $resultPageSet->populateFromPageIDs( $pageids );
167 }
168 }
169
170 public function getAllowedParams() {
171 return array(
172 'continue' => null,
173 'from' => null,
174 'to' => null,
175 'prefix' => null,
176 'unique' => false,
177 'prop' => array(
178 ApiBase::PARAM_ISMULTI => true,
179 ApiBase::PARAM_DFLT => 'title',
180 ApiBase::PARAM_TYPE => array(
181 'ids',
182 'title'
183 )
184 ),
185 'namespace' => array(
186 ApiBase::PARAM_DFLT => NS_MAIN,
187 ApiBase::PARAM_TYPE => 'namespace'
188 ),
189 'limit' => array(
190 ApiBase::PARAM_DFLT => 10,
191 ApiBase::PARAM_TYPE => 'limit',
192 ApiBase::PARAM_MIN => 1,
193 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
194 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
195 ),
196 'dir' => array(
197 ApiBase::PARAM_DFLT => 'ascending',
198 ApiBase::PARAM_TYPE => array(
199 'ascending',
200 'descending'
201 )
202 ),
203 );
204 }
205
206 public function getParamDescription() {
207 $p = $this->getModulePrefix();
208 return array(
209 'from' => 'The page title to start enumerating from',
210 'to' => 'The page title to stop enumerating at',
211 'prefix' => 'Search for all page titles that begin with this value',
212 'unique' => "Only show unique links. Cannot be used with generator or {$p}prop=ids",
213 'prop' => array(
214 'What pieces of information to include',
215 " ids - Adds pageid of where the link is from (Cannot be used with {$p}unique)",
216 ' title - Adds the title of the link',
217 ),
218 'namespace' => 'The namespace to enumerate',
219 'limit' => 'How many total links to return',
220 'continue' => 'When more results are available, use this to continue',
221 'dir' => 'The direction in which to list',
222 );
223 }
224
225 public function getResultProperties() {
226 return array(
227 'ids' => array(
228 'fromid' => 'integer'
229 ),
230 'title' => array(
231 'ns' => 'namespace',
232 'title' => 'string'
233 )
234 );
235 }
236
237 public function getDescription() {
238 return 'Enumerate all links that point to a given namespace';
239 }
240
241 public function getPossibleErrors() {
242 $m = $this->getModuleName();
243 return array_merge( parent::getPossibleErrors(), array(
244 array( 'code' => 'params', 'info' => "{$m} cannot be used as a generator in unique links mode" ),
245 array( 'code' => 'params', 'info' => "{$m} cannot return corresponding page ids in unique links mode" ),
246 array( 'code' => 'params', 'info' => 'alcontinue and alfrom cannot be used together' ),
247 array( 'code' => 'badcontinue', 'info' => 'Invalid continue parameter' ),
248 ) );
249 }
250
251 public function getExamples() {
252 return array(
253 'api.php?action=query&list=alllinks&alunique=&alfrom=B',
254 );
255 }
256
257 public function getHelpUrls() {
258 return 'https://www.mediawiki.org/wiki/API:Alllinks';
259 }
260
261 public function getVersion() {
262 return __CLASS__ . ': $Id$';
263 }
264 }