In Special:RevisionDelete:
[lhc/web/wiklou.git] / includes / specials / 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 * @file
7 * @ingroup 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 * Searches the database for files of the requested MIME type, comparing this with the
15 * 'img_major_mime' and 'img_minor_mime' fields in the image table.
16 * @ingroup SpecialPage
17 */
18 class MIMEsearchPage extends QueryPage {
19 var $major, $minor;
20
21 function MIMEsearchPage( $major, $minor ) {
22 $this->major = $major;
23 $this->minor = $minor;
24 }
25
26 function getName() { return 'MIMEsearch'; }
27
28 /**
29 * Due to this page relying upon extra fields being passed in the SELECT it
30 * will fail if it's set as expensive and misermode is on
31 */
32 function isExpensive() { return true; }
33 function isSyndicated() { return false; }
34
35 function linkParameters() {
36 $arr = array( $this->major, $this->minor );
37 $mime = implode( '/', $arr );
38 return array( 'mime' => $mime );
39 }
40
41 function getSQL() {
42 $dbr = wfGetDB( DB_SLAVE );
43 $image = $dbr->tableName( 'image' );
44 $major = $dbr->addQuotes( $this->major );
45 $minor = $dbr->addQuotes( $this->minor );
46
47 return
48 "SELECT 'MIMEsearch' AS type,
49 " . NS_FILE . " AS namespace,
50 img_name AS title,
51 img_major_mime AS value,
52
53 img_size,
54 img_width,
55 img_height,
56 img_user_text,
57 img_timestamp
58 FROM $image
59 WHERE img_major_mime = $major AND img_minor_mime = $minor
60 ";
61 }
62
63 function formatResult( $skin, $result ) {
64 global $wgContLang, $wgLang;
65
66 $nt = Title::makeTitle( $result->namespace, $result->title );
67 $text = $wgContLang->convert( $nt->getText() );
68 $plink = $skin->makeLink( $nt->getPrefixedText(), htmlspecialchars($text) );
69
70 $download = $skin->makeMediaLinkObj( $nt, wfMsgHtml( 'download' ) );
71 $bytes = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
72 $wgLang->formatNum( $result->img_size ) );
73 $dimensions = wfMsgHtml( 'widthheight', $wgLang->formatNum( $result->img_width ),
74 $wgLang->formatNum( $result->img_height ) );
75 $user = $skin->link( Title::makeTitle( NS_USER, $result->img_user_text ), htmlspecialchars( $result->img_user_text ) );
76 $time = htmlspecialchars( $wgLang->timeanddate( $result->img_timestamp ) );
77
78 return "($download) $plink . . $dimensions . . $bytes . . $user . . $time";
79 }
80 }
81
82 /**
83 * Output the HTML search form, and constructs the MIMEsearchPage object.
84 */
85 function wfSpecialMIMEsearch( $par = null ) {
86 global $wgRequest, $wgOut;
87
88 $mime = isset( $par ) ? $par : $wgRequest->getText( 'mime' );
89
90 $wgOut->addHTML(
91 Xml::openElement( 'form', array( 'id' => 'specialmimesearch', 'method' => 'get', 'action' => SpecialPage::getTitleFor( 'MIMEsearch' )->getLocalUrl() ) ) .
92 Xml::openElement( 'fieldset' ) .
93 Xml::hidden( 'title', SpecialPage::getTitleFor( 'MIMEsearch' )->getPrefixedText() ) .
94 Xml::element( 'legend', null, wfMsg( 'mimesearch' ) ) .
95 Xml::inputLabel( wfMsg( 'mimetype' ), 'mime', 'mime', 20, $mime ) . ' ' .
96 Xml::submitButton( wfMsg( 'ilsubmit' ) ) .
97 Xml::closeElement( 'fieldset' ) .
98 Xml::closeElement( 'form' )
99 );
100
101 list( $major, $minor ) = wfSpecialMIMEsearchParse( $mime );
102 if ( $major == '' or $minor == '' or !wfSpecialMIMEsearchValidType( $major ) )
103 return;
104 $wpp = new MIMEsearchPage( $major, $minor );
105
106 list( $limit, $offset ) = wfCheckLimits();
107 $wpp->doQuery( $offset, $limit );
108 }
109
110 function wfSpecialMIMEsearchParse( $str ) {
111 // searched for an invalid MIME type.
112 if( strpos( $str, '/' ) === false) {
113 return array ('', '');
114 }
115
116 list( $major, $minor ) = explode( '/', $str, 2 );
117
118 return array(
119 ltrim( $major, ' ' ),
120 rtrim( $minor, ' ' )
121 );
122 }
123
124 function wfSpecialMIMEsearchValidType( $type ) {
125 // From maintenance/tables.sql => img_major_mime
126 $types = array(
127 'unknown',
128 'application',
129 'audio',
130 'image',
131 'text',
132 'video',
133 'message',
134 'model',
135 'multipart'
136 );
137
138 return in_array( $type, $types );
139 }