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