Merge "Update formatting and docs"
[lhc/web/wiklou.git] / includes / api / ApiQueryLangLinks.php
1 <?php
2 /**
3 *
4 *
5 * Created on May 13, 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 /**
28 * A query module to list all langlinks (links to corresponding foreign language pages).
29 *
30 * @ingroup API
31 */
32 class ApiQueryLangLinks extends ApiQueryBase {
33
34 public function __construct( $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'll' );
36 }
37
38 public function execute() {
39 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
40 return;
41 }
42
43 $params = $this->extractRequestParams();
44
45 if ( isset( $params['title'] ) && !isset( $params['lang'] ) ) {
46 $this->dieUsageMsg( array( 'missingparam', 'lang' ) );
47 }
48
49 $this->addFields( array(
50 'll_from',
51 'll_lang',
52 'll_title'
53 ) );
54
55 $this->addTables( 'langlinks' );
56 $this->addWhereFld( 'll_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
57 if ( !is_null( $params['continue'] ) ) {
58 $cont = explode( '|', $params['continue'] );
59 $this->dieContinueUsageIf( count( $cont ) != 2 );
60 $op = $params['dir'] == 'descending' ? '<' : '>';
61 $llfrom = intval( $cont[0] );
62 $lllang = $this->getDB()->addQuotes( $cont[1] );
63 $this->addWhere(
64 "ll_from $op $llfrom OR " .
65 "(ll_from = $llfrom AND " .
66 "ll_lang $op= $lllang)"
67 );
68 }
69
70 // Note that, since (ll_from, ll_lang) is a unique key, we don't need
71 // to sort by ll_title to ensure deterministic ordering.
72 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
73 if ( isset( $params['lang'] ) ) {
74 $this->addWhereFld( 'll_lang', $params['lang'] );
75 if ( isset( $params['title'] ) ) {
76 $this->addWhereFld( 'll_title', $params['title'] );
77 }
78 $this->addOption( 'ORDER BY', 'll_from' . $sort );
79 } else {
80 // Don't order by ll_from if it's constant in the WHERE clause
81 if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
82 $this->addOption( 'ORDER BY', 'll_lang' . $sort );
83 } else {
84 $this->addOption( 'ORDER BY', array(
85 'll_from' . $sort,
86 'll_lang' . $sort
87 ));
88 }
89 }
90
91 $this->addOption( 'LIMIT', $params['limit'] + 1 );
92 $res = $this->select( __METHOD__ );
93
94 $count = 0;
95 foreach ( $res as $row ) {
96 if ( ++$count > $params['limit'] ) {
97 // We've reached the one extra which shows that
98 // there are additional pages to be had. Stop here...
99 $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
100 break;
101 }
102 $entry = array( 'lang' => $row->ll_lang );
103 if ( $params['url'] ) {
104 $title = Title::newFromText( "{$row->ll_lang}:{$row->ll_title}" );
105 if ( $title ) {
106 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
107 }
108 }
109 ApiResult::setContent( $entry, $row->ll_title );
110 $fit = $this->addPageSubItem( $row->ll_from, $entry );
111 if ( !$fit ) {
112 $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
113 break;
114 }
115 }
116 }
117
118 public function getCacheMode( $params ) {
119 return 'public';
120 }
121
122 public function getAllowedParams() {
123 return array(
124 'limit' => array(
125 ApiBase::PARAM_DFLT => 10,
126 ApiBase::PARAM_TYPE => 'limit',
127 ApiBase::PARAM_MIN => 1,
128 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
129 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
130 ),
131 'continue' => null,
132 'url' => false,
133 'lang' => null,
134 'title' => null,
135 'dir' => array(
136 ApiBase::PARAM_DFLT => 'ascending',
137 ApiBase::PARAM_TYPE => array(
138 'ascending',
139 'descending'
140 )
141 ),
142 );
143 }
144
145 public function getParamDescription() {
146 return array(
147 'limit' => 'How many langlinks to return',
148 'continue' => 'When more results are available, use this to continue',
149 'url' => 'Whether to get the full URL',
150 'lang' => 'Language code',
151 'title' => "Link to search for. Must be used with {$this->getModulePrefix()}lang",
152 'dir' => 'The direction in which to list',
153 );
154 }
155
156 public function getResultProperties() {
157 return array(
158 '' => array(
159 'lang' => 'string',
160 'url' => array(
161 ApiBase::PROP_TYPE => 'string',
162 ApiBase::PROP_NULLABLE => true
163 ),
164 '*' => 'string'
165 )
166 );
167 }
168
169 public function getDescription() {
170 return 'Returns all interlanguage links from the given page(s)';
171 }
172
173 public function getPossibleErrors() {
174 return array_merge( parent::getPossibleErrors(), array(
175 array( 'missingparam', 'lang' ),
176 ) );
177 }
178
179 public function getExamples() {
180 return array(
181 'api.php?action=query&prop=langlinks&titles=Main%20Page&redirects=' => 'Get interlanguage links from the [[Main Page]]',
182 );
183 }
184
185 public function getHelpUrls() {
186 return 'https://www.mediawiki.org/wiki/API:Properties#langlinks_.2F_ll';
187 }
188 }