Merge "Special:Newpages feed now shows first revision instead of latest revision"
[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, $mime;
32
33 function __construct( $name = 'MIMEsearch' ) {
34 parent::__construct( $name );
35 }
36
37 public function isExpensive() {
38 return false;
39 }
40
41 function isSyndicated() {
42 return false;
43 }
44
45 function isCacheable() {
46 return false;
47 }
48
49 function linkParameters() {
50 return [ 'mime' => "{$this->major}/{$this->minor}" ];
51 }
52
53 public function getQueryInfo() {
54 $minorType = [];
55 if ( $this->minor !== '*' ) {
56 // Allow wildcard searching
57 $minorType['img_minor_mime'] = $this->minor;
58 }
59 $qi = [
60 'tables' => [ 'image' ],
61 'fields' => [
62 'namespace' => NS_FILE,
63 'title' => 'img_name',
64 // Still have a value field just in case,
65 // but it isn't actually used for sorting.
66 'value' => 'img_name',
67 'img_size',
68 'img_width',
69 'img_height',
70 'img_user_text',
71 'img_timestamp'
72 ],
73 'conds' => [
74 'img_major_mime' => $this->major,
75 // This is in order to trigger using
76 // the img_media_mime index in "range" mode.
77 // @todo how is order defined? use MimeAnalyzer::getMediaTypes?
78 'img_media_type' => [
79 MEDIATYPE_BITMAP,
80 MEDIATYPE_DRAWING,
81 MEDIATYPE_AUDIO,
82 MEDIATYPE_VIDEO,
83 MEDIATYPE_MULTIMEDIA,
84 MEDIATYPE_UNKNOWN,
85 MEDIATYPE_OFFICE,
86 MEDIATYPE_TEXT,
87 MEDIATYPE_EXECUTABLE,
88 MEDIATYPE_ARCHIVE,
89 MEDIATYPE_3D,
90 ],
91 ] + $minorType,
92 ];
93
94 return $qi;
95 }
96
97 /**
98 * The index is on (img_media_type, img_major_mime, img_minor_mime)
99 * which unfortunately doesn't have img_name at the end for sorting.
100 * So tell db to sort it however it wishes (Its not super important
101 * that this report gives results in a logical order). As an aditional
102 * note, mysql seems to by default order things by img_name ASC, which
103 * is what we ideally want, so everything works out fine anyhow.
104 * @return array
105 */
106 function getOrderFields() {
107 return [];
108 }
109
110 /**
111 * Generate and output the form
112 */
113 function getPageHeader() {
114 $formDescriptor = [
115 'mime' => [
116 'type' => 'combobox',
117 'options' => $this->getSuggestionsForTypes(),
118 'name' => 'mime',
119 'label-message' => 'mimetype',
120 'required' => true,
121 'default' => $this->mime,
122 ],
123 ];
124
125 HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
126 ->setSubmitTextMsg( 'ilsubmit' )
127 ->setAction( $this->getPageTitle()->getLocalURL() )
128 ->setMethod( 'get' )
129 ->prepareForm()
130 ->displayForm( false );
131 }
132
133 protected function getSuggestionsForTypes() {
134 $dbr = wfGetDB( DB_REPLICA );
135 $lastMajor = null;
136 $suggestions = [];
137 $result = $dbr->select(
138 [ 'image' ],
139 // We ignore img_media_type, but using it in the query is needed for MySQL to choose a
140 // sensible execution plan
141 [ 'img_media_type', 'img_major_mime', 'img_minor_mime' ],
142 [],
143 __METHOD__,
144 [ 'GROUP BY' => [ 'img_media_type', 'img_major_mime', 'img_minor_mime' ] ]
145 );
146 foreach ( $result as $row ) {
147 $major = $row->img_major_mime;
148 $minor = $row->img_minor_mime;
149 $suggestions[ "$major/$minor" ] = "$major/$minor";
150 if ( $lastMajor === $major ) {
151 // If there are at least two with the same major mime type, also include the wildcard
152 $suggestions[ "$major/*" ] = "$major/*";
153 }
154 $lastMajor = $major;
155 }
156 ksort( $suggestions );
157 return $suggestions;
158 }
159
160 public function execute( $par ) {
161 $this->mime = $par ? $par : $this->getRequest()->getText( 'mime' );
162 $this->mime = trim( $this->mime );
163 list( $this->major, $this->minor ) = File::splitMime( $this->mime );
164
165 if ( $this->major == '' || $this->minor == '' || $this->minor == 'unknown' ||
166 !self::isValidType( $this->major )
167 ) {
168 $this->setHeaders();
169 $this->outputHeader();
170 $this->getPageHeader();
171 return;
172 }
173
174 parent::execute( $par );
175 }
176
177 /**
178 * @param Skin $skin
179 * @param object $result Result row
180 * @return string
181 */
182 function formatResult( $skin, $result ) {
183 global $wgContLang;
184
185 $linkRenderer = $this->getLinkRenderer();
186 $nt = Title::makeTitle( $result->namespace, $result->title );
187 $text = $wgContLang->convert( $nt->getText() );
188 $plink = $linkRenderer->makeLink(
189 Title::newFromText( $nt->getPrefixedText() ),
190 $text
191 );
192
193 $download = Linker::makeMediaLinkObj( $nt, $this->msg( 'download' )->escaped() );
194 $download = $this->msg( 'parentheses' )->rawParams( $download )->escaped();
195 $lang = $this->getLanguage();
196 $bytes = htmlspecialchars( $lang->formatSize( $result->img_size ) );
197 $dimensions = $this->msg( 'widthheight' )->numParams( $result->img_width,
198 $result->img_height )->escaped();
199 $user = $linkRenderer->makeLink(
200 Title::makeTitle( NS_USER, $result->img_user_text ),
201 $result->img_user_text
202 );
203
204 $time = $lang->userTimeAndDate( $result->img_timestamp, $this->getUser() );
205 $time = htmlspecialchars( $time );
206
207 return "$download $plink . . $dimensions . . $bytes . . $user . . $time";
208 }
209
210 /**
211 * @param string $type
212 * @return bool
213 */
214 protected static function isValidType( $type ) {
215 // From maintenance/tables.sql => img_major_mime
216 $types = [
217 'unknown',
218 'application',
219 'audio',
220 'image',
221 'text',
222 'video',
223 'message',
224 'model',
225 'multipart',
226 'chemical'
227 ];
228
229 return in_array( $type, $types );
230 }
231
232 public function preprocessResults( $db, $res ) {
233 $this->executeLBFromResultWrapper( $res );
234 }
235
236 protected function getGroupName() {
237 return 'media';
238 }
239 }