MediaWiki  master
ApiQueryStashImageInfo.php
Go to the documentation of this file.
1 <?php
25 
32 
34  private $repoGroup;
35 
43  public function __construct(
44  ApiQuery $query,
45  $moduleName,
46  RepoGroup $repoGroup,
47  Language $contentLanguage,
48  BadFileLookup $badFileLookup
49  ) {
50  parent::__construct(
51  $query,
52  $moduleName,
53  'sii',
54  $repoGroup,
55  $contentLanguage,
56  $badFileLookup
57  );
58  $this->repoGroup = $repoGroup;
59  }
60 
61  public function execute() {
62  if ( !$this->getUser()->isRegistered() ) {
63  $this->dieWithError( 'apierror-mustbeloggedin-uploadstash', 'notloggedin' );
64  }
65 
66  $params = $this->extractRequestParams();
67  $modulePrefix = $this->getModulePrefix();
68 
69  $prop = array_fill_keys( $params['prop'], true );
70 
71  $scale = $this->getScale( $params );
72 
73  $result = $this->getResult();
74 
75  $this->requireAtLeastOneParameter( $params, 'filekey', 'sessionkey' );
76 
77  // Alias sessionkey to filekey, but give an existing filekey precedence.
78  if ( !$params['filekey'] && $params['sessionkey'] ) {
79  $params['filekey'] = $params['sessionkey'];
80  }
81 
82  try {
83  $stash = $this->repoGroup->getLocalRepo()->getUploadStash( $this->getUser() );
84 
85  foreach ( $params['filekey'] as $filekey ) {
86  $file = $stash->getFile( $filekey );
87  $finalThumbParam = $this->mergeThumbParams( $file, $scale, $params['urlparam'] );
88  $imageInfo = ApiQueryImageInfo::getInfo( $file, $prop, $result, $finalThumbParam );
89  $result->addValue( [ 'query', $this->getModuleName() ], null, $imageInfo );
90  $result->addIndexedTagName( [ 'query', $this->getModuleName() ], $modulePrefix );
91  }
92  // @todo Update exception handling here to understand current getFile exceptions
93  } catch ( UploadStashFileNotFoundException $e ) {
94  $this->dieWithException( $e, [ 'wrap' => 'apierror-stashedfilenotfound' ] );
95  } catch ( UploadStashBadPathException $e ) {
96  $this->dieWithException( $e, [ 'wrap' => 'apierror-stashpathinvalid' ] );
97  }
98  }
99 
100  private static $propertyFilter = [
101  'user', 'userid', 'comment', 'parsedcomment',
102  'mediatype', 'archivename', 'uploadwarning',
103  ];
104 
111  public static function getPropertyNames( $filter = null ) {
112  return parent::getPropertyNames( $filter ?? self::$propertyFilter );
113  }
114 
121  public static function getPropertyMessages( $filter = null ) {
122  return parent::getPropertyMessages( $filter ?? self::$propertyFilter );
123  }
124 
125  public function getAllowedParams() {
126  return [
127  'filekey' => [
128  ParamValidator::PARAM_ISMULTI => true,
129  ],
130  'sessionkey' => [
131  ParamValidator::PARAM_ISMULTI => true,
132  ParamValidator::PARAM_DEPRECATED => true,
133  ],
134  'prop' => [
135  ParamValidator::PARAM_ISMULTI => true,
136  ParamValidator::PARAM_DEFAULT => 'timestamp|url',
137  ParamValidator::PARAM_TYPE => self::getPropertyNames(),
138  ApiBase::PARAM_HELP_MSG => 'apihelp-query+imageinfo-param-prop',
140  ],
141  'urlwidth' => [
142  ParamValidator::PARAM_TYPE => 'integer',
143  ParamValidator::PARAM_DEFAULT => -1,
145  'apihelp-query+imageinfo-param-urlwidth',
147  ],
148  ],
149  'urlheight' => [
150  ParamValidator::PARAM_TYPE => 'integer',
151  ParamValidator::PARAM_DEFAULT => -1,
152  ApiBase::PARAM_HELP_MSG => 'apihelp-query+imageinfo-param-urlheight',
153  ],
154  'urlparam' => [
155  ParamValidator::PARAM_TYPE => 'string',
156  ParamValidator::PARAM_DEFAULT => '',
157  ApiBase::PARAM_HELP_MSG => 'apihelp-query+imageinfo-param-urlparam',
158  ],
159  ];
160  }
161 
162  protected function getExamplesMessages() {
163  return [
164  'action=query&prop=stashimageinfo&siifilekey=124sd34rsdf567'
165  => 'apihelp-query+stashimageinfo-example-simple',
166  'action=query&prop=stashimageinfo&siifilekey=b34edoe3|bceffd4&' .
167  'siiurlwidth=120&siiprop=url'
168  => 'apihelp-query+stashimageinfo-example-params',
169  ];
170  }
171 
172  public function getHelpUrls() {
173  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Stashimageinfo';
174  }
175 }
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition: ApiBase.php:1460
getModulePrefix()
Get parameter prefix (usually two letters or an empty string).
Definition: ApiBase.php:514
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition: ApiBase.php:204
requireAtLeastOneParameter( $params,... $required)
Die if none of a certain set of parameters is set and not false.
Definition: ApiBase.php:971
getResult()
Get the result object.
Definition: ApiBase.php:637
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:773
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:166
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:506
dieWithException(Throwable $exception, array $options=[])
Abort execution with an error derived from a throwable.
Definition: ApiBase.php:1473
A query action to get image information and upload history.
mergeThumbParams( $image, $thumbParams, $otherParams)
Validate and merge scale parameters with handler thumb parameters, give error if invalid.
getScale( $params)
From parameters, construct a 'scale' array.
static getInfo( $file, $prop, $result, $thumbParams=null, $opts=false)
Get result information for an image revision.
A query action to get image information from temporarily stashed files.
getHelpUrls()
Return links to more detailed help pages about the module.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
static getPropertyMessages( $filter=null)
Returns messages for all possible parameters to siiprop.
static getPropertyNames( $filter=null)
Returns all possible parameters to siiprop.
__construct(ApiQuery $query, $moduleName, RepoGroup $repoGroup, Language $contentLanguage, BadFileLookup $badFileLookup)
getExamplesMessages()
Returns usage examples for this module.
This is the main query class.
Definition: ApiQuery.php:42
Base class for language-specific code.
Definition: Language.php:56
Prioritized list of file repositories.
Definition: RepoGroup.php:30
Service for formatting and validating API parameters.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42