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