82ee4be6e1181c6489e2dccc129dbd6dbfcb32bc
[lhc/web/wiklou.git] / includes / SpecialMIMEsearch.php
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 /**
14 * Searches the database for files of the requested MIME type, comparing this with the
15 * 'img_major_mime' and 'img_minor_mime' fields in the image table.
16 * @ingroup SpecialPage
17 */
18 class MIMEsearchPage extends QueryPage {
19 var $major, $minor;
20
21 function MIMEsearchPage( $major, $minor ) {
22 $this->major = $major;
23 $this->minor = $minor;
24 }
25
26 function getName() { return 'MIMEsearch'; }
27
28 /**
29 * Due to this page relying upon extra fields being passed in the SELECT it
30 * will fail if it's set as expensive and misermode is on
31 */
32 function isExpensive() { return true; }
33 function isSyndicated() { return false; }
34
35 function linkParameters() {
36 $arr = array( $this->major, $this->minor );
37 $mime = implode( '/', $arr );
38 return array( 'mime' => $mime );
39 }
40
41 function getSQL() {
42 $dbr = wfGetDB( DB_SLAVE );
43 $image = $dbr->tableName( 'image' );
44 $major = $dbr->addQuotes( $this->major );
45 $minor = $dbr->addQuotes( $this->minor );
46
47 return
48 "SELECT 'MIMEsearch' AS type,
49 " . NS_IMAGE . " AS namespace,
50 img_name AS title,
51 img_major_mime AS value,
52
53 img_size,
54 img_width,
55 img_height,
56 img_user_text,
57 img_timestamp
58 FROM $image
59 WHERE img_major_mime = $major AND img_minor_mime = $minor
60 ";
61 }
62
63 function formatResult( $skin, $result ) {
64 global $wgContLang, $wgLang;
65
66 $nt = Title::makeTitle( $result->namespace, $result->title );
67 $text = $wgContLang->convert( $nt->getText() );
68 $plink = $skin->makeLink( $nt->getPrefixedText(), $text );
69
70 $download = $skin->makeMediaLinkObj( $nt, wfMsgHtml( 'download' ) );
71 $bytes = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
72 $wgLang->formatNum( $result->img_size ) );
73 $dimensions = wfMsgHtml( 'widthheight', $wgLang->formatNum( $result->img_width ),
74 $wgLang->formatNum( $result->img_height ) );
75 $user = $skin->makeLinkObj( Title::makeTitle( NS_USER, $result->img_user_text ), $result->img_user_text );
76 $time = $wgLang->timeanddate( $result->img_timestamp );
77
78 return "($download) $plink . . $dimensions . . $bytes . . $user . . $time";
79 }
80 }
81
82 /**
83 * Output the HTML search form, and constructs the MIMEsearchPage object.
84 */
85 function wfSpecialMIMEsearch( $par = null ) {
86 global $wgRequest, $wgTitle, $wgOut;
87
88 $mime = isset( $par ) ? $par : $wgRequest->getText( 'mime' );
89
90 $wgOut->addHTML(
91 Xml::openElement( 'form', array( 'id' => 'specialmimesearch', 'method' => 'get', 'action' => $wgTitle->getLocalUrl() ) ) .
92 Xml::openElement( 'fieldset' ) .
93 Xml::element( 'legend', null, wfMsg( 'mimesearch' ) ) .
94 Xml::inputLabel( wfMsg( 'mimetype' ), 'mime', 'mime', 20, $mime ) . ' ' .
95 Xml::submitButton( wfMsg( 'ilsubmit' ) ) .
96 Xml::closeElement( 'fieldset' ) .
97 Xml::closeElement( 'form' )
98 );
99
100 list( $major, $minor ) = wfSpecialMIMEsearchParse( $mime );
101 if ( $major == '' or $minor == '' or !wfSpecialMIMEsearchValidType( $major ) )
102 return;
103 $wpp = new MIMEsearchPage( $major, $minor );
104
105 list( $limit, $offset ) = wfCheckLimits();
106 $wpp->doQuery( $offset, $limit );
107 }
108
109 function wfSpecialMIMEsearchParse( $str ) {
110 // searched for an invalid MIME type.
111 if( strpos( $str, '/' ) === false) {
112 return array ('', '');
113 }
114
115 list( $major, $minor ) = explode( '/', $str, 2 );
116
117 return array(
118 ltrim( $major, ' ' ),
119 rtrim( $minor, ' ' )
120 );
121 }
122
123 function wfSpecialMIMEsearchValidType( $type ) {
124 // From maintenance/tables.sql => img_major_mime
125 $types = array(
126 'unknown',
127 'application',
128 'audio',
129 'image',
130 'text',
131 'video',
132 'message',
133 'model',
134 'multipart'
135 );
136
137 return in_array( $type, $types );
138 }