MediaWiki REL1_35
MediaLinksHandler.php
Go to the documentation of this file.
1<?php
2
4
5use MediaFileTrait;
10use RepoGroup;
12use Title;
13use User;
17
22 use MediaFileTrait;
23
25 private const MAX_NUM_LINKS = 100;
26
29
32
34 private $repoGroup;
35
37 private $user;
38
42 private $title = null;
43
49 public function __construct(
53 ) {
54 $this->permissionManager = $permissionManager;
55 $this->loadBalancer = $loadBalancer;
56 $this->repoGroup = $repoGroup;
57
58 // @todo Inject this, when there is a good way to do that
59 $this->user = RequestContext::getMain()->getUser();
60 }
61
65 private function getTitle() {
66 if ( $this->title === null ) {
67 $this->title = Title::newFromText( $this->getValidatedParams()['title'] ) ?? false;
68 }
69 return $this->title;
70 }
71
77 public function run( $title ) {
78 $titleObj = Title::newFromText( $title );
79 if ( !$titleObj || !$titleObj->getArticleID() ) {
80 throw new LocalizedHttpException(
81 MessageValue::new( 'rest-nonexistent-title' )->plaintextParams( $title ),
82 404
83 );
84 }
85
86 if ( !$this->permissionManager->userCan( 'read', $this->user, $titleObj ) ) {
87 throw new LocalizedHttpException(
88 MessageValue::new( 'rest-permission-denied-title' )->plaintextParams( $title ),
89 403
90 );
91 }
92
93 // @todo: add continuation if too many links are found
94 $results = $this->getDbResults( $titleObj->getArticleID() );
95 if ( count( $results ) > self::MAX_NUM_LINKS ) {
96 throw new LocalizedHttpException(
97 MessageValue::new( 'rest-media-too-many-links' )
98 ->plaintextParams( $title )
99 ->numParams( self::MAX_NUM_LINKS ),
100 500
101 );
102 }
103 $response = $this->processDbResults( $results );
104 return $this->getResponseFactory()->createJson( $response );
105 }
106
111 private function getDbResults( int $pageId ) {
112 $dbr = $this->loadBalancer->getConnectionRef( DB_REPLICA );
113 return $dbr->selectFieldValues(
114 'imagelinks',
115 'il_to',
116 [ 'il_from' => $pageId ],
117 __METHOD__,
118 [
119 'ORDER BY' => 'il_to',
120 'LIMIT' => self::MAX_NUM_LINKS + 1,
121 ]
122 );
123 }
124
129 private function processDbResults( $results ) {
130 // Using "private" here means an equivalent of the Action API's "anon-public-user-private"
131 // caching model would be necessary, if caching is ever added to this endpoint.
132 $findTitles = array_map( function ( $title ) {
133 return [
134 'title' => $title,
135 'private' => $this->user,
136 ];
137 }, $results );
138
139 $files = $this->repoGroup->findFiles( $findTitles );
140 list( $maxWidth, $maxHeight ) = self::getImageLimitsFromOption( $this->user, 'imagesize' );
141 $transforms = [
142 'preferred' => [
143 'maxWidth' => $maxWidth,
144 'maxHeight' => $maxHeight,
145 ]
146 ];
147 $response = [];
148 foreach ( $files as $file ) {
149 $response[] = $this->getFileInfo( $file, $this->user, $transforms );
150 }
151
152 $response = [
153 'files' => $response
154 ];
155
156 return $response;
157 }
158
159 public function needsWriteAccess() {
160 return false;
161 }
162
163 public function getParamSettings() {
164 return [
165 'title' => [
166 self::PARAM_SOURCE => 'path',
167 ParamValidator::PARAM_TYPE => 'string',
168 ParamValidator::PARAM_REQUIRED => true,
169 ],
170 ];
171 }
172
177 protected function getETag(): ?string {
178 $title = $this->getTitle();
179 if ( !$title || !$title->getArticleID() ) {
180 return null;
181 }
182
183 // XXX: use hash of the rendered HTML?
184 return '"' . $title->getLatestRevID() . '@' . wfTimestamp( TS_MW, $title->getTouched() ) . '"';
185 }
186
191 protected function getLastModified(): ?string {
192 $title = $this->getTitle();
193 if ( !$title || !$title->getArticleID() ) {
194 return null;
195 }
196
197 return $title->getTouched();
198 }
199
203 protected function hasRepresentation() {
204 $title = $this->getTitle();
205 return $title ? $title->exists() : false;
206 }
207}
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
A service class for checking permissions To obtain an instance, use MediaWikiServices::getInstance()-...
Handler class for Core REST API endpoints that perform operations on revisions.
__construct(PermissionManager $permissionManager, ILoadBalancer $loadBalancer, RepoGroup $repoGroup)
const MAX_NUM_LINKS
int The maximum number of media links to return
getParamSettings()
Fetch ParamValidator settings for parameters.
needsWriteAccess()
Indicates whether this route requires write access.
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
Prioritized list of file repositories.
Definition RepoGroup.php:31
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.
Service for formatting and validating API parameters.
Database cluster connection, tracking, load balancing, and transaction manager interface.
const DB_REPLICA
Definition defines.php:25
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42