| 1 |
<?php |
| 2 |
|
| 3 |
namespace PageImages; |
| 4 |
|
| 5 |
use MediaWiki\Api\ApiBase; |
| 6 |
use MediaWiki\Api\ApiQuery; |
| 7 |
use MediaWiki\Api\ApiQueryBase; |
| 8 |
use MediaWiki\Page\PageReference; |
| 9 |
use MediaWiki\Page\PageReferenceValue; |
| 10 |
use RepoGroup; |
| 11 |
use Wikimedia\ParamValidator\ParamValidator; |
| 12 |
use Wikimedia\ParamValidator\TypeDef\IntegerDef; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
class ApiQueryPageImages extends ApiQueryBase { |
| 26 |
|
| 27 |
private $repoGroup; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public function __construct( |
| 35 |
ApiQuery $query, |
| 36 |
$moduleName, |
| 37 |
RepoGroup $repoGroup |
| 38 |
) { |
| 39 |
parent::__construct( $query, $moduleName, 'pi' ); |
| 40 |
$this->repoGroup = $repoGroup; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
protected function getTitles() { |
| 56 |
$pageSet = $this->getPageSet(); |
| 57 |
$titles = $pageSet->getGoodPages(); |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
$missingTitles = $pageSet->getMissingTitlesByNamespace(); |
| 63 |
$missingFileTitles = $missingTitles[NS_FILE] ?? []; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
foreach ( $missingFileTitles as $dbkey => $id ) { |
| 69 |
$titles[$id] = PageReferenceValue::localReference( NS_FILE, $dbkey ); |
| 70 |
} |
| 71 |
|
| 72 |
return $titles; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public function execute() { |
| 80 |
$params = $this->extractRequestParams(); |
| 81 |
$prop = array_flip( $params['prop'] ); |
| 82 |
if ( !count( $prop ) ) { |
| 83 |
$this->dieWithError( |
| 84 |
[ 'apierror-paramempty', $this->encodeParamName( 'prop' ) ], 'noprop' |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
$allTitles = $this->getTitles(); |
| 89 |
|
| 90 |
if ( count( $allTitles ) === 0 ) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
$offset = 0; |
| 96 |
if ( isset( $params['continue'] ) ) { |
| 97 |
|
| 98 |
|
| 99 |
$pageIds = array_keys( $allTitles ); |
| 100 |
$offset = array_search( intval( $params['continue'] ), $pageIds ); |
| 101 |
|
| 102 |
$this->dieContinueUsageIf( !$offset ); |
| 103 |
} |
| 104 |
|
| 105 |
$limit = $params['limit']; |
| 106 |
|
| 107 |
$titles = array_slice( $allTitles, $offset, $limit, true ); |
| 108 |
|
| 109 |
|
| 110 |
$nextItemArray = array_slice( $allTitles, $offset + $limit, 1, true ); |
| 111 |
if ( $nextItemArray ) { |
| 112 |
$this->setContinueEnumParameter( 'continue', key( $nextItemArray ) ); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
$filePageTitles = []; |
| 117 |
foreach ( $titles as $id => $title ) { |
| 118 |
if ( $title->getNamespace() === NS_FILE ) { |
| 119 |
$filePageTitles[$id] = $title; |
| 120 |
unset( $titles[$id] ); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
$size = $params['thumbsize']; |
| 125 |
$lang = $params['langcode']; |
| 126 |
|
| 127 |
if ( count( $titles ) > 0 ) { |
| 128 |
$this->addTables( 'page_props' ); |
| 129 |
$this->addFields( [ 'pp_page', 'pp_propname', 'pp_value' ] ); |
| 130 |
$this->addWhere( [ 'pp_page' => array_keys( $titles ), |
| 131 |
'pp_propname' => PageImages::getPropNames( $params['license'] ) ] ); |
| 132 |
|
| 133 |
$res = $this->select( __METHOD__ ); |
| 134 |
|
| 135 |
$buffer = []; |
| 136 |
$propNameAny = PageImages::getPropName( false ); |
| 137 |
foreach ( $res as $row ) { |
| 138 |
$pageId = $row->pp_page; |
| 139 |
if ( !array_key_exists( $pageId, $buffer ) || $row->pp_propname === $propNameAny ) { |
| 140 |
$buffer[$pageId] = $row; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
foreach ( $buffer as $pageId => $row ) { |
| 145 |
$fileName = $row->pp_value; |
| 146 |
$this->setResultValues( $prop, $pageId, $fileName, $size, $lang ); |
| 147 |
} |
| 148 |
|
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
foreach ( $filePageTitles as $pageId => $title ) { |
| 154 |
$fileName = $title->getDBkey(); |
| 155 |
$this->setResultValues( $prop, $pageId, $fileName, $size, $lang ); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public function getCacheMode( $params ) { |
| 166 |
return 'public'; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
protected function setResultValues( array $prop, $pageId, $fileName, $size, $lang ) { |
| 179 |
$vals = []; |
| 180 |
if ( isset( $prop['thumbnail'] ) || isset( $prop['original'] ) ) { |
| 181 |
$file = $this->repoGroup->findFile( $fileName ); |
| 182 |
if ( $file ) { |
| 183 |
if ( isset( $prop['thumbnail'] ) ) { |
| 184 |
$thumb = $file->transform( [ |
| 185 |
'width' => $size, |
| 186 |
'lang' => $lang |
| 187 |
] ); |
| 188 |
if ( $thumb && $thumb->getUrl() ) { |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
$reportedSize = $thumb->fileIsSource() ? $file : $thumb; |
| 193 |
$vals['thumbnail'] = [ |
| 194 |
'source' => wfExpandUrl( $thumb->getUrl(), PROTO_CURRENT ), |
| 195 |
'width' => $reportedSize->getWidth(), |
| 196 |
'height' => $reportedSize->getHeight(), |
| 197 |
]; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
if ( isset( $prop['original'] ) ) { |
| 202 |
$originalSize = [ |
| 203 |
'width' => $file->getWidth(), |
| 204 |
'height' => $file->getHeight() |
| 205 |
]; |
| 206 |
if ( $lang ) { |
| 207 |
$file = $file->transform( [ |
| 208 |
'lang' => $lang, |
| 209 |
'width' => $originalSize['width'], |
| 210 |
'height' => $originalSize['height'] |
| 211 |
] ); |
| 212 |
} |
| 213 |
$original_url = wfExpandUrl( $file->getUrl(), PROTO_CURRENT ); |
| 214 |
|
| 215 |
$vals['original'] = [ |
| 216 |
'source' => $original_url, |
| 217 |
'width' => $originalSize['width'], |
| 218 |
'height' => $originalSize['height'] |
| 219 |
]; |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
if ( isset( $prop['name'] ) ) { |
| 225 |
$vals['pageimage'] = $fileName; |
| 226 |
} |
| 227 |
|
| 228 |
$this->getResult()->addValue( [ 'query', 'pages' ], $pageId, $vals ); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
public function getAllowedParams() { |
| 236 |
return [ |
| 237 |
'prop' => [ |
| 238 |
ParamValidator::PARAM_TYPE => [ 'thumbnail', 'name', 'original' ], |
| 239 |
ParamValidator::PARAM_ISMULTI => true, |
| 240 |
ParamValidator::PARAM_DEFAULT => 'thumbnail|name', |
| 241 |
ApiBase::PARAM_HELP_MSG_PER_VALUE => [], |
| 242 |
], |
| 243 |
'thumbsize' => [ |
| 244 |
ParamValidator::PARAM_TYPE => 'integer', |
| 245 |
ParamValidator::PARAM_DEFAULT => 50, |
| 246 |
], |
| 247 |
'limit' => [ |
| 248 |
ParamValidator::PARAM_DEFAULT => 50, |
| 249 |
ParamValidator::PARAM_TYPE => 'limit', |
| 250 |
IntegerDef::PARAM_MIN => 1, |
| 251 |
IntegerDef::PARAM_MAX => 50, |
| 252 |
IntegerDef::PARAM_MAX2 => 100, |
| 253 |
], |
| 254 |
'license' => [ |
| 255 |
ParamValidator::PARAM_TYPE => [ PageImages::LICENSE_FREE, PageImages::LICENSE_ANY ], |
| 256 |
ParamValidator::PARAM_ISMULTI => false, |
| 257 |
ParamValidator::PARAM_DEFAULT => $this->getConfig()->get( 'PageImagesAPIDefaultLicense' ), |
| 258 |
ApiBase::PARAM_HELP_MSG_PER_VALUE => [], |
| 259 |
], |
| 260 |
'continue' => [ |
| 261 |
ParamValidator::PARAM_TYPE => 'integer', |
| 262 |
ApiBase::PARAM_HELP_MSG => 'api-help-param-continue', |
| 263 |
], |
| 264 |
'langcode' => [ |
| 265 |
ParamValidator::PARAM_TYPE => 'string', |
| 266 |
ParamValidator::PARAM_DEFAULT => null |
| 267 |
] |
| 268 |
]; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
protected function getExamplesMessages() { |
| 275 |
return [ |
| 276 |
'action=query&prop=pageimages&titles=Albert%20Einstein&pithumbsize=100' => |
| 277 |
'apihelp-query+pageimages-example-1', |
| 278 |
]; |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
public function getHelpUrls() { |
| 286 |
return "https://www.mediawiki.org/wiki/Special:MyLanguage/Extension:PageImages#API"; |
| 287 |
} |
| 288 |
|
| 289 |
} |