SpecialFileDuplicateSearch.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * A special page to search for files by hash value as defined in the
  4. * img_sha1 field in the image table
  5. *
  6. * @file
  7. * @ingroup SpecialPage
  8. *
  9. * @author Raimond Spekking, based on Special:MIMESearch by Ævar Arnfjörð Bjarmason
  10. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
  11. */
  12. /**
  13. * Searches the database for files of the requested hash, comparing this with the
  14. * 'img_sha1' field in the image table.
  15. * @ingroup SpecialPage
  16. */
  17. class FileDuplicateSearchPage extends QueryPage {
  18. var $hash, $filename;
  19. function FileDuplicateSearchPage( $hash, $filename ) {
  20. $this->hash = $hash;
  21. $this->filename = $filename;
  22. }
  23. function getName() { return 'FileDuplicateSearch'; }
  24. function isExpensive() { return false; }
  25. function isSyndicated() { return false; }
  26. function linkParameters() {
  27. return array( 'filename' => $this->filename );
  28. }
  29. function getSQL() {
  30. $dbr = wfGetDB( DB_SLAVE );
  31. $image = $dbr->tableName( 'image' );
  32. $hash = $dbr->addQuotes( $this->hash );
  33. return "SELECT 'FileDuplicateSearch' AS type,
  34. img_name AS title,
  35. img_sha1 AS value,
  36. img_user_text,
  37. img_timestamp
  38. FROM $image
  39. WHERE img_sha1 = $hash
  40. ";
  41. }
  42. function formatResult( $skin, $result ) {
  43. global $wgContLang, $wgLang;
  44. $nt = Title::makeTitle( NS_FILE, $result->title );
  45. $text = $wgContLang->convert( $nt->getText() );
  46. $plink = $skin->makeLink( $nt->getPrefixedText(), $text );
  47. $user = $skin->makeLinkObj( Title::makeTitle( NS_USER, $result->img_user_text ), $result->img_user_text );
  48. $time = $wgLang->timeanddate( $result->img_timestamp );
  49. return "$plink . . $user . . $time";
  50. }
  51. }
  52. /**
  53. * Output the HTML search form, and constructs the FileDuplicateSearch object.
  54. */
  55. function wfSpecialFileDuplicateSearch( $par = null ) {
  56. global $wgRequest, $wgOut, $wgLang, $wgContLang, $wgScript;
  57. $hash = '';
  58. $filename = isset( $par ) ? $par : $wgRequest->getText( 'filename' );
  59. $title = Title::newFromText( $filename );
  60. if( $title && $title->getText() != '' ) {
  61. $dbr = wfGetDB( DB_SLAVE );
  62. $image = $dbr->tableName( 'image' );
  63. $encFilename = $dbr->addQuotes( htmlspecialchars( $title->getDBKey() ) );
  64. $sql = "SELECT img_sha1 from $image where img_name = $encFilename";
  65. $res = $dbr->query( $sql );
  66. $row = $dbr->fetchRow( $res );
  67. if( $row !== false ) {
  68. $hash = $row[0];
  69. }
  70. $dbr->freeResult( $res );
  71. }
  72. # Create the input form
  73. $wgOut->addHTML(
  74. Xml::openElement( 'form', array( 'id' => 'fileduplicatesearch', 'method' => 'get', 'action' => $wgScript ) ) .
  75. Xml::hidden( 'title', SpecialPage::getTitleFor( 'FileDuplicateSearch' )->getPrefixedDbKey() ) .
  76. Xml::openElement( 'fieldset' ) .
  77. Xml::element( 'legend', null, wfMsg( 'fileduplicatesearch-legend' ) ) .
  78. Xml::inputLabel( wfMsg( 'fileduplicatesearch-filename' ), 'filename', 'filename', 50, $filename ) . ' ' .
  79. Xml::submitButton( wfMsg( 'fileduplicatesearch-submit' ) ) .
  80. Xml::closeElement( 'fieldset' ) .
  81. Xml::closeElement( 'form' )
  82. );
  83. if( $hash != '' ) {
  84. $align = $wgContLang->isRtl() ? 'left' : 'right';
  85. # Show a thumbnail of the file
  86. $img = wfFindFile( $title );
  87. if ( $img ) {
  88. $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
  89. if( $thumb ) {
  90. $wgOut->addHTML( '<div style="float:' . $align . '" id="mw-fileduplicatesearch-icon">' .
  91. $thumb->toHtml( array( 'desc-link' => false ) ) . '<br />' .
  92. wfMsgExt( 'fileduplicatesearch-info', array( 'parse' ),
  93. $wgLang->formatNum( $img->getWidth() ),
  94. $wgLang->formatNum( $img->getHeight() ),
  95. $wgLang->formatSize( $img->getSize() ),
  96. $img->getMimeType()
  97. ) .
  98. '</div>' );
  99. }
  100. }
  101. # Do the query
  102. $wpp = new FileDuplicateSearchPage( $hash, $filename );
  103. list( $limit, $offset ) = wfCheckLimits();
  104. $count = $wpp->doQuery( $offset, $limit );
  105. # Show a short summary
  106. if( $count == 1 ) {
  107. $wgOut->addHTML( '<p class="mw-fileduplicatesearch-result-1">' .
  108. wfMsgHtml( 'fileduplicatesearch-result-1', $filename ) .
  109. '</p>'
  110. );
  111. } elseif ( $count > 1 ) {
  112. $wgOut->addHTML( '<p class="mw-fileduplicatesearch-result-n">' .
  113. wfMsgExt( 'fileduplicatesearch-result-n', array( 'parseinline' ), $filename, $wgLang->formatNum( $count - 1 ) ) .
  114. '</p>'
  115. );
  116. }
  117. }
  118. }