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 
44  public function __construct(
48  ) {
49  $this->permissionManager = $permissionManager;
50  $this->loadBalancer = $loadBalancer;
51  $this->repoGroup = $repoGroup;
52 
53  // @todo Inject this, when there is a good way to do that
54  $this->user = RequestContext::getMain()->getUser();
55  }
56 
62  public function run( $title ) {
63  $titleObj = Title::newFromText( $title );
64  if ( !$titleObj || !$titleObj->getArticleID() ) {
65  throw new LocalizedHttpException(
66  MessageValue::new( 'rest-nonexistent-title' )->plaintextParams( $title ),
67  404
68  );
69  }
70 
71  if ( !$this->permissionManager->userCan( 'read', $this->user, $titleObj ) ) {
72  throw new LocalizedHttpException(
73  MessageValue::new( 'rest-permission-denied-title' )->plaintextParams( $title ),
74  403
75  );
76  }
77 
78  // @todo: add continuation if too many links are found
79  $results = $this->getDbResults( $titleObj->getArticleID() );
80  if ( count( $results ) > self::MAX_NUM_LINKS ) {
81  throw new LocalizedHttpException(
82  MessageValue::new( 'rest-media-too-many-links' )
83  ->plaintextParams( $title )
84  ->numParams( self::MAX_NUM_LINKS ),
85  500
86  );
87  }
88  $response = $this->processDbResults( $results );
89  return $this->getResponseFactory()->createJson( $response );
90  }
91 
96  private function getDbResults( int $pageId ) {
97  $dbr = $this->loadBalancer->getConnectionRef( DB_REPLICA );
98  return $dbr->selectFieldValues(
99  'imagelinks',
100  'il_to',
101  [ 'il_from' => $pageId ],
102  __METHOD__,
103  [
104  'ORDER BY' => 'il_to',
105  'LIMIT' => self::MAX_NUM_LINKS + 1,
106  ]
107  );
108  }
109 
114  private function processDbResults( $results ) {
115  // Using "private" here means an equivalent of the Action API's "anon-public-user-private"
116  // caching model would be necessary, if caching is ever added to this endpoint.
117  $findTitles = array_map( function ( $title ) {
118  return [
119  'title' => $title,
120  'private' => $this->user,
121  ];
122  }, $results );
123 
124  $files = $this->repoGroup->findFiles( $findTitles );
125  list( $maxWidth, $maxHeight ) = self::getImageLimitsFromOption( $this->user, 'imagesize' );
126  $transforms = [
127  'preferred' => [
128  'maxWidth' => $maxWidth,
129  'maxHeight' => $maxHeight,
130  ]
131  ];
132  $response = [];
133  foreach ( $files as $file ) {
134  $response[] = $this->getFileInfo( $file, $this->user, $transforms );
135  }
136 
137  $response = [
138  'files' => $response
139  ];
140 
141  return $response;
142  }
143 
144  public function needsWriteAccess() {
145  return false;
146  }
147 
148  public function getParamSettings() {
149  return [
150  'title' => [
151  self::PARAM_SOURCE => 'path',
152  ParamValidator::PARAM_TYPE => 'string',
153  ParamValidator::PARAM_REQUIRED => true,
154  ],
155  ];
156  }
157 }
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\getResponseFactory
getResponseFactory()
Get the ResponseFactory which can be used to generate Response objects.
Definition: Handler.php:92
$response
$response
Definition: opensearch_desc.php:44
MediaWiki\Rest\Handler\MediaLinksHandler\run
run( $title)
Definition: MediaLinksHandler.php:62
MediaWiki\Rest\Handler\MediaLinksHandler\$repoGroup
RepoGroup $repoGroup
Definition: MediaLinksHandler.php:34
MediaWiki\Rest\Handler\MediaLinksHandler\__construct
__construct(PermissionManager $permissionManager, ILoadBalancer $loadBalancer, RepoGroup $repoGroup)
Definition: MediaLinksHandler.php:44
$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:14
$dbr
$dbr
Definition: testCompression.php:52
MediaWiki\Rest\Handler\MediaLinksHandler\getParamSettings
getParamSettings()
Fetch ParamValidator settings for parameters.
Definition: MediaLinksHandler.php:148
MediaWiki\Rest\Response
Definition: Response.php:8
$title
$title
Definition: testCompression.php:36
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\Rest\Handler\MediaLinksHandler\processDbResults
processDbResults( $results)
Definition: MediaLinksHandler.php:114
MediaWiki\Rest\Handler\MediaLinksHandler\needsWriteAccess
needsWriteAccess()
Indicates whether this route requires write access.
Definition: MediaLinksHandler.php:144
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
Title
Represents a title within MediaWiki.
Definition: Title.php:42
MediaWiki\Rest\Handler\MediaLinksHandler
Handler class for Core REST API endpoints that perform operations on revisions.
Definition: MediaLinksHandler.php:21
MediaWiki\Rest\Handler\MediaLinksHandler\getDbResults
getDbResults(int $pageId)
Definition: MediaLinksHandler.php:96
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:38
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
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\LocalizedHttpException
Definition: LocalizedHttpException.php:7
MediaWiki\Rest\Handler\MediaLinksHandler\$loadBalancer
ILoadBalancer $loadBalancer
Definition: MediaLinksHandler.php:31
MediaWiki\Rest\SimpleHandler
Definition: SimpleHandler.php:14