Merge "Making listToText() not break if passed a 1-item list."
[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 $files = RepoGroup::singleton()->findFiles( array_keys( $images ) );
86
87 $fit = true;
88 $count = 0;
89 $titles = array();
90
91 $sha1s = array();
92 foreach ( $files as $file ) {
93 $sha1s[$file->getName()] = $file->getSha1();
94 }
95
96 // find all files with the hashes, result format is: array( hash => array( dup1, dup2 ), hash1 => ... )
97 $filesBySha1s = RepoGroup::singleton()->findBySha1s( array_unique( array_values( $sha1s ) ) );
98
99 // iterate over $images to handle continue param correct
100 foreach( $images as $image => $pageId ) {
101 if( !isset( $sha1s[$image] ) ) {
102 continue; //file does not exist
103 }
104 $sha1 = $sha1s[$image];
105 $dupFiles = $filesBySha1s[$sha1];
106 if( $params['dir'] == 'descending' ) {
107 $dupFiles = array_reverse( $dupFiles );
108 }
109 foreach ( $dupFiles as $dupFile ) {
110 $dupName = $dupFile->getName();
111 if( $image == $dupName ) {
112 continue; //ignore the file itself
113 }
114 if( $skipUntilThisDup !== false && $dupName < $skipUntilThisDup ) {
115 continue; //skip to pos after the image from continue param
116 }
117 $skipUntilThisDup = false;
118 if ( ++$count > $params['limit'] ) {
119 $fit = false; //break outer loop
120 // We're one over limit which shows that
121 // there are additional images to be had. Stop here...
122 $this->setContinueEnumParameter( 'continue', $image . '|' . $dupName );
123 break;
124 }
125 if ( !is_null( $resultPageSet ) ) {
126 $titles[] = $file->getTitle();
127 } else {
128 $r = array(
129 'name' => $dupName,
130 'user' => $dupFile->getUser( 'text' ),
131 'timestamp' => wfTimestamp( TS_ISO_8601, $dupFile->getTimestamp() )
132 );
133 $fit = $this->addPageSubItem( $pageId, $r );
134 if ( !$fit ) {
135 $this->setContinueEnumParameter( 'continue', $image . '|' . $dupName );
136 break;
137 }
138 }
139 }
140 if( !$fit ) {
141 break;
142 }
143 }
144 if ( !is_null( $resultPageSet ) ) {
145 $resultPageSet->populateFromTitles( $titles );
146 }
147 }
148
149 public function getAllowedParams() {
150 return array(
151 'limit' => array(
152 ApiBase::PARAM_DFLT => 10,
153 ApiBase::PARAM_TYPE => 'limit',
154 ApiBase::PARAM_MIN => 1,
155 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
156 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
157 ),
158 'continue' => null,
159 'dir' => array(
160 ApiBase::PARAM_DFLT => 'ascending',
161 ApiBase::PARAM_TYPE => array(
162 'ascending',
163 'descending'
164 )
165 ),
166 );
167 }
168
169 public function getParamDescription() {
170 return array(
171 'limit' => 'How many duplicate files to return',
172 'continue' => 'When more results are available, use this to continue',
173 'dir' => 'The direction in which to list',
174 );
175 }
176
177 public function getResultProperties() {
178 return array(
179 '' => array(
180 'name' => 'string',
181 'user' => 'string',
182 'timestamp' => 'timestamp'
183 )
184 );
185 }
186
187 public function getDescription() {
188 return 'List all files that are duplicates of the given file(s) based on hash values';
189 }
190
191 public function getPossibleErrors() {
192 return array_merge( parent::getPossibleErrors(), array(
193 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
194 ) );
195 }
196
197 public function getExamples() {
198 return array(
199 'api.php?action=query&titles=File:Albert_Einstein_Head.jpg&prop=duplicatefiles',
200 'api.php?action=query&generator=allimages&prop=duplicatefiles',
201 );
202 }
203
204 public function getHelpUrls() {
205 return 'https://www.mediawiki.org/wiki/API:Properties#duplicatefiles_.2F_df';
206 }
207
208 public function getVersion() {
209 return __CLASS__ . ': $Id$';
210 }
211 }