| 1 |
<?php |
| 2 |
|
| 3 |
" |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Api; |
| 24 |
|
| 25 |
use MediaWiki\Language\Language; |
| 26 |
use MediaWiki\Languages\LanguageNameUtils; |
| 27 |
use MediaWiki\MainConfigNames; |
| 28 |
use MediaWiki\Title\Title; |
| 29 |
use MediaWiki\Utils\UrlUtils; |
| 30 |
use Wikimedia\ParamValidator\ParamValidator; |
| 31 |
use Wikimedia\ParamValidator\TypeDef\IntegerDef; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class ApiQueryLangLinks extends ApiQueryBase { |
| 39 |
|
| 40 |
private LanguageNameUtils $languageNameUtils; |
| 41 |
private Language $contentLanguage; |
| 42 |
private UrlUtils $urlUtils; |
| 43 |
|
| 44 |
public function __construct( |
| 45 |
ApiQuery $query, |
| 46 |
string $moduleName, |
| 47 |
LanguageNameUtils $languageNameUtils, |
| 48 |
Language $contentLanguage, |
| 49 |
UrlUtils $urlUtils |
| 50 |
) { |
| 51 |
parent::__construct( $query, $moduleName, 'll' ); |
| 52 |
$this->languageNameUtils = $languageNameUtils; |
| 53 |
$this->contentLanguage = $contentLanguage; |
| 54 |
$this->urlUtils = $urlUtils; |
| 55 |
} |
| 56 |
|
| 57 |
public function execute() { |
| 58 |
$pages = $this->getPageSet()->getGoodPages(); |
| 59 |
if ( $pages === [] ) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
$params = $this->extractRequestParams(); |
| 64 |
$prop = array_fill_keys( (array)$params['prop'], true ); |
| 65 |
|
| 66 |
if ( isset( $params['title'] ) && !isset( $params['lang'] ) ) { |
| 67 |
$this->dieWithError( |
| 68 |
[ |
| 69 |
'apierror-invalidparammix-mustusewith', |
| 70 |
$this->encodeParamName( 'title' ), |
| 71 |
$this->encodeParamName( 'lang' ), |
| 72 |
], |
| 73 |
'invalidparammix' |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
$this->requireMaxOneParameter( $params, 'url', 'prop' ); |
| 79 |
if ( $params['url'] ) { |
| 80 |
$prop = [ 'url' => 1 ]; |
| 81 |
} |
| 82 |
|
| 83 |
$this->addFields( [ |
| 84 |
'll_from', |
| 85 |
'll_lang', |
| 86 |
'll_title' |
| 87 |
] ); |
| 88 |
|
| 89 |
$this->addTables( 'langlinks' ); |
| 90 |
$this->addWhereFld( 'll_from', array_keys( $pages ) ); |
| 91 |
if ( $params['continue'] !== null ) { |
| 92 |
$db = $this->getDB(); |
| 93 |
$cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'string' ] ); |
| 94 |
$op = $params['dir'] == 'descending' ? '<=' : '>='; |
| 95 |
$this->addWhere( $db->buildComparison( $op, [ |
| 96 |
'll_from' => $cont[0], |
| 97 |
'll_lang' => $cont[1], |
| 98 |
] ) ); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
$sort = ( $params['dir'] == 'descending' ? ' DESC' : '' ); |
| 108 |
if ( isset( $params['lang'] ) ) { |
| 109 |
$this->addWhereFld( 'll_lang', $params['lang'] ); |
| 110 |
if ( isset( $params['title'] ) ) { |
| 111 |
$this->addWhereFld( 'll_title', $params['title'] ); |
| 112 |
} |
| 113 |
$this->addOption( 'ORDER BY', 'll_from' . $sort ); |
| 114 |
} else { |
| 115 |
|
| 116 |
if ( count( $pages ) === 1 ) { |
| 117 |
$this->addOption( 'ORDER BY', 'll_lang' . $sort ); |
| 118 |
} else { |
| 119 |
$this->addOption( 'ORDER BY', [ |
| 120 |
'll_from' . $sort, |
| 121 |
'll_lang' . $sort |
| 122 |
] ); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
$this->addOption( 'LIMIT', $params['limit'] + 1 ); |
| 127 |
$res = $this->select( __METHOD__ ); |
| 128 |
|
| 129 |
$count = 0; |
| 130 |
foreach ( $res as $row ) { |
| 131 |
if ( ++$count > $params['limit'] ) { |
| 132 |
|
| 133 |
|
| 134 |
$this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" ); |
| 135 |
break; |
| 136 |
} |
| 137 |
|
| 138 |
$languageNameMap = $this->getConfig()->get( MainConfigNames::InterlanguageLinkCodeMap ); |
| 139 |
$displayLanguageCode = $languageNameMap[ $row->ll_lang ] ?? $row->ll_lang; |
| 140 |
|
| 141 |
|
| 142 |
$entry = [ 'lang' => $displayLanguageCode ]; |
| 143 |
if ( isset( $prop['url'] ) ) { |
| 144 |
$title = Title::newFromText( "{$row->ll_lang}:{$row->ll_title}" ); |
| 145 |
if ( $title ) { |
| 146 |
$entry['url'] = (string)$this->urlUtils->expand( $title->getFullURL(), PROTO_CURRENT ); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
if ( isset( $prop['langname'] ) ) { |
| 151 |
$entry['langname'] = $this->languageNameUtils |
| 152 |
->getLanguageName( $displayLanguageCode, $params['inlanguagecode'] ); |
| 153 |
} |
| 154 |
if ( isset( $prop['autonym'] ) ) { |
| 155 |
$entry['autonym'] = $this->languageNameUtils->getLanguageName( $displayLanguageCode ); |
| 156 |
} |
| 157 |
ApiResult::setContentValue( $entry, 'title', $row->ll_title ); |
| 158 |
$fit = $this->addPageSubItem( $row->ll_from, $entry ); |
| 159 |
if ( !$fit ) { |
| 160 |
$this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" ); |
| 161 |
break; |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
public function getCacheMode( $params ) { |
| 167 |
return 'public'; |
| 168 |
} |
| 169 |
|
| 170 |
public function getAllowedParams() { |
| 171 |
return [ |
| 172 |
'prop' => [ |
| 173 |
ParamValidator::PARAM_ISMULTI => true, |
| 174 |
ParamValidator::PARAM_TYPE => [ |
| 175 |
'url', |
| 176 |
'langname', |
| 177 |
'autonym', |
| 178 |
], |
| 179 |
ApiBase::PARAM_HELP_MSG_PER_VALUE => [], |
| 180 |
], |
| 181 |
'lang' => null, |
| 182 |
'title' => null, |
| 183 |
'dir' => [ |
| 184 |
ParamValidator::PARAM_DEFAULT => 'ascending', |
| 185 |
ParamValidator::PARAM_TYPE => [ |
| 186 |
'ascending', |
| 187 |
'descending' |
| 188 |
] |
| 189 |
], |
| 190 |
'inlanguagecode' => $this->contentLanguage->getCode(), |
| 191 |
'limit' => [ |
| 192 |
ParamValidator::PARAM_DEFAULT => 10, |
| 193 |
ParamValidator::PARAM_TYPE => 'limit', |
| 194 |
IntegerDef::PARAM_MIN => 1, |
| 195 |
IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1, |
| 196 |
IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2 |
| 197 |
], |
| 198 |
'continue' => [ |
| 199 |
ApiBase::PARAM_HELP_MSG => 'api-help-param-continue', |
| 200 |
], |
| 201 |
'url' => [ |
| 202 |
ParamValidator::PARAM_DEFAULT => false, |
| 203 |
ParamValidator::PARAM_DEPRECATED => true, |
| 204 |
], |
| 205 |
]; |
| 206 |
} |
| 207 |
|
| 208 |
protected function getExamplesMessages() { |
| 209 |
$title = Title::newMainPage()->getPrefixedText(); |
| 210 |
$mp = rawurlencode( $title ); |
| 211 |
|
| 212 |
return [ |
| 213 |
"action=query&prop=langlinks&titles={$mp}&redirects=" |
| 214 |
=> 'apihelp-query+langlinks-example-simple', |
| 215 |
]; |
| 216 |
} |
| 217 |
|
| 218 |
public function getHelpUrls() { |
| 219 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Langlinks'; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
class_alias( ApiQueryLangLinks::class, 'ApiQueryLangLinks' ); |