MediaWiki  master
MediaLinksHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Rest\Handler;
4 
5 use MediaFileTrait;
10 use RepoGroup;
11 use RequestContext;
12 use Title;
13 use User;
17 
22  use MediaFileTrait;
23 
25  private const MAX_NUM_LINKS = 100;
26 
29 
31  private $loadBalancer;
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 }
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:328
MediaWiki\Rest\Handler\getResponseFactory
getResponseFactory()
Get the ResponseFactory which can be used to generate Response objects.
Definition: Handler.php:151
MediaWiki\Rest\Handler\MediaLinksHandler\run
run( $title)
Definition: MediaLinksHandler.php:77
MediaWiki\Rest\Handler\MediaLinksHandler\$repoGroup
RepoGroup $repoGroup
Definition: MediaLinksHandler.php:34
MediaWiki\Rest\Handler\MediaLinksHandler\$title
Title bool null $title
Definition: MediaLinksHandler.php:42
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1815
Title\getArticleID
getArticleID( $flags=0)
Get the article ID for this Title from the link cache, adding it if necessary.
Definition: Title.php:3118
MediaWiki\Rest\Handler\MediaLinksHandler\__construct
__construct(PermissionManager $permissionManager, ILoadBalancer $loadBalancer, RepoGroup $repoGroup)
Definition: MediaLinksHandler.php:49
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
MediaWiki\Rest\Handler\MediaLinksHandler\$user
User $user
Definition: MediaLinksHandler.php:37
Wikimedia\Message\MessageValue
Value object representing a message for i18n.
Definition: MessageValue.php:16
$dbr
$dbr
Definition: testCompression.php:54
MediaWiki\Rest\Handler\MediaLinksHandler\getParamSettings
getParamSettings()
Fetch ParamValidator settings for parameters.
Definition: MediaLinksHandler.php:163
MediaWiki\Rest\Response
Definition: Response.php:8
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:39
MediaWiki\Rest\Handler\MediaLinksHandler\processDbResults
processDbResults( $results)
Definition: MediaLinksHandler.php:129
MediaWiki\Rest\Handler\getValidatedParams
getValidatedParams()
Fetch the validated parameters.
Definition: Handler.php:257
MediaWiki\Rest\Handler\MediaLinksHandler\getLastModified
getLastModified()
Definition: MediaLinksHandler.php:191
MediaWiki\Rest\Handler\MediaLinksHandler\needsWriteAccess
needsWriteAccess()
Indicates whether this route requires write access.
Definition: MediaLinksHandler.php:159
MediaWiki\Permissions\PermissionManager
A service class for checking permissions To obtain an instance, use MediaWikiServices::getInstance()-...
Definition: PermissionManager.php:49
MediaWiki\Rest\Handler\MediaLinksHandler\getETag
getETag()
Definition: MediaLinksHandler.php:177
Title\getLatestRevID
getLatestRevID( $flags=0)
What is the page_latest field for this page?
Definition: Title.php:3204
MediaWiki\Rest\Handler\MediaLinksHandler\hasRepresentation
hasRepresentation()
Definition: MediaLinksHandler.php:203
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:454
Title
Represents a title within MediaWiki.
Definition: Title.php:41
MediaWiki\Rest\Handler\MediaLinksHandler
Handler class for Core REST API endpoints that perform operations on revisions.
Definition: MediaLinksHandler.php:21
Title\getTouched
getTouched( $db=null)
Get the last touched timestamp.
Definition: Title.php:4136
MediaWiki\Rest\Handler\MediaLinksHandler\getDbResults
getDbResults(int $pageId)
Definition: MediaLinksHandler.php:111
RepoGroup
Prioritized list of file repositories.
Definition: RepoGroup.php:31
Wikimedia\Message\MessageValue\new
static new( $key, $params=[])
Static constructor for easier chaining of ->params() methods.
Definition: MessageValue.php:42
Title\exists
exists( $flags=0)
Check if page exists.
Definition: Title.php:3905
MediaWiki\Rest\Handler\MediaLinksHandler\$permissionManager
PermissionManager $permissionManager
Definition: MediaLinksHandler.php:28
MediaWiki\Rest\Handler\MediaLinksHandler\MAX_NUM_LINKS
const MAX_NUM_LINKS
int The maximum number of media links to return
Definition: MediaLinksHandler.php:25
MediaWiki\Rest\Handler\MediaLinksHandler\getTitle
getTitle()
Definition: MediaLinksHandler.php:65
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:56
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\LocalizedHttpException
@newable
Definition: LocalizedHttpException.php:10
MediaWiki\Rest\Handler\MediaLinksHandler\$loadBalancer
ILoadBalancer $loadBalancer
Definition: MediaLinksHandler.php:31
MediaWiki\Rest\SimpleHandler
Definition: SimpleHandler.php:15