MediaWiki REL1_39
ApiQueryDuplicateFiles.php
Go to the documentation of this file.
1<?php
25
32
34 private $repoGroup;
35
41 public function __construct(
42 ApiQuery $query,
43 $moduleName,
44 RepoGroup $repoGroup
45 ) {
46 parent::__construct( $query, $moduleName, 'df' );
47 $this->repoGroup = $repoGroup;
48 }
49
50 public function execute() {
51 $this->run();
52 }
53
54 public function getCacheMode( $params ) {
55 return 'public';
56 }
57
58 public function executeGenerator( $resultPageSet ) {
59 $this->run( $resultPageSet );
60 }
61
65 private function run( $resultPageSet = null ) {
66 $params = $this->extractRequestParams();
67 $namespaces = $this->getPageSet()->getGoodAndMissingTitlesByNamespace();
68 if ( empty( $namespaces[NS_FILE] ) ) {
69 return;
70 }
71 $images = $namespaces[NS_FILE];
72
73 if ( $params['dir'] == 'descending' ) {
74 $images = array_reverse( $images );
75 }
76
77 $skipUntilThisDup = false;
78 if ( isset( $params['continue'] ) ) {
79 $cont = explode( '|', $params['continue'] );
80 $this->dieContinueUsageIf( count( $cont ) != 2 );
81 $fromImage = $cont[0];
82 $skipUntilThisDup = $cont[1];
83 // Filter out any images before $fromImage
84 foreach ( $images as $image => $pageId ) {
85 if ( $image < $fromImage ) {
86 unset( $images[$image] );
87 } else {
88 break;
89 }
90 }
91 }
92
93 $filesToFind = array_keys( $images );
94 if ( $params['localonly'] ) {
95 $files = $this->repoGroup->getLocalRepo()->findFiles( $filesToFind );
96 } else {
97 $files = $this->repoGroup->findFiles( $filesToFind );
98 }
99
100 $fit = true;
101 $count = 0;
102 $titles = [];
103
104 $sha1s = [];
105 foreach ( $files as $file ) {
107 $sha1s[$file->getName()] = $file->getSha1();
108 }
109
110 // find all files with the hashes, result format is:
111 // [ hash => [ dup1, dup2 ], hash1 => ... ]
112 $filesToFindBySha1s = array_unique( array_values( $sha1s ) );
113 if ( $params['localonly'] ) {
114 $filesBySha1s = $this->repoGroup->getLocalRepo()->findBySha1s( $filesToFindBySha1s );
115 } else {
116 $filesBySha1s = $this->repoGroup->findBySha1s( $filesToFindBySha1s );
117 }
118
119 // iterate over $images to handle continue param correct
120 foreach ( $images as $image => $pageId ) {
121 if ( !isset( $sha1s[$image] ) ) {
122 continue; // file does not exist
123 }
124 $sha1 = $sha1s[$image];
125 $dupFiles = $filesBySha1s[$sha1];
126 if ( $params['dir'] == 'descending' ) {
127 $dupFiles = array_reverse( $dupFiles );
128 }
130 foreach ( $dupFiles as $dupFile ) {
131 $dupName = $dupFile->getName();
132 if ( $image == $dupName && $dupFile->isLocal() ) {
133 continue; // ignore the local file itself
134 }
135 if ( $skipUntilThisDup !== false && $dupName < $skipUntilThisDup ) {
136 continue; // skip to pos after the image from continue param
137 }
138 $skipUntilThisDup = false;
139 if ( ++$count > $params['limit'] ) {
140 $fit = false; // break outer loop
141 // We're one over limit which shows that
142 // there are additional images to be had. Stop here...
143 $this->setContinueEnumParameter( 'continue', $image . '|' . $dupName );
144 break;
145 }
146 if ( $resultPageSet !== null ) {
147 $titles[] = $dupFile->getTitle();
148 } else {
149 $r = [
150 'name' => $dupName,
151 'timestamp' => wfTimestamp( TS_ISO_8601, $dupFile->getTimestamp() ),
152 'shared' => !$dupFile->isLocal(),
153 ];
154 $uploader = $dupFile->getUploader( File::FOR_PUBLIC );
155 if ( $uploader ) {
156 $r['user'] = $uploader->getName();
157 }
158 $fit = $this->addPageSubItem( $pageId, $r );
159 if ( !$fit ) {
160 $this->setContinueEnumParameter( 'continue', $image . '|' . $dupName );
161 break;
162 }
163 }
164 }
165 if ( !$fit ) {
166 break;
167 }
168 }
169 if ( $resultPageSet !== null ) {
170 $resultPageSet->populateFromTitles( $titles );
171 }
172 }
173
174 public function getAllowedParams() {
175 return [
176 'limit' => [
177 ParamValidator::PARAM_DEFAULT => 10,
178 ParamValidator::PARAM_TYPE => 'limit',
179 IntegerDef::PARAM_MIN => 1,
180 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
181 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
182 ],
183 'continue' => [
184 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
185 ],
186 'dir' => [
187 ParamValidator::PARAM_DEFAULT => 'ascending',
188 ParamValidator::PARAM_TYPE => [
189 'ascending',
190 'descending'
191 ]
192 ],
193 'localonly' => false,
194 ];
195 }
196
197 protected function getExamplesMessages() {
198 return [
199 'action=query&titles=File:Albert_Einstein_Head.jpg&prop=duplicatefiles'
200 => 'apihelp-query+duplicatefiles-example-simple',
201 'action=query&generator=allimages&prop=duplicatefiles'
202 => 'apihelp-query+duplicatefiles-example-generated',
203 ];
204 }
205
206 public function getHelpUrls() {
207 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Duplicatefiles';
208 }
209}
const NS_FILE
Definition Defines.php:70
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition ApiBase.php:1643
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:221
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:765
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:163
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:223
addPageSubItem( $pageId, $item, $elemname=null)
Same as addPageSubItems(), but one element of $data at a time.
A query module to list duplicates of the given file(s)
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
executeGenerator( $resultPageSet)
Execute this module as a generator.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
getCacheMode( $params)
Get the cache mode for the data generated by this module.
getExamplesMessages()
Returns usage examples for this module.
getHelpUrls()
Return links to more detailed help pages about the module.
__construct(ApiQuery $query, $moduleName, RepoGroup $repoGroup)
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
getPageSet()
Get the PageSet object to work on.
This is the main query class.
Definition ApiQuery.php:41
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:67
getSha1()
Get the SHA-1 base 36 hash of the file.
Definition File.php:2328
Prioritized list of file repositories.
Definition RepoGroup.php:29
Service for formatting and validating API parameters.
Type definition for integer types.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42