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