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