SpecialMIMEsearch.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * A special page to search for files by MIME type as defined in the
  4. * img_major_mime and img_minor_mime fields in the image table
  5. *
  6. * @file
  7. * @ingroup SpecialPage
  8. *
  9. * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
  10. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
  11. */
  12. /**
  13. * Searches the database for files of the requested MIME type, comparing this with the
  14. * 'img_major_mime' and 'img_minor_mime' fields in the image table.
  15. * @ingroup SpecialPage
  16. */
  17. class MIMEsearchPage extends QueryPage {
  18. var $major, $minor;
  19. function MIMEsearchPage( $major, $minor ) {
  20. $this->major = $major;
  21. $this->minor = $minor;
  22. }
  23. function getName() { return 'MIMEsearch'; }
  24. /**
  25. * Due to this page relying upon extra fields being passed in the SELECT it
  26. * will fail if it's set as expensive and misermode is on
  27. */
  28. function isExpensive() { return true; }
  29. function isSyndicated() { return false; }
  30. function linkParameters() {
  31. $arr = array( $this->major, $this->minor );
  32. $mime = implode( '/', $arr );
  33. return array( 'mime' => $mime );
  34. }
  35. function getSQL() {
  36. $dbr = wfGetDB( DB_SLAVE );
  37. $image = $dbr->tableName( 'image' );
  38. $major = $dbr->addQuotes( $this->major );
  39. $minor = $dbr->addQuotes( $this->minor );
  40. return
  41. "SELECT 'MIMEsearch' AS type,
  42. " . NS_FILE . " AS namespace,
  43. img_name AS title,
  44. img_major_mime AS value,
  45. img_size,
  46. img_width,
  47. img_height,
  48. img_user_text,
  49. img_timestamp
  50. FROM $image
  51. WHERE img_major_mime = $major AND img_minor_mime = $minor
  52. ";
  53. }
  54. function formatResult( $skin, $result ) {
  55. global $wgContLang, $wgLang;
  56. $nt = Title::makeTitle( $result->namespace, $result->title );
  57. $text = $wgContLang->convert( $nt->getText() );
  58. $plink = $skin->makeLink( $nt->getPrefixedText(), $text );
  59. $download = $skin->makeMediaLinkObj( $nt, wfMsgHtml( 'download' ) );
  60. $bytes = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
  61. $wgLang->formatNum( $result->img_size ) );
  62. $dimensions = wfMsgHtml( 'widthheight', $wgLang->formatNum( $result->img_width ),
  63. $wgLang->formatNum( $result->img_height ) );
  64. $user = $skin->makeLinkObj( Title::makeTitle( NS_USER, $result->img_user_text ), $result->img_user_text );
  65. $time = $wgLang->timeanddate( $result->img_timestamp );
  66. return "($download) $plink . . $dimensions . . $bytes . . $user . . $time";
  67. }
  68. }
  69. /**
  70. * Output the HTML search form, and constructs the MIMEsearchPage object.
  71. */
  72. function wfSpecialMIMEsearch( $par = null ) {
  73. global $wgRequest, $wgTitle, $wgOut;
  74. $mime = isset( $par ) ? $par : $wgRequest->getText( 'mime' );
  75. $wgOut->addHTML(
  76. Xml::openElement( 'form', array( 'id' => 'specialmimesearch', 'method' => 'get', 'action' => $wgTitle->getLocalUrl() ) ) .
  77. Xml::openElement( 'fieldset' ) .
  78. Xml::element( 'legend', null, wfMsg( 'mimesearch' ) ) .
  79. Xml::inputLabel( wfMsg( 'mimetype' ), 'mime', 'mime', 20, $mime ) . ' ' .
  80. Xml::submitButton( wfMsg( 'ilsubmit' ) ) .
  81. Xml::closeElement( 'fieldset' ) .
  82. Xml::closeElement( 'form' )
  83. );
  84. list( $major, $minor ) = wfSpecialMIMEsearchParse( $mime );
  85. if ( $major == '' or $minor == '' or !wfSpecialMIMEsearchValidType( $major ) )
  86. return;
  87. $wpp = new MIMEsearchPage( $major, $minor );
  88. list( $limit, $offset ) = wfCheckLimits();
  89. $wpp->doQuery( $offset, $limit );
  90. }
  91. function wfSpecialMIMEsearchParse( $str ) {
  92. // searched for an invalid MIME type.
  93. if( strpos( $str, '/' ) === false) {
  94. return array ('', '');
  95. }
  96. list( $major, $minor ) = explode( '/', $str, 2 );
  97. return array(
  98. ltrim( $major, ' ' ),
  99. rtrim( $minor, ' ' )
  100. );
  101. }
  102. function wfSpecialMIMEsearchValidType( $type ) {
  103. // From maintenance/tables.sql => img_major_mime
  104. $types = array(
  105. 'unknown',
  106. 'application',
  107. 'audio',
  108. 'image',
  109. 'text',
  110. 'video',
  111. 'message',
  112. 'model',
  113. 'multipart'
  114. );
  115. return in_array( $type, $types );
  116. }