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 
55  public function __construct(
61  ) {
62  $this->loadBalancer = $loadBalancer;
63  $this->languageNameUtils = $languageNameUtils;
64  $this->permissionManager = $permissionManager;
65  $this->titleFormatter = $titleFormatter;
66  $this->titleParser = $titleParser;
67 
68  // @todo Inject this, when there is a good way to do that
69  $this->user = RequestContext::getMain()->getUser();
70  }
71 
77  public function run( $title ) {
78  $titleObj = Title::newFromText( $title );
79  if ( !$titleObj || !$titleObj->getArticleID() ) {
80  throw new LocalizedHttpException(
81  new MessageValue( 'rest-nonexistent-title',
82  [ new ScalarParam( ParamType::PLAINTEXT, $title ) ]
83  ),
84  404
85  );
86  }
87  if ( !$this->permissionManager->userCan( 'read', $this->user, $titleObj ) ) {
88  throw new LocalizedHttpException(
89  new MessageValue( 'rest-permission-denied-title',
90  [ new ScalarParam( ParamType::PLAINTEXT, $title ) ] ),
91  403
92  );
93  }
94 
95  return $this->getResponseFactory()
96  ->createJson( $this->fetchLinks( $titleObj->getArticleID() ) );
97  }
98 
99  private function fetchLinks( $pageId ) {
100  $result = [];
101  $res = $this->loadBalancer->getConnectionRef( DB_REPLICA )
102  ->select(
103  'langlinks',
104  '*',
105  [ 'll_from' => $pageId ],
106  __METHOD__,
107  [ 'ORDER BY' => 'll_lang' ]
108  );
109  foreach ( $res as $item ) {
110  try {
111  $targetTitle = $this->titleParser->parseTitle( $item->ll_title );
112  $result[] = [
113  'code' => $item->ll_lang,
114  'name' => $this->languageNameUtils->getLanguageName( $item->ll_lang ),
115  'key' => $this->titleFormatter->getPrefixedDBkey( $targetTitle ),
116  'title' => $this->titleFormatter->getPrefixedText( $targetTitle )
117  ];
118  } catch ( MalformedTitleException $e ) {
119  // skip malformed titles
120  }
121  }
122  return $result;
123  }
124 
125  public function needsWriteAccess() {
126  return false;
127  }
128 
129  public function getParamSettings() {
130  return [
131  'title' => [
132  self::PARAM_SOURCE => 'path',
133  ParamValidator::PARAM_TYPE => 'string',
134  ParamValidator::PARAM_REQUIRED => true,
135  ],
136  ];
137  }
138 }
MediaWiki\Rest\Handler
Definition: CompareHandler.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:335
MediaWiki\Rest\Handler\LanguageLinksHandler\getParamSettings
getParamSettings()
Fetch ParamValidator settings for parameters.
Definition: LanguageLinksHandler.php:129
MediaWiki\Rest\Handler\LanguageLinksHandler\__construct
__construct(ILoadBalancer $loadBalancer, LanguageNameUtils $languageNameUtils, PermissionManager $permissionManager, TitleFormatter $titleFormatter, TitleParser $titleParser)
Definition: LanguageLinksHandler.php:55
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:92
MediaWiki\Rest\Handler\LanguageLinksHandler\fetchLinks
fetchLinks( $pageId)
Definition: LanguageLinksHandler.php:99
Wikimedia\Message\ScalarParam
Value object representing a message parameter holding a single value.
Definition: ScalarParam.php:10
$res
$res
Definition: testCompression.php:54
MediaWiki\Rest\Handler\LanguageLinksHandler\needsWriteAccess
needsWriteAccess()
Indicates whether this route requires write access.
Definition: LanguageLinksHandler.php:125
Wikimedia\Message\MessageValue
Value object representing a message for i18n.
Definition: MessageValue.php:14
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:41
MediaWiki\Rest\Handler\LanguageLinksHandler\$loadBalancer
ILoadBalancer $loadBalancer
Definition: LanguageLinksHandler.php:31
MediaWiki\Rest\Handler\LanguageLinksHandler
Definition: LanguageLinksHandler.php:28
MediaWiki\Rest\Response
Definition: Response.php:8
$title
$title
Definition: testCompression.php:36
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.
Definition: RequestContext.php:34
MediaWiki\Permissions\PermissionManager
A service class for checking permissions To obtain an instance, use MediaWikiServices::getInstance()-...
Definition: PermissionManager.php:48
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:451
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:25
MediaWiki\Rest\Handler\LanguageLinksHandler\$titleFormatter
TitleFormatter $titleFormatter
Definition: LanguageLinksHandler.php:40
TitleFormatter
A title formatter service for MediaWiki.
Definition: TitleFormatter.php:34
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:52
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:77
MediaWiki\Rest\LocalizedHttpException
Definition: LocalizedHttpException.php:7
Wikimedia\Message\ParamType
The constants used to specify parameter types.
Definition: ParamType.php:11
MediaWiki\Rest\SimpleHandler
Definition: SimpleHandler.php:14