Rewrote r69339 etc. to clean up API cache header handling.
[lhc/web/wiklou.git] / includes / api / ApiQueryDuplicateFiles.php
1 <?php
2
3 /**
4 * Created on Sep 27, 2008
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright © 2008 Roan Kattow <Firstname>,<Lastname>@home.nl
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( "ApiQueryBase.php" );
29 }
30
31 /**
32 * A query module to list duplicates of the given file(s)
33 *
34 * @ingroup API
35 */
36 class ApiQueryDuplicateFiles extends ApiQueryGeneratorBase {
37
38 public function __construct( $query, $moduleName ) {
39 parent::__construct( $query, $moduleName, 'df' );
40 }
41
42 public function execute() {
43 $this->run();
44 }
45
46 public function getCacheMode( $params ) {
47 return 'public';
48 }
49
50 public function executeGenerator( $resultPageSet ) {
51 $this->run( $resultPageSet );
52 }
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 $orig = $this->getDB()->strencode( $this->titleTokey( $cont[0] ) );
84 $dup = $this->getDB()->strencode( $this->titleToKey( $cont[1] ) );
85 $this->addWhere(
86 "i1.img_name > '$orig' OR " .
87 "(i1.img_name = '$orig' AND " .
88 "i2.img_name >= '$dup')"
89 );
90 }
91
92 $this->addOption( 'ORDER BY', 'i1.img_name' );
93 $this->addOption( 'LIMIT', $params['limit'] + 1 );
94
95 $res = $this->select( __METHOD__ );
96 $count = 0;
97 $titles = array();
98 foreach ( $res as $row ) {
99 if ( ++$count > $params['limit'] ) {
100 // We've reached the one extra which shows that
101 // there are additional pages to be had. Stop here...
102 $this->setContinueEnumParameter( 'continue',
103 $this->keyToTitle( $row->orig_name ) . '|' .
104 $this->keyToTitle( $row->dup_name ) );
105 break;
106 }
107 if ( !is_null( $resultPageSet ) ) {
108 $titles[] = Title::makeTitle( NS_FILE, $row->dup_name );
109 } else {
110 $r = array(
111 'name' => $row->dup_name,
112 'user' => $row->dup_user_text,
113 'timestamp' => wfTimestamp( TS_ISO_8601, $row->dup_timestamp )
114 );
115 $fit = $this->addPageSubItem( $images[$row->orig_name], $r );
116 if ( !$fit ) {
117 $this->setContinueEnumParameter( 'continue',
118 $this->keyToTitle( $row->orig_name ) . '|' .
119 $this->keyToTitle( $row->dup_name ) );
120 break;
121 }
122 }
123 }
124 if ( !is_null( $resultPageSet ) ) {
125 $resultPageSet->populateFromTitles( $titles );
126 }
127 }
128
129 public function getAllowedParams() {
130 return array(
131 'limit' => array(
132 ApiBase::PARAM_DFLT => 10,
133 ApiBase::PARAM_TYPE => 'limit',
134 ApiBase::PARAM_MIN => 1,
135 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
136 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
137 ),
138 'continue' => null,
139 );
140 }
141
142 public function getParamDescription() {
143 return array(
144 'limit' => 'How many files to return',
145 'continue' => 'When more results are available, use this to continue',
146 );
147 }
148
149 public function getDescription() {
150 return 'List all files that are duplicates of the given file(s)';
151 }
152
153 public function getPossibleErrors() {
154 return array_merge( parent::getPossibleErrors(), array(
155 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
156 ) );
157 }
158
159 protected function getExamples() {
160 return array(
161 'api.php?action=query&titles=File:Albert_Einstein_Head.jpg&prop=duplicatefiles',
162 'api.php?action=query&generator=allimages&prop=duplicatefiles',
163 );
164 }
165
166 public function getVersion() {
167 return __CLASS__ . ': $Id$';
168 }
169 }