multi dbms support in api
[lhc/web/wiklou.git] / includes / api / ApiQueryLangBacklinks.php
1 <?php
2 /**
3 * API for MediaWiki 1.17+
4 *
5 * Created on May 14, 2011
6 *
7 * Copyright © 2011 Sam Reed
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 * @file
26 */
27
28 /**
29 * This gives links pointing to the given interwiki
30 * @ingroup API
31 */
32 class ApiQueryLangBacklinks extends ApiQueryGeneratorBase {
33
34 public function __construct( $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'lbl' );
36 }
37
38 public function execute() {
39 $this->run();
40 }
41
42 public function executeGenerator( $resultPageSet ) {
43 $this->run( $resultPageSet );
44 }
45
46 /**
47 * @param $resultPageSet ApiPageSet
48 * @return void
49 */
50 public function run( $resultPageSet = null ) {
51 $params = $this->extractRequestParams();
52
53 if ( isset( $params['title'] ) && !isset( $params['lang'] ) ) {
54 $this->dieUsageMsg( array( 'missingparam', 'lang' ) );
55 }
56
57 if ( !is_null( $params['continue'] ) ) {
58 $cont = explode( '|', $params['continue'] );
59 if ( count( $cont ) != 3 ) {
60 $this->dieUsage( 'Invalid continue param. You should pass the ' .
61 'original value returned by the previous query', '_badcontinue' );
62 }
63
64 $db = $this->getDB();
65 $prefix = $db->addQuotes( $cont[0] );
66 $title = $db->addQuotes( $this->titleToKey( $cont[1] ) );
67 $from = intval( $cont[2] );
68 $this->addWhere(
69 "ll_lang > $prefix OR " .
70 "(ll_lang = $prefix AND " .
71 "(ll_title > $title OR " .
72 "(ll_title = $title AND " .
73 "ll_from >= $from)))"
74 );
75 }
76
77 $prop = array_flip( $params['prop'] );
78 $lllang = isset( $prop['lllang'] );
79 $lltitle = isset( $prop['lltitle'] );
80
81 $this->addTables( array( 'langlinks', 'page' ) );
82 $this->addWhere( 'll_from = page_id' );
83
84 $this->addFields( array( 'page_id', 'page_title', 'page_namespace', 'page_is_redirect',
85 'll_from', 'll_lang', 'll_title' ) );
86
87 if ( isset( $params['lang'] ) ) {
88 $this->addWhereFld( 'll_lang', $params['lang'] );
89 if ( isset( $params['title'] ) ) {
90 $this->addWhereFld( 'll_title', $params['title'] );
91 $this->addOption( 'ORDER BY', 'll_from' );
92 } else {
93 $this->addOption( 'ORDER BY', 'll_title, ll_from' );
94 }
95 } else {
96 $this->addOption( 'ORDER BY', 'll_lang, ll_title, ll_from' );
97 }
98
99 $this->addOption( 'LIMIT', $params['limit'] + 1 );
100
101 $res = $this->select( __METHOD__ );
102
103 $pages = array();
104
105 $count = 0;
106 $result = $this->getResult();
107 foreach ( $res as $row ) {
108 if ( ++ $count > $params['limit'] ) {
109 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
110 // Continue string preserved in case the redirect query doesn't pass the limit
111 $this->setContinueEnumParameter( 'continue', "{$row->ll_lang}|{$row->ll_title}|{$row->ll_from}" );
112 break;
113 }
114
115 if ( !is_null( $resultPageSet ) ) {
116 $pages[] = Title::newFromRow( $row );
117 } else {
118 $entry = array( 'pageid' => $row->page_id );
119
120 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
121 ApiQueryBase::addTitleInfo( $entry, $title );
122
123 if ( $row->page_is_redirect ) {
124 $entry['redirect'] = '';
125 }
126
127 if ( $lllang ) {
128 $entry['lllang'] = $row->ll_lang;
129 }
130
131 if ( $lltitle ) {
132 $entry['lltitle'] = $row->ll_title;
133 }
134
135 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $entry );
136 if ( !$fit ) {
137 $this->setContinueEnumParameter( 'continue', "{$row->ll_lang}|{$row->ll_title}|{$row->ll_from}" );
138 break;
139 }
140 }
141 }
142
143 if ( is_null( $resultPageSet ) ) {
144 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'll' );
145 } else {
146 $resultPageSet->populateFromTitles( $pages );
147 }
148 }
149
150 public function getCacheMode( $params ) {
151 return 'public';
152 }
153
154 public function getAllowedParams() {
155 return array(
156 'lang' => null,
157 'title' => null,
158 'continue' => null,
159 'limit' => array(
160 ApiBase::PARAM_DFLT => 10,
161 ApiBase::PARAM_TYPE => 'limit',
162 ApiBase::PARAM_MIN => 1,
163 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
164 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
165 ),
166 'prop' => array(
167 ApiBase::PARAM_ISMULTI => true,
168 ApiBase::PARAM_DFLT => '',
169 ApiBase::PARAM_TYPE => array(
170 'lllang',
171 'lltitle',
172 ),
173 ),
174 );
175 }
176
177 public function getParamDescription() {
178 return array(
179 'lang' => 'Language for the language link',
180 'title' => "Language link to search for. Must be used with {$this->getModulePrefix()}lang",
181 'continue' => 'When more results are available, use this to continue',
182 'prop' => array(
183 'Which properties to get',
184 ' lllang - Adds the language code of the language link',
185 ' lltitle - Adds the title of the language ink',
186 ),
187 'limit' => 'How many total pages to return',
188 );
189 }
190
191 public function getDescription() {
192 return array( 'Find all pages that link to the given language link.',
193 'Can be used to find all links with a language code, or',
194 'all links to a title (with a given language).',
195 'Using neither parameter is effectively "All Language Links"',
196 );
197 }
198
199 public function getPossibleErrors() {
200 return array_merge( parent::getPossibleErrors(), array(
201 array( 'missingparam', 'lang' ),
202 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
203 ) );
204 }
205
206 public function getExamples() {
207 return array(
208 'api.php?action=query&list=langbacklinks&lbltitle=Test&lbllang=fr',
209 'api.php?action=query&generator=langbacklinks&glbltitle=Test&glbllang=fr&prop=info'
210 );
211 }
212
213 public function getVersion() {
214 return __CLASS__ . ': $Id$';
215 }
216 }