MediaWiki master
ApiQueryDuplicateFiles.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Api;
10
15use Wikimedia\Timestamp\TimestampFormat as TS;
16
23
24 public function __construct(
25 ApiQuery $query,
26 string $moduleName,
27 private readonly RepoGroup $repoGroup,
28 ) {
29 parent::__construct( $query, $moduleName, 'df' );
30 }
31
32 public function execute() {
33 $this->run();
34 }
35
37 public function getCacheMode( $params ) {
38 return 'public';
39 }
40
42 public function executeGenerator( $resultPageSet ) {
43 $this->run( $resultPageSet );
44 }
45
49 private function run( $resultPageSet = null ) {
50 $params = $this->extractRequestParams();
51 $namespaces = $this->getPageSet()->getGoodAndMissingTitlesByNamespace();
52 if ( empty( $namespaces[NS_FILE] ) ) {
53 return;
54 }
55 $images = $namespaces[NS_FILE];
56
57 if ( $params['dir'] == 'descending' ) {
58 $images = array_reverse( $images );
59 }
60
61 $skipUntilThisDup = false;
62 if ( isset( $params['continue'] ) ) {
63 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'string', 'string' ] );
64 $fromImage = $cont[0];
65 $skipUntilThisDup = $cont[1];
66 // Filter out any images before $fromImage
67 foreach ( $images as $image => $pageId ) {
68 if ( $image < $fromImage ) {
69 unset( $images[$image] );
70 } else {
71 break;
72 }
73 }
74 }
75
76 $filesToFind = array_keys( $images );
77 if ( $params['localonly'] ) {
78 $files = $this->repoGroup->getLocalRepo()->findFiles( $filesToFind );
79 } else {
80 $files = $this->repoGroup->findFiles( $filesToFind );
81 }
82
83 $fit = true;
84 $count = 0;
85 $titles = [];
86
87 $sha1s = [];
88 foreach ( $files as $file ) {
90 $sha1s[$file->getName()] = $file->getSha1();
91 }
92
93 // find all files with the hashes, result format is:
94 // [ hash => [ dup1, dup2 ], hash1 => ... ]
95 $filesToFindBySha1s = array_unique( array_values( $sha1s ) );
96 if ( $params['localonly'] ) {
97 $filesBySha1s = $this->repoGroup->getLocalRepo()->findBySha1s( $filesToFindBySha1s );
98 } else {
99 $filesBySha1s = $this->repoGroup->findBySha1s( $filesToFindBySha1s );
100 }
101
102 // iterate over $images to handle continue param correct
103 foreach ( $images as $image => $pageId ) {
104 if ( !isset( $sha1s[$image] ) ) {
105 continue; // file does not exist
106 }
107 $sha1 = $sha1s[$image];
108 $dupFiles = $filesBySha1s[$sha1];
109 if ( $params['dir'] == 'descending' ) {
110 $dupFiles = array_reverse( $dupFiles );
111 }
113 foreach ( $dupFiles as $dupFile ) {
114 $dupName = $dupFile->getName();
115 if ( $image == $dupName && $dupFile->isLocal() ) {
116 continue; // ignore the local file itself
117 }
118 if ( $skipUntilThisDup !== false && $dupName < $skipUntilThisDup ) {
119 continue; // skip to pos after the image from continue param
120 }
121 $skipUntilThisDup = false;
122 if ( ++$count > $params['limit'] ) {
123 $fit = false; // break outer loop
124 // We're one over limit which shows that
125 // there are additional images to be had. Stop here...
126 $this->setContinueEnumParameter( 'continue', $image . '|' . $dupName );
127 break;
128 }
129 if ( $resultPageSet !== null ) {
130 $titles[] = $dupFile->getTitle();
131 } else {
132 $r = [
133 'name' => $dupName,
134 'timestamp' => wfTimestamp( TS::ISO_8601, $dupFile->getTimestamp() ),
135 'shared' => !$dupFile->isLocal(),
136 ];
137 $uploader = $dupFile->getUploader( File::FOR_PUBLIC );
138 if ( $uploader ) {
139 $r['user'] = $uploader->getName();
140 }
141 $fit = $this->addPageSubItem( $pageId, $r );
142 if ( !$fit ) {
143 $this->setContinueEnumParameter( 'continue', $image . '|' . $dupName );
144 break;
145 }
146 }
147 }
148 if ( !$fit ) {
149 break;
150 }
151 }
152 if ( $resultPageSet !== null ) {
153 $resultPageSet->populateFromTitles( $titles );
154 }
155 }
156
158 public function getAllowedParams() {
159 return [
160 'limit' => [
161 ParamValidator::PARAM_DEFAULT => 10,
162 ParamValidator::PARAM_TYPE => 'limit',
163 IntegerDef::PARAM_MIN => 1,
164 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
165 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
166 ],
167 'continue' => [
168 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
169 ],
170 'dir' => [
171 ParamValidator::PARAM_DEFAULT => 'ascending',
172 ParamValidator::PARAM_TYPE => [
173 'ascending',
174 'descending'
175 ]
176 ],
177 'localonly' => false,
178 ];
179 }
180
182 protected function getExamplesMessages() {
183 return [
184 'action=query&titles=File:Albert_Einstein_Head.jpg&prop=duplicatefiles'
185 => 'apihelp-query+duplicatefiles-example-simple',
186 'action=query&generator=allimages&prop=duplicatefiles'
187 => 'apihelp-query+duplicatefiles-example-generated',
188 ];
189 }
190
192 public function getHelpUrls() {
193 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Duplicatefiles';
194 }
195}
196
198class_alias( ApiQueryDuplicateFiles::class, 'ApiQueryDuplicateFiles' );
const NS_FILE
Definition Defines.php:57
wfTimestamp( $outputtype=TS::UNIX, $ts=0)
Get a timestamp string in one of various formats.
parseContinueParamOrDie(string $continue, array $types)
Parse the 'continue' parameter in the usual format and validate the types of each part,...
Definition ApiBase.php:1707
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:166
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:233
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:837
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:231
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)
getExamplesMessages()
Returns usage examples for this module.Return value has query strings as keys, with values being eith...
__construct(ApiQuery $query, string $moduleName, private readonly RepoGroup $repoGroup,)
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
executeGenerator( $resultPageSet)
Execute this module as a generator.
getHelpUrls()
Return links to more detailed help pages about the module.1.25, returning boolean false is deprecated...
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.Override this in the module subclass....
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:36
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:80
Prioritized list of file repositories.
Definition RepoGroup.php:30
Service for formatting and validating API parameters.
Type definition for integer types.