ApiQueryFilearchive.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /**
  3. * API for MediaWiki 1.12+
  4. *
  5. * Copyright © 2010 Sam Reed
  6. * Copyright © 2008 Vasiliev Victor vasilvv@gmail.com,
  7. * based on ApiQueryAllPages.php
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. *
  24. * @file
  25. */
  26. use MediaWiki\Revision\RevisionRecord;
  27. /**
  28. * Query module to enumerate all deleted files.
  29. *
  30. * @ingroup API
  31. */
  32. class ApiQueryFilearchive extends ApiQueryBase {
  33. public function __construct( ApiQuery $query, $moduleName ) {
  34. parent::__construct( $query, $moduleName, 'fa' );
  35. }
  36. public function execute() {
  37. $user = $this->getUser();
  38. $db = $this->getDB();
  39. $commentStore = CommentStore::getStore();
  40. $params = $this->extractRequestParams();
  41. $prop = array_flip( $params['prop'] );
  42. $fld_sha1 = isset( $prop['sha1'] );
  43. $fld_timestamp = isset( $prop['timestamp'] );
  44. $fld_user = isset( $prop['user'] );
  45. $fld_size = isset( $prop['size'] );
  46. $fld_dimensions = isset( $prop['dimensions'] );
  47. $fld_description = isset( $prop['description'] ) || isset( $prop['parseddescription'] );
  48. $fld_mime = isset( $prop['mime'] );
  49. $fld_mediatype = isset( $prop['mediatype'] );
  50. $fld_metadata = isset( $prop['metadata'] );
  51. $fld_bitdepth = isset( $prop['bitdepth'] );
  52. $fld_archivename = isset( $prop['archivename'] );
  53. if ( $fld_description &&
  54. !$this->getPermissionManager()->userHasRight( $user, 'deletedhistory' )
  55. ) {
  56. $this->dieWithError( 'apierror-cantview-deleted-description', 'permissiondenied' );
  57. }
  58. if ( $fld_metadata &&
  59. !$this->getPermissionManager()->userHasAnyRight( $user, 'deletedtext', 'undelete' )
  60. ) {
  61. $this->dieWithError( 'apierror-cantview-deleted-metadata', 'permissiondenied' );
  62. }
  63. $fileQuery = ArchivedFile::getQueryInfo();
  64. $this->addTables( $fileQuery['tables'] );
  65. $this->addFields( $fileQuery['fields'] );
  66. $this->addJoinConds( $fileQuery['joins'] );
  67. if ( !is_null( $params['continue'] ) ) {
  68. $cont = explode( '|', $params['continue'] );
  69. $this->dieContinueUsageIf( count( $cont ) != 3 );
  70. $op = $params['dir'] == 'descending' ? '<' : '>';
  71. $cont_from = $db->addQuotes( $cont[0] );
  72. $cont_timestamp = $db->addQuotes( $db->timestamp( $cont[1] ) );
  73. $cont_id = (int)$cont[2];
  74. $this->dieContinueUsageIf( $cont[2] !== (string)$cont_id );
  75. $this->addWhere( "fa_name $op $cont_from OR " .
  76. "(fa_name = $cont_from AND " .
  77. "(fa_timestamp $op $cont_timestamp OR " .
  78. "(fa_timestamp = $cont_timestamp AND " .
  79. "fa_id $op= $cont_id )))"
  80. );
  81. }
  82. // Image filters
  83. $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
  84. $from = ( $params['from'] === null ? null : $this->titlePartToKey( $params['from'], NS_FILE ) );
  85. $to = ( $params['to'] === null ? null : $this->titlePartToKey( $params['to'], NS_FILE ) );
  86. $this->addWhereRange( 'fa_name', $dir, $from, $to );
  87. if ( isset( $params['prefix'] ) ) {
  88. $this->addWhere( 'fa_name' . $db->buildLike(
  89. $this->titlePartToKey( $params['prefix'], NS_FILE ),
  90. $db->anyString() ) );
  91. }
  92. $sha1Set = isset( $params['sha1'] );
  93. $sha1base36Set = isset( $params['sha1base36'] );
  94. if ( $sha1Set || $sha1base36Set ) {
  95. $sha1 = false;
  96. if ( $sha1Set ) {
  97. $sha1 = strtolower( $params['sha1'] );
  98. if ( !$this->validateSha1Hash( $sha1 ) ) {
  99. $this->dieWithError( 'apierror-invalidsha1hash' );
  100. }
  101. $sha1 = Wikimedia\base_convert( $sha1, 16, 36, 31 );
  102. } elseif ( $sha1base36Set ) {
  103. $sha1 = strtolower( $params['sha1base36'] );
  104. if ( !$this->validateSha1Base36Hash( $sha1 ) ) {
  105. $this->dieWithError( 'apierror-invalidsha1base36hash' );
  106. }
  107. }
  108. if ( $sha1 ) {
  109. $this->addWhereFld( 'fa_sha1', $sha1 );
  110. // Paranoia: avoid brute force searches (T19342)
  111. if ( !$this->getPermissionManager()->userHasRight( $user, 'deletedtext' ) ) {
  112. $bitmask = File::DELETED_FILE;
  113. } elseif ( !$this->getPermissionManager()
  114. ->userHasAnyRight( $user, 'suppressrevision', 'viewsuppressed' )
  115. ) {
  116. $bitmask = File::DELETED_FILE | File::DELETED_RESTRICTED;
  117. } else {
  118. $bitmask = 0;
  119. }
  120. if ( $bitmask ) {
  121. $this->addWhere( $this->getDB()->bitAnd( 'fa_deleted', $bitmask ) . " != $bitmask" );
  122. }
  123. }
  124. }
  125. $limit = $params['limit'];
  126. $this->addOption( 'LIMIT', $limit + 1 );
  127. $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
  128. $this->addOption( 'ORDER BY', [
  129. 'fa_name' . $sort,
  130. 'fa_timestamp' . $sort,
  131. 'fa_id' . $sort,
  132. ] );
  133. $res = $this->select( __METHOD__ );
  134. $count = 0;
  135. $result = $this->getResult();
  136. foreach ( $res as $row ) {
  137. if ( ++$count > $limit ) {
  138. // We've reached the one extra which shows that there are
  139. // additional pages to be had. Stop here...
  140. $this->setContinueEnumParameter(
  141. 'continue', "$row->fa_name|$row->fa_timestamp|$row->fa_id"
  142. );
  143. break;
  144. }
  145. $canViewFile = RevisionRecord::userCanBitfield( $row->fa_deleted, File::DELETED_FILE, $user );
  146. $file = [];
  147. $file['id'] = (int)$row->fa_id;
  148. $file['name'] = $row->fa_name;
  149. $title = Title::makeTitle( NS_FILE, $row->fa_name );
  150. self::addTitleInfo( $file, $title );
  151. if ( $fld_description &&
  152. RevisionRecord::userCanBitfield( $row->fa_deleted, File::DELETED_COMMENT, $user )
  153. ) {
  154. $file['description'] = $commentStore->getComment( 'fa_description', $row )->text;
  155. if ( isset( $prop['parseddescription'] ) ) {
  156. $file['parseddescription'] = Linker::formatComment(
  157. $file['description'], $title );
  158. }
  159. }
  160. if ( $fld_user &&
  161. RevisionRecord::userCanBitfield( $row->fa_deleted, File::DELETED_USER, $user )
  162. ) {
  163. $file['userid'] = (int)$row->fa_user;
  164. $file['user'] = $row->fa_user_text;
  165. }
  166. if ( $fld_sha1 && $canViewFile ) {
  167. $file['sha1'] = Wikimedia\base_convert( $row->fa_sha1, 36, 16, 40 );
  168. }
  169. if ( $fld_timestamp ) {
  170. $file['timestamp'] = wfTimestamp( TS_ISO_8601, $row->fa_timestamp );
  171. }
  172. if ( ( $fld_size || $fld_dimensions ) && $canViewFile ) {
  173. $file['size'] = $row->fa_size;
  174. $pageCount = ArchivedFile::newFromRow( $row )->pageCount();
  175. if ( $pageCount !== false ) {
  176. $file['pagecount'] = $pageCount;
  177. }
  178. $file['height'] = $row->fa_height;
  179. $file['width'] = $row->fa_width;
  180. }
  181. if ( $fld_mediatype && $canViewFile ) {
  182. $file['mediatype'] = $row->fa_media_type;
  183. }
  184. if ( $fld_metadata && $canViewFile ) {
  185. $file['metadata'] = $row->fa_metadata
  186. ? ApiQueryImageInfo::processMetaData( unserialize( $row->fa_metadata ), $result )
  187. : null;
  188. }
  189. if ( $fld_bitdepth && $canViewFile ) {
  190. $file['bitdepth'] = $row->fa_bits;
  191. }
  192. if ( $fld_mime && $canViewFile ) {
  193. $file['mime'] = "$row->fa_major_mime/$row->fa_minor_mime";
  194. }
  195. if ( $fld_archivename && !is_null( $row->fa_archive_name ) ) {
  196. $file['archivename'] = $row->fa_archive_name;
  197. }
  198. if ( $row->fa_deleted & File::DELETED_FILE ) {
  199. $file['filehidden'] = true;
  200. }
  201. if ( $row->fa_deleted & File::DELETED_COMMENT ) {
  202. $file['commenthidden'] = true;
  203. }
  204. if ( $row->fa_deleted & File::DELETED_USER ) {
  205. $file['userhidden'] = true;
  206. }
  207. if ( $row->fa_deleted & File::DELETED_RESTRICTED ) {
  208. // This file is deleted for normal admins
  209. $file['suppressed'] = true;
  210. }
  211. $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $file );
  212. if ( !$fit ) {
  213. $this->setContinueEnumParameter(
  214. 'continue', "$row->fa_name|$row->fa_timestamp|$row->fa_id"
  215. );
  216. break;
  217. }
  218. }
  219. $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'fa' );
  220. }
  221. public function getAllowedParams() {
  222. return [
  223. 'from' => null,
  224. 'to' => null,
  225. 'prefix' => null,
  226. 'dir' => [
  227. ApiBase::PARAM_DFLT => 'ascending',
  228. ApiBase::PARAM_TYPE => [
  229. 'ascending',
  230. 'descending'
  231. ]
  232. ],
  233. 'sha1' => null,
  234. 'sha1base36' => null,
  235. 'prop' => [
  236. ApiBase::PARAM_DFLT => 'timestamp',
  237. ApiBase::PARAM_ISMULTI => true,
  238. ApiBase::PARAM_TYPE => [
  239. 'sha1',
  240. 'timestamp',
  241. 'user',
  242. 'size',
  243. 'dimensions',
  244. 'description',
  245. 'parseddescription',
  246. 'mime',
  247. 'mediatype',
  248. 'metadata',
  249. 'bitdepth',
  250. 'archivename',
  251. ],
  252. ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
  253. ],
  254. 'limit' => [
  255. ApiBase::PARAM_DFLT => 10,
  256. ApiBase::PARAM_TYPE => 'limit',
  257. ApiBase::PARAM_MIN => 1,
  258. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  259. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
  260. ],
  261. 'continue' => [
  262. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  263. ],
  264. ];
  265. }
  266. protected function getExamplesMessages() {
  267. return [
  268. 'action=query&list=filearchive'
  269. => 'apihelp-query+filearchive-example-simple',
  270. ];
  271. }
  272. public function getHelpUrls() {
  273. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Filearchive';
  274. }
  275. }