]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/api/ApiQueryDuplicateFiles.php
MediaWiki 1.15.0
[autoinstallsdev/mediawiki.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 (C) 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  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 executeGenerator($resultPageSet) {
47                 $this->run($resultPageSet);
48         }
49
50         private function run($resultPageSet = null) {
51                 $params = $this->extractRequestParams();
52                 $namespaces = $this->getPageSet()->getAllTitlesByNamespace();
53                 if ( empty( $namespaces[NS_FILE] ) ) {
54                         return;
55                 }
56                 $images = $namespaces[NS_FILE];
57                 
58                 $this->addTables('image', 'i1');
59                 $this->addTables('image', 'i2');
60                 $this->addFields(array(
61                         'i1.img_name AS orig_name',
62                         'i2.img_name AS dup_name',
63                         'i2.img_user_text AS dup_user_text',
64                         'i2.img_timestamp AS dup_timestamp'
65                 ));
66                 $this->addWhere(array(
67                         'i1.img_name' => array_keys($images),
68                         'i1.img_sha1 = i2.img_sha1',
69                         'i1.img_name != i2.img_name',
70                 ));
71                 if(isset($params['continue']))
72                 {
73                         $cont = explode('|', $params['continue']);
74                         if(count($cont) != 2)
75                                 $this->dieUsage("Invalid continue param. You should pass the " .
76                                         "original value returned by the previous query", "_badcontinue");
77                         $orig = $this->getDB()->strencode($this->titleTokey($cont[0]));
78                         $dup = $this->getDB()->strencode($this->titleToKey($cont[1]));
79                         $this->addWhere("i1.img_name > '$orig' OR ".
80                                         "(i1.img_name = '$orig' AND ".
81                                         "i2.img_name >= '$dup')");
82                 }
83                 $this->addOption('ORDER BY', 'i1.img_name');
84                 $this->addOption('LIMIT', $params['limit'] + 1);
85
86                 $res = $this->select(__METHOD__);
87                 $db = $this->getDB();
88                 $count = 0;
89                 $titles = array();
90                 while($row = $db->fetchObject($res))
91                 {
92                         if(++$count > $params['limit'])
93                         {
94                                 // We've reached the one extra which shows that
95                                 // there are additional pages to be had. Stop here...
96                                 $this->setContinueEnumParameter('continue',
97                                         $this->keyToTitle($row->orig_name) . '|' .
98                                         $this->keyToTitle($row->dup_name));
99                                 break;
100                         }
101                         if(!is_null($resultPageSet))
102                                 $titles[] = Title::makeTitle(NS_FILE, $row->dup_name);
103                         else
104                         {
105                                 $r = array(
106                                         'name' => $row->dup_name,
107                                         'user' => $row->dup_user_text,
108                                         'timestamp' => wfTimestamp(TS_ISO_8601, $row->dup_timestamp)
109                                 );
110                                 $fit = $this->addPageSubItem($images[$row->orig_name], $r);
111                                 if(!$fit)
112                                 {
113                                         $this->setContinueEnumParameter('continue',
114                                                         $this->keyToTitle($row->orig_name) . '|' .
115                                                         $this->keyToTitle($row->dup_name));
116                                         break;
117                                 }
118                         }
119                 }
120                 if(!is_null($resultPageSet))
121                         $resultPageSet->populateFromTitles($titles);
122                 $db->freeResult($res);
123         }
124
125         public function getAllowedParams() {
126                 return array (
127                         'limit' => array(
128                                 ApiBase :: PARAM_DFLT => 10,
129                                 ApiBase :: PARAM_TYPE => 'limit',
130                                 ApiBase :: PARAM_MIN => 1,
131                                 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
132                                 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
133                         ),
134                         'continue' => null,
135                 );
136         }
137
138         public function getParamDescription() {
139                 return array (
140                         'limit' => 'How many files to return',
141                         'continue' => 'When more results are available, use this to continue',
142                 );
143         }
144
145         public function getDescription() {
146                 return 'List all files that are duplicates of the given file(s).';
147         }
148
149         protected function getExamples() {
150                 return array (  'api.php?action=query&titles=File:Albert_Einstein_Head.jpg&prop=duplicatefiles',
151                                 'api.php?action=query&generator=allimages&prop=duplicatefiles',
152                         );
153         }
154
155         public function getVersion() {
156                 return __CLASS__ . ': $Id: ApiQueryDuplicateFiles.php 48215 2009-03-09 10:44:34Z catrope $';
157         }
158 }