Follow-up r79964: Use the existing message 'parentheses' instead of introducing an...
[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 protected $major, $minor;
32
33 function __construct( $name = 'MIMEsearch' ) {
34 parent::__construct( $name );
35 }
36
37 function isExpensive() { return true; }
38 function isSyndicated() { return false; }
39 function isCacheable() { return false; }
40
41 function linkParameters() {
42 return array( 'mime' => "{$this->major}/{$this->minor}" );
43 }
44
45 public function getQueryInfo() {
46 return array(
47 'tables' => array( 'image' ),
48 'fields' => array( "'" . NS_FILE . "' AS namespace",
49 'img_name AS title',
50 'img_major_mime AS value',
51 'img_size',
52 'img_width',
53 'img_height',
54 'img_user_text',
55 'img_timestamp' ),
56 'conds' => array( 'img_major_mime' => $this->major,
57 'img_minor_mime' => $this->minor )
58 );
59 }
60
61 function execute( $par ) {
62 global $wgRequest, $wgOut;
63 $mime = $par ? $par : $wgRequest->getText( 'mime' );
64
65 $this->setHeaders();
66 $this->outputHeader();
67 $wgOut->addHTML(
68 Xml::openElement( 'form', array( 'id' => 'specialmimesearch', 'method' => 'get', 'action' => SpecialPage::getTitleFor( 'MIMEsearch' )->getLocalUrl() ) ) .
69 Xml::openElement( 'fieldset' ) .
70 Html::hidden( 'title', SpecialPage::getTitleFor( 'MIMEsearch' )->getPrefixedText() ) .
71 Xml::element( 'legend', null, wfMsg( 'mimesearch' ) ) .
72 Xml::inputLabel( wfMsg( 'mimetype' ), 'mime', 'mime', 20, $mime ) . ' ' .
73 Xml::submitButton( wfMsg( 'ilsubmit' ) ) .
74 Xml::closeElement( 'fieldset' ) .
75 Xml::closeElement( 'form' )
76 );
77
78 list( $this->major, $this->minor ) = self::parseMIME( $mime );
79 if ( $this->major == '' || $this->minor == '' || !self::isValidType( $this->major ) ) {
80 return;
81 }
82 parent::execute( $par );
83 }
84
85
86 function formatResult( $skin, $result ) {
87 global $wgContLang, $wgLang;
88
89 $nt = Title::makeTitle( $result->namespace, $result->title );
90 $text = $wgContLang->convert( $nt->getText() );
91 $plink = $skin->link(
92 Title::newFromText( $nt->getPrefixedText() ),
93 htmlspecialchars( $text )
94 );
95
96 $download = $skin->makeMediaLinkObj( $nt, wfMsgHtml( 'download' ) );
97 $bytes = wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
98 $wgLang->formatNum( $result->img_size ) );
99 $dimensions = htmlspecialchars( wfMsg( 'widthheight',
100 $wgLang->formatNum( $result->img_width ),
101 $wgLang->formatNum( $result->img_height )
102 ) );
103 $user = $skin->link( Title::makeTitle( NS_USER, $result->img_user_text ), htmlspecialchars( $result->img_user_text ) );
104 $time = htmlspecialchars( $wgLang->timeanddate( $result->img_timestamp ) );
105
106 return "($download) $plink . . $dimensions . . $bytes . . $user . . $time";
107 }
108
109 protected static function parseMIME( $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 protected static function isValidType( $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 return in_array( $type, $types );
137 }
138 }