Revert r19877; no reason is given for it but it breaks things such as parameter subst...
[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 * @addtogroup SpecialPage
7 *
8 * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
9 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
10 */
11
12 /**
13 * @addtogroup SpecialPage
14 */
15 class MIMEsearchPage extends QueryPage {
16 var $major, $minor;
17
18 function MIMEsearchPage( $major, $minor ) {
19 $this->major = $major;
20 $this->minor = $minor;
21 }
22
23 function getName() { return 'MIMEsearch'; }
24
25 /**
26 * Due to this page relying upon extra fields being passed in the SELECT it
27 * will fail if it's set as expensive and misermode is on
28 */
29 function isExpensive() { return true; }
30 function isSyndicated() { return false; }
31
32 function linkParameters() {
33 $arr = array( $this->major, $this->minor );
34 $mime = implode( '/', $arr );
35 return array( 'mime' => $mime );
36 }
37
38 function getSQL() {
39 $dbr = wfGetDB( DB_SLAVE );
40 $image = $dbr->tableName( 'image' );
41 $major = $dbr->addQuotes( $this->major );
42 $minor = $dbr->addQuotes( $this->minor );
43
44 return
45 "SELECT 'MIMEsearch' AS type,
46 " . NS_IMAGE . " AS namespace,
47 img_name AS title,
48 img_major_mime AS value,
49
50 img_size,
51 img_width,
52 img_height,
53 img_user_text,
54 img_timestamp
55 FROM $image
56 WHERE img_major_mime = $major AND img_minor_mime = $minor
57 ";
58 }
59
60 function formatResult( $skin, $result ) {
61 global $wgContLang, $wgLang;
62
63 $nt = Title::makeTitle( $result->namespace, $result->title );
64 $text = $wgContLang->convert( $nt->getText() );
65 $plink = $skin->makeLink( $nt->getPrefixedText(), $text );
66
67 $download = $skin->makeMediaLink( $nt->getText(), 'fuck me!', wfMsgHtml( 'download' ) );
68 $bytes = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
69 $wgLang->formatNum( $result->img_size ) );
70 $dimensions = wfMsgHtml( 'widthheight', $wgLang->formatNum( $result->img_width ),
71 $wgLang->formatNum( $result->img_height ) );
72 $user = $skin->makeLinkObj( Title::makeTitle( NS_USER, $result->img_user_text ), $result->img_user_text );
73 $time = $wgLang->timeanddate( $result->img_timestamp );
74
75 return "($download) $plink . . $dimensions . . $bytes . . $user . . $time";
76 }
77 }
78
79 /**
80 * constructor
81 */
82 function wfSpecialMIMEsearch( $par = null ) {
83 global $wgRequest, $wgTitle, $wgOut;
84
85 $mime = isset( $par ) ? $par : $wgRequest->getText( 'mime' );
86
87 $wgOut->addHTML(
88 wfElement( 'form',
89 array(
90 'id' => 'specialmimesearch',
91 'method' => 'get',
92 'action' => $wgTitle->escapeLocalUrl()
93 ),
94 null
95 ) .
96 wfOpenElement( 'label' ) .
97 wfMsgHtml( 'mimetype' ) .
98 wfElement( 'input', array(
99 'type' => 'text',
100 'size' => 20,
101 'name' => 'mime',
102 'value' => $mime
103 ),
104 ''
105 ) .
106 ' ' .
107 wfElement( 'input', array(
108 'type' => 'submit',
109 'value' => wfMsg( 'ilsubmit' )
110 ),
111 ''
112 ) .
113 wfCloseElement( 'label' ) .
114 wfCloseElement( 'form' )
115 );
116
117 list( $major, $minor ) = wfSpecialMIMEsearchParse( $mime );
118 if ( $major == '' or $minor == '' or !wfSpecialMIMEsearchValidType( $major ) )
119 return;
120 $wpp = new MIMEsearchPage( $major, $minor );
121
122 list( $limit, $offset ) = wfCheckLimits();
123 $wpp->doQuery( $offset, $limit );
124 }
125
126 function wfSpecialMIMEsearchParse( $str ) {
127 // searched for an invalid MIME type.
128 if( strpos( $str, '/' ) === false) {
129 return array ('', '');
130 }
131
132 list( $major, $minor ) = explode( '/', $str, 2 );
133
134 return array(
135 ltrim( $major, ' ' ),
136 rtrim( $minor, ' ' )
137 );
138 }
139
140 function wfSpecialMIMEsearchValidType( $type ) {
141 // From maintenance/tables.sql => img_major_mime
142 $types = array(
143 'unknown',
144 'application',
145 'audio',
146 'image',
147 'text',
148 'video',
149 'message',
150 'model',
151 'multipart'
152 );
153
154 return in_array( $type, $types );
155 }
156 ?>