Merge "API: Expose wfIsBadImage() in prop=imageinfo"
[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 // @todo Internationalize the exceptions
67 } catch ( UploadStashFileNotFoundException $e ) {
68 $this->dieWithError( [ 'apierror-stashedfilenotfound', wfEscapeWikiText( $e->getMessage() ) ] );
69 } catch ( UploadStashBadPathException $e ) {
70 $this->dieWithError( [ 'apierror-stashpathinvalid', wfEscapeWikiText( $e->getMessage() ) ] );
71 }
72 }
73
74 private $propertyFilter = [
75 'user', 'userid', 'comment', 'parsedcomment',
76 'mediatype', 'archivename', 'uploadwarning',
77 ];
78
79 public function getAllowedParams() {
80 return [
81 'filekey' => [
82 ApiBase::PARAM_ISMULTI => true,
83 ],
84 'sessionkey' => [
85 ApiBase::PARAM_ISMULTI => true,
86 ApiBase::PARAM_DEPRECATED => true,
87 ],
88 'prop' => [
89 ApiBase::PARAM_ISMULTI => true,
90 ApiBase::PARAM_DFLT => 'timestamp|url',
91 ApiBase::PARAM_TYPE => self::getPropertyNames( $this->propertyFilter ),
92 ApiBase::PARAM_HELP_MSG => 'apihelp-query+imageinfo-param-prop',
93 ApiBase::PARAM_HELP_MSG_PER_VALUE => self::getPropertyMessages( $this->propertyFilter )
94 ],
95 'urlwidth' => [
96 ApiBase::PARAM_TYPE => 'integer',
97 ApiBase::PARAM_DFLT => -1,
98 ApiBase::PARAM_HELP_MSG => [
99 'apihelp-query+imageinfo-param-urlwidth',
100 ApiQueryImageInfo::TRANSFORM_LIMIT,
101 ],
102 ],
103 'urlheight' => [
104 ApiBase::PARAM_TYPE => 'integer',
105 ApiBase::PARAM_DFLT => -1,
106 ApiBase::PARAM_HELP_MSG => 'apihelp-query+imageinfo-param-urlheight',
107 ],
108 'urlparam' => [
109 ApiBase::PARAM_TYPE => 'string',
110 ApiBase::PARAM_DFLT => '',
111 ApiBase::PARAM_HELP_MSG => 'apihelp-query+imageinfo-param-urlparam',
112 ],
113 ];
114 }
115
116 protected function getExamplesMessages() {
117 return [
118 'action=query&prop=stashimageinfo&siifilekey=124sd34rsdf567'
119 => 'apihelp-query+stashimageinfo-example-simple',
120 'action=query&prop=stashimageinfo&siifilekey=b34edoe3|bceffd4&' .
121 'siiurlwidth=120&siiprop=url'
122 => 'apihelp-query+stashimageinfo-example-params',
123 ];
124 }
125
126 public function getHelpUrls() {
127 return 'https://www.mediawiki.org/wiki/API:Stashimageinfo';
128 }
129 }