MediaWiki
REL1_24
|
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 $this->setHeaders(); 00100 $this->outputHeader(); 00101 00102 $this->filename = $par !== null ? $par : $this->getRequest()->getText( 'filename' ); 00103 $this->file = null; 00104 $this->hash = ''; 00105 $title = Title::newFromText( $this->filename, NS_FILE ); 00106 if ( $title && $title->getText() != '' ) { 00107 $this->file = wfFindFile( $title ); 00108 } 00109 00110 $out = $this->getOutput(); 00111 00112 # Create the input form 00113 $out->addHTML( 00114 Html::openElement( 00115 'form', 00116 array( 'id' => 'fileduplicatesearch', 'method' => 'get', 'action' => wfScript() ) 00117 ) . "\n" . 00118 Html::hidden( 'title', $this->getPageTitle()->getPrefixedDBkey() ) . "\n" . 00119 Html::openElement( 'fieldset' ) . "\n" . 00120 Html::element( 'legend', null, $this->msg( 'fileduplicatesearch-legend' )->text() ) . "\n" . 00121 Xml::inputLabel( 00122 $this->msg( 'fileduplicatesearch-filename' )->text(), 00123 'filename', 00124 'filename', 00125 50, 00126 $this->filename 00127 ) . "\n" . 00128 Xml::submitButton( $this->msg( 'fileduplicatesearch-submit' )->text() ) . "\n" . 00129 Html::closeElement( 'fieldset' ) . "\n" . 00130 Html::closeElement( 'form' ) 00131 ); 00132 00133 if ( $this->file ) { 00134 $this->hash = $this->file->getSha1(); 00135 } elseif ( $this->filename !== '' ) { 00136 $out->wrapWikiMsg( 00137 "<p class='mw-fileduplicatesearch-noresults'>\n$1\n</p>", 00138 array( 'fileduplicatesearch-noresults', wfEscapeWikiText( $this->filename ) ) 00139 ); 00140 } 00141 00142 if ( $this->hash != '' ) { 00143 # Show a thumbnail of the file 00144 $img = $this->file; 00145 if ( $img ) { 00146 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) ); 00147 if ( $thumb ) { 00148 $out->addHTML( '<div id="mw-fileduplicatesearch-icon">' . 00149 $thumb->toHtml( array( 'desc-link' => false ) ) . '<br />' . 00150 $this->msg( 'fileduplicatesearch-info' )->numParams( 00151 $img->getWidth(), $img->getHeight() )->params( 00152 $this->getLanguage()->formatSize( $img->getSize() ), 00153 $img->getMimeType() )->parseAsBlock() . 00154 '</div>' ); 00155 } 00156 } 00157 00158 $dupes = $this->getDupes(); 00159 $numRows = count( $dupes ); 00160 00161 # Show a short summary 00162 if ( $numRows == 1 ) { 00163 $out->wrapWikiMsg( 00164 "<p class='mw-fileduplicatesearch-result-1'>\n$1\n</p>", 00165 array( 'fileduplicatesearch-result-1', wfEscapeWikiText( $this->filename ) ) 00166 ); 00167 } elseif ( $numRows ) { 00168 $out->wrapWikiMsg( 00169 "<p class='mw-fileduplicatesearch-result-n'>\n$1\n</p>", 00170 array( 'fileduplicatesearch-result-n', wfEscapeWikiText( $this->filename ), 00171 $this->getLanguage()->formatNum( $numRows - 1 ) ) 00172 ); 00173 } 00174 00175 $this->doBatchLookups( $dupes ); 00176 $this->showList( $dupes ); 00177 } 00178 } 00179 00180 function doBatchLookups( $list ) { 00181 $batch = new LinkBatch(); 00183 foreach ( $list as $file ) { 00184 $batch->addObj( $file->getTitle() ); 00185 if ( $file->isLocal() ) { 00186 $userName = $file->getUser( 'text' ); 00187 $batch->add( NS_USER, $userName ); 00188 $batch->add( NS_USER_TALK, $userName ); 00189 } 00190 } 00191 00192 $batch->execute(); 00193 } 00194 00201 function formatResult( $skin, $result ) { 00202 global $wgContLang; 00203 00204 $nt = $result->getTitle(); 00205 $text = $wgContLang->convert( $nt->getText() ); 00206 $plink = Linker::link( 00207 Title::newFromText( $nt->getPrefixedText() ), 00208 $text 00209 ); 00210 00211 $userText = $result->getUser( 'text' ); 00212 if ( $result->isLocal() ) { 00213 $userId = $result->getUser( 'id' ); 00214 $user = Linker::userLink( $userId, $userText ); 00215 $user .= $this->getContext()->msg( 'word-separator' )->plain(); 00216 $user .= '<span style="white-space: nowrap;">'; 00217 $user .= Linker::userToolLinks( $userId, $userText ); 00218 $user .= '</span>'; 00219 } else { 00220 $user = htmlspecialchars( $userText ); 00221 } 00222 00223 $time = $this->getLanguage()->userTimeAndDate( $result->getTimestamp(), $this->getUser() ); 00224 00225 return "$plink . . $user . . $time"; 00226 } 00227 00228 protected function getGroupName() { 00229 return 'media'; 00230 } 00231 }