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