5 * Created on Sep 27, 2008
7 * Copyright © 2008 Roan Kattouw <Firstname>.<Lastname>@gmail.com
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.
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.
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
28 * A query module to list duplicates of the given file(s)
32 class ApiQueryDuplicateFiles
extends ApiQueryGeneratorBase
{
34 public function __construct( $query, $moduleName ) {
35 parent
::__construct( $query, $moduleName, 'df' );
38 public function execute() {
42 public function getCacheMode( $params ) {
46 public function executeGenerator( $resultPageSet ) {
47 $this->run( $resultPageSet );
51 * @param $resultPageSet ApiPageSet
54 private function run( $resultPageSet = null ) {
55 $params = $this->extractRequestParams();
56 $namespaces = $this->getPageSet()->getAllTitlesByNamespace();
57 if ( empty( $namespaces[NS_FILE
] ) ) {
60 $images = $namespaces[NS_FILE
];
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'
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',
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' );
83 $orig = $this->getDB()->strencode( $this->titleTokey( $cont[0] ) );
84 $dup = $this->getDB()->strencode( $this->titleToKey( $cont[1] ) );
86 "i1.img_name > '$orig' OR " .
87 "(i1.img_name = '$orig' AND " .
88 "i2.img_name >= '$dup')"
92 $dir = ( $params['dir'] == 'descending' ?
' DESC' : '' );
93 $this->addOption( 'ORDER BY', 'i1.img_name' . $dir );
94 $this->addOption( 'LIMIT', $params['limit'] +
1 );
96 $res = $this->select( __METHOD__
);
99 foreach ( $res as $row ) {
100 if ( ++
$count > $params['limit'] ) {
101 // We've reached the one extra which shows that
102 // there are additional pages to be had. Stop here...
103 $this->setContinueEnumParameter( 'continue',
104 $this->keyToTitle( $row->orig_name
) . '|' .
105 $this->keyToTitle( $row->dup_name
) );
108 if ( !is_null( $resultPageSet ) ) {
109 $titles[] = Title
::makeTitle( NS_FILE
, $row->dup_name
);
112 'name' => $row->dup_name
,
113 'user' => $row->dup_user_text
,
114 'timestamp' => wfTimestamp( TS_ISO_8601
, $row->dup_timestamp
)
116 $fit = $this->addPageSubItem( $images[$row->orig_name
], $r );
118 $this->setContinueEnumParameter( 'continue',
119 $this->keyToTitle( $row->orig_name
) . '|' .
120 $this->keyToTitle( $row->dup_name
) );
125 if ( !is_null( $resultPageSet ) ) {
126 $resultPageSet->populateFromTitles( $titles );
130 public function getAllowedParams() {
133 ApiBase
::PARAM_DFLT
=> 10,
134 ApiBase
::PARAM_TYPE
=> 'limit',
135 ApiBase
::PARAM_MIN
=> 1,
136 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
137 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
141 ApiBase
::PARAM_DFLT
=> 'ascending',
142 ApiBase
::PARAM_TYPE
=> array(
150 public function getParamDescription() {
152 'limit' => 'How many files to return',
153 'continue' => 'When more results are available, use this to continue',
154 'dir' => 'The direction in which to list',
158 public function getDescription() {
159 return 'List all files that are duplicates of the given file(s)';
162 public function getPossibleErrors() {
163 return array_merge( parent
::getPossibleErrors(), array(
164 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
168 public function getExamples() {
170 'api.php?action=query&titles=File:Albert_Einstein_Head.jpg&prop=duplicatefiles',
171 'api.php?action=query&generator=allimages&prop=duplicatefiles',
175 public function getHelpUrls() {
176 return 'http://www.mediawiki.org/wiki/API:Properties#duplicatefiles_.2F_df';
179 public function getVersion() {
180 return __CLASS__
. ': $Id$';