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