Get rid of PHP4-style constructors
[lhc/web/wiklou.git] / includes / specials / SpecialMIMEsearch.php
1 <?php
2 /**
3 * Implements Special:MIMESearch
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
23 */
24
25 /**
26 * Searches the database for files of the requested MIME type, comparing this with the
27 * 'img_major_mime' and 'img_minor_mime' fields in the image table.
28 * @ingroup SpecialPage
29 */
30 class MIMEsearchPage extends QueryPage {
31 var $major, $minor;
32
33 function __construct( $major, $minor ) {
34 $this->major = $major;
35 $this->minor = $minor;
36 }
37
38 function getName() { return 'MIMEsearch'; }
39
40 /**
41 * Due to this page relying upon extra fields being passed in the SELECT it
42 * will fail if it's set as expensive and misermode is on
43 */
44 function isExpensive() { return true; }
45 function isSyndicated() { return false; }
46
47 function linkParameters() {
48 $arr = array( $this->major, $this->minor );
49 $mime = implode( '/', $arr );
50 return array( 'mime' => $mime );
51 }
52
53 function getSQL() {
54 $dbr = wfGetDB( DB_SLAVE );
55 $image = $dbr->tableName( 'image' );
56 $major = $dbr->addQuotes( $this->major );
57 $minor = $dbr->addQuotes( $this->minor );
58
59 return
60 "SELECT 'MIMEsearch' AS type,
61 " . NS_FILE . " AS namespace,
62 img_name AS title,
63 img_major_mime AS value,
64
65 img_size,
66 img_width,
67 img_height,
68 img_user_text,
69 img_timestamp
70 FROM $image
71 WHERE img_major_mime = $major AND img_minor_mime = $minor
72 ";
73 }
74
75 function formatResult( $skin, $result ) {
76 global $wgContLang, $wgLang;
77
78 $nt = Title::makeTitle( $result->namespace, $result->title );
79 $text = $wgContLang->convert( $nt->getText() );
80 $plink = $skin->link(
81 Title::newFromText( $nt->getPrefixedText() ),
82 htmlspecialchars( $text )
83 );
84
85 $download = $skin->makeMediaLinkObj( $nt, wfMsgHtml( 'download' ) );
86 $bytes = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
87 $wgLang->formatNum( $result->img_size ) );
88 $dimensions = htmlspecialchars( wfMsg( 'widthheight',
89 $wgLang->formatNum( $result->img_width ),
90 $wgLang->formatNum( $result->img_height )
91 ) );
92 $user = $skin->link( Title::makeTitle( NS_USER, $result->img_user_text ), htmlspecialchars( $result->img_user_text ) );
93 $time = htmlspecialchars( $wgLang->timeanddate( $result->img_timestamp ) );
94
95 return "($download) $plink . . $dimensions . . $bytes . . $user . . $time";
96 }
97 }
98
99 /**
100 * Output the HTML search form, and constructs the MIMEsearchPage object.
101 */
102 function wfSpecialMIMEsearch( $par = null ) {
103 global $wgRequest, $wgOut;
104
105 $mime = isset( $par ) ? $par : $wgRequest->getText( 'mime' );
106
107 $wgOut->addHTML(
108 Xml::openElement( 'form', array( 'id' => 'specialmimesearch', 'method' => 'get', 'action' => SpecialPage::getTitleFor( 'MIMEsearch' )->getLocalUrl() ) ) .
109 Xml::openElement( 'fieldset' ) .
110 Xml::hidden( 'title', SpecialPage::getTitleFor( 'MIMEsearch' )->getPrefixedText() ) .
111 Xml::element( 'legend', null, wfMsg( 'mimesearch' ) ) .
112 Xml::inputLabel( wfMsg( 'mimetype' ), 'mime', 'mime', 20, $mime ) . ' ' .
113 Xml::submitButton( wfMsg( 'ilsubmit' ) ) .
114 Xml::closeElement( 'fieldset' ) .
115 Xml::closeElement( 'form' )
116 );
117
118 list( $major, $minor ) = wfSpecialMIMEsearchParse( $mime );
119 if ( $major == '' or $minor == '' or !wfSpecialMIMEsearchValidType( $major ) )
120 return;
121 $wpp = new MIMEsearchPage( $major, $minor );
122
123 list( $limit, $offset ) = wfCheckLimits();
124 $wpp->doQuery( $offset, $limit );
125 }
126
127 function wfSpecialMIMEsearchParse( $str ) {
128 // searched for an invalid MIME type.
129 if( strpos( $str, '/' ) === false) {
130 return array ('', '');
131 }
132
133 list( $major, $minor ) = explode( '/', $str, 2 );
134
135 return array(
136 ltrim( $major, ' ' ),
137 rtrim( $minor, ' ' )
138 );
139 }
140
141 function wfSpecialMIMEsearchValidType( $type ) {
142 // From maintenance/tables.sql => img_major_mime
143 $types = array(
144 'unknown',
145 'application',
146 'audio',
147 'image',
148 'text',
149 'video',
150 'message',
151 'model',
152 'multipart'
153 );
154
155 return in_array( $type, $types );
156 }