MediaWiki  master
MediaLinksHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Rest\Handler;
4 
5 use MediaFileTrait;
11 use RepoGroup;
15 
20  use MediaFileTrait;
21 
23  private const MAX_NUM_LINKS = 100;
24 
26  private $loadBalancer;
27 
29  private $repoGroup;
30 
32  private $pageLookup;
33 
37  private $page = false;
38 
44  public function __construct(
48  ) {
49  $this->loadBalancer = $loadBalancer;
50  $this->repoGroup = $repoGroup;
51  $this->pageLookup = $pageLookup;
52  }
53 
57  private function getPage(): ?ExistingPageRecord {
58  if ( $this->page === false ) {
59  $this->page = $this->pageLookup->getExistingPageByText(
60  $this->getValidatedParams()['title']
61  );
62  }
63  return $this->page;
64  }
65 
71  public function run( $title ) {
72  $page = $this->getPage();
73  if ( !$page ) {
74  throw new LocalizedHttpException(
75  MessageValue::new( 'rest-nonexistent-title' )->plaintextParams( $title ),
76  404
77  );
78  }
79 
80  if ( !$this->getAuthority()->authorizeRead( 'read', $page ) ) {
81  throw new LocalizedHttpException(
82  MessageValue::new( 'rest-permission-denied-title' )->plaintextParams( $title ),
83  403
84  );
85  }
86 
87  // @todo: add continuation if too many links are found
88  $results = $this->getDbResults( $page->getId() );
89  if ( count( $results ) > self::MAX_NUM_LINKS ) {
90  throw new LocalizedHttpException(
91  MessageValue::new( 'rest-media-too-many-links' )
92  ->plaintextParams( $title )
93  ->numParams( self::MAX_NUM_LINKS ),
94  500
95  );
96  }
97  $response = $this->processDbResults( $results );
98  return $this->getResponseFactory()->createJson( $response );
99  }
100 
105  private function getDbResults( int $pageId ) {
106  $dbr = $this->loadBalancer->getConnectionRef( DB_REPLICA );
107  return $dbr->selectFieldValues(
108  'imagelinks',
109  'il_to',
110  [ 'il_from' => $pageId ],
111  __METHOD__,
112  [
113  'ORDER BY' => 'il_to',
114  'LIMIT' => self::MAX_NUM_LINKS + 1,
115  ]
116  );
117  }
118 
123  private function processDbResults( $results ) {
124  // Using "private" here means an equivalent of the Action API's "anon-public-user-private"
125  // caching model would be necessary, if caching is ever added to this endpoint.
126  $performer = $this->getAuthority();
127  $findTitles = array_map( static function ( $title ) use ( $performer ) {
128  return [
129  'title' => $title,
130  'private' => $performer,
131  ];
132  }, $results );
133 
134  $files = $this->repoGroup->findFiles( $findTitles );
135  list( $maxWidth, $maxHeight ) = self::getImageLimitsFromOption(
136  $this->getAuthority()->getUser(),
137  'imagesize'
138  );
139  $transforms = [
140  'preferred' => [
141  'maxWidth' => $maxWidth,
142  'maxHeight' => $maxHeight,
143  ]
144  ];
145  $response = [];
146  foreach ( $files as $file ) {
147  $response[] = $this->getFileInfo( $file, $performer, $transforms );
148  }
149 
150  $response = [
151  'files' => $response
152  ];
153 
154  return $response;
155  }
156 
157  public function needsWriteAccess() {
158  return false;
159  }
160 
161  public function getParamSettings() {
162  return [
163  'title' => [
164  self::PARAM_SOURCE => 'path',
165  ParamValidator::PARAM_TYPE => 'string',
166  ParamValidator::PARAM_REQUIRED => true,
167  ],
168  ];
169  }
170 
175  protected function getETag(): ?string {
176  $page = $this->getPage();
177  if ( !$page ) {
178  return null;
179  }
180 
181  // XXX: use hash of the rendered HTML?
182  return '"' . $page->getLatest() . '@' . wfTimestamp( TS_MW, $page->getTouched() ) . '"';
183  }
184 
189  protected function getLastModified(): ?string {
190  $page = $this->getPage();
191  if ( !$page ) {
192  return null;
193  }
194 
195  return $page->getTouched();
196  }
197 
201  protected function hasRepresentation() {
202  return (bool)$this->getPage();
203  }
204 }
MediaWiki\Rest\Handler
Definition: AbstractContributionHandler.php:3
MediaWiki\Rest\Handler\getResponseFactory
getResponseFactory()
Get the ResponseFactory which can be used to generate Response objects.
Definition: Handler.php:170
Page\PageRecord\getLatest
getLatest( $wikiId=self::LOCAL)
The ID of the page's latest revision.
MediaWiki\Rest\Handler\MediaLinksHandler\run
run( $title)
Definition: MediaLinksHandler.php:71
MediaWiki\Rest\Handler\MediaLinksHandler\$repoGroup
RepoGroup $repoGroup
Definition: MediaLinksHandler.php:29
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1668
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
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:161
Page\ProperPageIdentity\getId
getId( $wikiId=self::LOCAL)
Returns the page ID.
MediaWiki\Rest\Response
Definition: Response.php:8
Page\PageRecord\getTouched
getTouched()
Timestamp at which the page was last flagged for rerendering.
$title
$title
Definition: testCompression.php:38
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
MediaWiki\Rest\Handler\MediaLinksHandler\processDbResults
processDbResults( $results)
Definition: MediaLinksHandler.php:123
MediaWiki\Rest\Handler\getValidatedParams
getValidatedParams()
Fetch the validated parameters.
Definition: Handler.php:282
MediaWiki\Rest\Handler\MediaLinksHandler\getLastModified
getLastModified()
Definition: MediaLinksHandler.php:189
MediaWiki\Rest\Handler\MediaLinksHandler\needsWriteAccess
needsWriteAccess()
Indicates whether this route requires write access.
Definition: MediaLinksHandler.php:157
Page\ExistingPageRecord
Data record representing a page that currently exists as an editable page on a wiki.
Definition: ExistingPageRecord.php:15
MediaWiki\Rest\Handler\MediaLinksHandler\__construct
__construct(ILoadBalancer $loadBalancer, RepoGroup $repoGroup, PageLookup $pageLookup)
Definition: MediaLinksHandler.php:44
MediaWiki\Rest\Handler\MediaLinksHandler\getETag
getETag()
Definition: MediaLinksHandler.php:175
MediaWiki\Rest\Handler\MediaLinksHandler\$pageLookup
PageLookup $pageLookup
Definition: MediaLinksHandler.php:32
Page\PageLookup
Service interface for looking up infermation about wiki pages.
Definition: PageLookup.php:14
MediaWiki\Rest\Handler\MediaLinksHandler\hasRepresentation
hasRepresentation()
Definition: MediaLinksHandler.php:201
MediaWiki\Rest\Handler\MediaLinksHandler
Handler class for Core REST API endpoints that perform operations on revisions.
Definition: MediaLinksHandler.php:19
MediaWiki\Rest\Handler\MediaLinksHandler\getDbResults
getDbResults(int $pageId)
Definition: MediaLinksHandler.php:105
RepoGroup
Prioritized list of file repositories.
Definition: RepoGroup.php:32
Wikimedia\Message\MessageValue\new
static new( $key, $params=[])
Static constructor for easier chaining of ->params() methods.
Definition: MessageValue.php:42
MediaWiki\Rest\Handler\MediaLinksHandler\$page
ExistingPageRecord false null $page
Definition: MediaLinksHandler.php:37
MediaWiki\Rest\Handler\MediaLinksHandler\MAX_NUM_LINKS
const MAX_NUM_LINKS
int The maximum number of media links to return
Definition: MediaLinksHandler.php:23
MediaWiki\Rest\Handler\MediaLinksHandler\getPage
getPage()
Definition: MediaLinksHandler.php:57
MediaWiki\Rest\Handler\getAuthority
getAuthority()
Get the current acting authority.
Definition: Handler.php:148
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:26
MediaWiki\Rest\SimpleHandler
Definition: SimpleHandler.php:15