SpecialListDuplicatedFiles.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Implements Special:ListDuplicatedFiles
  4. *
  5. * Copyright © 2013 Brian Wolff
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. * http://www.gnu.org/copyleft/gpl.html
  21. *
  22. * @file
  23. * @ingroup SpecialPage
  24. * @author Brian Wolff
  25. */
  26. use Wikimedia\Rdbms\IResultWrapper;
  27. use Wikimedia\Rdbms\IDatabase;
  28. /**
  29. * Special:ListDuplicatedFiles Lists all files where the current version is
  30. * a duplicate of the current version of some other file.
  31. * @ingroup SpecialPage
  32. */
  33. class SpecialListDuplicatedFiles extends QueryPage {
  34. function __construct( $name = 'ListDuplicatedFiles' ) {
  35. parent::__construct( $name );
  36. }
  37. public function isExpensive() {
  38. return true;
  39. }
  40. function isSyndicated() {
  41. return false;
  42. }
  43. /**
  44. * Get all the duplicates by grouping on sha1s.
  45. *
  46. * A cheaper (but less useful) version of this
  47. * query would be to not care how many duplicates a
  48. * particular file has, and do a self-join on image table.
  49. * However this version should be no more expensive then
  50. * Special:MostLinked, which seems to get handled fine
  51. * with however we are doing cached special pages.
  52. * @return array
  53. */
  54. public function getQueryInfo() {
  55. return [
  56. 'tables' => [ 'image' ],
  57. 'fields' => [
  58. 'namespace' => NS_FILE,
  59. 'title' => 'MIN(img_name)',
  60. 'value' => 'count(*)'
  61. ],
  62. 'options' => [
  63. 'GROUP BY' => 'img_sha1',
  64. 'HAVING' => 'count(*) > 1',
  65. ],
  66. ];
  67. }
  68. /**
  69. * Pre-fill the link cache
  70. *
  71. * @param IDatabase $db
  72. * @param IResultWrapper $res
  73. */
  74. function preprocessResults( $db, $res ) {
  75. $this->executeLBFromResultWrapper( $res );
  76. }
  77. /**
  78. * @param Skin $skin
  79. * @param object $result Result row
  80. * @return string
  81. */
  82. function formatResult( $skin, $result ) {
  83. // Future version might include a list of the first 5 duplicates
  84. // perhaps separated by an "↔".
  85. $image1 = Title::makeTitle( $result->namespace, $result->title );
  86. $dupeSearch = SpecialPage::getTitleFor( 'FileDuplicateSearch', $image1->getDBkey() );
  87. $msg = $this->msg( 'listduplicatedfiles-entry' )
  88. ->params( $image1->getText() )
  89. ->numParams( $result->value - 1 )
  90. ->params( $dupeSearch->getPrefixedDBkey() );
  91. return $msg->parse();
  92. }
  93. public function execute( $par ) {
  94. $this->addHelpLink( 'Help:Managing_files' );
  95. parent::execute( $par );
  96. }
  97. protected function getGroupName() {
  98. return 'media';
  99. }
  100. }