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 
32  private $languageNameUtils;
33 
35  private $titleFormatter;
36 
38  private $titleParser;
39 
41  private $pageLookup;
42 
46  private $page = false;
47 
55  public function __construct(
56  ILoadBalancer $loadBalancer,
57  LanguageNameUtils $languageNameUtils,
58  TitleFormatter $titleFormatter,
59  TitleParser $titleParser,
60  PageLookup $pageLookup
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',
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 }
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
if(!defined('MW_SETUP_CALLBACK'))
The persistent session ID (if any) loaded at startup.
Definition: WebStart.php:82
MalformedTitleException is thrown when a TitleParser is unable to parse a title string.
A service that provides utilities to do with language names and codes.
__construct(ILoadBalancer $loadBalancer, LanguageNameUtils $languageNameUtils, TitleFormatter $titleFormatter, TitleParser $titleParser, PageLookup $pageLookup)
getParamSettings()
Fetch ParamValidator settings for parameters.
needsWriteAccess()
Indicates whether this route requires write access.
getValidatedParams()
Fetch the validated parameters.
Definition: Handler.php:358
getAuthority()
Get the current acting authority.
Definition: Handler.php:157
getResponseFactory()
Get the ResponseFactory which can be used to generate Response objects.
Definition: Handler.php:179
Value object representing a message for i18n.
The constants used to specify parameter types.
Definition: ParamType.php:11
Value object representing a message parameter holding a single value.
Definition: ScalarParam.php:14
Service for formatting and validating API parameters.
const PARAM_TYPE
(string|array) Type of the parameter.
const PARAM_REQUIRED
(bool) Indicate that the parameter is required.
Data record representing a page that currently exists as an editable page on a wiki.
Service for looking up information about wiki pages.
Definition: PageLookup.php:17
A title formatter service for MediaWiki.
A title parser service for MediaWiki.
Definition: TitleParser.php:33
Create and track the database connections and transactions for a given database cluster.
Copyright (C) 2011-2020 Wikimedia Foundation and others.
const DB_REPLICA
Definition: defines.php:26