From: Sam Reed Date: Mon, 10 May 2010 22:41:16 +0000 (+0000) Subject: * (bug 21346) Make deleted images searchable by hash X-Git-Tag: 1.31.0-rc.0~36893 X-Git-Url: https://git.cyclocoop.org/%7B%24admin_url%7Dmembres/modifier.php?a=commitdiff_plain;h=e4eca2c65e3625def76d9da67950291586e6ea41;p=lhc%2Fweb%2Fwiklou.git * (bug 21346) Make deleted images searchable by hash Based roughly on ApiQueryAllimages --- diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 1e224e01da..a308a8274f 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -172,6 +172,7 @@ in a negative namespace (which is invalid). * (bug 19721) API action=help should have a way to just list for a specific module * (bug 23458) Add support for pageid parameter to action=parse requests * (bug 23460) Parse action should have a section option +* (bug 21346) Make deleted images searchable by hash === Languages updated in 1.17 === diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 4074a2631a..55f9c48415 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -313,6 +313,7 @@ $wgAutoloadLocalClasses = array( 'ApiQueryDisabled' => 'includes/api/ApiQueryDisabled.php', 'ApiQueryDuplicateFiles' => 'includes/api/ApiQueryDuplicateFiles.php', 'ApiQueryExtLinksUsage' => 'includes/api/ApiQueryExtLinksUsage.php', + 'ApiQueryFilearchive' => 'includes/api/ApiQueryFilearchive.php', 'ApiQueryExternalLinks' => 'includes/api/ApiQueryExternalLinks.php', 'ApiQueryGeneratorBase' => 'includes/api/ApiQueryBase.php', 'ApiQueryImageInfo' => 'includes/api/ApiQueryImageInfo.php', diff --git a/includes/api/ApiQuery.php b/includes/api/ApiQuery.php index ed84a70647..452c32688d 100644 --- a/includes/api/ApiQuery.php +++ b/includes/api/ApiQuery.php @@ -70,6 +70,7 @@ class ApiQuery extends ApiBase { 'categorymembers' => 'ApiQueryCategoryMembers', 'deletedrevs' => 'ApiQueryDeletedrevs', 'embeddedin' => 'ApiQueryBacklinks', + 'filearchive' => 'ApiQueryFilearchive', 'imageusage' => 'ApiQueryBacklinks', 'logevents' => 'ApiQueryLogEvents', 'recentchanges' => 'ApiQueryRecentChanges', diff --git a/includes/api/ApiQueryFilearchive.php b/includes/api/ApiQueryFilearchive.php new file mode 100644 index 0000000000..f50c035552 --- /dev/null +++ b/includes/api/ApiQueryFilearchive.php @@ -0,0 +1,249 @@ +isAllowed( 'deletedhistory' ) ) { + $this->dieUsage( 'You don\'t have permission to view deleted file information', 'permissiondenied' ); + } + + $db = $this->getDB(); + + $params = $this->extractRequestParams(); + + $prop = array_flip( $params['prop'] ); + $fld_id = isset( $prop['id'] ); + $fld_sha1 = isset( $prop['sha1'] ); + $fld_timestamp = isset( $prop['timestamp'] ); + $fld_user = isset( $prop['user'] ); + $fld_size = isset( $prop['size'] ); + $fld_dimensions = isset( $prop['dimensions'] ); + $fld_description = isset( $prop['description'] ); + $fld_mime = isset( $prop['mime'] ); + $fld_metadata = isset( $prop['metadata'] ); + $fld_bitdepth = isset( $prop['bitdepth'] ); + + $this->addTables( 'filearchive' ); + + $this->addFields( 'fa_name' ); + $this->addFieldsIf( 'fa_storage_key', $fld_sha1 ); + $this->addFieldsIf( 'fa_timestamp', $fld_timestamp ); + $this->addFieldsIf( 'fa_user', $fld_user ); + $this->addFieldsIf( 'fa_size', $fld_size ); + + if ( $fld_dimensions ) { + $this->addFields( 'fa_height' ); + $this->addFields( 'fa_width' ); + } + + $this->addFieldsIf( 'fa_description', $fld_description ); + + if ( $fld_mime ) { + $this->addFields( 'fa_major_mime' ); + $this->addFields( 'fa_minor_mime' ); + } + + $this->addFieldsIf( 'fa_metadata', $fld_metadata ); + $this->addFieldsIf( 'fa_bitdepth', $fld_bitdepths ); + + // Image filters + $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' ); + $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) ); + $this->addWhereRange( 'fa_name', $dir, $from, null ); + if ( isset( $params['prefix'] ) ) + $this->addWhere( 'fa_name' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) ); + + if ( isset( $params['minsize'] ) ) { + $this->addWhere( 'fa_size>=' . intval( $params['minsize'] ) ); + } + + if ( isset( $params['maxsize'] ) ) { + $this->addWhere( 'fa_size<=' . intval( $params['maxsize'] ) ); + } + + $sha1 = false; + if ( isset( $params['sha1'] ) ) { + $sha1 = wfBaseConvert( $params['sha1'], 16, 36, 31 ); + } elseif ( isset( $params['sha1base36'] ) ) { + $sha1 = $params['sha1base36']; + } + if ( $sha1 ) { + $this->addWhere( 'fa_storage_key=' . $db->addQuotes( $sha1 ) ); + } + + $limit = $params['limit']; + $this->addOption( 'LIMIT', $limit + 1 ); + $this->addOption( 'ORDER BY', 'fa_name' . + ( $params['dir'] == 'descending' ? ' DESC' : '' ) ); + + $res = $this->select( __METHOD__ ); + + $titles = array(); + $count = 0; + $result = $this->getResult(); + while ( $row = $db->fetchObject( $res ) ) { + if ( ++$count > $limit ) { + // We've reached the one extra which shows that there are additional pages to be had. Stop here... + $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->fa_name ) ); + break; + } + + $file = array(); + $file['name'] = $row->fa_name; + if ( $fld_sha1 ) { + $file['sha1'] = $row->fa_storage_key; + } + if ( $fld_timestamp ) { + $file['timestamp'] = $row->fa_timestamp; + } + if ( $fld_user ) { + $file['user'] = $row->fa_user; + } + if ( $fld_size ) { + $file['size'] = $row->fa_size; + } + if ( $fld_dimensions ) { + $file['height'] = $row->fa_height; + $file['width'] = $row->fa_width; + } + if ( $fld_description ){ + $file['description'] = $row->fa_description; + } + if ( $fld_metadata ){ + $file['metadata'] = $row->fa_metadata; + } + if ( $fld_bitdepth ){ + $file['bitdepth'] = $row->fa_bitdepth; + } + if ( $fld_mime ) { + $file['mime'] = "$row->fa_major_mime/$row->fa_minor_mime"; + } + + $result->addValue( array( 'query', $this->getModuleName() ), null, $file ); + } + $db->freeResult( $res ); + + $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'fa' ); + } + + public function getAllowedParams() { + return array ( + 'from' => null, + 'prefix' => null, + 'minsize' => array( + ApiBase::PARAM_TYPE => 'integer', + ), + 'maxsize' => array( + ApiBase::PARAM_TYPE => 'integer', + ), + 'limit' => array( + ApiBase::PARAM_DFLT => 10, + ApiBase::PARAM_TYPE => 'limit', + ApiBase::PARAM_MIN => 1, + ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1, + ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2 + ), + 'dir' => array( + ApiBase::PARAM_DFLT => 'ascending', + ApiBase::PARAM_TYPE => array( + 'ascending', + 'descending' + ) + ), + 'sha1' => null, + 'sha1base36' => null, + 'prop' => array( + ApiBase::PARAM_DFLT => 'timestamp', + ApiBase::PARAM_ISMULTI => true, + ApiBase::PARAM_TYPE => array( + 'sha1', + 'timestamp', + 'user', + 'size', + 'dimensions', + 'description', + 'mime', + 'metadata', + 'bitdepth' + ), + ), + ); + } + + public function getParamDescription() { + return array( + 'from' => 'The image title to start enumerating from.', + 'prefix' => 'Search for all image titles that begin with this value.', + 'dir' => 'The direction in which to list', + 'minsize' => 'Limit to images with at least this many bytes', + 'maxsize' => 'Limit to images with at most this many bytes', + 'limit' => 'How many total images to return.', + 'sha1' => 'SHA1 hash of image', + 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)', + 'prop' => 'Which properties to get', + ); + } + + public function getDescription() { + return 'Enumerate all deleted files sequentially'; + } + + public function getPossibleErrors() { + return array_merge( parent::getPossibleErrors(), array( + array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted file information' ), + ) ); + } + + protected function getExamples() { + return array( + 'Simple Use', + ' Show a list of all deleted files', + ' api.php?action=query&list=filearchive', + ); + } + + public function getVersion() { + return __CLASS__ . ': $Id$'; + } +}