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