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
23 namespace MediaWiki\Api
;
27 use Wikimedia\ParamValidator\ParamValidator
;
28 use Wikimedia\ParamValidator\TypeDef\IntegerDef
;
31 * A query module to list duplicates of the given file(s)
35 class ApiQueryDuplicateFiles
extends ApiQueryGeneratorBase
{
37 private RepoGroup
$repoGroup;
39 public function __construct(
44 parent
::__construct( $query, $moduleName, 'df' );
45 $this->repoGroup
= $repoGroup;
48 public function execute() {
52 public function getCacheMode( $params ) {
56 public function executeGenerator( $resultPageSet ) {
57 $this->run( $resultPageSet );
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
] ) ) {
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] );
90 $filesToFind = array_keys( $images );
91 if ( $params['localonly'] ) {
92 $files = $this->repoGroup
->getLocalRepo()->findFiles( $filesToFind );
94 $files = $this->repoGroup
->findFiles( $filesToFind );
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 );
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 );
143 if ( $resultPageSet !== null ) {
144 $titles[] = $dupFile->getTitle();
148 'timestamp' => wfTimestamp( TS_ISO_8601
, $dupFile->getTimestamp() ),
149 'shared' => !$dupFile->isLocal(),
151 $uploader = $dupFile->getUploader( File
::FOR_PUBLIC
);
153 $r['user'] = $uploader->getName();
155 $fit = $this->addPageSubItem( $pageId, $r );
157 $this->setContinueEnumParameter( 'continue', $image . '|' . $dupName );
166 if ( $resultPageSet !== null ) {
167 $resultPageSet->populateFromTitles( $titles );
171 public function getAllowedParams() {
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
181 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
184 ParamValidator
::PARAM_DEFAULT
=> 'ascending',
185 ParamValidator
::PARAM_TYPE
=> [
190 'localonly' => false,
194 protected function getExamplesMessages() {
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' );