ApiQueryMyStashedFiles.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * API for MediaWiki 1.27+
  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. * action=query&list=mystashedfiles module, gets all stashed files for
  24. * the current user.
  25. *
  26. * @ingroup API
  27. */
  28. class ApiQueryMyStashedFiles extends ApiQueryBase {
  29. public function __construct( ApiQuery $query, $moduleName ) {
  30. parent::__construct( $query, $moduleName, 'msf' );
  31. }
  32. public function execute() {
  33. $user = $this->getUser();
  34. if ( $user->isAnon() ) {
  35. $this->dieWithError( 'apierror-mustbeloggedin-uploadstash', 'stashnotloggedin' );
  36. }
  37. // Note: If user is logged in but cannot upload, they can still see
  38. // the list of stashed uploads...but it will probably be empty.
  39. $params = $this->extractRequestParams();
  40. $this->addTables( 'uploadstash' );
  41. $this->addFields( [ 'us_id', 'us_key', 'us_status' ] );
  42. $this->addWhere( [ 'us_user' => $user->getId() ] );
  43. if ( $params['continue'] !== null ) {
  44. $cont = explode( '|', $params['continue'] );
  45. $this->dieContinueUsageIf( count( $cont ) != 1 );
  46. $cont_from = (int)$cont[0];
  47. $this->dieContinueUsageIf( strval( $cont_from ) !== $cont[0] );
  48. $this->addWhere( "us_id >= $cont_from" );
  49. }
  50. $this->addOption( 'LIMIT', $params['limit'] + 1 );
  51. $this->addOption( 'ORDER BY', 'us_id' );
  52. $prop = array_flip( $params['prop'] );
  53. $this->addFieldsIf(
  54. [
  55. 'us_size',
  56. 'us_image_width',
  57. 'us_image_height',
  58. 'us_image_bits'
  59. ],
  60. isset( $prop['size'] )
  61. );
  62. $this->addFieldsIf( [ 'us_mime', 'us_media_type' ], isset( $prop['type'] ) );
  63. $res = $this->select( __METHOD__ );
  64. $result = $this->getResult();
  65. $count = 0;
  66. foreach ( $res as $row ) {
  67. if ( ++$count > $params['limit'] ) {
  68. // We've reached the one extra which shows that there are
  69. // additional files to be had. Stop here...
  70. $this->setContinueEnumParameter( 'continue', $row->us_id );
  71. break;
  72. }
  73. $item = [
  74. 'filekey' => $row->us_key,
  75. 'status' => $row->us_status,
  76. ];
  77. if ( isset( $prop['size'] ) ) {
  78. $item['size'] = (int)$row->us_size;
  79. $item['width'] = (int)$row->us_image_width;
  80. $item['height'] = (int)$row->us_image_height;
  81. $item['bits'] = (int)$row->us_image_bits;
  82. }
  83. if ( isset( $prop['type'] ) ) {
  84. $item['mimetype'] = $row->us_mime;
  85. $item['mediatype'] = $row->us_media_type;
  86. }
  87. $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $item );
  88. if ( !$fit ) {
  89. $this->setContinueEnumParameter( 'continue', $row->us_id );
  90. break;
  91. }
  92. }
  93. $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'file' );
  94. }
  95. public function getAllowedParams() {
  96. return [
  97. 'prop' => [
  98. ApiBase::PARAM_ISMULTI => true,
  99. ApiBase::PARAM_DFLT => '',
  100. ApiBase::PARAM_TYPE => [ 'size', 'type' ],
  101. ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
  102. ],
  103. 'limit' => [
  104. ApiBase::PARAM_TYPE => 'limit',
  105. ApiBase::PARAM_DFLT => 10,
  106. ApiBase::PARAM_MIN => 1,
  107. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  108. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2,
  109. ],
  110. 'continue' => [
  111. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  112. ],
  113. ];
  114. }
  115. protected function getExamplesMessages() {
  116. return [
  117. 'action=query&list=mystashedfiles&msfprop=size'
  118. => 'apihelp-query+mystashedfiles-example-simple',
  119. ];
  120. }
  121. public function getHelpUrls() {
  122. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:mystashedfiles';
  123. }
  124. }