4 * Created on Sep 27, 2008
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2008 Roan Kattow <Firstname>,<Lastname>@home.nl
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.
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.
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ("ApiQueryBase.php");
32 * A query module to list duplicates of the given file(s)
36 class ApiQueryDuplicateFiles
extends ApiQueryGeneratorBase
{
38 public function __construct($query, $moduleName) {
39 parent
:: __construct($query, $moduleName, 'df');
42 public function execute() {
46 public function executeGenerator($resultPageSet) {
47 $this->run($resultPageSet);
50 private function run($resultPageSet = null) {
51 $params = $this->extractRequestParams();
52 $namespaces = $this->getPageSet()->getAllTitlesByNamespace();
53 $images = $namespaces[NS_IMAGE
];
57 $this->addTables('image', 'i1');
58 $this->addTables('image', 'i2');
59 $this->addFields(array(
60 'i1.img_name AS orig_name',
61 'i2.img_name AS dup_name',
62 'i2.img_user_text AS dup_user_text',
63 'i2.img_timestamp AS dup_timestamp'
65 $this->addWhere(array(
66 'i1.img_name' => array_keys($images),
67 'i1.img_sha1 = i2.img_sha1',
68 'i1.img_name != i2.img_name',
70 if(isset($params['continue']))
72 $cont = explode('|', $params['continue']);
74 $this->dieUsage("Invalid continue param. You should pass the " .
75 "original value returned by the previous query", "_badcontinue");
76 $orig = $this->getDB()->strencode($this->titleTokey($cont[0]));
77 $dup = $this->getDB()->strencode($this->titleToKey($cont[1]));
78 $this->addWhere("i1.img_name > '$orig' OR ".
79 "(i1.img_name = '$orig' AND ".
80 "i2.img_name >= '$dup')");
82 $this->addOption('ORDER BY', 'i1.img_name');
83 $this->addOption('LIMIT', $params['limit'] +
1);
85 $res = $this->select(__METHOD__
);
91 while($row = $db->fetchObject($res))
93 if(++
$count > $params['limit'])
95 // We've reached the one extra which shows that
96 // there are additional pages to be had. Stop here...
97 $this->setContinueEnumParameter('continue',
98 $this->keyToTitle($row->orig_name
) . '|' .
99 $this->keyToTitle($row->dup_name
));
102 if(!is_null($resultPageSet))
103 $titles[] = Title
::makeTitle(NS_IMAGE
, $row->dup_name
);
106 if($row->orig_name
!= $lastName)
110 $this->addPageSubItems($images[$lastName], $data);
113 $lastName = $row->orig_name
;
117 'name' => $row->dup_name
,
118 'user' => $row->dup_user_text
,
119 'timestamp' => wfTimestamp(TS_ISO_8601
, $row->dup_timestamp
)
123 if(!is_null($resultPageSet))
124 $resultPageSet->populateFromTitles($titles);
125 else if($lastName != '')
126 $this->addPageSubItems($images[$lastName], $data);
127 $db->freeResult($res);
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
143 public function getParamDescription() {
145 'limit' => 'How many files to return',
146 'continue' => 'When more results are available, use this to continue',
150 public function getDescription() {
151 return 'List all files that are duplicates of the given file(s).';
154 protected function getExamples() {
160 public function getVersion() {
161 return __CLASS__
. ': $Id$';