MediaWiki  1.23.1
ApiQueryLangLinks.php
Go to the documentation of this file.
1 <?php
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  $prop = array_flip( (array)$params['prop'] );
45 
46  if ( isset( $params['title'] ) && !isset( $params['lang'] ) ) {
47  $this->dieUsageMsg( array( 'missingparam', 'lang' ) );
48  }
49 
50  // Handle deprecated param
51  $this->requireMaxOneParameter( $params, 'url', 'prop' );
52  if ( $params['url'] ) {
53  $prop = array( 'url' => 1 );
54  }
55 
56  $this->addFields( array(
57  'll_from',
58  'll_lang',
59  'll_title'
60  ) );
61 
62  $this->addTables( 'langlinks' );
63  $this->addWhereFld( 'll_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
64  if ( !is_null( $params['continue'] ) ) {
65  $cont = explode( '|', $params['continue'] );
66  $this->dieContinueUsageIf( count( $cont ) != 2 );
67  $op = $params['dir'] == 'descending' ? '<' : '>';
68  $llfrom = intval( $cont[0] );
69  $lllang = $this->getDB()->addQuotes( $cont[1] );
70  $this->addWhere(
71  "ll_from $op $llfrom OR " .
72  "(ll_from = $llfrom AND " .
73  "ll_lang $op= $lllang)"
74  );
75  }
76 
77  //FIXME: (follow-up) To allow extensions to add to the language links, we need
78  // to load them all, add the extra links, then apply paging.
79  // Should not be terrible, it's not going to be more than a few hundred links.
80 
81  // Note that, since (ll_from, ll_lang) is a unique key, we don't need
82  // to sort by ll_title to ensure deterministic ordering.
83  $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
84  if ( isset( $params['lang'] ) ) {
85  $this->addWhereFld( 'll_lang', $params['lang'] );
86  if ( isset( $params['title'] ) ) {
87  $this->addWhereFld( 'll_title', $params['title'] );
88  }
89  $this->addOption( 'ORDER BY', 'll_from' . $sort );
90  } else {
91  // Don't order by ll_from if it's constant in the WHERE clause
92  if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
93  $this->addOption( 'ORDER BY', 'll_lang' . $sort );
94  } else {
95  $this->addOption( 'ORDER BY', array(
96  'll_from' . $sort,
97  'll_lang' . $sort
98  ) );
99  }
100  }
101 
102  $this->addOption( 'LIMIT', $params['limit'] + 1 );
103  $res = $this->select( __METHOD__ );
104 
105  $count = 0;
106  foreach ( $res as $row ) {
107  if ( ++$count > $params['limit'] ) {
108  // We've reached the one extra which shows that
109  // there are additional pages to be had. Stop here...
110  $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
111  break;
112  }
113  $entry = array( 'lang' => $row->ll_lang );
114  if ( isset( $prop['url'] ) ) {
115  $title = Title::newFromText( "{$row->ll_lang}:{$row->ll_title}" );
116  if ( $title ) {
117  $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
118  }
119  }
120  if ( isset( $prop['langname'] ) ) {
121  $entry['langname'] = Language::fetchLanguageName( $row->ll_lang, $params['inlanguagecode'] );
122  }
123  if ( isset( $prop['autonym'] ) ) {
124  $entry['autonym'] = Language::fetchLanguageName( $row->ll_lang );
125  }
126  ApiResult::setContent( $entry, $row->ll_title );
127  $fit = $this->addPageSubItem( $row->ll_from, $entry );
128  if ( !$fit ) {
129  $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
130  break;
131  }
132  }
133  }
134 
135  public function getCacheMode( $params ) {
136  return 'public';
137  }
138 
139  public function getAllowedParams() {
141  return array(
142  'limit' => array(
143  ApiBase::PARAM_DFLT => 10,
144  ApiBase::PARAM_TYPE => 'limit',
145  ApiBase::PARAM_MIN => 1,
148  ),
149  'continue' => null,
150  'url' => array(
151  ApiBase::PARAM_DFLT => false,
153  ),
154  'prop' => array(
155  ApiBase::PARAM_ISMULTI => true,
157  'url',
158  'langname',
159  'autonym',
160  )
161  ),
162  'lang' => null,
163  'title' => null,
164  'dir' => array(
165  ApiBase::PARAM_DFLT => 'ascending',
167  'ascending',
168  'descending'
169  )
170  ),
171  'inlanguagecode' => $wgContLang->getCode(),
172  );
173  }
174 
175  public function getParamDescription() {
176  return array(
177  'limit' => 'How many langlinks to return',
178  'continue' => 'When more results are available, use this to continue',
179  'url' => "Whether to get the full URL (Cannot be used with {$this->getModulePrefix()}prop)",
180  'prop' => array(
181  'Which additional properties to get for each interlanguage link',
182  ' url - Adds the full URL',
183  ' langname - Adds the localised language name (best effort, use CLDR extension)',
184  " Use {$this->getModulePrefix()}inlanguagecode to control the language",
185  ' autonym - Adds the native language name',
186  ),
187  'lang' => 'Language code',
188  'title' => "Link to search for. Must be used with {$this->getModulePrefix()}lang",
189  'dir' => 'The direction in which to list',
190  'inlanguagecode' => 'Language code for localised language names',
191  );
192  }
193 
194  public function getResultProperties() {
195  return array(
196  '' => array(
197  'lang' => 'string',
198  'url' => array(
199  ApiBase::PROP_TYPE => 'string',
200  ApiBase::PROP_NULLABLE => true
201  ),
202  'langname' => array(
203  ApiBase::PROP_TYPE => 'string',
204  ApiBase::PROP_NULLABLE => true
205  ),
206  'autonym' => array(
207  ApiBase::PROP_TYPE => 'string',
208  ApiBase::PROP_NULLABLE => true
209  ),
210  '*' => 'string'
211  )
212  );
213  }
214 
215  public function getDescription() {
216  return 'Returns all interlanguage links from the given page(s).';
217  }
218 
219  public function getPossibleErrors() {
220  return array_merge( parent::getPossibleErrors(),
222  array( 'url', 'prop' )
223  ),
224  array(
225  array( 'missingparam', 'lang' ),
226  )
227  );
228  }
229 
230  public function getExamples() {
231  return array(
232  'api.php?action=query&prop=langlinks&titles=Main%20Page&redirects='
233  => 'Get interlanguage links from the [[Main Page]]',
234  );
235  }
236 
237  public function getHelpUrls() {
238  return 'https://www.mediawiki.org/wiki/API:Properties#langlinks_.2F_ll';
239  }
240 }
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:189
ApiQueryBase\addFields
addFields( $value)
Add a set of fields to select to the internal array.
Definition: ApiQueryBase.php:117
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
ApiResult\setContent
static setContent(&$arr, $value, $subElemName=null)
Adds a content element to an array.
Definition: ApiResult.php:201
ApiBase\dieUsageMsg
dieUsageMsg( $error)
Output the error message related to a certain array.
Definition: ApiBase.php:1929
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:50
ApiQueryBase\select
select( $method, $extraQuery=array())
Execute a SELECT query based on the values in the internal arrays.
Definition: ApiQueryBase.php:274
$params
$params
Definition: styleTest.css.php:40
$wgContLang
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the content language as $wgContLang
Definition: design.txt:56
ApiQueryBase\addOption
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
Definition: ApiQueryBase.php:252
ApiBase\PARAM_DEPRECATED
const PARAM_DEPRECATED
Definition: ApiBase.php:60
ApiBase\PARAM_MIN
const PARAM_MIN
Definition: ApiBase.php:56
ApiQueryBase
This is a base class for all Query modules.
Definition: ApiQueryBase.php:34
ApiBase\LIMIT_BIG1
const LIMIT_BIG1
Definition: ApiBase.php:78
ApiQueryBase\getDB
getDB()
Get the Query database connection (read-only)
Definition: ApiQueryBase.php:417
PROTO_CURRENT
const PROTO_CURRENT
Definition: Defines.php:270
ApiBase\PARAM_MAX
const PARAM_MAX
Definition: ApiBase.php:52
ApiQueryBase\addTables
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
Definition: ApiQueryBase.php:82
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
ApiBase\PROP_TYPE
const PROP_TYPE
Definition: ApiBase.php:74
$sort
$sort
Definition: profileinfo.php:301
ApiBase\extractRequestParams
extractRequestParams( $parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:687
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
ApiBase\dieContinueUsageIf
dieContinueUsageIf( $condition)
Die with the $prefix.
Definition: ApiBase.php:1965
ApiBase\PROP_NULLABLE
const PROP_NULLABLE
Definition: ApiBase.php:76
ApiQueryBase\addWhereFld
addWhereFld( $field, $value)
Equivalent to addWhere(array($field => $value))
Definition: ApiQueryBase.php:185
Language\fetchLanguageName
static fetchLanguageName( $code, $inLanguage=null, $include='all')
Definition: Language.php:936
ApiQueryBase\getPageSet
getPageSet()
Get the PageSet object to work on.
Definition: ApiQueryBase.php:441
$count
$count
Definition: UtfNormalTest2.php:96
ApiBase\LIMIT_BIG2
const LIMIT_BIG2
Definition: ApiBase.php:79
ApiBase\PARAM_DFLT
const PARAM_DFLT
Definition: ApiBase.php:46
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
Definition: ApiBase.php:48
ApiBase\PARAM_MAX2
const PARAM_MAX2
Definition: ApiBase.php:54
ApiQueryBase\addWhere
addWhere( $value)
Add a set of WHERE clauses to the internal array.
Definition: ApiQueryBase.php:152
ApiQueryBase\setContinueEnumParameter
setContinueEnumParameter( $paramName, $paramValue)
Set a query-continue value.
Definition: ApiQueryBase.php:404
ApiBase\getRequireMaxOneParameterErrorMessages
getRequireMaxOneParameterErrorMessages( $params)
Generates the possible error requireMaxOneParameter() can die with.
Definition: ApiBase.php:791
$query
return true to allow those checks to and false if checking is done use this to change the tables headers temp or archived zone change it to an object instance and return false override the list derivative used the name of the old file when set the default code will be skipped add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition: hooks.txt:1105
ApiBase\requireMaxOneParameter
requireMaxOneParameter( $params)
Die if more than one of a certain set of parameters is set and not false.
Definition: ApiBase.php:769
$res
$res
Definition: database.txt:21
ApiQueryBase\addPageSubItem
addPageSubItem( $pageId, $item, $elemname=null)
Same as addPageSubItems(), but one element of $data at a time.
Definition: ApiQueryBase.php:383
wfExpandUrl
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
Definition: GlobalFunctions.php:497