Follow-up r93258, r93266, r93266: Move the defines to Defines.php
[lhc/web/wiklou.git] / includes / api / ApiQueryLinks.php
1 <?php
2 /**
3 *
4 *
5 * Created on May 12, 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 * A query module to list all wiki links on a given set of pages.
34 *
35 * @ingroup API
36 */
37 class ApiQueryLinks extends ApiQueryGeneratorBase {
38
39 const LINKS = 'links';
40 const TEMPLATES = 'templates';
41
42 private $table, $prefix, $description, $helpUrl;
43
44 public function __construct( $query, $moduleName ) {
45 switch ( $moduleName ) {
46 case self::LINKS:
47 $this->table = 'pagelinks';
48 $this->prefix = 'pl';
49 $this->description = 'link';
50 $this->titlesParam = 'titles';
51 $this->helpUrl = 'http://www.mediawiki.org/wiki/API:Properties#links_.2F_pl';
52 break;
53 case self::TEMPLATES:
54 $this->table = 'templatelinks';
55 $this->prefix = 'tl';
56 $this->description = 'template';
57 $this->titlesParam = 'templates';
58 $this->helpUrl = 'http://www.mediawiki.org/wiki/API:Properties#templates_.2F_tl';
59 break;
60 default:
61 ApiBase::dieDebug( __METHOD__, 'Unknown module name' );
62 }
63
64 parent::__construct( $query, $moduleName, $this->prefix );
65 }
66
67 public function execute() {
68 $this->run();
69 }
70
71 public function getCacheMode( $params ) {
72 return 'public';
73 }
74
75 public function executeGenerator( $resultPageSet ) {
76 $this->run( $resultPageSet );
77 }
78
79 /**
80 * @param $resultPageSet ApiPageSet
81 * @return
82 */
83 private function run( $resultPageSet = null ) {
84 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
85 return; // nothing to do
86 }
87
88 $params = $this->extractRequestParams();
89
90 $this->addFields( array(
91 $this->prefix . '_from AS pl_from',
92 $this->prefix . '_namespace AS pl_namespace',
93 $this->prefix . '_title AS pl_title'
94 ) );
95
96 $this->addTables( $this->table );
97 $this->addWhereFld( $this->prefix . '_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
98 $this->addWhereFld( $this->prefix . '_namespace', $params['namespace'] );
99
100 if ( !is_null( $params[$this->titlesParam] ) ) {
101 $lb = new LinkBatch;
102 foreach ( $params[$this->titlesParam] as $t ) {
103 $title = Title::newFromText( $t );
104 if ( !$title ) {
105 $this->setWarning( "``$t'' is not a valid title" );
106 } else {
107 $lb->addObj( $title );
108 }
109 }
110 $cond = $lb->constructSet( $this->prefix, $this->getDB() );
111 if ( $cond ) {
112 $this->addWhere( $cond );
113 }
114 }
115
116 if ( !is_null( $params['continue'] ) ) {
117 $cont = explode( '|', $params['continue'] );
118 if ( count( $cont ) != 3 ) {
119 $this->dieUsage( 'Invalid continue param. You should pass the ' .
120 'original value returned by the previous query', '_badcontinue' );
121 }
122 $plfrom = intval( $cont[0] );
123 $plns = intval( $cont[1] );
124 $pltitle = $this->getDB()->strencode( $this->titleToKey( $cont[2] ) );
125 $this->addWhere(
126 "{$this->prefix}_from > $plfrom OR " .
127 "({$this->prefix}_from = $plfrom AND " .
128 "({$this->prefix}_namespace > $plns OR " .
129 "({$this->prefix}_namespace = $plns AND " .
130 "{$this->prefix}_title >= '$pltitle')))"
131 );
132 }
133
134 // Here's some MySQL craziness going on: if you use WHERE foo='bar'
135 // and later ORDER BY foo MySQL doesn't notice the ORDER BY is pointless
136 // but instead goes and filesorts, because the index for foo was used
137 // already. To work around this, we drop constant fields in the WHERE
138 // clause from the ORDER BY clause
139 $order = array();
140 if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) {
141 $order[] = "{$this->prefix}_from";
142 }
143 if ( count( $params['namespace'] ) != 1 ) {
144 $order[] = "{$this->prefix}_namespace";
145 }
146
147 $order[] = "{$this->prefix}_title";
148 $this->addOption( 'ORDER BY', implode( ', ', $order ) );
149 $this->addOption( 'USE INDEX', "{$this->prefix}_from" );
150 $this->addOption( 'LIMIT', $params['limit'] + 1 );
151
152 $res = $this->select( __METHOD__ );
153
154 if ( is_null( $resultPageSet ) ) {
155 $count = 0;
156 foreach ( $res as $row ) {
157 if ( ++$count > $params['limit'] ) {
158 // We've reached the one extra which shows that
159 // there are additional pages to be had. Stop here...
160 $this->setContinueEnumParameter( 'continue',
161 "{$row->pl_from}|{$row->pl_namespace}|" .
162 $this->keyToTitle( $row->pl_title ) );
163 break;
164 }
165 $vals = array();
166 ApiQueryBase::addTitleInfo( $vals, Title::makeTitle( $row->pl_namespace, $row->pl_title ) );
167 $fit = $this->addPageSubItem( $row->pl_from, $vals );
168 if ( !$fit ) {
169 $this->setContinueEnumParameter( 'continue',
170 "{$row->pl_from}|{$row->pl_namespace}|" .
171 $this->keyToTitle( $row->pl_title ) );
172 break;
173 }
174 }
175 } else {
176 $titles = array();
177 $count = 0;
178 foreach ( $res as $row ) {
179 if ( ++$count > $params['limit'] ) {
180 // We've reached the one extra which shows that
181 // there are additional pages to be had. Stop here...
182 $this->setContinueEnumParameter( 'continue',
183 "{$row->pl_from}|{$row->pl_namespace}|" .
184 $this->keyToTitle( $row->pl_title ) );
185 break;
186 }
187 $titles[] = Title::makeTitle( $row->pl_namespace, $row->pl_title );
188 }
189 $resultPageSet->populateFromTitles( $titles );
190 }
191 }
192
193 public function getAllowedParams() {
194 return array(
195 'namespace' => array(
196 ApiBase::PARAM_TYPE => 'namespace',
197 ApiBase::PARAM_ISMULTI => true
198 ),
199 'limit' => array(
200 ApiBase::PARAM_DFLT => 10,
201 ApiBase::PARAM_TYPE => 'limit',
202 ApiBase::PARAM_MIN => 1,
203 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
204 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
205 ),
206 'continue' => null,
207 $this->titlesParam => array(
208 ApiBase::PARAM_ISMULTI => true,
209 ),
210 );
211 }
212
213 public function getParamDescription() {
214 $desc = $this->description;
215 $arr = array(
216 'namespace' => "Show {$desc}s in this namespace(s) only",
217 'limit' => "How many {$desc}s to return",
218 'continue' => 'When more results are available, use this to continue',
219 );
220 if ( $this->getModuleName() == self::LINKS ) {
221 $arr[$this->titlesParam] = 'Only list links to these titles. Useful for checking whether a certain page links to a certain title.';
222 } elseif ( $this->getModuleName() == self::TEMPLATES ) {
223 $arr[$this->titlesParam] = 'Only list these templates. Useful for checking whether a certain page uses a certain template.';
224 }
225 return $arr;
226 }
227
228 public function getDescription() {
229 return "Returns all {$this->description}s from the given page(s)";
230 }
231
232 protected function getExamples() {
233 return array(
234 "Get {$this->description}s from the [[Main Page]]:",
235 " api.php?action=query&prop={$this->getModuleName()}&titles=Main%20Page",
236 "Get information about the {$this->description} pages in the [[Main Page]]:",
237 " api.php?action=query&generator={$this->getModuleName()}&titles=Main%20Page&prop=info",
238 "Get {$this->description}s from the Main Page in the User and Template namespaces:",
239 " api.php?action=query&prop={$this->getModuleName()}&titles=Main%20Page&{$this->prefix}namespace=2|10"
240 );
241 }
242
243 public function getHelpUrls() {
244 return $this->helpUrl;
245 }
246
247 public function getVersion() {
248 return __CLASS__ . ': $Id$';
249 }
250 }