MediaWiki  master
ApiQueryDuplicateFiles.php
Go to the documentation of this file.
1 <?php
29 
30  public function __construct( ApiQuery $query, $moduleName ) {
31  parent::__construct( $query, $moduleName, 'df' );
32  }
33 
34  public function execute() {
35  $this->run();
36  }
37 
38  public function getCacheMode( $params ) {
39  return 'public';
40  }
41 
42  public function executeGenerator( $resultPageSet ) {
43  $this->run( $resultPageSet );
44  }
45 
49  private function run( $resultPageSet = null ) {
50  $params = $this->extractRequestParams();
51  $namespaces = $this->getPageSet()->getGoodAndMissingTitlesByNamespace();
52  if ( empty( $namespaces[NS_FILE] ) ) {
53  return;
54  }
55  $images = $namespaces[NS_FILE];
56 
57  if ( $params['dir'] == 'descending' ) {
58  $images = array_reverse( $images );
59  }
60 
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] );
71  } else {
72  break;
73  }
74  }
75  }
76 
77  $filesToFind = array_keys( $images );
78  if ( $params['localonly'] ) {
79  $files = RepoGroup::singleton()->getLocalRepo()->findFiles( $filesToFind );
80  } else {
81  $files = RepoGroup::singleton()->findFiles( $filesToFind );
82  }
83 
84  $fit = true;
85  $count = 0;
86  $titles = [];
87 
88  $sha1s = [];
89  foreach ( $files as $file ) {
91  $sha1s[$file->getName()] = $file->getSha1();
92  }
93 
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 );
99  } else {
100  $filesBySha1s = RepoGroup::singleton()->findBySha1s( $filesToFindBySha1s );
101  }
102 
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
107  }
108  $sha1 = $sha1s[$image];
109  $dupFiles = $filesBySha1s[$sha1];
110  if ( $params['dir'] == 'descending' ) {
111  $dupFiles = array_reverse( $dupFiles );
112  }
114  foreach ( $dupFiles as $dupFile ) {
115  $dupName = $dupFile->getName();
116  if ( $image == $dupName && $dupFile->isLocal() ) {
117  continue; // ignore the local file itself
118  }
119  if ( $skipUntilThisDup !== false && $dupName < $skipUntilThisDup ) {
120  continue; // skip to pos after the image from continue param
121  }
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 );
128  break;
129  }
130  if ( !is_null( $resultPageSet ) ) {
131  $titles[] = $dupFile->getTitle();
132  } else {
133  $r = [
134  'name' => $dupName,
135  'user' => $dupFile->getUser( 'text' ),
136  'timestamp' => wfTimestamp( TS_ISO_8601, $dupFile->getTimestamp() ),
137  'shared' => !$dupFile->isLocal(),
138  ];
139  $fit = $this->addPageSubItem( $pageId, $r );
140  if ( !$fit ) {
141  $this->setContinueEnumParameter( 'continue', $image . '|' . $dupName );
142  break;
143  }
144  }
145  }
146  if ( !$fit ) {
147  break;
148  }
149  }
150  if ( !is_null( $resultPageSet ) ) {
151  $resultPageSet->populateFromTitles( $titles );
152  }
153  }
154 
155  public function getAllowedParams() {
156  return [
157  'limit' => [
158  ApiBase::PARAM_DFLT => 10,
159  ApiBase::PARAM_TYPE => 'limit',
160  ApiBase::PARAM_MIN => 1,
163  ],
164  'continue' => [
165  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
166  ],
167  'dir' => [
168  ApiBase::PARAM_DFLT => 'ascending',
170  'ascending',
171  'descending'
172  ]
173  ],
174  'localonly' => false,
175  ];
176  }
177 
178  protected function getExamplesMessages() {
179  return [
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',
184  ];
185  }
186 
187  public function getHelpUrls() {
188  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Duplicatefiles';
189  }
190 }
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below...
Definition: ApiBase.php:94
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition: ApiBase.php:261
return true to allow those checks to and false if checking is done remove or add to the links of a group of changes in EnhancedChangesList Hook subscribers can return false to omit this line from recentchanges use this to change the tables headers change it to an object instance and return false override the list derivative used $groups Array of ChangesListFilterGroup objects(added in 1.34) 'FileDeleteComplete' 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:1529
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Definition: router.php:42
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:55
const LIMIT_BIG1
Fast query, standard limit.
Definition: ApiBase.php:259
const PARAM_MAX
(integer) Max value allowed for the parameter, for PARAM_TYPE &#39;integer&#39; and &#39;limit&#39;.
Definition: ApiBase.php:97
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user...
Definition: ApiBase.php:761
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
addPageSubItem( $pageId, $item, $elemname=null)
Same as addPageSubItems(), but one element of $data at a time.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
static singleton()
Definition: RepoGroup.php:60
dieContinueUsageIf( $condition)
Die with the &#39;badcontinue&#39; error.
Definition: ApiBase.php:2200
$params
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:767
const NS_FILE
Definition: Defines.php:66
const PARAM_MAX2
(integer) Max value allowed for the parameter for users with the apihighlimits right, for PARAM_TYPE &#39;limit&#39;.
Definition: ApiBase.php:103
This is the main query class.
Definition: ApiQuery.php:36
namespace and then decline to actually register it & $namespaces
Definition: hooks.txt:912
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
Definition: distributors.txt:9
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter...
Definition: ApiBase.php:131
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:35
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
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password 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:767
A query module to list duplicates of the given file(s)
run( $resultPageSet=null)
const PARAM_MIN
(integer) Lowest value allowed for the parameter, for PARAM_TYPE &#39;integer&#39; and &#39;limit&#39;.
Definition: ApiBase.php:106
getPageSet()
Get the PageSet object to work on.
__construct(ApiQuery $query, $moduleName)