merging latest master
[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( 'namespace' => NS_FILE,
49 'title' => 'img_name',
50 'value' => 'img_major_mime',
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 $wgScript;
63
64 $mime = $par ? $par : $this->getRequest()->getText( 'mime' );
65
66 $this->setHeaders();
67 $this->outputHeader();
68 $this->getOutput()->addHTML(
69 Xml::openElement( 'form', array( 'id' => 'specialmimesearch', 'method' => 'get', 'action' => $wgScript ) ) .
70 Xml::openElement( 'fieldset' ) .
71 Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
72 Xml::element( 'legend', null, $this->msg( 'mimesearch' )->text() ) .
73 Xml::inputLabel( $this->msg( 'mimetype' )->text(), 'mime', 'mime', 20, $mime ) . ' ' .
74 Xml::submitButton( $this->msg( 'ilsubmit' )->text() ) .
75 Xml::closeElement( 'fieldset' ) .
76 Xml::closeElement( 'form' )
77 );
78
79 list( $this->major, $this->minor ) = File::splitMime( $mime );
80 if ( $this->major == '' || $this->minor == '' || $this->minor == 'unknown' ||
81 !self::isValidType( $this->major ) ) {
82 return;
83 }
84 parent::execute( $par );
85 }
86
87
88 function formatResult( $skin, $result ) {
89 global $wgContLang;
90
91 $nt = Title::makeTitle( $result->namespace, $result->title );
92 $text = $wgContLang->convert( $nt->getText() );
93 $plink = Linker::link(
94 Title::newFromText( $nt->getPrefixedText() ),
95 htmlspecialchars( $text )
96 );
97
98 $download = Linker::makeMediaLinkObj( $nt, $this->msg( 'download' )->escaped() );
99 $download = $this->msg( 'parentheses' )->rawParams( $download )->escaped();
100 $lang = $this->getLanguage();
101 $bytes = htmlspecialchars( $lang->formatSize( $result->img_size ) );
102 $dimensions = $this->msg( 'widthheight' )->numParams( $result->img_width,
103 $result->img_height )->escaped();
104 $user = Linker::link( Title::makeTitle( NS_USER, $result->img_user_text ), htmlspecialchars( $result->img_user_text ) );
105 $time = htmlspecialchars( $lang->userTimeAndDate( $result->img_timestamp, $this->getUser() ) );
106
107 return "$download $plink . . $dimensions . . $bytes . . $user . . $time";
108 }
109
110 /**
111 * @param $type string
112 * @return bool
113 */
114 protected static function isValidType( $type ) {
115 // From maintenance/tables.sql => img_major_mime
116 $types = array(
117 'unknown',
118 'application',
119 'audio',
120 'image',
121 'text',
122 'video',
123 'message',
124 'model',
125 'multipart'
126 );
127 return in_array( $type, $types );
128 }
129 }