Merge "Added a separate error message for mkdir failures"
[lhc/web/wiklou.git] / includes / api / ApiQueryStashImageInfo.php
1 <?php
2 /**
3 * API for MediaWiki 1.16+
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 /**
24 * A query action to get image information from temporarily stashed files.
25 *
26 * @ingroup API
27 */
28 class ApiQueryStashImageInfo extends ApiQueryImageInfo {
29
30 public function __construct( ApiQuery $query, $moduleName ) {
31 parent::__construct( $query, $moduleName, 'sii' );
32 }
33
34 public function execute() {
35 if ( !$this->getUser()->isLoggedIn() ) {
36 $this->dieWithError( 'apierror-mustbeloggedin-uploadstash', 'notloggedin' );
37 }
38
39 $params = $this->extractRequestParams();
40 $modulePrefix = $this->getModulePrefix();
41
42 $prop = array_flip( $params['prop'] );
43
44 $scale = $this->getScale( $params );
45
46 $result = $this->getResult();
47
48 $this->requireAtLeastOneParameter( $params, 'filekey', 'sessionkey' );
49
50 // Alias sessionkey to filekey, but give an existing filekey precedence.
51 if ( !$params['filekey'] && $params['sessionkey'] ) {
52 $params['filekey'] = $params['sessionkey'];
53 }
54
55 try {
56 $stash = RepoGroup::singleton()->getLocalRepo()->getUploadStash( $this->getUser() );
57
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 );
64 }
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' ] );
70 }
71 }
72
73 private $propertyFilter = [
74 'user', 'userid', 'comment', 'parsedcomment',
75 'mediatype', 'archivename', 'uploadwarning',
76 ];
77
78 public function getAllowedParams() {
79 return [
80 'filekey' => [
81 ApiBase::PARAM_ISMULTI => true,
82 ],
83 'sessionkey' => [
84 ApiBase::PARAM_ISMULTI => true,
85 ApiBase::PARAM_DEPRECATED => true,
86 ],
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 )
93 ],
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,
100 ],
101 ],
102 'urlheight' => [
103 ApiBase::PARAM_TYPE => 'integer',
104 ApiBase::PARAM_DFLT => -1,
105 ApiBase::PARAM_HELP_MSG => 'apihelp-query+imageinfo-param-urlheight',
106 ],
107 'urlparam' => [
108 ApiBase::PARAM_TYPE => 'string',
109 ApiBase::PARAM_DFLT => '',
110 ApiBase::PARAM_HELP_MSG => 'apihelp-query+imageinfo-param-urlparam',
111 ],
112 ];
113 }
114
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',
122 ];
123 }
124
125 public function getHelpUrls() {
126 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Stashimageinfo';
127 }
128 }