Merge querypage-work2 branch from trunk. The most relevant changes are:
[lhc/web/wiklou.git] / includes / specials / SpecialFileDuplicateSearch.php
index b0a6b39..6eccb90 100644 (file)
  * @ingroup SpecialPage
  */
 class FileDuplicateSearchPage extends QueryPage {
-       var $hash, $filename;
+       protected $hash, $filename;
 
-       function __construct( $hash, $filename ) {
-               $this->hash = $hash;
-               $this->filename = $filename;
+       function __construct( $name = 'FileDuplicateSearch' ) {
+               parent::__construct( $name );
        }
 
-       function getName() { return 'FileDuplicateSearch'; }
-       function isExpensive() { return false; }
        function isSyndicated() { return false; }
+       function isCacheable() { return false; }
 
        function linkParameters() {
                return array( 'filename' => $this->filename );
        }
 
-       function getSQL() {
-               $dbr = wfGetDB( DB_SLAVE );
-               $image = $dbr->tableName( 'image' );
-               $hash = $dbr->addQuotes( $this->hash );
+       function getQueryInfo() {
+               return array(
+                       'tables' => array( 'image' ),
+                       'fields' => array(
+                               'img_name AS title',
+                               'img_sha1 AS value',
+                               'img_user_text',
+                               'img_timestamp'
+                       ),
+                       'conds' => array( 'img_sha1' => $this->hash )
+               );
+       }
+       
+       function execute( $par ) {
+               global $wgRequest, $wgOut, $wgLang, $wgContLang, $wgScript;
+               
+               $this->setHeaders();
+               $this->outputHeader();
+               
+               $this->filename =  isset( $par ) ?  $par : $wgRequest->getText( 'filename' );
+               $this->hash = '';
+               $title = Title::makeTitleSafe( NS_FILE, $this->filename );
+               if( $title && $title->getText() != '' ) {
+                       $dbr = wfGetDB( DB_SLAVE );
+                       $this->hash = $dbr->selectField( 'image', 'img_sha1', array( 'img_name' => $title->getDBkey() ), __METHOD__ );
+               }
+
+               # Create the input form
+               $wgOut->addHTML(
+                       Xml::openElement( 'form', array( 'id' => 'fileduplicatesearch', 'method' => 'get', 'action' => $wgScript ) ) .
+                       Html::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) .
+                       Xml::openElement( 'fieldset' ) .
+                       Xml::element( 'legend', null, wfMsg( 'fileduplicatesearch-legend' ) ) .
+                       Xml::inputLabel( wfMsg( 'fileduplicatesearch-filename' ), 'filename', 'filename', 50, $this->filename ) . ' ' .
+                       Xml::submitButton( wfMsg( 'fileduplicatesearch-submit' ) ) .
+                       Xml::closeElement( 'fieldset' ) .
+                       Xml::closeElement( 'form' )
+               );
+
+               if( $this->hash != '' ) {
+                       $align = $wgContLang->alignEnd();
+
+                       # Show a thumbnail of the file
+                       $img = wfFindFile( $title );
+                       if ( $img ) {
+                               $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
+                               if( $thumb ) {
+                                       $wgOut->addHTML( '<div style="float:' . $align . '" id="mw-fileduplicatesearch-icon">' .
+                                               $thumb->toHtml( array( 'desc-link' => false ) ) . '<br />' .
+                                               wfMsgExt( 'fileduplicatesearch-info', array( 'parse' ),
+                                                       $wgLang->formatNum( $img->getWidth() ),
+                                                       $wgLang->formatNum( $img->getHeight() ),
+                                                       $wgLang->formatSize( $img->getSize() ),
+                                                       $img->getMimeType()
+                                               ) .
+                                               '</div>' );
+                               }
+                       }
 
-               return "SELECT 'FileDuplicateSearch' AS type,
-                               img_name AS title,
-                               img_sha1 AS value,
-                               img_user_text,
-                               img_timestamp
-                       FROM $image
-                       WHERE img_sha1 = $hash
-                       ";
+                       parent::execute( $par );
+
+                       # Show a short summary
+                       if( $this->numRows == 1 ) {
+                               $wgOut->wrapWikiMsg(
+                                       "<p class='mw-fileduplicatesearch-result-1'>\n$1\n</p>",
+                                       array( 'fileduplicatesearch-result-1', $this->filename )
+                               );
+                       } elseif ( $this->numRows > 1 ) {
+                               $wgOut->wrapWikiMsg(
+                                       "<p class='mw-fileduplicatesearch-result-n'>\n$1\n</p>",
+                                       array( 'fileduplicatesearch-result-n', $this->filename,
+                                               $wgLang->formatNum( $this->numRows - 1 ) )
+                               );
+                       }
+               }
        }
 
        function formatResult( $skin, $result ) {
@@ -75,77 +135,3 @@ class FileDuplicateSearchPage extends QueryPage {
                return "$plink . . $user . . $time";
        }
 }
-
-/**
- * Output the HTML search form, and constructs the FileDuplicateSearch object.
- */
-function wfSpecialFileDuplicateSearch( $par = null ) {
-       global $wgRequest, $wgOut, $wgLang, $wgContLang, $wgScript;
-
-       $hash = '';
-       $filename =  isset( $par ) ?  $par : $wgRequest->getText( 'filename' );
-
-       $title = Title::newFromText( $filename );
-       if( $title && $title->getText() != '' ) {
-               $dbr = wfGetDB( DB_SLAVE );
-               $image = $dbr->tableName( 'image' );
-               $encFilename = $dbr->addQuotes( htmlspecialchars( $title->getDBkey() ) );
-               $sql = "SELECT img_sha1 from $image where img_name = $encFilename";
-               $res = $dbr->query( $sql );
-               $row = $dbr->fetchRow( $res );
-               if( $row !== false ) {
-                       $hash = $row[0];
-               }
-       }
-
-       # Create the input form
-       $wgOut->addHTML(
-               Xml::openElement( 'form', array( 'id' => 'fileduplicatesearch', 'method' => 'get', 'action' => $wgScript ) ) .
-               Html::hidden( 'title', SpecialPage::getTitleFor( 'FileDuplicateSearch' )->getPrefixedDbKey() ) .
-               Xml::openElement( 'fieldset' ) .
-               Xml::element( 'legend', null, wfMsg( 'fileduplicatesearch-legend' ) ) .
-               Xml::inputLabel( wfMsg( 'fileduplicatesearch-filename' ), 'filename', 'filename', 50, $filename ) . ' ' .
-               Xml::submitButton( wfMsg( 'fileduplicatesearch-submit' ) ) .
-               Xml::closeElement( 'fieldset' ) .
-               Xml::closeElement( 'form' )
-       );
-
-       if( $hash != '' ) {
-               $align = $wgContLang->alignEnd();
-
-               # Show a thumbnail of the file
-               $img = wfFindFile( $title );
-               if ( $img ) {
-                       $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
-                       if( $thumb ) {
-                               $wgOut->addHTML( '<div style="float:' . $align . '" id="mw-fileduplicatesearch-icon">' .
-                                       $thumb->toHtml( array( 'desc-link' => false ) ) . '<br />' .
-                                       wfMsgExt( 'fileduplicatesearch-info', array( 'parse' ),
-                                               $wgLang->formatNum( $img->getWidth() ),
-                                               $wgLang->formatNum( $img->getHeight() ),
-                                               $wgLang->formatSize( $img->getSize() ),
-                                               $img->getMimeType()
-                                       ) .
-                                       '</div>' );
-                       }
-               }
-
-               # Do the query
-               $wpp = new FileDuplicateSearchPage( $hash, $filename );
-               list( $limit, $offset ) = wfCheckLimits();
-               $count = $wpp->doQuery( $offset, $limit );
-
-               # Show a short summary
-               if( $count == 1 ) {
-                       $wgOut->wrapWikiMsg(
-                               "<p class='mw-fileduplicatesearch-result-1'>\n$1\n</p>",
-                               array( 'fileduplicatesearch-result-1', $filename )
-                       );
-               } elseif ( $count > 1 ) {
-                       $wgOut->wrapWikiMsg(
-                               "<p class='mw-fileduplicatesearch-result-n'>\n$1\n</p>",
-                               array( 'fileduplicatesearch-result-n', $filename, $wgLang->formatNum( $count - 1 ) )
-                       );
-               }
-       }
-}