| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Rest\Handler; |
| 4 |
|
| 5 |
use MediaFileTrait; |
| 6 |
use MediaWiki\Page\ExistingPageRecord; |
| 7 |
use MediaWiki\Page\PageLookup; |
| 8 |
use MediaWiki\Rest\LocalizedHttpException; |
| 9 |
use MediaWiki\Rest\Response; |
| 10 |
use MediaWiki\Rest\SimpleHandler; |
| 11 |
use RepoGroup; |
| 12 |
use Wikimedia\Message\MessageValue; |
| 13 |
use Wikimedia\ParamValidator\ParamValidator; |
| 14 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
class MediaLinksHandler extends SimpleHandler { |
| 20 |
use MediaFileTrait; |
| 21 |
|
| 22 |
|
| 23 |
private const MAX_NUM_LINKS = 100; |
| 24 |
|
| 25 |
private IConnectionProvider $dbProvider; |
| 26 |
private RepoGroup $repoGroup; |
| 27 |
private PageLookup $pageLookup; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
private $page = false; |
| 33 |
|
| 34 |
public function __construct( |
| 35 |
IConnectionProvider $dbProvider, |
| 36 |
RepoGroup $repoGroup, |
| 37 |
PageLookup $pageLookup |
| 38 |
) { |
| 39 |
$this->dbProvider = $dbProvider; |
| 40 |
$this->repoGroup = $repoGroup; |
| 41 |
$this->pageLookup = $pageLookup; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
private function getPage(): ?ExistingPageRecord { |
| 48 |
if ( $this->page === false ) { |
| 49 |
$this->page = $this->pageLookup->getExistingPageByText( |
| 50 |
$this->getValidatedParams()['title'] |
| 51 |
); |
| 52 |
} |
| 53 |
return $this->page; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public function run( $title ) { |
| 62 |
$page = $this->getPage(); |
| 63 |
if ( !$page ) { |
| 64 |
throw new LocalizedHttpException( |
| 65 |
MessageValue::new( 'rest-nonexistent-title' )->plaintextParams( $title ), |
| 66 |
404 |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
if ( !$this->getAuthority()->authorizeRead( 'read', $page ) ) { |
| 71 |
throw new LocalizedHttpException( |
| 72 |
MessageValue::new( 'rest-permission-denied-title' )->plaintextParams( $title ), |
| 73 |
403 |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
$results = $this->getDbResults( $page->getId() ); |
| 79 |
if ( count( $results ) > $this->getMaxNumLinks() ) { |
| 80 |
throw new LocalizedHttpException( |
| 81 |
MessageValue::new( 'rest-media-too-many-links' ) |
| 82 |
->plaintextParams( $title ) |
| 83 |
->numParams( $this->getMaxNumLinks() ), |
| 84 |
400 |
| 85 |
); |
| 86 |
} |
| 87 |
$response = $this->processDbResults( $results ); |
| 88 |
return $this->getResponseFactory()->createJson( $response ); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
private function getDbResults( int $pageId ) { |
| 96 |
return $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder() |
| 97 |
->select( 'il_to' ) |
| 98 |
->from( 'imagelinks' ) |
| 99 |
->where( [ 'il_from' => $pageId ] ) |
| 100 |
->orderBy( 'il_to' ) |
| 101 |
->limit( $this->getMaxNumLinks() + 1 ) |
| 102 |
->caller( __METHOD__ )->fetchFieldValues(); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
private function processDbResults( $results ) { |
| 110 |
|
| 111 |
|
| 112 |
$performer = $this->getAuthority(); |
| 113 |
$findTitles = array_map( static function ( $title ) use ( $performer ) { |
| 114 |
return [ |
| 115 |
'title' => $title, |
| 116 |
'private' => $performer, |
| 117 |
]; |
| 118 |
}, $results ); |
| 119 |
|
| 120 |
$files = $this->repoGroup->findFiles( $findTitles ); |
| 121 |
[ $maxWidth, $maxHeight ] = self::getImageLimitsFromOption( |
| 122 |
$this->getAuthority()->getUser(), |
| 123 |
'imagesize' |
| 124 |
); |
| 125 |
$transforms = [ |
| 126 |
'preferred' => [ |
| 127 |
'maxWidth' => $maxWidth, |
| 128 |
'maxHeight' => $maxHeight, |
| 129 |
] |
| 130 |
]; |
| 131 |
$response = []; |
| 132 |
foreach ( $files as $file ) { |
| 133 |
$response[] = $this->getFileInfo( $file, $performer, $transforms ); |
| 134 |
} |
| 135 |
|
| 136 |
$response = [ |
| 137 |
'files' => $response |
| 138 |
]; |
| 139 |
|
| 140 |
return $response; |
| 141 |
} |
| 142 |
|
| 143 |
public function needsWriteAccess() { |
| 144 |
return false; |
| 145 |
} |
| 146 |
|
| 147 |
public function getParamSettings() { |
| 148 |
return [ |
| 149 |
'title' => [ |
| 150 |
self::PARAM_SOURCE => 'path', |
| 151 |
ParamValidator::PARAM_TYPE => 'string', |
| 152 |
ParamValidator::PARAM_REQUIRED => true, |
| 153 |
], |
| 154 |
]; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
protected function getETag(): ?string { |
| 162 |
$page = $this->getPage(); |
| 163 |
if ( !$page ) { |
| 164 |
return null; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
return '"' . $page->getLatest() . '@' . wfTimestamp( TS_MW, $page->getTouched() ) . '"'; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
protected function getLastModified(): ?string { |
| 176 |
$page = $this->getPage(); |
| 177 |
return $page ? $page->getTouched() : null; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
protected function hasRepresentation() { |
| 184 |
return (bool)$this->getPage(); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
protected function getMaxNumLinks(): int { |
| 193 |
return self::MAX_NUM_LINKS; |
| 194 |
} |
| 195 |
|
| 196 |
public function getResponseBodySchemaFileName( string $method ): ?string { |
| 197 |
return 'includes/Rest/Handler/Schema/MediaLinks.json'; |
| 198 |
} |
| 199 |
} |