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