Merge "Add function to clear mPreparedEdit, to use in cases of mutable content"
[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 ' .
47 'for checking whether a certain page links to a certain title.';
48 $this->helpUrl = 'https://www.mediawiki.org/wiki/API:Properties#links_.2F_pl';
49 break;
50 case self::TEMPLATES:
51 $this->table = 'templatelinks';
52 $this->prefix = 'tl';
53 $this->description = 'template';
54 $this->titlesParam = 'templates';
55 $this->titlesParamDescription = 'Only list these templates. Useful ' .
56 'for checking whether a certain page uses a certain template.';
57 $this->helpUrl = 'https://www.mediawiki.org/wiki/API:Properties#templates_.2F_tl';
58 break;
59 default:
60 ApiBase::dieDebug( __METHOD__, 'Unknown module name' );
61 }
62
63 parent::__construct( $query, $moduleName, $this->prefix );
64 }
65
66 public function execute() {
67 $this->run();
68 }
69
70 public function getCacheMode( $params ) {
71 return 'public';
72 }
73
74 public function executeGenerator( $resultPageSet ) {
75 $this->run( $resultPageSet );
76 }
77
78 /**
79 * @param $resultPageSet ApiPageSet
80 * @return
81 */
82 private function run( $resultPageSet = null ) {
83 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
84 return; // nothing to do
85 }
86
87 $params = $this->extractRequestParams();
88
89 $this->addFields( array(
90 'pl_from' => $this->prefix . '_from',
91 'pl_namespace' => $this->prefix . '_namespace',
92 'pl_title' => $this->prefix . '_title'
93 ) );
94
95 $this->addTables( $this->table );
96 $this->addWhereFld( $this->prefix . '_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
97 $this->addWhereFld( $this->prefix . '_namespace', $params['namespace'] );
98
99 if ( !is_null( $params[$this->titlesParam] ) ) {
100 $lb = new LinkBatch;
101 foreach ( $params[$this->titlesParam] as $t ) {
102 $title = Title::newFromText( $t );
103 if ( !$title ) {
104 $this->setWarning( "\"$t\" is not a valid title" );
105 } else {
106 $lb->addObj( $title );
107 }
108 }
109 $cond = $lb->constructSet( $this->prefix, $this->getDB() );
110 if ( $cond ) {
111 $this->addWhere( $cond );
112 }
113 }
114
115 if ( !is_null( $params['continue'] ) ) {
116 $cont = explode( '|', $params['continue'] );
117 $this->dieContinueUsageIf( count( $cont ) != 3 );
118 $op = $params['dir'] == 'descending' ? '<' : '>';
119 $plfrom = intval( $cont[0] );
120 $plns = intval( $cont[1] );
121 $pltitle = $this->getDB()->addQuotes( $cont[2] );
122 $this->addWhere(
123 "{$this->prefix}_from $op $plfrom OR " .
124 "({$this->prefix}_from = $plfrom AND " .
125 "({$this->prefix}_namespace $op $plns OR " .
126 "({$this->prefix}_namespace = $plns AND " .
127 "{$this->prefix}_title $op= $pltitle)))"
128 );
129 }
130
131 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
132 // Here's some MySQL craziness going on: if you use WHERE foo='bar'
133 // and later ORDER BY foo MySQL doesn't notice the ORDER BY is pointless
134 // but instead goes and filesorts, because the index for foo was used
135 // already. To work around this, we drop constant fields in the WHERE
136 // clause from the ORDER BY clause
137 $order = array();
138 if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) {
139 $order[] = $this->prefix . '_from' . $sort;
140 }
141 if ( count( $params['namespace'] ) != 1 ) {
142 $order[] = $this->prefix . '_namespace' . $sort;
143 }
144
145 $order[] = $this->prefix . '_title' . $sort;
146 $this->addOption( 'ORDER BY', $order );
147 $this->addOption( 'USE INDEX', $this->prefix . '_from' );
148 $this->addOption( 'LIMIT', $params['limit'] + 1 );
149
150 $res = $this->select( __METHOD__ );
151
152 if ( is_null( $resultPageSet ) ) {
153 $count = 0;
154 foreach ( $res as $row ) {
155 if ( ++$count > $params['limit'] ) {
156 // We've reached the one extra which shows that
157 // there are additional pages to be had. Stop here...
158 $this->setContinueEnumParameter( 'continue',
159 "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" );
160 break;
161 }
162 $vals = array();
163 ApiQueryBase::addTitleInfo( $vals, Title::makeTitle( $row->pl_namespace, $row->pl_title ) );
164 $fit = $this->addPageSubItem( $row->pl_from, $vals );
165 if ( !$fit ) {
166 $this->setContinueEnumParameter( 'continue',
167 "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" );
168 break;
169 }
170 }
171 } else {
172 $titles = array();
173 $count = 0;
174 foreach ( $res as $row ) {
175 if ( ++$count > $params['limit'] ) {
176 // We've reached the one extra which shows that
177 // there are additional pages to be had. Stop here...
178 $this->setContinueEnumParameter( 'continue',
179 "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" );
180 break;
181 }
182 $titles[] = Title::makeTitle( $row->pl_namespace, $row->pl_title );
183 }
184 $resultPageSet->populateFromTitles( $titles );
185 }
186 }
187
188 public function getAllowedParams() {
189 return array(
190 'namespace' => array(
191 ApiBase::PARAM_TYPE => 'namespace',
192 ApiBase::PARAM_ISMULTI => true
193 ),
194 'limit' => array(
195 ApiBase::PARAM_DFLT => 10,
196 ApiBase::PARAM_TYPE => 'limit',
197 ApiBase::PARAM_MIN => 1,
198 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
199 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
200 ),
201 'continue' => null,
202 $this->titlesParam => array(
203 ApiBase::PARAM_ISMULTI => true,
204 ),
205 'dir' => array(
206 ApiBase::PARAM_DFLT => 'ascending',
207 ApiBase::PARAM_TYPE => array(
208 'ascending',
209 'descending'
210 )
211 ),
212 );
213 }
214
215 public function getParamDescription() {
216 $desc = $this->description;
217
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
244 return array(
245 "api.php?action=query&prop={$name}&titles=Main%20Page" => "Get {$desc}s from the [[Main Page]]",
246 "api.php?action=query&generator={$name}&titles=Main%20Page&prop=info"
247 => "Get information about the {$desc} pages in the [[Main Page]]",
248 "api.php?action=query&prop={$name}&titles=Main%20Page&{$this->prefix}namespace=2|10"
249 => "Get {$desc}s from the Main Page in the User and Template namespaces",
250 );
251 }
252
253 public function getHelpUrls() {
254 return $this->helpUrl;
255 }
256 }