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