ApiQueryStashImageInfo.php 4.8 KB

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