e05ffb6fae528b096714c730324c6bde5352d5fb
[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 $this->addTables( 'image', 'i1' );
63 $this->addTables( 'image', 'i2' );
64 $this->addFields( array(
65 'i1.img_name AS orig_name',
66 'i2.img_name AS dup_name',
67 'i2.img_user_text AS dup_user_text',
68 'i2.img_timestamp AS dup_timestamp'
69 ) );
70
71 $this->addWhere( array(
72 'i1.img_name' => array_keys( $images ),
73 'i1.img_sha1 = i2.img_sha1',
74 'i1.img_name != i2.img_name',
75 ) );
76
77 if ( isset( $params['continue'] ) ) {
78 $cont = explode( '|', $params['continue'] );
79 if ( count( $cont ) != 2 ) {
80 $this->dieUsage( 'Invalid continue param. You should pass the ' .
81 'original value returned by the previous query', '_badcontinue' );
82 }
83 $op = $params['dir'] == 'descending' ? '<' : '>';
84 $db = $this->getDB();
85 $orig = $db->addQuotes( $cont[0] );
86 $dup = $db->addQuotes( $cont[1] );
87 $this->addWhere(
88 "i1.img_name $op $orig OR " .
89 "(i1.img_name = $orig AND " .
90 "i2.img_name $op= $dup)"
91 );
92 }
93
94 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
95 // Don't order by i1.img_name if it's constant in the WHERE clause
96 if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
97 $this->addOption( 'ORDER BY', 'i2.img_name' . $sort );
98 } else {
99 $this->addOption( 'ORDER BY', array(
100 'i1.img_name' . $sort,
101 'i2.img_name' . $sort
102 ));
103 }
104 $this->addOption( 'LIMIT', $params['limit'] + 1 );
105
106 $res = $this->select( __METHOD__ );
107 $count = 0;
108 $titles = array();
109 foreach ( $res as $row ) {
110 if ( ++$count > $params['limit'] ) {
111 // We've reached the one extra which shows that
112 // there are additional pages to be had. Stop here...
113 $this->setContinueEnumParameter( 'continue', $row->orig_name . '|' . $row->dup_name );
114 break;
115 }
116 if ( !is_null( $resultPageSet ) ) {
117 $titles[] = Title::makeTitle( NS_FILE, $row->dup_name );
118 } else {
119 $r = array(
120 'name' => $row->dup_name,
121 'user' => $row->dup_user_text,
122 'timestamp' => wfTimestamp( TS_ISO_8601, $row->dup_timestamp )
123 );
124 $fit = $this->addPageSubItem( $images[$row->orig_name], $r );
125 if ( !$fit ) {
126 $this->setContinueEnumParameter( 'continue', $row->orig_name . '|' . $row->dup_name );
127 break;
128 }
129 }
130 }
131 if ( !is_null( $resultPageSet ) ) {
132 $resultPageSet->populateFromTitles( $titles );
133 }
134 }
135
136 public function getAllowedParams() {
137 return array(
138 'limit' => array(
139 ApiBase::PARAM_DFLT => 10,
140 ApiBase::PARAM_TYPE => 'limit',
141 ApiBase::PARAM_MIN => 1,
142 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
143 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
144 ),
145 'continue' => null,
146 'dir' => array(
147 ApiBase::PARAM_DFLT => 'ascending',
148 ApiBase::PARAM_TYPE => array(
149 'ascending',
150 'descending'
151 )
152 ),
153 );
154 }
155
156 public function getParamDescription() {
157 return array(
158 'limit' => 'How many files to return',
159 'continue' => 'When more results are available, use this to continue',
160 'dir' => 'The direction in which to list',
161 );
162 }
163
164 public function getResultProperties() {
165 return array(
166 '' => array(
167 'name' => 'string',
168 'user' => 'string',
169 'timestamp' => 'timestamp'
170 )
171 );
172 }
173
174 public function getDescription() {
175 return 'List all files that are duplicates of the given file(s)';
176 }
177
178 public function getPossibleErrors() {
179 return array_merge( parent::getPossibleErrors(), array(
180 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
181 ) );
182 }
183
184 public function getExamples() {
185 return array(
186 'api.php?action=query&titles=File:Albert_Einstein_Head.jpg&prop=duplicatefiles',
187 'api.php?action=query&generator=allimages&prop=duplicatefiles',
188 );
189 }
190
191 public function getHelpUrls() {
192 return 'https://www.mediawiki.org/wiki/API:Properties#duplicatefiles_.2F_df';
193 }
194
195 public function getVersion() {
196 return __CLASS__ . ': $Id$';
197 }
198 }