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