ed2268a2701f8517925ba5123422e3ab3c97e808
[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 * @package MediaWiki
7 * @subpackage SpecialPage
8 *
9 * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
10 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
11 */
12
13 /* */
14 require_once 'QueryPage.php';
15
16 /**
17 * @package MediaWiki
18 * @subpackage SpecialPage
19 */
20 class MIMEsearchPage extends QueryPage {
21 var $major, $minor;
22
23 function MIMEsearchPage( $major, $minor ) {
24 $this->major = $major;
25 $this->minor = $minor;
26 }
27
28 function getName() { return 'MIMEsearch'; }
29
30 /**
31 * Due to this page relying upon extra fields being passed in the SELECT it
32 * will fail if it's set as expensive and misermode is on
33 */
34 function isExpensive() { return false; }
35 function isSyndicated() { return false; }
36
37 function linkParameters() {
38 $arr = array( $this->major, $this->minor );
39 $mime = implode( '/', $arr );
40 return array( 'mime' => $mime );
41 }
42
43 function getSQL() {
44 $dbr =& wfGetDB( DB_SLAVE );
45 $image = $dbr->tableName( 'image' );
46 $major = $dbr->addQuotes( $this->major );
47 $minor = $dbr->addQuotes( $this->minor );
48
49 return
50 "SELECT 'MIMEsearch' AS type,
51 " . NS_IMAGE . " AS namespace,
52 img_name AS title,
53 img_major_mime AS value,
54
55 img_size,
56 img_width,
57 img_height,
58 img_timestamp
59 FROM $image
60 WHERE img_major_mime = $major AND img_minor_mime = $minor
61 ";
62 }
63
64 function formatResult( $skin, $result ) {
65 global $wgContLang, $wgLang;
66
67 $nt = Title::makeTitle( $result->namespace, $result->title );
68 $text = $wgContLang->convert( $nt->getPrefixedText() );
69 $plink = $skin->makeLink( $nt->getPrefixedText(), $text );
70
71 $download = $skin->makeMediaLink( $nt->getText(), 'fuck me!', wfMsgHtml( 'download' ) );
72 $bytes = wfMsg( 'nbytes', $wgLang->formatNum( $result->img_size ) );
73 $dimensions = wfMsg( 'widthheight', $result->img_width, $result->img_height );
74 $time = $wgLang->timeanddate( $result->img_timestamp );
75
76 return "($download) $plink .. $dimensions .. $bytes .. $time";
77 }
78 }
79
80 /**
81 * constructor
82 */
83 function wfSpecialMIMEsearch() {
84 global $wgRequest, $wgTitle, $wgOut;
85
86 $mime = $wgRequest->getText( 'mime' );
87
88 $wgOut->addHTML(
89 wfElement( 'form',
90 array(
91 'id' => 'specialmimesearch',
92 'method' => 'get',
93 'action' => $wgTitle->escapeLocalUrl()
94 ),
95 null
96 ) .
97 wfOpenElement( 'label' ) .
98 wfMsgHtml( 'mimetype' ) .
99 wfElement( 'input', array(
100 'type' => 'text',
101 'size' => 20,
102 'name' => 'mime',
103 'value' => $mime
104 ),
105 ''
106 ) .
107 ' ' .
108 wfElement( 'input', array(
109 'type' => 'submit',
110 'value' => wfMsg( 'ilsubmit' )
111 ),
112 ''
113 ) .
114 wfCloseElement( 'label' ) .
115 wfCloseElement( '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 wfSuppressWarnings();
129 list( $major, $minor ) = explode( '/', $str, 2 );
130 wfRestoreWarnings();
131
132 return array(
133 ltrim( $major, ' ' ),
134 rtrim( $minor, ' ' )
135 );
136 }
137
138 function wfSpecialMIMEsearchValidType( $type ) {
139 // From maintenance/tables.sql => img_major_mime
140 $types = array(
141 'unknown',
142 'application',
143 'audio',
144 'image',
145 'text',
146 'video',
147 'message',
148 'model',
149 'multipart'
150 );
151
152 return in_array( $type, $types );
153 }
154 ?>