MediaWiki REL1_28
ApiQueryDuplicateFiles.php
Go to the documentation of this file.
1<?php
33
34 public function __construct( ApiQuery $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'df' );
36 }
37
38 public function execute() {
39 $this->run();
40 }
41
42 public function getCacheMode( $params ) {
43 return 'public';
44 }
45
46 public function executeGenerator( $resultPageSet ) {
47 $this->run( $resultPageSet );
48 }
49
53 private function run( $resultPageSet = null ) {
55 $namespaces = $this->getPageSet()->getGoodAndMissingTitlesByNamespace();
56 if ( empty( $namespaces[NS_FILE] ) ) {
57 return;
58 }
59 $images = $namespaces[NS_FILE];
60
61 if ( $params['dir'] == 'descending' ) {
62 $images = array_reverse( $images );
63 }
64
65 $skipUntilThisDup = false;
66 if ( isset( $params['continue'] ) ) {
67 $cont = explode( '|', $params['continue'] );
68 $this->dieContinueUsageIf( count( $cont ) != 2 );
69 $fromImage = $cont[0];
70 $skipUntilThisDup = $cont[1];
71 // Filter out any images before $fromImage
72 foreach ( $images as $image => $pageId ) {
73 if ( $image < $fromImage ) {
74 unset( $images[$image] );
75 } else {
76 break;
77 }
78 }
79 }
80
81 $filesToFind = array_keys( $images );
82 if ( $params['localonly'] ) {
83 $files = RepoGroup::singleton()->getLocalRepo()->findFiles( $filesToFind );
84 } else {
85 $files = RepoGroup::singleton()->findFiles( $filesToFind );
86 }
87
88 $fit = true;
89 $count = 0;
90 $titles = [];
91
92 $sha1s = [];
93 foreach ( $files as $file ) {
95 $sha1s[$file->getName()] = $file->getSha1();
96 }
97
98 // find all files with the hashes, result format is:
99 // [ hash => [ dup1, dup2 ], hash1 => ... ]
100 $filesToFindBySha1s = array_unique( array_values( $sha1s ) );
101 if ( $params['localonly'] ) {
102 $filesBySha1s = RepoGroup::singleton()->getLocalRepo()->findBySha1s( $filesToFindBySha1s );
103 } else {
104 $filesBySha1s = RepoGroup::singleton()->findBySha1s( $filesToFindBySha1s );
105 }
106
107 // iterate over $images to handle continue param correct
108 foreach ( $images as $image => $pageId ) {
109 if ( !isset( $sha1s[$image] ) ) {
110 continue; // file does not exist
111 }
112 $sha1 = $sha1s[$image];
113 $dupFiles = $filesBySha1s[$sha1];
114 if ( $params['dir'] == 'descending' ) {
115 $dupFiles = array_reverse( $dupFiles );
116 }
118 foreach ( $dupFiles as $dupFile ) {
119 $dupName = $dupFile->getName();
120 if ( $image == $dupName && $dupFile->isLocal() ) {
121 continue; // ignore the local file itself
122 }
123 if ( $skipUntilThisDup !== false && $dupName < $skipUntilThisDup ) {
124 continue; // skip to pos after the image from continue param
125 }
126 $skipUntilThisDup = false;
127 if ( ++$count > $params['limit'] ) {
128 $fit = false; // break outer loop
129 // We're one over limit which shows that
130 // there are additional images to be had. Stop here...
131 $this->setContinueEnumParameter( 'continue', $image . '|' . $dupName );
132 break;
133 }
134 if ( !is_null( $resultPageSet ) ) {
135 $titles[] = $dupFile->getTitle();
136 } else {
137 $r = [
138 'name' => $dupName,
139 'user' => $dupFile->getUser( 'text' ),
140 'timestamp' => wfTimestamp( TS_ISO_8601, $dupFile->getTimestamp() ),
141 'shared' => !$dupFile->isLocal(),
142 ];
143 $fit = $this->addPageSubItem( $pageId, $r );
144 if ( !$fit ) {
145 $this->setContinueEnumParameter( 'continue', $image . '|' . $dupName );
146 break;
147 }
148 }
149 }
150 if ( !$fit ) {
151 break;
152 }
153 }
154 if ( !is_null( $resultPageSet ) ) {
155 $resultPageSet->populateFromTitles( $titles );
156 }
157 }
158
159 public function getAllowedParams() {
160 return [
161 'limit' => [
163 ApiBase::PARAM_TYPE => 'limit',
167 ],
168 'continue' => [
169 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
170 ],
171 'dir' => [
172 ApiBase::PARAM_DFLT => 'ascending',
174 'ascending',
175 'descending'
176 ]
177 ],
178 'localonly' => false,
179 ];
180 }
181
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
191 public function getHelpUrls() {
192 return 'https://www.mediawiki.org/wiki/API:Duplicatefiles';
193 }
194}
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
const PARAM_MAX2
(integer) Max value allowed for the parameter for users with the apihighlimits right,...
Definition ApiBase.php:97
const PARAM_MAX
(integer) Max value allowed for the parameter, for PARAM_TYPE 'integer' and 'limit'.
Definition ApiBase.php:91
dieContinueUsageIf( $condition)
Die with the $prefix.
Definition ApiBase.php:2240
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition ApiBase.php:88
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition ApiBase.php:50
extractRequestParams( $parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:685
const PARAM_MIN
(integer) Lowest value allowed for the parameter, for PARAM_TYPE 'integer' and 'limit'.
Definition ApiBase.php:100
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:184
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:125
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:186
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)
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
executeGenerator( $resultPageSet)
Execute this module as a generator.
__construct(ApiQuery $query, $moduleName)
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.
getExamplesMessages()
Returns usage examples for this module.
getHelpUrls()
Return links to more detailed help pages about the module.
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:38
static singleton()
Get a RepoGroup instance.
Definition RepoGroup.php:59
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
const NS_FILE
Definition Defines.php:62
namespace and then decline to actually register it & $namespaces
Definition hooks.txt:956
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check $image
Definition hooks.txt:917
null for the local wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition hooks.txt:1595
$files
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
linkcache txt The LinkCache class maintains a list of article titles and the information about whether or not the article exists in the database This is used to mark up links when displaying a page If the same link appears more than once on any page then it only has to be looked up once In most cases link lookups are done in batches with the LinkBatch class or the equivalent in so the link cache is mostly useful for short snippets of parsed and for links in the navigation areas of the skin The link cache was formerly used to track links used in a document for the purposes of updating the link tables This application is now deprecated To create a you can use the following $titles
Definition linkcache.txt:17
$params
const TS_ISO_8601
ISO 8601 format with no timezone: 1986-02-09T20:00:00Z.
Definition defines.php:28