(bug 28070) Fix watchlist RSS for databases that store timestamps in a real timestamp...
[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 /**
78 * @param $resultPageSet ApiPageSet
79 * @return
80 */
81 private function run( $resultPageSet = null ) {
82 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
83 return; // nothing to do
84 }
85
86 $params = $this->extractRequestParams();
87
88 $this->addFields( array(
89 $this->prefix . '_from AS pl_from',
90 $this->prefix . '_namespace AS pl_namespace',
91 $this->prefix . '_title AS pl_title'
92 ) );
93
94 $this->addTables( $this->table );
95 $this->addWhereFld( $this->prefix . '_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
96 $this->addWhereFld( $this->prefix . '_namespace', $params['namespace'] );
97
98 if ( !is_null( $params[$this->titlesParam] ) ) {
99 $lb = new LinkBatch;
100 foreach ( $params[$this->titlesParam] as $t ) {
101 $title = Title::newFromText( $t );
102 if ( !$title ) {
103 $this->setWarning( "``$t'' is not a valid title" );
104 } else {
105 $lb->addObj( $title );
106 }
107 }
108 $cond = $lb->constructSet( $this->prefix, $this->getDB() );
109 if ( $cond ) {
110 $this->addWhere( $cond );
111 }
112 }
113
114 if ( !is_null( $params['continue'] ) ) {
115 $cont = explode( '|', $params['continue'] );
116 if ( count( $cont ) != 3 ) {
117 $this->dieUsage( 'Invalid continue param. You should pass the ' .
118 'original value returned by the previous query', '_badcontinue' );
119 }
120 $plfrom = intval( $cont[0] );
121 $plns = intval( $cont[1] );
122 $pltitle = $this->getDB()->strencode( $this->titleToKey( $cont[2] ) );
123 $this->addWhere(
124 "{$this->prefix}_from > $plfrom OR " .
125 "({$this->prefix}_from = $plfrom AND " .
126 "({$this->prefix}_namespace > $plns OR " .
127 "({$this->prefix}_namespace = $plns AND " .
128 "{$this->prefix}_title >= '$pltitle')))"
129 );
130 }
131
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";
140 }
141 if ( count( $params['namespace'] ) != 1 ) {
142 $order[] = "{$this->prefix}_namespace";
143 }
144
145 $order[] = "{$this->prefix}_title";
146 $this->addOption( 'ORDER BY', implode( ', ', $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}|" .
160 $this->keyToTitle( $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}|" .
169 $this->keyToTitle( $row->pl_title ) );
170 break;
171 }
172 }
173 } else {
174 $titles = array();
175 $count = 0;
176 foreach ( $res as $row ) {
177 if ( ++$count > $params['limit'] ) {
178 // We've reached the one extra which shows that
179 // there are additional pages to be had. Stop here...
180 $this->setContinueEnumParameter( 'continue',
181 "{$row->pl_from}|{$row->pl_namespace}|" .
182 $this->keyToTitle( $row->pl_title ) );
183 break;
184 }
185 $titles[] = Title::makeTitle( $row->pl_namespace, $row->pl_title );
186 }
187 $resultPageSet->populateFromTitles( $titles );
188 }
189 }
190
191 public function getAllowedParams() {
192 return array(
193 'namespace' => array(
194 ApiBase::PARAM_TYPE => 'namespace',
195 ApiBase::PARAM_ISMULTI => true
196 ),
197 'limit' => array(
198 ApiBase::PARAM_DFLT => 10,
199 ApiBase::PARAM_TYPE => 'limit',
200 ApiBase::PARAM_MIN => 1,
201 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
202 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
203 ),
204 'continue' => null,
205 $this->titlesParam => array(
206 ApiBase::PARAM_ISMULTI => true,
207 ),
208 );
209 }
210
211 public function getParamDescription() {
212 $desc = $this->description;
213 $arr = array(
214 'namespace' => "Show {$desc}s in this namespace(s) only",
215 'limit' => "How many {$desc}s to return",
216 'continue' => 'When more results are available, use this to continue',
217 );
218 if ( $this->getModuleName() == self::LINKS ) {
219 $arr[$this->titlesParam] = 'Only list links to these titles. Useful for checking whether a certain page links to a certain title.';
220 } else if ( $this->getModuleName() == self::TEMPLATES ) {
221 $arr[$this->titlesParam] = 'Only list these templates. Useful for checking whether a certain page uses a certain template.';
222 }
223 return $arr;
224 }
225
226 public function getDescription() {
227 return "Returns all {$this->description}s from the given page(s)";
228 }
229
230 protected function getExamples() {
231 return array(
232 "Get {$this->description}s from the [[Main Page]]:",
233 " api.php?action=query&prop={$this->getModuleName()}&titles=Main%20Page",
234 "Get information about the {$this->description} pages in the [[Main Page]]:",
235 " api.php?action=query&generator={$this->getModuleName()}&titles=Main%20Page&prop=info",
236 "Get {$this->description}s from the Main Page in the User and Template namespaces:",
237 " api.php?action=query&prop={$this->getModuleName()}&titles=Main%20Page&{$this->prefix}namespace=2|10"
238 );
239 }
240
241 public function getVersion() {
242 return __CLASS__ . ': $Id$';
243 }
244 }