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