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