| 1 |
<?php |
| 2 |
|
| 3 |
> |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Api; |
| 24 |
|
| 25 |
use File; |
| 26 |
use RepoGroup; |
| 27 |
use Wikimedia\ParamValidator\ParamValidator; |
| 28 |
use Wikimedia\ParamValidator\TypeDef\IntegerDef; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
class ApiQueryDuplicateFiles extends ApiQueryGeneratorBase { |
| 36 |
|
| 37 |
private RepoGroup $repoGroup; |
| 38 |
|
| 39 |
public function __construct( |
| 40 |
ApiQuery $query, |
| 41 |
string $moduleName, |
| 42 |
RepoGroup $repoGroup |
| 43 |
) { |
| 44 |
parent::__construct( $query, $moduleName, 'df' ); |
| 45 |
$this->repoGroup = $repoGroup; |
| 46 |
} |
| 47 |
|
| 48 |
public function execute() { |
| 49 |
$this->run(); |
| 50 |
} |
| 51 |
|
| 52 |
public function getCacheMode( $params ) { |
| 53 |
return 'public'; |
| 54 |
} |
| 55 |
|
| 56 |
public function executeGenerator( $resultPageSet ) { |
| 57 |
$this->run( $resultPageSet ); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
private function run( $resultPageSet = null ) { |
| 64 |
$params = $this->extractRequestParams(); |
| 65 |
$namespaces = $this->getPageSet()->getGoodAndMissingTitlesByNamespace(); |
| 66 |
if ( empty( $namespaces[NS_FILE] ) ) { |
| 67 |
return; |
| 68 |
} |
| 69 |
$images = $namespaces[NS_FILE]; |
| 70 |
|
| 71 |
if ( $params['dir'] == 'descending' ) { |
| 72 |
$images = array_reverse( $images ); |
| 73 |
} |
| 74 |
|
| 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 |
|
| 81 |
foreach ( $images as $image => $pageId ) { |
| 82 |
if ( $image < $fromImage ) { |
| 83 |
unset( $images[$image] ); |
| 84 |
} else { |
| 85 |
break; |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
$filesToFind = array_keys( $images ); |
| 91 |
if ( $params['localonly'] ) { |
| 92 |
$files = $this->repoGroup->getLocalRepo()->findFiles( $filesToFind ); |
| 93 |
} else { |
| 94 |
$files = $this->repoGroup->findFiles( $filesToFind ); |
| 95 |
} |
| 96 |
|
| 97 |
$fit = true; |
| 98 |
$count = 0; |
| 99 |
$titles = []; |
| 100 |
|
| 101 |
$sha1s = []; |
| 102 |
foreach ( $files as $file ) { |
| 103 |
|
| 104 |
$sha1s[$file->getName()] = $file->getSha1(); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 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 ); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
foreach ( $images as $image => $pageId ) { |
| 118 |
if ( !isset( $sha1s[$image] ) ) { |
| 119 |
continue; |
| 120 |
} |
| 121 |
$sha1 = $sha1s[$image]; |
| 122 |
$dupFiles = $filesBySha1s[$sha1]; |
| 123 |
if ( $params['dir'] == 'descending' ) { |
| 124 |
$dupFiles = array_reverse( $dupFiles ); |
| 125 |
} |
| 126 |
|
| 127 |
foreach ( $dupFiles as $dupFile ) { |
| 128 |
$dupName = $dupFile->getName(); |
| 129 |
if ( $image == $dupName && $dupFile->isLocal() ) { |
| 130 |
continue; |
| 131 |
} |
| 132 |
if ( $skipUntilThisDup !== false && $dupName < $skipUntilThisDup ) { |
| 133 |
continue; |
| 134 |
} |
| 135 |
$skipUntilThisDup = false; |
| 136 |
if ( ++$count > $params['limit'] ) { |
| 137 |
$fit = false; |
| 138 |
|
| 139 |
|
| 140 |
$this->setContinueEnumParameter( 'continue', $image . '|' . $dupName ); |
| 141 |
break; |
| 142 |
} |
| 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(), |
| 150 |
]; |
| 151 |
$uploader = $dupFile->getUploader( File::FOR_PUBLIC ); |
| 152 |
if ( $uploader ) { |
| 153 |
$r['user'] = $uploader->getName(); |
| 154 |
} |
| 155 |
$fit = $this->addPageSubItem( $pageId, $r ); |
| 156 |
if ( !$fit ) { |
| 157 |
$this->setContinueEnumParameter( 'continue', $image . '|' . $dupName ); |
| 158 |
break; |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
if ( !$fit ) { |
| 163 |
break; |
| 164 |
} |
| 165 |
} |
| 166 |
if ( $resultPageSet !== null ) { |
| 167 |
$resultPageSet->populateFromTitles( $titles ); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 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 |
| 179 |
], |
| 180 |
'continue' => [ |
| 181 |
ApiBase::PARAM_HELP_MSG => 'api-help-param-continue', |
| 182 |
], |
| 183 |
'dir' => [ |
| 184 |
ParamValidator::PARAM_DEFAULT => 'ascending', |
| 185 |
ParamValidator::PARAM_TYPE => [ |
| 186 |
'ascending', |
| 187 |
'descending' |
| 188 |
] |
| 189 |
], |
| 190 |
'localonly' => false, |
| 191 |
]; |
| 192 |
} |
| 193 |
|
| 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', |
| 200 |
]; |
| 201 |
} |
| 202 |
|
| 203 |
public function getHelpUrls() { |
| 204 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Duplicatefiles'; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
class_alias( ApiQueryDuplicateFiles::class, 'ApiQueryDuplicateFiles' ); |