MediaWiki  master
ApiQueryLangLinks.php
Go to the documentation of this file.
1 <?php
24 
31 
34 
37 
38  public function __construct(
39  ApiQuery $query,
40  $moduleName,
43  ) {
44  parent::__construct( $query, $moduleName, 'll' );
45  $this->languageNameUtils = $languageNameUtils;
46  $this->contentLanguage = $contentLanguage;
47  }
48 
49  public function execute() {
50  if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
51  return;
52  }
53 
54  $params = $this->extractRequestParams();
55  $prop = array_fill_keys( (array)$params['prop'], true );
56 
57  if ( isset( $params['title'] ) && !isset( $params['lang'] ) ) {
58  $this->dieWithError(
59  [
60  'apierror-invalidparammix-mustusewith',
61  $this->encodeParamName( 'title' ),
62  $this->encodeParamName( 'lang' ),
63  ],
64  'invalidparammix'
65  );
66  }
67 
68  // Handle deprecated param
69  $this->requireMaxOneParameter( $params, 'url', 'prop' );
70  if ( $params['url'] ) {
71  $prop = [ 'url' => 1 ];
72  }
73 
74  $this->addFields( [
75  'll_from',
76  'll_lang',
77  'll_title'
78  ] );
79 
80  $this->addTables( 'langlinks' );
81  $this->addWhereFld( 'll_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
82  if ( $params['continue'] !== null ) {
83  $cont = explode( '|', $params['continue'] );
84  $this->dieContinueUsageIf( count( $cont ) != 2 );
85  $op = $params['dir'] == 'descending' ? '<' : '>';
86  $llfrom = (int)$cont[0];
87  $lllang = $this->getDB()->addQuotes( $cont[1] );
88  $this->addWhere(
89  "ll_from $op $llfrom OR " .
90  "(ll_from = $llfrom AND " .
91  "ll_lang $op= $lllang)"
92  );
93  }
94 
95  // FIXME: (follow-up) To allow extensions to add to the language links, we need
96  // to load them all, add the extra links, then apply paging.
97  // Should not be terrible, it's not going to be more than a few hundred links.
98 
99  // Note that, since (ll_from, ll_lang) is a unique key, we don't need
100  // to sort by ll_title to ensure deterministic ordering.
101  $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
102  if ( isset( $params['lang'] ) ) {
103  $this->addWhereFld( 'll_lang', $params['lang'] );
104  if ( isset( $params['title'] ) ) {
105  $this->addWhereFld( 'll_title', $params['title'] );
106  }
107  $this->addOption( 'ORDER BY', 'll_from' . $sort );
108  } else {
109  // Don't order by ll_from if it's constant in the WHERE clause
110  if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
111  $this->addOption( 'ORDER BY', 'll_lang' . $sort );
112  } else {
113  $this->addOption( 'ORDER BY', [
114  'll_from' . $sort,
115  'll_lang' . $sort
116  ] );
117  }
118  }
119 
120  $this->addOption( 'LIMIT', $params['limit'] + 1 );
121  $res = $this->select( __METHOD__ );
122 
123  $count = 0;
124  foreach ( $res as $row ) {
125  if ( ++$count > $params['limit'] ) {
126  // We've reached the one extra which shows that
127  // there are additional pages to be had. Stop here...
128  $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
129  break;
130  }
131 
132  $languageNameMap = $this->getConfig()->get( 'InterlanguageLinkCodeMap' );
133  $displayLanguageCode = $languageNameMap[ $row->ll_lang ] ?? $row->ll_lang;
134 
135  // This is potentially risky and confusing (request `no`, but get `nb` in the result).
136  $entry = [ 'lang' => $displayLanguageCode ];
137  if ( isset( $prop['url'] ) ) {
138  $title = Title::newFromText( "{$row->ll_lang}:{$row->ll_title}" );
139  if ( $title ) {
140  $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
141  }
142  }
143 
144  if ( isset( $prop['langname'] ) ) {
145  $entry['langname'] = $this->languageNameUtils
146  ->getLanguageName( $displayLanguageCode, $params['inlanguagecode'] );
147  }
148  if ( isset( $prop['autonym'] ) ) {
149  $entry['autonym'] = $this->languageNameUtils->getLanguageName( $displayLanguageCode );
150  }
151  ApiResult::setContentValue( $entry, 'title', $row->ll_title );
152  $fit = $this->addPageSubItem( $row->ll_from, $entry );
153  if ( !$fit ) {
154  $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
155  break;
156  }
157  }
158  }
159 
160  public function getCacheMode( $params ) {
161  return 'public';
162  }
163 
164  public function getAllowedParams() {
165  return [
166  'prop' => [
167  ApiBase::PARAM_ISMULTI => true,
169  'url',
170  'langname',
171  'autonym',
172  ],
174  ],
175  'lang' => null,
176  'title' => null,
177  'dir' => [
178  ApiBase::PARAM_DFLT => 'ascending',
180  'ascending',
181  'descending'
182  ]
183  ],
184  'inlanguagecode' => $this->contentLanguage->getCode(),
185  'limit' => [
186  ApiBase::PARAM_DFLT => 10,
187  ApiBase::PARAM_TYPE => 'limit',
188  ApiBase::PARAM_MIN => 1,
191  ],
192  'continue' => [
193  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
194  ],
195  'url' => [
196  ApiBase::PARAM_DFLT => false,
198  ],
199  ];
200  }
201 
202  protected function getExamplesMessages() {
203  return [
204  'action=query&prop=langlinks&titles=Main%20Page&redirects='
205  => 'apihelp-query+langlinks-example-simple',
206  ];
207  }
208 
209  public function getHelpUrls() {
210  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Langlinks';
211  }
212 }
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:72
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:395
ApiQueryBase\addFields
addFields( $value)
Add a set of fields to select to the internal array.
Definition: ApiQueryBase.php:212
ApiQuery
This is the main query class.
Definition: ApiQuery.php:39
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1436
ApiBase\PARAM_HELP_MSG
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:162
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:81
ApiQueryBase\addOption
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
Definition: ApiQueryBase.php:378
$res
$res
Definition: testCompression.php:57
MediaWiki\Languages\LanguageNameUtils
A service that provides utilities to do with language names and codes.
Definition: LanguageNameUtils.php:43
ApiResult\setContentValue
static setContentValue(array &$arr, $name, $value, $flags=0)
Add an output value to the array by name and mark as META_CONTENT.
Definition: ApiResult.php:466
ApiBase\PARAM_DEPRECATED
const PARAM_DEPRECATED
Definition: ApiBase.php:101
ApiBase\PARAM_MIN
const PARAM_MIN
Definition: ApiBase.php:93
ApiQueryBase
This is a base class for all Query modules.
Definition: ApiQueryBase.php:37
ApiBase\LIMIT_BIG1
const LIMIT_BIG1
Fast query, standard limit.
Definition: ApiBase.php:220
ApiQueryBase\getDB
getDB()
Get the Query database connection (read-only)
Definition: ApiQueryBase.php:117
ApiBase\PARAM_MAX
const PARAM_MAX
Definition: ApiBase.php:85
ApiQueryBase\addTables
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
Definition: ApiQueryBase.php:182
ApiQueryBase\select
select( $method, $extraQuery=[], array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
Definition: ApiQueryBase.php:399
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:764
$title
$title
Definition: testCompression.php:38
PROTO_CURRENT
const PROTO_CURRENT
Definition: Defines.php:195
ApiBase\requireMaxOneParameter
requireMaxOneParameter( $params,... $required)
Die if more than one of a certain set of parameters is set and not false.
Definition: ApiBase.php:936
ApiBase\dieContinueUsageIf
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition: ApiBase.php:1626
ApiBase\encodeParamName
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition: ApiBase.php:742
ApiQueryBase\addWhereFld
addWhereFld( $field, $value)
Equivalent to addWhere( [ $field => $value ] )
Definition: ApiQueryBase.php:282
ApiQueryBase\getPageSet
getPageSet()
Get the PageSet object to work on.
Definition: ApiQueryBase.php:143
ApiBase\LIMIT_BIG2
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition: ApiBase.php:222
ApiBase\PARAM_DFLT
const PARAM_DFLT
Definition: ApiBase.php:73
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
Definition: ApiBase.php:77
ApiBase\PARAM_MAX2
const PARAM_MAX2
Definition: ApiBase.php:89
ApiQueryBase\addWhere
addWhere( $value)
Add a set of WHERE clauses to the internal array.
Definition: ApiQueryBase.php:245
ApiQueryBase\setContinueEnumParameter
setContinueEnumParameter( $paramName, $paramValue)
Set a query-continue value.
Definition: ApiQueryBase.php:515
ApiBase\PARAM_HELP_MSG_PER_VALUE
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, this is an array mapping those values to $msg...
Definition: ApiBase.php:195
ApiQueryBase\addPageSubItem
addPageSubItem( $pageId, $item, $elemname=null)
Same as addPageSubItems(), but one element of $data at a time.
Definition: ApiQueryBase.php:494
Language
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition: Language.php:42
wfExpandUrl
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
Definition: GlobalFunctions.php:474