3 * Copyright © 2008 Roan Kattouw "<Firstname>.<Lastname>@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
24 * A query module to list duplicates of the given file(s)
28 class ApiQueryDuplicateFiles
extends ApiQueryGeneratorBase
{
30 public function __construct( ApiQuery
$query, $moduleName ) {
31 parent
::__construct( $query, $moduleName, 'df' );
34 public function execute() {
38 public function getCacheMode( $params ) {
42 public function executeGenerator( $resultPageSet ) {
43 $this->run( $resultPageSet );
47 * @param ApiPageSet $resultPageSet
49 private function run( $resultPageSet = null ) {
50 $params = $this->extractRequestParams();
51 $namespaces = $this->getPageSet()->getGoodAndMissingTitlesByNamespace();
52 if ( empty( $namespaces[NS_FILE
] ) ) {
55 $images = $namespaces[NS_FILE
];
57 if ( $params['dir'] == 'descending' ) {
58 $images = array_reverse( $images );
61 $skipUntilThisDup = false;
62 if ( isset( $params['continue'] ) ) {
63 $cont = explode( '|', $params['continue'] );
64 $this->dieContinueUsageIf( count( $cont ) != 2 );
65 $fromImage = $cont[0];
66 $skipUntilThisDup = $cont[1];
67 // Filter out any images before $fromImage
68 foreach ( $images as $image => $pageId ) {
69 if ( $image < $fromImage ) {
70 unset( $images[$image] );
77 $filesToFind = array_keys( $images );
78 if ( $params['localonly'] ) {
79 $files = RepoGroup
::singleton()->getLocalRepo()->findFiles( $filesToFind );
81 $files = RepoGroup
::singleton()->findFiles( $filesToFind );
89 foreach ( $files as $file ) {
90 /** @var File $file */
91 $sha1s[$file->getName()] = $file->getSha1();
94 // find all files with the hashes, result format is:
95 // [ hash => [ dup1, dup2 ], hash1 => ... ]
96 $filesToFindBySha1s = array_unique( array_values( $sha1s ) );
97 if ( $params['localonly'] ) {
98 $filesBySha1s = RepoGroup
::singleton()->getLocalRepo()->findBySha1s( $filesToFindBySha1s );
100 $filesBySha1s = RepoGroup
::singleton()->findBySha1s( $filesToFindBySha1s );
103 // iterate over $images to handle continue param correct
104 foreach ( $images as $image => $pageId ) {
105 if ( !isset( $sha1s[$image] ) ) {
106 continue; // file does not exist
108 $sha1 = $sha1s[$image];
109 $dupFiles = $filesBySha1s[$sha1];
110 if ( $params['dir'] == 'descending' ) {
111 $dupFiles = array_reverse( $dupFiles );
113 /** @var File $dupFile */
114 foreach ( $dupFiles as $dupFile ) {
115 $dupName = $dupFile->getName();
116 if ( $image == $dupName && $dupFile->isLocal() ) {
117 continue; // ignore the local file itself
119 if ( $skipUntilThisDup !== false && $dupName < $skipUntilThisDup ) {
120 continue; // skip to pos after the image from continue param
122 $skipUntilThisDup = false;
123 if ( ++
$count > $params['limit'] ) {
124 $fit = false; // break outer loop
125 // We're one over limit which shows that
126 // there are additional images to be had. Stop here...
127 $this->setContinueEnumParameter( 'continue', $image . '|' . $dupName );
130 if ( !is_null( $resultPageSet ) ) {
131 $titles[] = $dupFile->getTitle();
135 'user' => $dupFile->getUser( 'text' ),
136 'timestamp' => wfTimestamp( TS_ISO_8601
, $dupFile->getTimestamp() ),
137 'shared' => !$dupFile->isLocal(),
139 $fit = $this->addPageSubItem( $pageId, $r );
141 $this->setContinueEnumParameter( 'continue', $image . '|' . $dupName );
150 if ( !is_null( $resultPageSet ) ) {
151 $resultPageSet->populateFromTitles( $titles );
155 public function getAllowedParams() {
158 ApiBase
::PARAM_DFLT
=> 10,
159 ApiBase
::PARAM_TYPE
=> 'limit',
160 ApiBase
::PARAM_MIN
=> 1,
161 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
162 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
165 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
168 ApiBase
::PARAM_DFLT
=> 'ascending',
169 ApiBase
::PARAM_TYPE
=> [
174 'localonly' => false,
178 protected function getExamplesMessages() {
180 'action=query&titles=File:Albert_Einstein_Head.jpg&prop=duplicatefiles'
181 => 'apihelp-query+duplicatefiles-example-simple',
182 'action=query&generator=allimages&prop=duplicatefiles'
183 => 'apihelp-query+duplicatefiles-example-generated',
187 public function getHelpUrls() {
188 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Duplicatefiles';