ApiQueryDuplicateFiles.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /*
  3. * Created on Sep 27, 2008
  4. *
  5. * API for MediaWiki 1.8+
  6. *
  7. * Copyright (C) 2008 Roan Kattow <Firstname>,<Lastname>@home.nl
  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. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. */
  24. if (!defined('MEDIAWIKI')) {
  25. // Eclipse helper - will be ignored in production
  26. require_once ("ApiQueryBase.php");
  27. }
  28. /**
  29. * A query module to list duplicates of the given file(s)
  30. *
  31. * @ingroup API
  32. */
  33. class ApiQueryDuplicateFiles extends ApiQueryGeneratorBase {
  34. public function __construct($query, $moduleName) {
  35. parent :: __construct($query, $moduleName, 'df');
  36. }
  37. public function execute() {
  38. $this->run();
  39. }
  40. public function executeGenerator($resultPageSet) {
  41. $this->run($resultPageSet);
  42. }
  43. private function run($resultPageSet = null) {
  44. $params = $this->extractRequestParams();
  45. $namespaces = $this->getPageSet()->getAllTitlesByNamespace();
  46. if ( empty( $namespaces[NS_FILE] ) ) {
  47. return;
  48. }
  49. $images = $namespaces[NS_FILE];
  50. $this->addTables('image', 'i1');
  51. $this->addTables('image', 'i2');
  52. $this->addFields(array(
  53. 'i1.img_name AS orig_name',
  54. 'i2.img_name AS dup_name',
  55. 'i2.img_user_text AS dup_user_text',
  56. 'i2.img_timestamp AS dup_timestamp'
  57. ));
  58. $this->addWhere(array(
  59. 'i1.img_name' => array_keys($images),
  60. 'i1.img_sha1 = i2.img_sha1',
  61. 'i1.img_name != i2.img_name',
  62. ));
  63. if(isset($params['continue']))
  64. {
  65. $cont = explode('|', $params['continue']);
  66. if(count($cont) != 2)
  67. $this->dieUsage("Invalid continue param. You should pass the " .
  68. "original value returned by the previous query", "_badcontinue");
  69. $orig = $this->getDB()->strencode($this->titleTokey($cont[0]));
  70. $dup = $this->getDB()->strencode($this->titleToKey($cont[1]));
  71. $this->addWhere("i1.img_name > '$orig' OR ".
  72. "(i1.img_name = '$orig' AND ".
  73. "i2.img_name >= '$dup')");
  74. }
  75. $this->addOption('ORDER BY', 'i1.img_name');
  76. $this->addOption('LIMIT', $params['limit'] + 1);
  77. $res = $this->select(__METHOD__);
  78. $db = $this->getDB();
  79. $count = 0;
  80. $titles = array();
  81. while($row = $db->fetchObject($res))
  82. {
  83. if(++$count > $params['limit'])
  84. {
  85. // We've reached the one extra which shows that
  86. // there are additional pages to be had. Stop here...
  87. $this->setContinueEnumParameter('continue',
  88. $this->keyToTitle($row->orig_name) . '|' .
  89. $this->keyToTitle($row->dup_name));
  90. break;
  91. }
  92. if(!is_null($resultPageSet))
  93. $titles[] = Title::makeTitle(NS_FILE, $row->dup_name);
  94. else
  95. {
  96. $r = array(
  97. 'name' => $row->dup_name,
  98. 'user' => $row->dup_user_text,
  99. 'timestamp' => wfTimestamp(TS_ISO_8601, $row->dup_timestamp)
  100. );
  101. $fit = $this->addPageSubItem($images[$row->orig_name], $r);
  102. if(!$fit)
  103. {
  104. $this->setContinueEnumParameter('continue',
  105. $this->keyToTitle($row->orig_name) . '|' .
  106. $this->keyToTitle($row->dup_name));
  107. break;
  108. }
  109. }
  110. }
  111. if(!is_null($resultPageSet))
  112. $resultPageSet->populateFromTitles($titles);
  113. $db->freeResult($res);
  114. }
  115. public function getAllowedParams() {
  116. return array (
  117. 'limit' => array(
  118. ApiBase :: PARAM_DFLT => 10,
  119. ApiBase :: PARAM_TYPE => 'limit',
  120. ApiBase :: PARAM_MIN => 1,
  121. ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
  122. ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
  123. ),
  124. 'continue' => null,
  125. );
  126. }
  127. public function getParamDescription() {
  128. return array (
  129. 'limit' => 'How many files to return',
  130. 'continue' => 'When more results are available, use this to continue',
  131. );
  132. }
  133. public function getDescription() {
  134. return 'List all files that are duplicates of the given file(s).';
  135. }
  136. protected function getExamples() {
  137. return array ( 'api.php?action=query&titles=File:Albert_Einstein_Head.jpg&prop=duplicatefiles',
  138. 'api.php?action=query&generator=allimages&prop=duplicatefiles',
  139. );
  140. }
  141. public function getVersion() {
  142. return __CLASS__ . ': $Id: ApiQueryDuplicateFiles.php 48215 2009-03-09 10:44:34Z catrope $';
  143. }
  144. }