Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / api / ApiQueryStashImageInfo.php
blobabb827fe4f4540c0acc1b45652d0060209a9f564
1 <?php
2 /**
3 * API for MediaWiki 1.16+
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
23 /**
24 * A query action to get image information from temporarily stashed files.
26 * @ingroup API
28 class ApiQueryStashImageInfo extends ApiQueryImageInfo {
30 public function __construct( ApiQuery $query, $moduleName ) {
31 parent::__construct( $query, $moduleName, 'sii' );
34 public function execute() {
35 if ( !$this->getUser()->isLoggedIn() ) {
36 $this->dieWithError( 'apierror-mustbeloggedin-uploadstash', 'notloggedin' );
39 $params = $this->extractRequestParams();
40 $modulePrefix = $this->getModulePrefix();
42 $prop = array_flip( $params['prop'] );
44 $scale = $this->getScale( $params );
46 $result = $this->getResult();
48 $this->requireAtLeastOneParameter( $params, 'filekey', 'sessionkey' );
50 // Alias sessionkey to filekey, but give an existing filekey precedence.
51 if ( !$params['filekey'] && $params['sessionkey'] ) {
52 $params['filekey'] = $params['sessionkey'];
55 try {
56 $stash = RepoGroup::singleton()->getLocalRepo()->getUploadStash( $this->getUser() );
58 foreach ( $params['filekey'] as $filekey ) {
59 $file = $stash->getFile( $filekey );
60 $finalThumbParam = $this->mergeThumbParams( $file, $scale, $params['urlparam'] );
61 $imageInfo = ApiQueryImageInfo::getInfo( $file, $prop, $result, $finalThumbParam );
62 $result->addValue( [ 'query', $this->getModuleName() ], null, $imageInfo );
63 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], $modulePrefix );
65 // @todo Update exception handling here to understand current getFile exceptions
66 } catch ( UploadStashFileNotFoundException $e ) {
67 $this->dieWithException( $e, [ 'wrap' => 'apierror-stashedfilenotfound' ] );
68 } catch ( UploadStashBadPathException $e ) {
69 $this->dieWithException( $e, [ 'wrap' => 'apierror-stashpathinvalid' ] );
73 private $propertyFilter = [
74 'user', 'userid', 'comment', 'parsedcomment',
75 'mediatype', 'archivename', 'uploadwarning',
78 public function getAllowedParams() {
79 return [
80 'filekey' => [
81 ApiBase::PARAM_ISMULTI => true,
83 'sessionkey' => [
84 ApiBase::PARAM_ISMULTI => true,
85 ApiBase::PARAM_DEPRECATED => true,
87 'prop' => [
88 ApiBase::PARAM_ISMULTI => true,
89 ApiBase::PARAM_DFLT => 'timestamp|url',
90 ApiBase::PARAM_TYPE => self::getPropertyNames( $this->propertyFilter ),
91 ApiBase::PARAM_HELP_MSG => 'apihelp-query+imageinfo-param-prop',
92 ApiBase::PARAM_HELP_MSG_PER_VALUE => self::getPropertyMessages( $this->propertyFilter )
94 'urlwidth' => [
95 ApiBase::PARAM_TYPE => 'integer',
96 ApiBase::PARAM_DFLT => -1,
97 ApiBase::PARAM_HELP_MSG => [
98 'apihelp-query+imageinfo-param-urlwidth',
99 ApiQueryImageInfo::TRANSFORM_LIMIT,
102 'urlheight' => [
103 ApiBase::PARAM_TYPE => 'integer',
104 ApiBase::PARAM_DFLT => -1,
105 ApiBase::PARAM_HELP_MSG => 'apihelp-query+imageinfo-param-urlheight',
107 'urlparam' => [
108 ApiBase::PARAM_TYPE => 'string',
109 ApiBase::PARAM_DFLT => '',
110 ApiBase::PARAM_HELP_MSG => 'apihelp-query+imageinfo-param-urlparam',
115 protected function getExamplesMessages() {
116 return [
117 'action=query&prop=stashimageinfo&siifilekey=124sd34rsdf567'
118 => 'apihelp-query+stashimageinfo-example-simple',
119 'action=query&prop=stashimageinfo&siifilekey=b34edoe3|bceffd4&' .
120 'siiurlwidth=120&siiprop=url'
121 => 'apihelp-query+stashimageinfo-example-params',
125 public function getHelpUrls() {
126 return 'https://www.mediawiki.org/wiki/API:Stashimageinfo';