MediaWiki  1.23.8
ApiQueryDuplicateFiles.php
Go to the documentation of this file.
1 <?php
33 
34  public function __construct( $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 ) {
54  $params = $this->extractRequestParams();
55  $namespaces = $this->getPageSet()->getAllTitlesByNamespace();
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 = array();
91 
92  $sha1s = array();
93  foreach ( $files as $file ) {
95  $sha1s[$file->getName()] = $file->getSha1();
96  }
97 
98  // find all files with the hashes, result format is:
99  // array( hash => array( 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 = array(
138  'name' => $dupName,
139  'user' => $dupFile->getUser( 'text' ),
140  'timestamp' => wfTimestamp( TS_ISO_8601, $dupFile->getTimestamp() )
141  );
142  if ( !$dupFile->isLocal() ) {
143  $r['shared'] = '';
144  }
145  $fit = $this->addPageSubItem( $pageId, $r );
146  if ( !$fit ) {
147  $this->setContinueEnumParameter( 'continue', $image . '|' . $dupName );
148  break;
149  }
150  }
151  }
152  if ( !$fit ) {
153  break;
154  }
155  }
156  if ( !is_null( $resultPageSet ) ) {
157  $resultPageSet->populateFromTitles( $titles );
158  }
159  }
160 
161  public function getAllowedParams() {
162  return array(
163  'limit' => array(
164  ApiBase::PARAM_DFLT => 10,
165  ApiBase::PARAM_TYPE => 'limit',
166  ApiBase::PARAM_MIN => 1,
169  ),
170  'continue' => null,
171  'dir' => array(
172  ApiBase::PARAM_DFLT => 'ascending',
174  'ascending',
175  'descending'
176  )
177  ),
178  'localonly' => false,
179  );
180  }
181 
182  public function getParamDescription() {
183  return array(
184  'limit' => 'How many duplicate files to return',
185  'continue' => 'When more results are available, use this to continue',
186  'dir' => 'The direction in which to list',
187  'localonly' => 'Look only for files in the local repository',
188  );
189  }
190 
191  public function getResultProperties() {
192  return array(
193  '' => array(
194  'name' => 'string',
195  'user' => 'string',
196  'timestamp' => 'timestamp',
197  'shared' => 'boolean',
198  )
199  );
200  }
201 
202  public function getDescription() {
203  return 'List all files that are duplicates of the given file(s) based on hash values.';
204  }
205 
206  public function getExamples() {
207  return array(
208  'api.php?action=query&titles=File:Albert_Einstein_Head.jpg&prop=duplicatefiles',
209  'api.php?action=query&generator=allimages&prop=duplicatefiles',
210  );
211  }
212 
213  public function getHelpUrls() {
214  return 'https://www.mediawiki.org/wiki/API:Properties#duplicatefiles_.2F_df';
215  }
216 }
RepoGroup\singleton
static singleton()
Get a RepoGroup instance.
Definition: RepoGroup.php:53
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
$files
$files
Definition: importImages.php:67
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:2483
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:50
NS_FILE
const NS_FILE
Definition: Defines.php:85
$params
$params
Definition: styleTest.css.php:40
ApiQueryGeneratorBase\setContinueEnumParameter
setContinueEnumParameter( $paramName, $paramValue)
Overrides base in case of generator & smart continue to notify ApiQueryMain instead of adding them to...
Definition: ApiQueryBase.php:676
ApiBase\PARAM_MIN
const PARAM_MIN
Definition: ApiBase.php:56
ApiQueryGeneratorBase\getPageSet
getPageSet()
Get the PageSet object to work on.
Definition: ApiQueryBase.php:649
$titles
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
ApiQueryDuplicateFiles\getCacheMode
getCacheMode( $params)
Get the cache mode for the data generated by this module.
Definition: ApiQueryDuplicateFiles.php:42
ApiBase\LIMIT_BIG1
const LIMIT_BIG1
Definition: ApiBase.php:78
TS_ISO_8601
const TS_ISO_8601
ISO 8601 format with no timezone: 1986-02-09T20:00:00Z.
Definition: GlobalFunctions.php:2448
ApiQueryDuplicateFiles\run
run( $resultPageSet=null)
Definition: ApiQueryDuplicateFiles.php:53
ApiBase\PARAM_MAX
const PARAM_MAX
Definition: ApiBase.php:52
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
ApiQueryDuplicateFiles\getResultProperties
getResultProperties()
Returns possible properties in the result, grouped by the value of the prop parameter that shows them...
Definition: ApiQueryDuplicateFiles.php:191
ApiQueryDuplicateFiles\getExamples
getExamples()
Returns usage examples for this module.
Definition: ApiQueryDuplicateFiles.php:206
ApiQueryDuplicateFiles\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiQueryDuplicateFiles.php:161
ApiBase\extractRequestParams
extractRequestParams( $parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:687
ApiBase\dieContinueUsageIf
dieContinueUsageIf( $condition)
Die with the $prefix.
Definition: ApiBase.php:1969
ApiQueryDuplicateFiles\getDescription
getDescription()
Returns the description string for this module.
Definition: ApiQueryDuplicateFiles.php:202
ApiQueryDuplicateFiles\executeGenerator
executeGenerator( $resultPageSet)
Execute this module as a generator.
Definition: ApiQueryDuplicateFiles.php:46
$file
if(PHP_SAPI !='cli') $file
Definition: UtfNormalTest2.php:30
$count
$count
Definition: UtfNormalTest2.php:96
ApiQueryGeneratorBase
Definition: ApiQueryBase.php:626
$namespaces
namespace and then decline to actually register it & $namespaces
Definition: hooks.txt:815
ApiQueryDuplicateFiles\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiQueryDuplicateFiles.php:38
ApiBase\LIMIT_BIG2
const LIMIT_BIG2
Definition: ApiBase.php:79
ApiQueryDuplicateFiles\__construct
__construct( $query, $moduleName)
Definition: ApiQueryDuplicateFiles.php:34
ApiBase\PARAM_DFLT
const PARAM_DFLT
Definition: ApiBase.php:46
as
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
ApiQueryDuplicateFiles\getParamDescription
getParamDescription()
Returns an array of parameter descriptions.
Definition: ApiQueryDuplicateFiles.php:182
ApiBase\PARAM_MAX2
const PARAM_MAX2
Definition: ApiBase.php:54
ApiQueryDuplicateFiles\getHelpUrls
getHelpUrls()
Definition: ApiQueryDuplicateFiles.php:213
ApiQueryDuplicateFiles
A query module to list duplicates of the given file(s)
Definition: ApiQueryDuplicateFiles.php:32
$query
return true to allow those checks to and false if checking is done use this to change the tables headers temp or archived zone change it to an object instance and return false override the list derivative used the name of the old file when set the default code will be skipped add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition: hooks.txt:1105
ApiQueryBase\addPageSubItem
addPageSubItem( $pageId, $item, $elemname=null)
Same as addPageSubItems(), but one element of $data at a time.
Definition: ApiQueryBase.php:383