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