MediaWiki REL1_35
LanguageLinksHandler.php
Go to the documentation of this file.
1<?php
2
4
12use Title;
14use TitleParser;
15use User;
21
29
32
35
38
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}
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
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.
A service class for checking permissions To obtain an instance, use MediaWikiServices::getInstance()-...
getParamSettings()
Fetch ParamValidator settings for parameters.
needsWriteAccess()
Indicates whether this route requires write access.
__construct(ILoadBalancer $loadBalancer, LanguageNameUtils $languageNameUtils, PermissionManager $permissionManager, TitleFormatter $titleFormatter, TitleParser $titleParser)
getValidatedParams()
Fetch the validated parameters.
Definition Handler.php:257
getResponseFactory()
Get the ResponseFactory which can be used to generate Response objects.
Definition Handler.php:151
Group all the pieces relevant to the context of a request into one instance @newable.
Represents a title within MediaWiki.
Definition Title.php:42
getLatestRevID( $flags=0)
What is the page_latest field for this page?
Definition Title.php:3311
getTouched( $db=null)
Get the last touched timestamp.
Definition Title.php:4243
getArticleID( $flags=0)
Get the article ID for this Title from the link cache, adding it if necessary.
Definition Title.php:3225
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:60
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.
Service for formatting and validating API parameters.
A title formatter service for MediaWiki.
A title parser service for MediaWiki.
Database cluster connection, tracking, load balancing, and transaction manager interface.
const DB_REPLICA
Definition defines.php:25