Merge "Move up devunt's name to Developers"
[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->dieUsage( 'You must be logged-in to have an upload stash', '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 if ( !$params['filekey'] && !$params['sessionkey'] ) {
49 $this->dieUsage( 'One of filekey or sessionkey must be supplied', 'nofilekey' );
50 }
51
52 // Alias sessionkey to filekey, but give an existing filekey precedence.
53 if ( !$params['filekey'] && $params['sessionkey'] ) {
54 $params['filekey'] = $params['sessionkey'];
55 }
56
57 try {
58 $stash = RepoGroup::singleton()->getLocalRepo()->getUploadStash( $this->getUser() );
59
60 foreach ( $params['filekey'] as $filekey ) {
61 $file = $stash->getFile( $filekey );
62 $finalThumbParam = $this->mergeThumbParams( $file, $scale, $params['urlparam'] );
63 $imageInfo = ApiQueryImageInfo::getInfo( $file, $prop, $result, $finalThumbParam );
64 $result->addValue( [ 'query', $this->getModuleName() ], null, $imageInfo );
65 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], $modulePrefix );
66 }
67 // @todo Update exception handling here to understand current getFile exceptions
68 } catch ( UploadStashFileNotFoundException $e ) {
69 $this->dieUsage( 'File not found: ' . $e->getMessage(), 'invalidsessiondata' );
70 } catch ( UploadStashBadPathException $e ) {
71 $this->dieUsage( 'Bad path: ' . $e->getMessage(), 'invalidsessiondata' );
72 }
73 }
74
75 private $propertyFilter = [
76 'user', 'userid', 'comment', 'parsedcomment',
77 'mediatype', 'archivename', 'uploadwarning',
78 ];
79
80 public function getAllowedParams() {
81 return [
82 'filekey' => [
83 ApiBase::PARAM_ISMULTI => true,
84 ],
85 'sessionkey' => [
86 ApiBase::PARAM_ISMULTI => true,
87 ApiBase::PARAM_DEPRECATED => true,
88 ],
89 'prop' => [
90 ApiBase::PARAM_ISMULTI => true,
91 ApiBase::PARAM_DFLT => 'timestamp|url',
92 ApiBase::PARAM_TYPE => self::getPropertyNames( $this->propertyFilter ),
93 ApiBase::PARAM_HELP_MSG => 'apihelp-query+imageinfo-param-prop',
94 ApiBase::PARAM_HELP_MSG_PER_VALUE => self::getPropertyMessages( $this->propertyFilter )
95 ],
96 'urlwidth' => [
97 ApiBase::PARAM_TYPE => 'integer',
98 ApiBase::PARAM_DFLT => -1,
99 ApiBase::PARAM_HELP_MSG => [
100 'apihelp-query+imageinfo-param-urlwidth',
101 ApiQueryImageInfo::TRANSFORM_LIMIT,
102 ],
103 ],
104 'urlheight' => [
105 ApiBase::PARAM_TYPE => 'integer',
106 ApiBase::PARAM_DFLT => -1,
107 ApiBase::PARAM_HELP_MSG => 'apihelp-query+imageinfo-param-urlheight',
108 ],
109 'urlparam' => [
110 ApiBase::PARAM_TYPE => 'string',
111 ApiBase::PARAM_DFLT => '',
112 ApiBase::PARAM_HELP_MSG => 'apihelp-query+imageinfo-param-urlparam',
113 ],
114 ];
115 }
116
117 protected function getExamplesMessages() {
118 return [
119 'action=query&prop=stashimageinfo&siifilekey=124sd34rsdf567'
120 => 'apihelp-query+stashimageinfo-example-simple',
121 'action=query&prop=stashimageinfo&siifilekey=b34edoe3|bceffd4&' .
122 'siiurlwidth=120&siiprop=url'
123 => 'apihelp-query+stashimageinfo-example-params',
124 ];
125 }
126
127 public function getHelpUrls() {
128 return 'https://www.mediawiki.org/wiki/API:Stashimageinfo';
129 }
130 }