Merge "Clean up applyPatch() usage in the installer."
[lhc/web/wiklou.git] / includes / api / ApiQueryDuplicateFiles.php
1 <?php
2 /**
3 *
4 *
5 * Created on Sep 27, 2008
6 *
7 * Copyright © 2008 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 /**
28 * A query module to list duplicates of the given file(s)
29 *
30 * @ingroup API
31 */
32 class ApiQueryDuplicateFiles extends ApiQueryGeneratorBase {
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
50 /**
51 * @param $resultPageSet ApiPageSet
52 * @return
53 */
54 private function run( $resultPageSet = null ) {
55 $params = $this->extractRequestParams();
56 $namespaces = $this->getPageSet()->getAllTitlesByNamespace();
57 if ( empty( $namespaces[NS_FILE] ) ) {
58 return;
59 }
60 $images = $namespaces[NS_FILE];
61
62 if( $params['dir'] == 'descending' ) {
63 $images = array_reverse( $images );
64 }
65
66 $skipUntilThisDup = false;
67 if ( isset( $params['continue'] ) ) {
68 $cont = explode( '|', $params['continue'] );
69 if ( count( $cont ) != 2 ) {
70 $this->dieUsage( 'Invalid continue param. You should pass the ' .
71 'original value returned by the previous query', '_badcontinue' );
72 }
73 $fromImage = $cont[0];
74 $skipUntilThisDup = $cont[1];
75 // Filter out any images before $fromImage
76 foreach ( $images as $image => $pageId ) {
77 if ( $image < $fromImage ) {
78 unset( $images[$image] );
79 } else {
80 break;
81 }
82 }
83 }
84
85 $filesToFind = array_keys( $images );
86 if( $params['localonly'] ) {
87 $files = RepoGroup::singleton()->getLocalRepo()->findFiles( $filesToFind );
88 } else {
89 $files = RepoGroup::singleton()->findFiles( $filesToFind );
90 }
91
92 $fit = true;
93 $count = 0;
94 $titles = array();
95
96 $sha1s = array();
97 foreach ( $files as $file ) {
98 $sha1s[$file->getName()] = $file->getSha1();
99 }
100
101 // find all files with the hashes, result format is: array( hash => array( dup1, dup2 ), hash1 => ... )
102 $filesToFindBySha1s = array_unique( array_values( $sha1s ) );
103 if( $params['localonly'] ) {
104 $filesBySha1s = RepoGroup::singleton()->getLocalRepo()->findBySha1s( $filesToFindBySha1s );
105 } else {
106 $filesBySha1s = RepoGroup::singleton()->findBySha1s( $filesToFindBySha1s );
107 }
108
109 // iterate over $images to handle continue param correct
110 foreach( $images as $image => $pageId ) {
111 if( !isset( $sha1s[$image] ) ) {
112 continue; //file does not exist
113 }
114 $sha1 = $sha1s[$image];
115 $dupFiles = $filesBySha1s[$sha1];
116 if( $params['dir'] == 'descending' ) {
117 $dupFiles = array_reverse( $dupFiles );
118 }
119 foreach ( $dupFiles as $dupFile ) {
120 $dupName = $dupFile->getName();
121 if( $image == $dupName ) {
122 continue; //ignore the file itself
123 }
124 if( $skipUntilThisDup !== false && $dupName < $skipUntilThisDup ) {
125 continue; //skip to pos after the image from continue param
126 }
127 $skipUntilThisDup = false;
128 if ( ++$count > $params['limit'] ) {
129 $fit = false; //break outer loop
130 // We're one over limit which shows that
131 // there are additional images to be had. Stop here...
132 $this->setContinueEnumParameter( 'continue', $image . '|' . $dupName );
133 break;
134 }
135 if ( !is_null( $resultPageSet ) ) {
136 $titles[] = $file->getTitle();
137 } else {
138 $r = array(
139 'name' => $dupName,
140 'user' => $dupFile->getUser( 'text' ),
141 'timestamp' => wfTimestamp( TS_ISO_8601, $dupFile->getTimestamp() )
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 array(
161 'limit' => array(
162 ApiBase::PARAM_DFLT => 10,
163 ApiBase::PARAM_TYPE => 'limit',
164 ApiBase::PARAM_MIN => 1,
165 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
166 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
167 ),
168 'continue' => null,
169 'dir' => array(
170 ApiBase::PARAM_DFLT => 'ascending',
171 ApiBase::PARAM_TYPE => array(
172 'ascending',
173 'descending'
174 )
175 ),
176 'localonly' => false,
177 );
178 }
179
180 public function getParamDescription() {
181 return array(
182 'limit' => 'How many duplicate files to return',
183 'continue' => 'When more results are available, use this to continue',
184 'dir' => 'The direction in which to list',
185 'localonly' => 'Look only for files in the local repository',
186 );
187 }
188
189 public function getResultProperties() {
190 return array(
191 '' => array(
192 'name' => 'string',
193 'user' => 'string',
194 'timestamp' => 'timestamp'
195 )
196 );
197 }
198
199 public function getDescription() {
200 return 'List all files that are duplicates of the given file(s) based on hash values';
201 }
202
203 public function getPossibleErrors() {
204 return array_merge( parent::getPossibleErrors(), array(
205 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
206 ) );
207 }
208
209 public function getExamples() {
210 return array(
211 'api.php?action=query&titles=File:Albert_Einstein_Head.jpg&prop=duplicatefiles',
212 'api.php?action=query&generator=allimages&prop=duplicatefiles',
213 );
214 }
215
216 public function getHelpUrls() {
217 return 'https://www.mediawiki.org/wiki/API:Properties#duplicatefiles_.2F_df';
218 }
219
220 public function getVersion() {
221 return __CLASS__ . ': $Id$';
222 }
223 }