Remove double globals.
[lhc/web/wiklou.git] / includes / api / ApiQueryAllLinks.php
1 <?php
2
3 /**
4 * Created on July 7, 2007
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiQueryBase.php' );
29 }
30
31 /**
32 * Query module to enumerate links from all pages together.
33 *
34 * @ingroup API
35 */
36 class ApiQueryAllLinks extends ApiQueryGeneratorBase {
37
38 public function __construct( $query, $moduleName ) {
39 parent::__construct( $query, $moduleName, 'al' );
40 }
41
42 public function execute() {
43 $this->run();
44 }
45
46 public function getCacheMode( $params ) {
47 return 'public';
48 }
49
50 public function executeGenerator( $resultPageSet ) {
51 $this->run( $resultPageSet );
52 }
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 $arr = explode( '|', $params['continue'] );
80 if ( count( $arr ) != 2 ) {
81 $this->dieUsage( 'Invalid continue parameter', 'badcontinue' );
82 }
83 $from = $this->getDB()->strencode( $this->titleToKey( $arr[0] ) );
84 $id = intval( $arr[1] );
85 $this->addWhere(
86 "pl_title > '$from' OR " .
87 "(pl_title = '$from' AND " .
88 "pl_from > $id)"
89 );
90 }
91
92 if ( !is_null( $params['from'] ) ) {
93 $this->addWhere( 'pl_title>=' . $db->addQuotes( $this->titlePartToKey( $params['from'] ) ) );
94 }
95 if ( isset( $params['prefix'] ) ) {
96 $this->addWhere( 'pl_title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
97 }
98
99 $this->addFields( array(
100 'pl_title',
101 ) );
102 $this->addFieldsIf( 'pl_from', !$params['unique'] );
103
104 $this->addOption( 'USE INDEX', 'pl_namespace' );
105 $limit = $params['limit'];
106 $this->addOption( 'LIMIT', $limit + 1 );
107 if ( $params['unique'] ) {
108 $this->addOption( 'ORDER BY', 'pl_title' );
109 } else {
110 $this->addOption( 'ORDER BY', 'pl_title, pl_from' );
111 }
112
113 $res = $this->select( __METHOD__ );
114
115 $pageids = array();
116 $count = 0;
117 $result = $this->getResult();
118 foreach ( $res as $row ) {
119 if ( ++ $count > $limit ) {
120 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
121 // TODO: Security issue - if the user has no right to view next title, it will still be shown
122 if ( $params['unique'] ) {
123 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->pl_title ) );
124 } else {
125 $this->setContinueEnumParameter( 'continue', $this->keyToTitle( $row->pl_title ) . "|" . $row->pl_from );
126 }
127 break;
128 }
129
130 if ( is_null( $resultPageSet ) ) {
131 $vals = array();
132 if ( $fld_ids ) {
133 $vals['fromid'] = intval( $row->pl_from );
134 }
135 if ( $fld_title ) {
136 $title = Title::makeTitle( $params['namespace'], $row->pl_title );
137 ApiQueryBase::addTitleInfo( $vals, $title );
138 }
139 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
140 if ( !$fit ) {
141 if ( $params['unique'] ) {
142 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->pl_title ) );
143 } else {
144 $this->setContinueEnumParameter( 'continue', $this->keyToTitle( $row->pl_title ) . "|" . $row->pl_from );
145 }
146 break;
147 }
148 } else {
149 $pageids[] = $row->pl_from;
150 }
151 }
152
153 if ( is_null( $resultPageSet ) ) {
154 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'l' );
155 } else {
156 $resultPageSet->populateFromPageIDs( $pageids );
157 }
158 }
159
160 public function getAllowedParams() {
161 return array(
162 'continue' => null,
163 'from' => null,
164 'prefix' => null,
165 'unique' => false,
166 'prop' => array(
167 ApiBase::PARAM_ISMULTI => true,
168 ApiBase::PARAM_DFLT => 'title',
169 ApiBase::PARAM_TYPE => array(
170 'ids',
171 'title'
172 )
173 ),
174 'namespace' => array(
175 ApiBase::PARAM_DFLT => 0,
176 ApiBase::PARAM_TYPE => 'namespace'
177 ),
178 'limit' => array(
179 ApiBase::PARAM_DFLT => 10,
180 ApiBase::PARAM_TYPE => 'limit',
181 ApiBase::PARAM_MIN => 1,
182 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
183 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
184 )
185 );
186 }
187
188 public function getParamDescription() {
189 $p = $this->getModulePrefix();
190 return array(
191 'from' => 'The page title to start enumerating from',
192 'prefix' => 'Search for all page titles that begin with this value',
193 'unique' => "Only show unique links. Cannot be used with generator or {$p}prop=ids",
194 'prop' => array(
195 'What pieces of information to include',
196 " ids - Adds pageid of where the link is from (Cannot be used with {$p}unique)",
197 ' title - Adds the title of the link',
198 ),
199 'namespace' => 'The namespace to enumerate',
200 'limit' => 'How many total links to return',
201 'continue' => 'When more results are available, use this to continue',
202 );
203 }
204
205 public function getDescription() {
206 return 'Enumerate all links that point to a given namespace';
207 }
208
209 public function getPossibleErrors() {
210 $m = $this->getModuleName();
211 return array_merge( parent::getPossibleErrors(), array(
212 array( 'code' => 'params', 'info' => "{$m} cannot be used as a generator in unique links mode" ),
213 array( 'code' => 'params', 'info' => "{$m} cannot return corresponding page ids in unique links mode" ),
214 array( 'code' => 'params', 'info' => 'alcontinue and alfrom cannot be used together' ),
215 array( 'code' => 'badcontinue', 'info' => 'Invalid continue parameter' ),
216 ) );
217 }
218
219 protected function getExamples() {
220 return array(
221 'api.php?action=query&list=alllinks&alunique&alfrom=B',
222 );
223 }
224
225 public function getVersion() {
226 return __CLASS__ . ': $Id$';
227 }
228 }