Merge "Check validity and availability of usernames during signup via AJAX"
[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( $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 $params['filekey'] = $params['sessionkey'];
51 }
52
53 try {
54 $stash = RepoGroup::singleton()->getLocalRepo()->getUploadStash();
55
56 foreach ( $params['filekey'] as $filekey ) {
57 $file = $stash->getFile( $filekey );
58 $finalThumbParam = $this->mergeThumbParams( $file, $scale, $params['urlparam'] );
59 $imageInfo = ApiQueryImageInfo::getInfo( $file, $prop, $result, $finalThumbParam );
60 $result->addValue( array( 'query', $this->getModuleName() ), null, $imageInfo );
61 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), $modulePrefix );
62 }
63 // @todo Update exception handling here to understand current getFile exceptions
64 } catch ( UploadStashNotAvailableException $e ) {
65 $this->dieUsage( "Session not available: " . $e->getMessage(), "nosession" );
66 } catch ( UploadStashFileNotFoundException $e ) {
67 $this->dieUsage( "File not found: " . $e->getMessage(), "invalidsessiondata" );
68 } catch ( UploadStashBadPathException $e ) {
69 $this->dieUsage( "Bad path: " . $e->getMessage(), "invalidsessiondata" );
70 }
71 }
72
73 private $propertyFilter = array(
74 'user', 'userid', 'comment', 'parsedcomment',
75 'mediatype', 'archivename',
76 );
77
78 public function getAllowedParams() {
79 return array(
80 'filekey' => array(
81 ApiBase::PARAM_ISMULTI => true,
82 ApiBase::PARAM_DFLT => null
83 ),
84 'sessionkey' => array(
85 ApiBase::PARAM_ISMULTI => true,
86 ApiBase::PARAM_DEPRECATED => true,
87 ApiBase::PARAM_DFLT => null
88 ),
89 'prop' => array(
90 ApiBase::PARAM_ISMULTI => true,
91 ApiBase::PARAM_DFLT => 'timestamp|url',
92 ApiBase::PARAM_TYPE => self::getPropertyNames( $this->propertyFilter )
93 ),
94 'urlwidth' => array(
95 ApiBase::PARAM_TYPE => 'integer',
96 ApiBase::PARAM_DFLT => -1
97 ),
98 'urlheight' => array(
99 ApiBase::PARAM_TYPE => 'integer',
100 ApiBase::PARAM_DFLT => -1
101 ),
102 'urlparam' => array(
103 ApiBase::PARAM_TYPE => 'string',
104 ApiBase::PARAM_DFLT => '',
105 ),
106 );
107 }
108
109 /**
110 * Return the API documentation for the parameters.
111 * @return Array parameter documentation.
112 */
113 public function getParamDescription() {
114 $p = $this->getModulePrefix();
115
116 return array(
117 'prop' => self::getPropertyDescriptions( $this->propertyFilter, $p ),
118 'filekey' => 'Key that identifies a previous upload that was stashed temporarily.',
119 'sessionkey' => 'Alias for filekey, for backward compatibility.',
120 'urlwidth' => "If {$p}prop=url is set, a URL to an image scaled to this width will be returned.",
121 'urlheight' => "Similar to {$p}urlwidth. Cannot be used without {$p}urlwidth",
122 'urlparam' => array( "A handler specific parameter string. For example, pdf's ",
123 "might use 'page15-100px'. {$p}urlwidth must be used and be consistent with {$p}urlparam" ),
124 );
125 }
126
127 public function getResultProperties() {
128 return ApiQueryImageInfo::getResultPropertiesFiltered( $this->propertyFilter );
129 }
130
131 public function getDescription() {
132 return 'Returns image information for stashed images.';
133 }
134
135 public function getExamples() {
136 return array(
137 'api.php?action=query&prop=stashimageinfo&siifilekey=124sd34rsdf567',
138 'api.php?action=query&prop=stashimageinfo&siifilekey=b34edoe3|bceffd4&' .
139 'siiurlwidth=120&siiprop=url',
140 );
141 }
142 }