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 $dbProvider;
27 
29  private $repoGroup;
30 
32  private $pageLookup;
33 
37  private $page = false;
38 
44  public function __construct(
45  IConnectionProvider $dbProvider,
46  RepoGroup $repoGroup,
47  PageLookup $pageLookup
48  ) {
49  $this->dbProvider = $dbProvider;
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  return $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder()
107  ->select( 'il_to' )
108  ->from( 'imagelinks' )
109  ->where( [ 'il_from' => $pageId ] )
110  ->orderBy( 'il_to' )
111  ->limit( self::MAX_NUM_LINKS + 1 )
112  ->caller( __METHOD__ )->fetchFieldValues();
113  }
114 
119  private function processDbResults( $results ) {
120  // Using "private" here means an equivalent of the Action API's "anon-public-user-private"
121  // caching model would be necessary, if caching is ever added to this endpoint.
122  $performer = $this->getAuthority();
123  $findTitles = array_map( static function ( $title ) use ( $performer ) {
124  return [
125  'title' => $title,
126  'private' => $performer,
127  ];
128  }, $results );
129 
130  $files = $this->repoGroup->findFiles( $findTitles );
131  [ $maxWidth, $maxHeight ] = self::getImageLimitsFromOption(
132  $this->getAuthority()->getUser(),
133  'imagesize'
134  );
135  $transforms = [
136  'preferred' => [
137  'maxWidth' => $maxWidth,
138  'maxHeight' => $maxHeight,
139  ]
140  ];
141  $response = [];
142  foreach ( $files as $file ) {
143  $response[] = $this->getFileInfo( $file, $performer, $transforms );
144  }
145 
146  $response = [
147  'files' => $response
148  ];
149 
150  return $response;
151  }
152 
153  public function needsWriteAccess() {
154  return false;
155  }
156 
157  public function getParamSettings() {
158  return [
159  'title' => [
160  self::PARAM_SOURCE => 'path',
161  ParamValidator::PARAM_TYPE => 'string',
163  ],
164  ];
165  }
166 
171  protected function getETag(): ?string {
172  $page = $this->getPage();
173  if ( !$page ) {
174  return null;
175  }
176 
177  // XXX: use hash of the rendered HTML?
178  return '"' . $page->getLatest() . '@' . wfTimestamp( TS_MW, $page->getTouched() ) . '"';
179  }
180 
185  protected function getLastModified(): ?string {
186  $page = $this->getPage();
187  return $page ? $page->getTouched() : null;
188  }
189 
193  protected function hasRepresentation() {
194  return (bool)$this->getPage();
195  }
196 }
getUser()
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
if(!defined('MW_SETUP_CALLBACK'))
Definition: WebStart.php:88
Handler class for Core REST API endpoints that perform operations on revisions.
__construct(IConnectionProvider $dbProvider, RepoGroup $repoGroup, PageLookup $pageLookup)
getParamSettings()
Fetch ParamValidator settings for parameters.
needsWriteAccess()
Indicates whether this route requires write access.
getValidatedParams()
Fetch the validated parameters.
Definition: Handler.php:371
getAuthority()
Get the current acting authority.
Definition: Handler.php:166
getResponseFactory()
Get the ResponseFactory which can be used to generate Response objects.
Definition: Handler.php:188
Prioritized list of file repositories.
Definition: RepoGroup.php:30
Value object representing a message for i18n.
static new( $key, $params=[])
Static constructor for easier chaining of ->params() methods.
Service for formatting and validating API parameters.
const PARAM_TYPE
(string|array) Type of the parameter.
const PARAM_REQUIRED
(bool) Indicate that the parameter is required.
Data record representing a page that currently exists as an editable page on a wiki.
Service for looking up information about wiki pages.
Definition: PageLookup.php:17
Provide primary and replica IDatabase connections.
Copyright (C) 2011-2020 Wikimedia Foundation and others.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42