MediaWiki  master
LanguageLinksHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Rest\Handler;
4 
11 use RequestContext;
12 use Title;
13 use TitleFormatter;
14 use TitleParser;
15 use User;
21 
29 
31  private $loadBalancer;
32 
35 
38 
40  private $titleFormatter;
41 
43  private $titleParser;
44 
46  private $user;
47 
51  private $title = null;
52 
60  public function __construct(
66  ) {
67  $this->loadBalancer = $loadBalancer;
68  $this->languageNameUtils = $languageNameUtils;
69  $this->permissionManager = $permissionManager;
70  $this->titleFormatter = $titleFormatter;
71  $this->titleParser = $titleParser;
72 
73  // @todo Inject this, when there is a good way to do that
74  $this->user = RequestContext::getMain()->getUser();
75  }
76 
80  private function getTitle() {
81  if ( $this->title === null ) {
82  $this->title = Title::newFromText( $this->getValidatedParams()['title'] ) ?? false;
83  }
84  return $this->title;
85  }
86 
92  public function run( $title ) {
93  $titleObj = $this->getTitle();
94  if ( !$titleObj || !$titleObj->getArticleID() ) {
95  throw new LocalizedHttpException(
96  new MessageValue( 'rest-nonexistent-title',
97  [ new ScalarParam( ParamType::PLAINTEXT, $title ) ]
98  ),
99  404
100  );
101  }
102  if ( !$this->permissionManager->userCan( 'read', $this->user, $titleObj ) ) {
103  throw new LocalizedHttpException(
104  new MessageValue( 'rest-permission-denied-title',
105  [ new ScalarParam( ParamType::PLAINTEXT, $title ) ] ),
106  403
107  );
108  }
109 
110  return $this->getResponseFactory()
111  ->createJson( $this->fetchLinks( $titleObj->getArticleID() ) );
112  }
113 
114  private function fetchLinks( $pageId ) {
115  $result = [];
116  $res = $this->loadBalancer->getConnectionRef( DB_REPLICA )
117  ->select(
118  'langlinks',
119  '*',
120  [ 'll_from' => $pageId ],
121  __METHOD__,
122  [ 'ORDER BY' => 'll_lang' ]
123  );
124  foreach ( $res as $item ) {
125  try {
126  $targetTitle = $this->titleParser->parseTitle( $item->ll_title );
127  $result[] = [
128  'code' => $item->ll_lang,
129  'name' => $this->languageNameUtils->getLanguageName( $item->ll_lang ),
130  'key' => $this->titleFormatter->getPrefixedDBkey( $targetTitle ),
131  'title' => $this->titleFormatter->getPrefixedText( $targetTitle )
132  ];
133  } catch ( MalformedTitleException $e ) {
134  // skip malformed titles
135  }
136  }
137  return $result;
138  }
139 
140  public function needsWriteAccess() {
141  return false;
142  }
143 
144  public function getParamSettings() {
145  return [
146  'title' => [
147  self::PARAM_SOURCE => 'path',
148  ParamValidator::PARAM_TYPE => 'string',
149  ParamValidator::PARAM_REQUIRED => true,
150  ],
151  ];
152  }
153 
158  protected function getETag(): ?string {
159  $title = $this->getTitle();
160  if ( !$title || !$title->getArticleID() ) {
161  return null;
162  }
163 
164  // XXX: use hash of the rendered HTML?
165  return '"' . $title->getLatestRevID() . '@' . wfTimestamp( TS_MW, $title->getTouched() ) . '"';
166  }
167 
172  protected function getLastModified(): ?string {
173  $title = $this->getTitle();
174  if ( !$title || !$title->getArticleID() ) {
175  return null;
176  }
177 
178  return $title->getTouched();
179  }
180 
184  protected function hasRepresentation() {
185  $title = $this->getTitle();
186  return $title ? $title->exists() : false;
187  }
188 
189 }
MediaWiki\Rest\Handler
Definition: AbstractContributionHandler.php:3
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:329
MediaWiki\Rest\Handler\LanguageLinksHandler\getParamSettings
getParamSettings()
Fetch ParamValidator settings for parameters.
Definition: LanguageLinksHandler.php:144
MediaWiki\Rest\Handler\LanguageLinksHandler\__construct
__construct(ILoadBalancer $loadBalancer, LanguageNameUtils $languageNameUtils, PermissionManager $permissionManager, TitleFormatter $titleFormatter, TitleParser $titleParser)
Definition: LanguageLinksHandler.php:60
MediaWiki\Rest\Handler\LanguageLinksHandler\$titleParser
TitleParser $titleParser
Definition: LanguageLinksHandler.php:43
MediaWiki\Rest\Handler\getResponseFactory
getResponseFactory()
Get the ResponseFactory which can be used to generate Response objects.
Definition: Handler.php:151
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1808
MediaWiki\Rest\Handler\LanguageLinksHandler\fetchLinks
fetchLinks( $pageId)
Definition: LanguageLinksHandler.php:114
Title\getArticleID
getArticleID( $flags=0)
Get the article ID for this Title from the link cache, adding it if necessary.
Definition: Title.php:3216
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:158
MediaWiki\Rest\Handler\LanguageLinksHandler\needsWriteAccess
needsWriteAccess()
Indicates whether this route requires write access.
Definition: LanguageLinksHandler.php:140
Wikimedia\Message\MessageValue
Value object representing a message for i18n.
Definition: MessageValue.php:16
MediaWiki\Rest\Handler\LanguageLinksHandler\$permissionManager
PermissionManager $permissionManager
Definition: LanguageLinksHandler.php:37
MediaWiki\Languages\LanguageNameUtils
A service that provides utilities to do with language names and codes.
Definition: LanguageNameUtils.php:42
MediaWiki\Rest\Handler\LanguageLinksHandler\$loadBalancer
ILoadBalancer $loadBalancer
Definition: LanguageLinksHandler.php:31
MediaWiki\Rest\Handler\LanguageLinksHandler
Definition: LanguageLinksHandler.php:28
MediaWiki\Rest\Handler\LanguageLinksHandler\$title
Title bool null $title
Definition: LanguageLinksHandler.php:51
MediaWiki\Rest\Response
Definition: Response.php:8
TitleParser
A title parser service for MediaWiki.
Definition: TitleParser.php:33
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
RequestContext
Group all the pieces relevant to the context of a request into one instance @newable.
Definition: RequestContext.php:38
MediaWiki\Rest\Handler\getValidatedParams
getValidatedParams()
Fetch the validated parameters.
Definition: Handler.php:257
MediaWiki\Rest\Handler\LanguageLinksHandler\getLastModified
getLastModified()
Definition: LanguageLinksHandler.php:172
MediaWiki\Rest\Handler\LanguageLinksHandler\hasRepresentation
hasRepresentation()
Definition: LanguageLinksHandler.php:184
MediaWiki\Rest\Handler\LanguageLinksHandler\getTitle
getTitle()
Definition: LanguageLinksHandler.php:80
MediaWiki\Permissions\PermissionManager
A service class for checking permissions To obtain an instance, use MediaWikiServices::getInstance()-...
Definition: PermissionManager.php:49
Title\getLatestRevID
getLatestRevID( $flags=0)
What is the page_latest field for this page?
Definition: Title.php:3302
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:453
MediaWiki\Rest\Handler\LanguageLinksHandler\$user
User $user
Definition: LanguageLinksHandler.php:46
Title
Represents a title within MediaWiki.
Definition: Title.php:42
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:40
TitleFormatter
A title formatter service for MediaWiki.
Definition: TitleFormatter.php:34
Title\getTouched
getTouched( $db=null)
Get the last touched timestamp.
Definition: Title.php:4234
Title\exists
exists( $flags=0)
Check if page exists.
Definition: Title.php:4003
MediaWiki\Rest\Handler\LanguageLinksHandler\$languageNameUtils
LanguageNameUtils $languageNameUtils
Definition: LanguageLinksHandler.php:34
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:55
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:92
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