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