Remove score display from search engine
[mediawiki.git] / includes / api / ApiQueryLangBacklinks.php
blob13711e6c83675a210024604b7781d11a55df7ff5
1 <?php
2 /**
3 * API for MediaWiki 1.17+
5 * Created on May 14, 2011
7 * Copyright © 2011 Sam Reed
8 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
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.
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.
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
25 * @file
28 /**
29 * This gives links pointing to the given interwiki
30 * @ingroup API
32 class ApiQueryLangBacklinks extends ApiQueryGeneratorBase {
34 public function __construct( ApiQuery $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'lbl' );
38 public function execute() {
39 $this->run();
42 public function executeGenerator( $resultPageSet ) {
43 $this->run( $resultPageSet );
46 /**
47 * @param ApiPageSet $resultPageSet
48 * @return void
50 public function run( $resultPageSet = null ) {
51 $params = $this->extractRequestParams();
53 if ( isset( $params['title'] ) && !isset( $params['lang'] ) ) {
54 $this->dieUsageMsg( array( 'missingparam', 'lang' ) );
57 if ( !is_null( $params['continue'] ) ) {
58 $cont = explode( '|', $params['continue'] );
59 $this->dieContinueUsageIf( count( $cont ) != 3 );
61 $db = $this->getDB();
62 $op = $params['dir'] == 'descending' ? '<' : '>';
63 $prefix = $db->addQuotes( $cont[0] );
64 $title = $db->addQuotes( $cont[1] );
65 $from = intval( $cont[2] );
66 $this->addWhere(
67 "ll_lang $op $prefix OR " .
68 "(ll_lang = $prefix AND " .
69 "(ll_title $op $title OR " .
70 "(ll_title = $title AND " .
71 "ll_from $op= $from)))"
75 $prop = array_flip( $params['prop'] );
76 $lllang = isset( $prop['lllang'] );
77 $lltitle = isset( $prop['lltitle'] );
79 $this->addTables( array( 'langlinks', 'page' ) );
80 $this->addWhere( 'll_from = page_id' );
82 $this->addFields( array( 'page_id', 'page_title', 'page_namespace', 'page_is_redirect',
83 'll_from', 'll_lang', 'll_title' ) );
85 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
86 if ( isset( $params['lang'] ) ) {
87 $this->addWhereFld( 'll_lang', $params['lang'] );
88 if ( isset( $params['title'] ) ) {
89 $this->addWhereFld( 'll_title', $params['title'] );
90 $this->addOption( 'ORDER BY', 'll_from' . $sort );
91 } else {
92 $this->addOption( 'ORDER BY', array(
93 'll_title' . $sort,
94 'll_from' . $sort
95 ) );
97 } else {
98 $this->addOption( 'ORDER BY', array(
99 'll_lang' . $sort,
100 'll_title' . $sort,
101 'll_from' . $sort
102 ) );
105 $this->addOption( 'LIMIT', $params['limit'] + 1 );
107 $res = $this->select( __METHOD__ );
109 $pages = array();
111 $count = 0;
112 $result = $this->getResult();
113 foreach ( $res as $row ) {
114 if ( ++$count > $params['limit'] ) {
115 // We've reached the one extra which shows that there are
116 // additional pages to be had. Stop here... Continue string
117 // preserved in case the redirect query doesn't pass the limit.
118 $this->setContinueEnumParameter(
119 'continue',
120 "{$row->ll_lang}|{$row->ll_title}|{$row->ll_from}"
122 break;
125 if ( !is_null( $resultPageSet ) ) {
126 $pages[] = Title::newFromRow( $row );
127 } else {
128 $entry = array( 'pageid' => $row->page_id );
130 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
131 ApiQueryBase::addTitleInfo( $entry, $title );
133 if ( $row->page_is_redirect ) {
134 $entry['redirect'] = '';
137 if ( $lllang ) {
138 $entry['lllang'] = $row->ll_lang;
141 if ( $lltitle ) {
142 $entry['lltitle'] = $row->ll_title;
145 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $entry );
146 if ( !$fit ) {
147 $this->setContinueEnumParameter(
148 'continue',
149 "{$row->ll_lang}|{$row->ll_title}|{$row->ll_from}"
151 break;
156 if ( is_null( $resultPageSet ) ) {
157 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'll' );
158 } else {
159 $resultPageSet->populateFromTitles( $pages );
163 public function getCacheMode( $params ) {
164 return 'public';
167 public function getAllowedParams() {
168 return array(
169 'lang' => null,
170 'title' => null,
171 'continue' => null,
172 'limit' => array(
173 ApiBase::PARAM_DFLT => 10,
174 ApiBase::PARAM_TYPE => 'limit',
175 ApiBase::PARAM_MIN => 1,
176 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
177 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
179 'prop' => array(
180 ApiBase::PARAM_ISMULTI => true,
181 ApiBase::PARAM_DFLT => '',
182 ApiBase::PARAM_TYPE => array(
183 'lllang',
184 'lltitle',
187 'dir' => array(
188 ApiBase::PARAM_DFLT => 'ascending',
189 ApiBase::PARAM_TYPE => array(
190 'ascending',
191 'descending'
197 public function getParamDescription() {
198 return array(
199 'lang' => 'Language for the language link',
200 'title' => "Language link to search for. Must be used with {$this->getModulePrefix()}lang",
201 'continue' => 'When more results are available, use this to continue',
202 'prop' => array(
203 'Which properties to get',
204 ' lllang - Adds the language code of the language link',
205 ' lltitle - Adds the title of the language link',
207 'limit' => 'How many total pages to return',
208 'dir' => 'The direction in which to list',
212 public function getResultProperties() {
213 return array(
214 '' => array(
215 'pageid' => 'integer',
216 'ns' => 'namespace',
217 'title' => 'string',
218 'redirect' => 'boolean'
220 'lllang' => array(
221 'lllang' => 'string'
223 'lltitle' => array(
224 'lltitle' => 'string'
229 public function getDescription() {
230 return array( 'Find all pages that link to the given language link.',
231 'Can be used to find all links with a language code, or',
232 'all links to a title (with a given language).',
233 'Using neither parameter is effectively "All Language Links".',
234 'Note that this may not consider language links added by extensions.',
238 public function getPossibleErrors() {
239 return array_merge( parent::getPossibleErrors(), array(
240 array( 'missingparam', 'lang' ),
241 ) );
244 public function getExamples() {
245 return array(
246 'api.php?action=query&list=langbacklinks&lbltitle=Test&lbllang=fr',
247 'api.php?action=query&generator=langbacklinks&glbltitle=Test&glbllang=fr&prop=info'
251 public function getHelpUrls() {
252 return 'https://www.mediawiki.org/wiki/API:Langbacklinks';