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