Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 74
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryStashImageInfo
0.00% covered (danger)
0.00%
0 / 74
0.00% covered (danger)
0.00%
0 / 7
182
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
56
 getPropertyNames
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPropertyMessages
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 getHelpUrls
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
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
23use MediaWiki\Page\File\BadFileLookup;
24use Wikimedia\ParamValidator\ParamValidator;
25
26/**
27 * A query action to get image information from temporarily stashed files.
28 *
29 * @ingroup API
30 */
31class ApiQueryStashImageInfo extends ApiQueryImageInfo {
32
33    private RepoGroup $repoGroup;
34
35    /**
36     * @param ApiQuery $query
37     * @param string $moduleName
38     * @param RepoGroup $repoGroup
39     * @param Language $contentLanguage
40     * @param BadFileLookup $badFileLookup
41     */
42    public function __construct(
43        ApiQuery $query,
44        $moduleName,
45        RepoGroup $repoGroup,
46        Language $contentLanguage,
47        BadFileLookup $badFileLookup
48    ) {
49        parent::__construct(
50            $query,
51            $moduleName,
52            'sii',
53            $repoGroup,
54            $contentLanguage,
55            $badFileLookup
56        );
57        $this->repoGroup = $repoGroup;
58    }
59
60    public function execute() {
61        if ( !$this->getUser()->isRegistered() ) {
62            $this->dieWithError( 'apierror-mustbeloggedin-uploadstash', 'notloggedin' );
63        }
64
65        $params = $this->extractRequestParams();
66        $modulePrefix = $this->getModulePrefix();
67
68        $prop = array_fill_keys( $params['prop'], true );
69
70        $scale = $this->getScale( $params );
71
72        $result = $this->getResult();
73
74        $this->requireAtLeastOneParameter( $params, 'filekey', 'sessionkey' );
75
76        // Alias sessionkey to filekey, but give an existing filekey precedence.
77        if ( !$params['filekey'] && $params['sessionkey'] ) {
78            $params['filekey'] = $params['sessionkey'];
79        }
80
81        try {
82            $stash = $this->repoGroup->getLocalRepo()->getUploadStash( $this->getUser() );
83
84            foreach ( $params['filekey'] as $filekey ) {
85                $file = $stash->getFile( $filekey );
86                $finalThumbParam = $this->mergeThumbParams( $file, $scale, $params['urlparam'] );
87                $imageInfo = ApiQueryImageInfo::getInfo( $file, $prop, $result, $finalThumbParam );
88                $result->addValue( [ 'query', $this->getModuleName() ], null, $imageInfo );
89                $result->addIndexedTagName( [ 'query', $this->getModuleName() ], $modulePrefix );
90            }
91        // @todo Update exception handling here to understand current getFile exceptions
92        } catch ( UploadStashFileNotFoundException $e ) {
93            $this->dieWithException( $e, [ 'wrap' => 'apierror-stashedfilenotfound' ] );
94        } catch ( UploadStashBadPathException $e ) {
95            $this->dieWithException( $e, [ 'wrap' => 'apierror-stashpathinvalid' ] );
96        }
97    }
98
99    private static $propertyFilter = [
100        'user', 'userid', 'comment', 'parsedcomment',
101        'mediatype', 'archivename', 'uploadwarning',
102    ];
103
104    /**
105     * Returns all possible parameters to siiprop
106     *
107     * @param array|null $filter List of properties to filter out
108     * @return array
109     */
110    public static function getPropertyNames( $filter = null ) {
111        return parent::getPropertyNames( $filter ?? self::$propertyFilter );
112    }
113
114    /**
115     * Returns messages for all possible parameters to siiprop
116     *
117     * @param array|null $filter List of properties to filter out
118     * @return array
119     */
120    public static function getPropertyMessages( $filter = null ) {
121        return parent::getPropertyMessages( $filter ?? self::$propertyFilter );
122    }
123
124    public function getAllowedParams() {
125        return [
126            'filekey' => [
127                ParamValidator::PARAM_ISMULTI => true,
128            ],
129            'sessionkey' => [
130                ParamValidator::PARAM_ISMULTI => true,
131                ParamValidator::PARAM_DEPRECATED => true,
132            ],
133            'prop' => [
134                ParamValidator::PARAM_ISMULTI => true,
135                ParamValidator::PARAM_DEFAULT => 'timestamp|url',
136                ParamValidator::PARAM_TYPE => self::getPropertyNames(),
137                ApiBase::PARAM_HELP_MSG => 'apihelp-query+imageinfo-param-prop',
138                ApiBase::PARAM_HELP_MSG_PER_VALUE => self::getPropertyMessages()
139            ],
140            'urlwidth' => [
141                ParamValidator::PARAM_TYPE => 'integer',
142                ParamValidator::PARAM_DEFAULT => -1,
143                ApiBase::PARAM_HELP_MSG => [
144                    'apihelp-query+imageinfo-param-urlwidth',
145                    ApiQueryImageInfo::TRANSFORM_LIMIT,
146                ],
147            ],
148            'urlheight' => [
149                ParamValidator::PARAM_TYPE => 'integer',
150                ParamValidator::PARAM_DEFAULT => -1,
151                ApiBase::PARAM_HELP_MSG => 'apihelp-query+imageinfo-param-urlheight',
152            ],
153            'urlparam' => [
154                ParamValidator::PARAM_TYPE => 'string',
155                ParamValidator::PARAM_DEFAULT => '',
156                ApiBase::PARAM_HELP_MSG => 'apihelp-query+imageinfo-param-urlparam',
157            ],
158        ];
159    }
160
161    protected function getExamplesMessages() {
162        return [
163            'action=query&prop=stashimageinfo&siifilekey=124sd34rsdf567'
164                => 'apihelp-query+stashimageinfo-example-simple',
165            'action=query&prop=stashimageinfo&siifilekey=b34edoe3|bceffd4&' .
166                'siiurlwidth=120&siiprop=url'
167                => 'apihelp-query+stashimageinfo-example-params',
168        ];
169    }
170
171    public function getHelpUrls() {
172        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Stashimageinfo';
173    }
174}