MediaWiki
REL1_20
|
00001 <?php 00031 class FileDuplicateSearchPage extends QueryPage { 00032 protected $hash = '', $filename = ''; 00033 00037 protected $file = null; 00038 00039 function __construct( $name = 'FileDuplicateSearch' ) { 00040 parent::__construct( $name ); 00041 } 00042 00043 function isSyndicated() { return false; } 00044 function isCacheable() { return false; } 00045 function isCached() { return false; } 00046 00047 function linkParameters() { 00048 return array( 'filename' => $this->filename ); 00049 } 00050 00056 function getDupes() { 00057 return RepoGroup::singleton()->findBySha1( $this->hash ); 00058 } 00059 00064 function showList( $dupes ) { 00065 $html = array(); 00066 $html[] = $this->openList( 0 ); 00067 00068 foreach ( $dupes as $dupe ) { 00069 $line = $this->formatResult( null, $dupe ); 00070 $html[] = "<li>" . $line . "</li>"; 00071 } 00072 $html[] = $this->closeList(); 00073 00074 $this->getOutput()->addHtml( implode( "\n", $html ) ); 00075 } 00076 00077 function getQueryInfo() { 00078 return array( 00079 'tables' => array( 'image' ), 00080 'fields' => array( 00081 'title' => 'img_name', 00082 'value' => 'img_sha1', 00083 'img_user_text', 00084 'img_timestamp' 00085 ), 00086 'conds' => array( 'img_sha1' => $this->hash ) 00087 ); 00088 } 00089 00090 function execute( $par ) { 00091 global $wgScript; 00092 00093 $this->setHeaders(); 00094 $this->outputHeader(); 00095 00096 $this->filename = isset( $par ) ? $par : $this->getRequest()->getText( 'filename' ); 00097 $this->file = null; 00098 $this->hash = ''; 00099 $title = Title::newFromText( $this->filename, NS_FILE ); 00100 if( $title && $title->getText() != '' ) { 00101 $this->file = wfFindFile( $title ); 00102 } 00103 00104 $out = $this->getOutput(); 00105 00106 # Create the input form 00107 $out->addHTML( 00108 Xml::openElement( 'form', array( 'id' => 'fileduplicatesearch', 'method' => 'get', 'action' => $wgScript ) ) . 00109 Html::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) . 00110 Xml::openElement( 'fieldset' ) . 00111 Xml::element( 'legend', null, $this->msg( 'fileduplicatesearch-legend' )->text() ) . 00112 Xml::inputLabel( $this->msg( 'fileduplicatesearch-filename' )->text(), 'filename', 'filename', 50, $this->filename ) . ' ' . 00113 Xml::submitButton( $this->msg( 'fileduplicatesearch-submit' )->text() ) . 00114 Xml::closeElement( 'fieldset' ) . 00115 Xml::closeElement( 'form' ) 00116 ); 00117 00118 if( $this->file ) { 00119 $this->hash = $this->file->getSha1(); 00120 } elseif( $this->filename !== '' ) { 00121 $out->wrapWikiMsg( 00122 "<p class='mw-fileduplicatesearch-noresults'>\n$1\n</p>", 00123 array( 'fileduplicatesearch-noresults', wfEscapeWikiText( $this->filename ) ) 00124 ); 00125 } 00126 00127 if( $this->hash != '' ) { 00128 # Show a thumbnail of the file 00129 $img = $this->file; 00130 if ( $img ) { 00131 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) ); 00132 if( $thumb ) { 00133 $out->addHTML( '<div id="mw-fileduplicatesearch-icon">' . 00134 $thumb->toHtml( array( 'desc-link' => false ) ) . '<br />' . 00135 $this->msg( 'fileduplicatesearch-info' )->numParams( 00136 $img->getWidth(), $img->getHeight() )->params( 00137 $this->getLanguage()->formatSize( $img->getSize() ), 00138 $img->getMimeType() )->parseAsBlock() . 00139 '</div>' ); 00140 } 00141 } 00142 00143 $dupes = $this->getDupes(); 00144 $numRows = count( $dupes ); 00145 00146 # Show a short summary 00147 if( $numRows == 1 ) { 00148 $out->wrapWikiMsg( 00149 "<p class='mw-fileduplicatesearch-result-1'>\n$1\n</p>", 00150 array( 'fileduplicatesearch-result-1', wfEscapeWikiText( $this->filename ) ) 00151 ); 00152 } elseif ( $numRows ) { 00153 $out->wrapWikiMsg( 00154 "<p class='mw-fileduplicatesearch-result-n'>\n$1\n</p>", 00155 array( 'fileduplicatesearch-result-n', wfEscapeWikiText( $this->filename ), 00156 $this->getLanguage()->formatNum( $numRows - 1 ) ) 00157 ); 00158 } 00159 00160 $this->doBatchLookups( $dupes ); 00161 $this->showList( $dupes ); 00162 } 00163 } 00164 00165 function doBatchLookups( $list ) { 00166 $batch = new LinkBatch(); 00167 foreach( $list as $file ) { 00168 $batch->addObj( $file->getTitle() ); 00169 if( $file->isLocal() ) { 00170 $userName = $file->getUser( 'text' ); 00171 $batch->add( NS_USER, $userName ); 00172 $batch->add( NS_USER_TALK, $userName ); 00173 } 00174 } 00175 $batch->execute(); 00176 } 00177 00184 function formatResult( $skin, $result ) { 00185 global $wgContLang; 00186 00187 $nt = $result->getTitle(); 00188 $text = $wgContLang->convert( $nt->getText() ); 00189 $plink = Linker::link( 00190 Title::newFromText( $nt->getPrefixedText() ), 00191 $text 00192 ); 00193 00194 $userText = $result->getUser( 'text' ); 00195 if ( $result->isLocal() ) { 00196 $userId = $result->getUser( 'id' ); 00197 $user = Linker::userLink( $userId, $userText ); 00198 $user .= $this->getContext()->msg( 'word-separator' )->plain(); 00199 $user .= '<span style="white-space: nowrap;">'; 00200 $user .= Linker::userToolLinks( $userId, $userText ); 00201 $user .= '</span>'; 00202 } else { 00203 $user = htmlspecialchars( $userText ); 00204 } 00205 00206 $time = $this->getLanguage()->userTimeAndDate( $result->getTimestamp(), $this->getUser() ); 00207 00208 return "$plink . . $user . . $time"; 00209 } 00210 }