Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiQueryDuplicateFiles.php
blobfbdd6add07b2cdea2f58d7d7d5623154cd8f7e2c
1 <?php
2 /**
3 * Copyright © 2008 Roan Kattouw <roan.kattouw@gmail.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
23 namespace MediaWiki\Api;
25 use File;
26 use RepoGroup;
27 use Wikimedia\ParamValidator\ParamValidator;
28 use Wikimedia\ParamValidator\TypeDef\IntegerDef;
30 /**
31 * A query module to list duplicates of the given file(s)
33 * @ingroup API
35 class ApiQueryDuplicateFiles extends ApiQueryGeneratorBase {
37 private RepoGroup $repoGroup;
39 public function __construct(
40 ApiQuery $query,
41 string $moduleName,
42 RepoGroup $repoGroup
43 ) {
44 parent::__construct( $query, $moduleName, 'df' );
45 $this->repoGroup = $repoGroup;
48 public function execute() {
49 $this->run();
52 public function getCacheMode( $params ) {
53 return 'public';
56 public function executeGenerator( $resultPageSet ) {
57 $this->run( $resultPageSet );
60 /**
61 * @param ApiPageSet|null $resultPageSet
63 private function run( $resultPageSet = null ) {
64 $params = $this->extractRequestParams();
65 $namespaces = $this->getPageSet()->getGoodAndMissingTitlesByNamespace();
66 if ( empty( $namespaces[NS_FILE] ) ) {
67 return;
69 $images = $namespaces[NS_FILE];
71 if ( $params['dir'] == 'descending' ) {
72 $images = array_reverse( $images );
75 $skipUntilThisDup = false;
76 if ( isset( $params['continue'] ) ) {
77 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'string', 'string' ] );
78 $fromImage = $cont[0];
79 $skipUntilThisDup = $cont[1];
80 // Filter out any images before $fromImage
81 foreach ( $images as $image => $pageId ) {
82 if ( $image < $fromImage ) {
83 unset( $images[$image] );
84 } else {
85 break;
90 $filesToFind = array_keys( $images );
91 if ( $params['localonly'] ) {
92 $files = $this->repoGroup->getLocalRepo()->findFiles( $filesToFind );
93 } else {
94 $files = $this->repoGroup->findFiles( $filesToFind );
97 $fit = true;
98 $count = 0;
99 $titles = [];
101 $sha1s = [];
102 foreach ( $files as $file ) {
103 /** @var File $file */
104 $sha1s[$file->getName()] = $file->getSha1();
107 // find all files with the hashes, result format is:
108 // [ hash => [ dup1, dup2 ], hash1 => ... ]
109 $filesToFindBySha1s = array_unique( array_values( $sha1s ) );
110 if ( $params['localonly'] ) {
111 $filesBySha1s = $this->repoGroup->getLocalRepo()->findBySha1s( $filesToFindBySha1s );
112 } else {
113 $filesBySha1s = $this->repoGroup->findBySha1s( $filesToFindBySha1s );
116 // iterate over $images to handle continue param correct
117 foreach ( $images as $image => $pageId ) {
118 if ( !isset( $sha1s[$image] ) ) {
119 continue; // file does not exist
121 $sha1 = $sha1s[$image];
122 $dupFiles = $filesBySha1s[$sha1];
123 if ( $params['dir'] == 'descending' ) {
124 $dupFiles = array_reverse( $dupFiles );
126 /** @var File $dupFile */
127 foreach ( $dupFiles as $dupFile ) {
128 $dupName = $dupFile->getName();
129 if ( $image == $dupName && $dupFile->isLocal() ) {
130 continue; // ignore the local file itself
132 if ( $skipUntilThisDup !== false && $dupName < $skipUntilThisDup ) {
133 continue; // skip to pos after the image from continue param
135 $skipUntilThisDup = false;
136 if ( ++$count > $params['limit'] ) {
137 $fit = false; // break outer loop
138 // We're one over limit which shows that
139 // there are additional images to be had. Stop here...
140 $this->setContinueEnumParameter( 'continue', $image . '|' . $dupName );
141 break;
143 if ( $resultPageSet !== null ) {
144 $titles[] = $dupFile->getTitle();
145 } else {
146 $r = [
147 'name' => $dupName,
148 'timestamp' => wfTimestamp( TS_ISO_8601, $dupFile->getTimestamp() ),
149 'shared' => !$dupFile->isLocal(),
151 $uploader = $dupFile->getUploader( File::FOR_PUBLIC );
152 if ( $uploader ) {
153 $r['user'] = $uploader->getName();
155 $fit = $this->addPageSubItem( $pageId, $r );
156 if ( !$fit ) {
157 $this->setContinueEnumParameter( 'continue', $image . '|' . $dupName );
158 break;
162 if ( !$fit ) {
163 break;
166 if ( $resultPageSet !== null ) {
167 $resultPageSet->populateFromTitles( $titles );
171 public function getAllowedParams() {
172 return [
173 'limit' => [
174 ParamValidator::PARAM_DEFAULT => 10,
175 ParamValidator::PARAM_TYPE => 'limit',
176 IntegerDef::PARAM_MIN => 1,
177 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
178 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
180 'continue' => [
181 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
183 'dir' => [
184 ParamValidator::PARAM_DEFAULT => 'ascending',
185 ParamValidator::PARAM_TYPE => [
186 'ascending',
187 'descending'
190 'localonly' => false,
194 protected function getExamplesMessages() {
195 return [
196 'action=query&titles=File:Albert_Einstein_Head.jpg&prop=duplicatefiles'
197 => 'apihelp-query+duplicatefiles-example-simple',
198 'action=query&generator=allimages&prop=duplicatefiles'
199 => 'apihelp-query+duplicatefiles-example-generated',
203 public function getHelpUrls() {
204 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Duplicatefiles';
208 /** @deprecated class alias since 1.43 */
209 class_alias( ApiQueryDuplicateFiles::class, 'ApiQueryDuplicateFiles' );