MediaWiki  master
LanguageLinksHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Rest\Handler;
4 
12 use TitleFormatter;
13 use TitleParser;
19 
27 
29  private $loadBalancer;
30 
33 
35  private $titleFormatter;
36 
38  private $titleParser;
39 
41  private $pageLookup;
42 
46  private $page = false;
47 
55  public function __construct(
61  ) {
62  $this->loadBalancer = $loadBalancer;
63  $this->languageNameUtils = $languageNameUtils;
64  $this->titleFormatter = $titleFormatter;
65  $this->titleParser = $titleParser;
66  $this->pageLookup = $pageLookup;
67  }
68 
72  private function getPage(): ?ExistingPageRecord {
73  if ( $this->page === false ) {
74  $this->page = $this->pageLookup->getExistingPageByText(
75  $this->getValidatedParams()['title']
76  );
77  }
78  return $this->page;
79  }
80 
86  public function run( $title ) {
87  $page = $this->getPage();
88  if ( !$page ) {
89  throw new LocalizedHttpException(
90  new MessageValue( 'rest-nonexistent-title',
91  [ new ScalarParam( ParamType::PLAINTEXT, $title ) ]
92  ),
93  404
94  );
95  }
96  if ( !$this->getAuthority()->authorizeRead( 'read', $page ) ) {
97  throw new LocalizedHttpException(
98  new MessageValue( 'rest-permission-denied-title',
99  [ new ScalarParam( ParamType::PLAINTEXT, $title ) ] ),
100  403
101  );
102  }
103 
104  return $this->getResponseFactory()
105  ->createJson( $this->fetchLinks( $page->getId() ) );
106  }
107 
108  private function fetchLinks( $pageId ) {
109  $result = [];
110  $res = $this->loadBalancer->getConnectionRef( DB_REPLICA )
111  ->select(
112  'langlinks',
113  '*',
114  [ 'll_from' => $pageId ],
115  __METHOD__,
116  [ 'ORDER BY' => 'll_lang' ]
117  );
118  foreach ( $res as $item ) {
119  try {
120  $targetTitle = $this->titleParser->parseTitle( $item->ll_title );
121  $result[] = [
122  'code' => $item->ll_lang,
123  'name' => $this->languageNameUtils->getLanguageName( $item->ll_lang ),
124  'key' => $this->titleFormatter->getPrefixedDBkey( $targetTitle ),
125  'title' => $this->titleFormatter->getPrefixedText( $targetTitle )
126  ];
127  } catch ( MalformedTitleException $e ) {
128  // skip malformed titles
129  }
130  }
131  return $result;
132  }
133 
134  public function needsWriteAccess() {
135  return false;
136  }
137 
138  public function getParamSettings() {
139  return [
140  'title' => [
141  self::PARAM_SOURCE => 'path',
142  ParamValidator::PARAM_TYPE => 'string',
143  ParamValidator::PARAM_REQUIRED => true,
144  ],
145  ];
146  }
147 
151  protected function getETag(): ?string {
152  $page = $this->getPage();
153  if ( !$page ) {
154  return null;
155  }
156 
157  // XXX: use hash of the rendered HTML?
158  return '"' . $page->getLatest() . '@' . wfTimestamp( TS_MW, $page->getTouched() ) . '"';
159  }
160 
164  protected function getLastModified(): ?string {
165  $page = $this->getPage();
166  if ( !$page ) {
167  return null;
168  }
169 
170  return $page->getTouched();
171  }
172 
176  protected function hasRepresentation() {
177  return (bool)$this->getPage();
178  }
179 
180 }
MediaWiki\Rest\Handler
Definition: AbstractContributionHandler.php:3
MediaWiki\Rest\Handler\LanguageLinksHandler\getParamSettings
getParamSettings()
Fetch ParamValidator settings for parameters.
Definition: LanguageLinksHandler.php:138
MediaWiki\Rest\Handler\LanguageLinksHandler\$titleParser
TitleParser $titleParser
Definition: LanguageLinksHandler.php:38
MediaWiki\Rest\Handler\getResponseFactory
getResponseFactory()
Get the ResponseFactory which can be used to generate Response objects.
Definition: Handler.php:170
Page\PageRecord\getLatest
getLatest( $wikiId=self::LOCAL)
The ID of the page's latest revision.
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1668
MediaWiki\Rest\Handler\LanguageLinksHandler\$page
ExistingPageRecord false null $page
Definition: LanguageLinksHandler.php:46
MediaWiki\Rest\Handler\LanguageLinksHandler\fetchLinks
fetchLinks( $pageId)
Definition: LanguageLinksHandler.php:108
Wikimedia\Message\ScalarParam
Value object representing a message parameter holding a single value.
Definition: ScalarParam.php:12
$res
$res
Definition: testCompression.php:57
MediaWiki\Rest\Handler\LanguageLinksHandler\getETag
getETag()
Definition: LanguageLinksHandler.php:151
MediaWiki\Rest\Handler\LanguageLinksHandler\needsWriteAccess
needsWriteAccess()
Indicates whether this route requires write access.
Definition: LanguageLinksHandler.php:134
Wikimedia\Message\MessageValue
Value object representing a message for i18n.
Definition: MessageValue.php:16
MediaWiki\Languages\LanguageNameUtils
A service that provides utilities to do with language names and codes.
Definition: LanguageNameUtils.php:42
Page\ProperPageIdentity\getId
getId( $wikiId=self::LOCAL)
Returns the page ID.
MediaWiki\Rest\Handler\LanguageLinksHandler\$loadBalancer
ILoadBalancer $loadBalancer
Definition: LanguageLinksHandler.php:29
MediaWiki\Rest\Handler\LanguageLinksHandler
Definition: LanguageLinksHandler.php:26
MediaWiki\Rest\Response
Definition: Response.php:8
Page\PageRecord\getTouched
getTouched()
Timestamp at which the page was last flagged for rerendering.
MediaWiki\Rest\Handler\LanguageLinksHandler\$pageLookup
PageLookup $pageLookup
Definition: LanguageLinksHandler.php:41
$title
$title
Definition: testCompression.php:38
MediaWiki\Rest\Handler\LanguageLinksHandler\getPage
getPage()
Definition: LanguageLinksHandler.php:72
TitleParser
A title parser service for MediaWiki.
Definition: TitleParser.php:33
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
MediaWiki\Rest\Handler\getValidatedParams
getValidatedParams()
Fetch the validated parameters.
Definition: Handler.php:282
MediaWiki\Rest\Handler\LanguageLinksHandler\getLastModified
getLastModified()
Definition: LanguageLinksHandler.php:164
MediaWiki\Rest\Handler\LanguageLinksHandler\hasRepresentation
hasRepresentation()
Definition: LanguageLinksHandler.php:176
Page\ExistingPageRecord
Data record representing a page that currently exists as an editable page on a wiki.
Definition: ExistingPageRecord.php:15
Page\PageLookup
Service interface for looking up infermation about wiki pages.
Definition: PageLookup.php:14
MalformedTitleException
MalformedTitleException is thrown when a TitleParser is unable to parse a title string.
Definition: MalformedTitleException.php:26
MediaWiki\Rest\Handler\LanguageLinksHandler\$titleFormatter
TitleFormatter $titleFormatter
Definition: LanguageLinksHandler.php:35
TitleFormatter
A title formatter service for MediaWiki.
Definition: TitleFormatter.php:35
MediaWiki\Rest\Handler\LanguageLinksHandler\__construct
__construct(ILoadBalancer $loadBalancer, LanguageNameUtils $languageNameUtils, TitleFormatter $titleFormatter, TitleParser $titleParser, PageLookup $pageLookup)
Definition: LanguageLinksHandler.php:55
MediaWiki\Rest\Handler\LanguageLinksHandler\$languageNameUtils
LanguageNameUtils $languageNameUtils
Definition: LanguageLinksHandler.php:32
MediaWiki\Rest\Handler\getAuthority
getAuthority()
Get the current acting authority.
Definition: Handler.php:148
Wikimedia\ParamValidator\ParamValidator
Service for formatting and validating API parameters.
Definition: ParamValidator.php:42
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:81
MediaWiki\Rest\Handler\LanguageLinksHandler\run
run( $title)
Definition: LanguageLinksHandler.php:86
MediaWiki\Rest\LocalizedHttpException
@newable
Definition: LocalizedHttpException.php:10
Wikimedia\Message\ParamType
The constants used to specify parameter types.
Definition: ParamType.php:11
MediaWiki\Rest\SimpleHandler
Definition: SimpleHandler.php:15