PageRenderTime 39ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/api/ApiQueryFilearchive.php

https://gitlab.com/qiusct/mediawiki-i
PHP | 400 lines | 334 code | 29 blank | 37 comment | 40 complexity | 610e70d3ba403dce0e951ad95dc735e6 MD5 | raw file
Possible License(s): Apache-2.0, MIT, GPL-2.0
  1. <?php
  2. /**
  3. * API for MediaWiki 1.12+
  4. *
  5. * Created on May 10, 2010
  6. *
  7. * Copyright © 2010 Sam Reed
  8. * Copyright © 2008 Vasiliev Victor vasilvv@gmail.com,
  9. * based on ApiQueryAllPages.php
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  24. * http://www.gnu.org/copyleft/gpl.html
  25. *
  26. * @file
  27. */
  28. /**
  29. * Query module to enumerate all deleted files.
  30. *
  31. * @ingroup API
  32. */
  33. class ApiQueryFilearchive extends ApiQueryBase {
  34. public function __construct( ApiQuery $query, $moduleName ) {
  35. parent::__construct( $query, $moduleName, 'fa' );
  36. }
  37. public function execute() {
  38. $user = $this->getUser();
  39. // Before doing anything at all, let's check permissions
  40. if ( !$user->isAllowed( 'deletedhistory' ) ) {
  41. $this->dieUsage(
  42. 'You don\'t have permission to view deleted file information',
  43. 'permissiondenied'
  44. );
  45. }
  46. $db = $this->getDB();
  47. $params = $this->extractRequestParams();
  48. $prop = array_flip( $params['prop'] );
  49. $fld_sha1 = isset( $prop['sha1'] );
  50. $fld_timestamp = isset( $prop['timestamp'] );
  51. $fld_user = isset( $prop['user'] );
  52. $fld_size = isset( $prop['size'] );
  53. $fld_dimensions = isset( $prop['dimensions'] );
  54. $fld_description = isset( $prop['description'] ) || isset( $prop['parseddescription'] );
  55. $fld_mime = isset( $prop['mime'] );
  56. $fld_mediatype = isset( $prop['mediatype'] );
  57. $fld_metadata = isset( $prop['metadata'] );
  58. $fld_bitdepth = isset( $prop['bitdepth'] );
  59. $fld_archivename = isset( $prop['archivename'] );
  60. $this->addTables( 'filearchive' );
  61. $this->addFields( ArchivedFile::selectFields() );
  62. $this->addFields( array( 'fa_name', 'fa_deleted' ) );
  63. $this->addFieldsIf( 'fa_sha1', $fld_sha1 );
  64. $this->addFieldsIf( 'fa_timestamp', $fld_timestamp );
  65. $this->addFieldsIf( array( 'fa_user', 'fa_user_text' ), $fld_user );
  66. $this->addFieldsIf( array( 'fa_height', 'fa_width', 'fa_size' ), $fld_dimensions || $fld_size );
  67. $this->addFieldsIf( 'fa_description', $fld_description );
  68. $this->addFieldsIf( array( 'fa_major_mime', 'fa_minor_mime' ), $fld_mime );
  69. $this->addFieldsIf( 'fa_media_type', $fld_mediatype );
  70. $this->addFieldsIf( 'fa_metadata', $fld_metadata );
  71. $this->addFieldsIf( 'fa_bits', $fld_bitdepth );
  72. $this->addFieldsIf( 'fa_archive_name', $fld_archivename );
  73. if ( !is_null( $params['continue'] ) ) {
  74. $cont = explode( '|', $params['continue'] );
  75. $this->dieContinueUsageIf( count( $cont ) != 1 );
  76. $op = $params['dir'] == 'descending' ? '<' : '>';
  77. $cont_from = $db->addQuotes( $cont[0] );
  78. $this->addWhere( "fa_name $op= $cont_from" );
  79. }
  80. // Image filters
  81. $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
  82. $from = ( $params['from'] === null ? null : $this->titlePartToKey( $params['from'], NS_FILE ) );
  83. if ( !is_null( $params['continue'] ) ) {
  84. $from = $params['continue'];
  85. }
  86. $to = ( $params['to'] === null ? null : $this->titlePartToKey( $params['to'], NS_FILE ) );
  87. $this->addWhereRange( 'fa_name', $dir, $from, $to );
  88. if ( isset( $params['prefix'] ) ) {
  89. $this->addWhere( 'fa_name' . $db->buildLike(
  90. $this->titlePartToKey( $params['prefix'], NS_FILE ),
  91. $db->anyString() ) );
  92. }
  93. $sha1Set = isset( $params['sha1'] );
  94. $sha1base36Set = isset( $params['sha1base36'] );
  95. if ( $sha1Set || $sha1base36Set ) {
  96. $sha1 = false;
  97. if ( $sha1Set ) {
  98. $sha1 = strtolower( $params['sha1'] );
  99. if ( !$this->validateSha1Hash( $sha1 ) ) {
  100. $this->dieUsage( 'The SHA1 hash provided is not valid', 'invalidsha1hash' );
  101. }
  102. $sha1 = wfBaseConvert( $sha1, 16, 36, 31 );
  103. } elseif ( $sha1base36Set ) {
  104. $sha1 = strtolower( $params['sha1base36'] );
  105. if ( !$this->validateSha1Base36Hash( $sha1 ) ) {
  106. $this->dieUsage( 'The SHA1Base36 hash provided is not valid', 'invalidsha1base36hash' );
  107. }
  108. }
  109. if ( $sha1 ) {
  110. $this->addWhereFld( 'fa_sha1', $sha1 );
  111. }
  112. }
  113. // Exclude files this user can't view.
  114. if ( !$user->isAllowed( 'deletedtext' ) ) {
  115. $bitmask = File::DELETED_FILE;
  116. } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
  117. $bitmask = File::DELETED_FILE | File::DELETED_RESTRICTED;
  118. } else {
  119. $bitmask = 0;
  120. }
  121. if ( $bitmask ) {
  122. $this->addWhere( $this->getDB()->bitAnd( 'fa_deleted', $bitmask ) . " != $bitmask" );
  123. }
  124. $limit = $params['limit'];
  125. $this->addOption( 'LIMIT', $limit + 1 );
  126. $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
  127. $this->addOption( 'ORDER BY', 'fa_name' . $sort );
  128. $res = $this->select( __METHOD__ );
  129. $count = 0;
  130. $result = $this->getResult();
  131. foreach ( $res as $row ) {
  132. if ( ++$count > $limit ) {
  133. // We've reached the one extra which shows that there are
  134. // additional pages to be had. Stop here...
  135. $this->setContinueEnumParameter( 'continue', $row->fa_name );
  136. break;
  137. }
  138. $file = array();
  139. $file['name'] = $row->fa_name;
  140. $title = Title::makeTitle( NS_FILE, $row->fa_name );
  141. self::addTitleInfo( $file, $title );
  142. if ( $fld_description &&
  143. Revision::userCanBitfield( $row->fa_deleted, File::DELETED_COMMENT, $user )
  144. ) {
  145. $file['description'] = $row->fa_description;
  146. if ( isset( $prop['parseddescription'] ) ) {
  147. $file['parseddescription'] = Linker::formatComment(
  148. $row->fa_description, $title );
  149. }
  150. }
  151. if ( $fld_user &&
  152. Revision::userCanBitfield( $row->fa_deleted, File::DELETED_USER, $user )
  153. ) {
  154. $file['userid'] = $row->fa_user;
  155. $file['user'] = $row->fa_user_text;
  156. }
  157. if ( $fld_sha1 ) {
  158. $file['sha1'] = wfBaseConvert( $row->fa_sha1, 36, 16, 40 );
  159. }
  160. if ( $fld_timestamp ) {
  161. $file['timestamp'] = wfTimestamp( TS_ISO_8601, $row->fa_timestamp );
  162. }
  163. if ( $fld_size || $fld_dimensions ) {
  164. $file['size'] = $row->fa_size;
  165. $pageCount = ArchivedFile::newFromRow( $row )->pageCount();
  166. if ( $pageCount !== false ) {
  167. $vals['pagecount'] = $pageCount;
  168. }
  169. $file['height'] = $row->fa_height;
  170. $file['width'] = $row->fa_width;
  171. }
  172. if ( $fld_mediatype ) {
  173. $file['mediatype'] = $row->fa_media_type;
  174. }
  175. if ( $fld_metadata ) {
  176. $file['metadata'] = $row->fa_metadata
  177. ? ApiQueryImageInfo::processMetaData( unserialize( $row->fa_metadata ), $result )
  178. : null;
  179. }
  180. if ( $fld_bitdepth ) {
  181. $file['bitdepth'] = $row->fa_bits;
  182. }
  183. if ( $fld_mime ) {
  184. $file['mime'] = "$row->fa_major_mime/$row->fa_minor_mime";
  185. }
  186. if ( $fld_archivename && !is_null( $row->fa_archive_name ) ) {
  187. $file['archivename'] = $row->fa_archive_name;
  188. }
  189. if ( $row->fa_deleted & File::DELETED_FILE ) {
  190. $file['filehidden'] = '';
  191. }
  192. if ( $row->fa_deleted & File::DELETED_COMMENT ) {
  193. $file['commenthidden'] = '';
  194. }
  195. if ( $row->fa_deleted & File::DELETED_USER ) {
  196. $file['userhidden'] = '';
  197. }
  198. if ( $row->fa_deleted & File::DELETED_RESTRICTED ) {
  199. // This file is deleted for normal admins
  200. $file['suppressed'] = '';
  201. }
  202. $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $file );
  203. if ( !$fit ) {
  204. $this->setContinueEnumParameter( 'continue', $row->fa_name );
  205. break;
  206. }
  207. }
  208. $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'fa' );
  209. }
  210. public function getAllowedParams() {
  211. return array(
  212. 'from' => null,
  213. 'continue' => null,
  214. 'to' => null,
  215. 'prefix' => null,
  216. 'limit' => array(
  217. ApiBase::PARAM_DFLT => 10,
  218. ApiBase::PARAM_TYPE => 'limit',
  219. ApiBase::PARAM_MIN => 1,
  220. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  221. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
  222. ),
  223. 'dir' => array(
  224. ApiBase::PARAM_DFLT => 'ascending',
  225. ApiBase::PARAM_TYPE => array(
  226. 'ascending',
  227. 'descending'
  228. )
  229. ),
  230. 'sha1' => null,
  231. 'sha1base36' => null,
  232. 'prop' => array(
  233. ApiBase::PARAM_DFLT => 'timestamp',
  234. ApiBase::PARAM_ISMULTI => true,
  235. ApiBase::PARAM_TYPE => array(
  236. 'sha1',
  237. 'timestamp',
  238. 'user',
  239. 'size',
  240. 'dimensions',
  241. 'description',
  242. 'parseddescription',
  243. 'mime',
  244. 'mediatype',
  245. 'metadata',
  246. 'bitdepth',
  247. 'archivename',
  248. ),
  249. ),
  250. );
  251. }
  252. public function getParamDescription() {
  253. return array(
  254. 'from' => 'The image title to start enumerating from',
  255. 'continue' => 'When more results are available, use this to continue',
  256. 'to' => 'The image title to stop enumerating at',
  257. 'prefix' => 'Search for all image titles that begin with this value',
  258. 'dir' => 'The direction in which to list',
  259. 'limit' => 'How many images to return in total',
  260. 'sha1' => "SHA1 hash of image. Overrides {$this->getModulePrefix()}sha1base36",
  261. 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
  262. 'prop' => array(
  263. 'What image information to get:',
  264. ' sha1 - Adds SHA-1 hash for the image',
  265. ' timestamp - Adds timestamp for the uploaded version',
  266. ' user - Adds user who uploaded the image version',
  267. ' size - Adds the size of the image in bytes and the height, ' .
  268. 'width and page count (if applicable)',
  269. ' dimensions - Alias for size',
  270. ' description - Adds description the image version',
  271. ' parseddescription - Parse the description on the version',
  272. ' mime - Adds MIME of the image',
  273. ' mediatype - Adds the media type of the image',
  274. ' metadata - Lists Exif metadata for the version of the image',
  275. ' bitdepth - Adds the bit depth of the version',
  276. ' archivename - Adds the file name of the archive version for non-latest versions'
  277. ),
  278. );
  279. }
  280. public function getResultProperties() {
  281. return array(
  282. '' => array(
  283. 'name' => 'string',
  284. 'ns' => 'namespace',
  285. 'title' => 'string',
  286. 'filehidden' => 'boolean',
  287. 'commenthidden' => 'boolean',
  288. 'userhidden' => 'boolean',
  289. 'suppressed' => 'boolean'
  290. ),
  291. 'sha1' => array(
  292. 'sha1' => 'string'
  293. ),
  294. 'timestamp' => array(
  295. 'timestamp' => 'timestamp'
  296. ),
  297. 'user' => array(
  298. 'userid' => 'integer',
  299. 'user' => 'string'
  300. ),
  301. 'size' => array(
  302. 'size' => 'integer',
  303. 'pagecount' => array(
  304. ApiBase::PROP_TYPE => 'integer',
  305. ApiBase::PROP_NULLABLE => true
  306. ),
  307. 'height' => 'integer',
  308. 'width' => 'integer'
  309. ),
  310. 'dimensions' => array(
  311. 'size' => 'integer',
  312. 'pagecount' => array(
  313. ApiBase::PROP_TYPE => 'integer',
  314. ApiBase::PROP_NULLABLE => true
  315. ),
  316. 'height' => 'integer',
  317. 'width' => 'integer'
  318. ),
  319. 'description' => array(
  320. 'description' => 'string'
  321. ),
  322. 'parseddescription' => array(
  323. 'description' => 'string',
  324. 'parseddescription' => 'string'
  325. ),
  326. 'metadata' => array(
  327. 'metadata' => 'string'
  328. ),
  329. 'bitdepth' => array(
  330. 'bitdepth' => 'integer'
  331. ),
  332. 'mime' => array(
  333. 'mime' => 'string'
  334. ),
  335. 'mediatype' => array(
  336. 'mediatype' => 'string'
  337. ),
  338. 'archivename' => array(
  339. 'archivename' => 'string'
  340. ),
  341. );
  342. }
  343. public function getDescription() {
  344. return 'Enumerate all deleted files sequentially.';
  345. }
  346. public function getPossibleErrors() {
  347. return array_merge( parent::getPossibleErrors(), array(
  348. array(
  349. 'code' => 'permissiondenied',
  350. 'info' => 'You don\'t have permission to view deleted file information'
  351. ),
  352. array( 'code' => 'hashsearchdisabled', 'info' => 'Search by hash disabled in Miser Mode' ),
  353. array( 'code' => 'invalidsha1hash', 'info' => 'The SHA-1 hash provided is not valid' ),
  354. array(
  355. 'code' => 'invalidsha1base36hash',
  356. 'info' => 'The SHA1Base36 hash provided is not valid'
  357. ),
  358. ) );
  359. }
  360. public function getExamples() {
  361. return array(
  362. 'api.php?action=query&list=filearchive' => array(
  363. 'Simple Use',
  364. 'Show a list of all deleted files',
  365. ),
  366. );
  367. }
  368. public function getHelpUrls() {
  369. return 'https://www.mediawiki.org/wiki/API:Filearchive';
  370. }
  371. }