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