| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Rest\Handler; |
| 4 |
|
| 5 |
use MediaWiki\Languages\LanguageNameUtils; |
| 6 |
use MediaWiki\Page\ExistingPageRecord; |
| 7 |
use MediaWiki\Page\PageLookup; |
| 8 |
use MediaWiki\Rest\Handler\Helper\PageRedirectHelper; |
| 9 |
use MediaWiki\Rest\Handler\Helper\PageRestHelperFactory; |
| 10 |
use MediaWiki\Rest\LocalizedHttpException; |
| 11 |
use MediaWiki\Rest\Response; |
| 12 |
use MediaWiki\Rest\SimpleHandler; |
| 13 |
use MediaWiki\Title\MalformedTitleException; |
| 14 |
use MediaWiki\Title\TitleFormatter; |
| 15 |
use MediaWiki\Title\TitleParser; |
| 16 |
use Wikimedia\Message\MessageValue; |
| 17 |
use Wikimedia\Message\ParamType; |
| 18 |
use Wikimedia\Message\ScalarParam; |
| 19 |
use Wikimedia\ParamValidator\ParamValidator; |
| 20 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
class LanguageLinksHandler extends SimpleHandler { |
| 29 |
|
| 30 |
private IConnectionProvider $dbProvider; |
| 31 |
private LanguageNameUtils $languageNameUtils; |
| 32 |
private TitleFormatter $titleFormatter; |
| 33 |
private TitleParser $titleParser; |
| 34 |
private PageLookup $pageLookup; |
| 35 |
private PageRestHelperFactory $helperFactory; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
private $page = false; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public function __construct( |
| 51 |
IConnectionProvider $dbProvider, |
| 52 |
LanguageNameUtils $languageNameUtils, |
| 53 |
TitleFormatter $titleFormatter, |
| 54 |
TitleParser $titleParser, |
| 55 |
PageLookup $pageLookup, |
| 56 |
PageRestHelperFactory $helperFactory |
| 57 |
) { |
| 58 |
$this->dbProvider = $dbProvider; |
| 59 |
$this->languageNameUtils = $languageNameUtils; |
| 60 |
$this->titleFormatter = $titleFormatter; |
| 61 |
$this->titleParser = $titleParser; |
| 62 |
$this->pageLookup = $pageLookup; |
| 63 |
$this->helperFactory = $helperFactory; |
| 64 |
} |
| 65 |
|
| 66 |
private function getRedirectHelper(): PageRedirectHelper { |
| 67 |
return $this->helperFactory->newPageRedirectHelper( |
| 68 |
$this->getResponseFactory(), |
| 69 |
$this->getRouter(), |
| 70 |
$this->getPath(), |
| 71 |
$this->getRequest() |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
private function getPage(): ?ExistingPageRecord { |
| 79 |
if ( $this->page === false ) { |
| 80 |
$this->page = $this->pageLookup->getExistingPageByText( |
| 81 |
$this->getValidatedParams()['title'] |
| 82 |
); |
| 83 |
} |
| 84 |
return $this->page; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public function run( $title ) { |
| 93 |
$page = $this->getPage(); |
| 94 |
$params = $this->getValidatedParams(); |
| 95 |
|
| 96 |
if ( !$page ) { |
| 97 |
throw new LocalizedHttpException( |
| 98 |
new MessageValue( 'rest-nonexistent-title', |
| 99 |
[ new ScalarParam( ParamType::PLAINTEXT, $title ) ] |
| 100 |
), |
| 101 |
404 |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
'@phan-var \MediaWiki\Page\ExistingPageRecord $page'; |
| 106 |
$redirectResponse = $this->getRedirectHelper()->createNormalizationRedirectResponseIfNeeded( |
| 107 |
$page, |
| 108 |
$params['title'] ?? null |
| 109 |
); |
| 110 |
|
| 111 |
if ( $redirectResponse !== null ) { |
| 112 |
return $redirectResponse; |
| 113 |
} |
| 114 |
|
| 115 |
if ( !$this->getAuthority()->authorizeRead( 'read', $page ) ) { |
| 116 |
throw new LocalizedHttpException( |
| 117 |
new MessageValue( 'rest-permission-denied-title', |
| 118 |
[ new ScalarParam( ParamType::PLAINTEXT, $title ) ] ), |
| 119 |
403 |
| 120 |
); |
| 121 |
} |
| 122 |
|
| 123 |
return $this->getResponseFactory() |
| 124 |
->createJson( $this->fetchLinks( $page->getId() ) ); |
| 125 |
} |
| 126 |
|
| 127 |
private function fetchLinks( $pageId ) { |
| 128 |
$result = []; |
| 129 |
$res = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder() |
| 130 |
->select( [ 'll_title', 'll_lang' ] ) |
| 131 |
->from( 'langlinks' ) |
| 132 |
->where( [ 'll_from' => $pageId ] ) |
| 133 |
->orderBy( 'll_lang' ) |
| 134 |
->caller( __METHOD__ )->fetchResultSet(); |
| 135 |
foreach ( $res as $item ) { |
| 136 |
try { |
| 137 |
$targetTitle = $this->titleParser->parseTitle( $item->ll_title ); |
| 138 |
$result[] = [ |
| 139 |
'code' => $item->ll_lang, |
| 140 |
'name' => $this->languageNameUtils->getLanguageName( $item->ll_lang ), |
| 141 |
'key' => $this->titleFormatter->getPrefixedDBkey( $targetTitle ), |
| 142 |
'title' => $this->titleFormatter->getPrefixedText( $targetTitle ) |
| 143 |
]; |
| 144 |
} catch ( MalformedTitleException $e ) { |
| 145 |
|
| 146 |
} |
| 147 |
} |
| 148 |
return $result; |
| 149 |
} |
| 150 |
|
| 151 |
public function needsWriteAccess() { |
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
public function getParamSettings() { |
| 156 |
return [ |
| 157 |
'title' => [ |
| 158 |
self::PARAM_SOURCE => 'path', |
| 159 |
ParamValidator::PARAM_TYPE => 'string', |
| 160 |
ParamValidator::PARAM_REQUIRED => true, |
| 161 |
], |
| 162 |
]; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
protected function getETag(): ?string { |
| 169 |
$page = $this->getPage(); |
| 170 |
if ( !$page ) { |
| 171 |
return null; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
return '"' . $page->getLatest() . '@' . wfTimestamp( TS_MW, $page->getTouched() ) . '"'; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
protected function getLastModified(): ?string { |
| 182 |
$page = $this->getPage(); |
| 183 |
return $page ? $page->getTouched() : null; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
protected function hasRepresentation() { |
| 190 |
return (bool)$this->getPage(); |
| 191 |
} |
| 192 |
|
| 193 |
public function getResponseBodySchemaFileName( string $method ): ?string { |
| 194 |
return 'includes/Rest/Handler/Schema/PageLanguageLinks.json'; |
| 195 |
} |
| 196 |
} |