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