(bug 28070) Fix watchlist RSS for databases that store timestamps in a real timestamp...
[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 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( "ApiQueryBase.php" );
30 }
31
32 /**
33 * A query module to list duplicates of the given file(s)
34 *
35 * @ingroup API
36 */
37 class ApiQueryDuplicateFiles extends ApiQueryGeneratorBase {
38
39 public function __construct( $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'df' );
41 }
42
43 public function execute() {
44 $this->run();
45 }
46
47 public function getCacheMode( $params ) {
48 return 'public';
49 }
50
51 public function executeGenerator( $resultPageSet ) {
52 $this->run( $resultPageSet );
53 }
54
55 /**
56 * @param $resultPageSet ApiPageSet
57 * @return
58 */
59 private function run( $resultPageSet = null ) {
60 $params = $this->extractRequestParams();
61 $namespaces = $this->getPageSet()->getAllTitlesByNamespace();
62 if ( empty( $namespaces[NS_FILE] ) ) {
63 return;
64 }
65 $images = $namespaces[NS_FILE];
66
67 $this->addTables( 'image', 'i1' );
68 $this->addTables( 'image', 'i2' );
69 $this->addFields( array(
70 'i1.img_name AS orig_name',
71 'i2.img_name AS dup_name',
72 'i2.img_user_text AS dup_user_text',
73 'i2.img_timestamp AS dup_timestamp'
74 ) );
75
76 $this->addWhere( array(
77 'i1.img_name' => array_keys( $images ),
78 'i1.img_sha1 = i2.img_sha1',
79 'i1.img_name != i2.img_name',
80 ) );
81
82 if ( isset( $params['continue'] ) ) {
83 $cont = explode( '|', $params['continue'] );
84 if ( count( $cont ) != 2 ) {
85 $this->dieUsage( 'Invalid continue param. You should pass the ' .
86 'original value returned by the previous query', '_badcontinue' );
87 }
88 $orig = $this->getDB()->strencode( $this->titleTokey( $cont[0] ) );
89 $dup = $this->getDB()->strencode( $this->titleToKey( $cont[1] ) );
90 $this->addWhere(
91 "i1.img_name > '$orig' OR " .
92 "(i1.img_name = '$orig' AND " .
93 "i2.img_name >= '$dup')"
94 );
95 }
96
97 $this->addOption( 'ORDER BY', 'i1.img_name' );
98 $this->addOption( 'LIMIT', $params['limit'] + 1 );
99
100 $res = $this->select( __METHOD__ );
101 $count = 0;
102 $titles = array();
103 foreach ( $res as $row ) {
104 if ( ++$count > $params['limit'] ) {
105 // We've reached the one extra which shows that
106 // there are additional pages to be had. Stop here...
107 $this->setContinueEnumParameter( 'continue',
108 $this->keyToTitle( $row->orig_name ) . '|' .
109 $this->keyToTitle( $row->dup_name ) );
110 break;
111 }
112 if ( !is_null( $resultPageSet ) ) {
113 $titles[] = Title::makeTitle( NS_FILE, $row->dup_name );
114 } else {
115 $r = array(
116 'name' => $row->dup_name,
117 'user' => $row->dup_user_text,
118 'timestamp' => wfTimestamp( TS_ISO_8601, $row->dup_timestamp )
119 );
120 $fit = $this->addPageSubItem( $images[$row->orig_name], $r );
121 if ( !$fit ) {
122 $this->setContinueEnumParameter( 'continue',
123 $this->keyToTitle( $row->orig_name ) . '|' .
124 $this->keyToTitle( $row->dup_name ) );
125 break;
126 }
127 }
128 }
129 if ( !is_null( $resultPageSet ) ) {
130 $resultPageSet->populateFromTitles( $titles );
131 }
132 }
133
134 public function getAllowedParams() {
135 return array(
136 'limit' => array(
137 ApiBase::PARAM_DFLT => 10,
138 ApiBase::PARAM_TYPE => 'limit',
139 ApiBase::PARAM_MIN => 1,
140 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
141 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
142 ),
143 'continue' => null,
144 );
145 }
146
147 public function getParamDescription() {
148 return array(
149 'limit' => 'How many files to return',
150 'continue' => 'When more results are available, use this to continue',
151 );
152 }
153
154 public function getDescription() {
155 return 'List all files that are duplicates of the given file(s)';
156 }
157
158 public function getPossibleErrors() {
159 return array_merge( parent::getPossibleErrors(), array(
160 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
161 ) );
162 }
163
164 protected function getExamples() {
165 return array(
166 'api.php?action=query&titles=File:Albert_Einstein_Head.jpg&prop=duplicatefiles',
167 'api.php?action=query&generator=allimages&prop=duplicatefiles',
168 );
169 }
170
171 public function getVersion() {
172 return __CLASS__ . ': $Id$';
173 }
174 }