Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / content / FileContentHandler.php
blobb38493647b6d7971e339c7cd8210dfd226900bac
1 <?php
3 namespace MediaWiki\Content;
5 use MediaWiki\MediaWikiServices;
6 use MediaWiki\Parser\ParserOutput;
7 use MediaWiki\Revision\RevisionRecord;
8 use SearchEngine;
9 use SearchIndexField;
10 use WikiPage;
12 /**
13 * Content handler for File: files
14 * TODO: this handler is not used directly now,
15 * but instead manually called by WikitextHandler.
16 * This should be fixed in the future.
18 class FileContentHandler extends WikitextContentHandler {
20 public function getFieldsForSearchIndex( SearchEngine $engine ) {
21 $fields = [];
22 $fields['file_media_type'] =
23 $engine->makeSearchFieldMapping( 'file_media_type', SearchIndexField::INDEX_TYPE_KEYWORD );
24 $fields['file_media_type']->setFlag( SearchIndexField::FLAG_CASEFOLD );
25 $fields['file_mime'] =
26 $engine->makeSearchFieldMapping( 'file_mime', SearchIndexField::INDEX_TYPE_SHORT_TEXT );
27 $fields['file_mime']->setFlag( SearchIndexField::FLAG_CASEFOLD );
28 $fields['file_size'] =
29 $engine->makeSearchFieldMapping( 'file_size', SearchIndexField::INDEX_TYPE_INTEGER );
30 $fields['file_width'] =
31 $engine->makeSearchFieldMapping( 'file_width', SearchIndexField::INDEX_TYPE_INTEGER );
32 $fields['file_height'] =
33 $engine->makeSearchFieldMapping( 'file_height', SearchIndexField::INDEX_TYPE_INTEGER );
34 $fields['file_bits'] =
35 $engine->makeSearchFieldMapping( 'file_bits', SearchIndexField::INDEX_TYPE_INTEGER );
36 $fields['file_resolution'] =
37 $engine->makeSearchFieldMapping( 'file_resolution', SearchIndexField::INDEX_TYPE_INTEGER );
38 $fields['file_text'] =
39 $engine->makeSearchFieldMapping( 'file_text', SearchIndexField::INDEX_TYPE_TEXT );
40 return $fields;
43 public function getDataForSearchIndex(
44 WikiPage $page,
45 ParserOutput $parserOutput,
46 SearchEngine $engine,
47 ?RevisionRecord $revision = null
48 ) {
49 $fields = [];
51 $title = $page->getTitle();
52 if ( NS_FILE != $title->getNamespace() ) {
53 return [];
55 $file = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo()
56 ->newFile( $title );
57 if ( !$file || !$file->exists() ) {
58 return [];
61 $handler = $file->getHandler();
62 if ( $handler ) {
63 $fileText = $handler->getEntireText( $file );
64 if ( $fileText !== false ) {
65 $fields['file_text'] = $fileText;
68 $fields['file_media_type'] = $file->getMediaType();
69 $fields['file_mime'] = $file->getMimeType();
70 $fields['file_size'] = $file->getSize();
71 $fields['file_width'] = $file->getWidth();
72 $fields['file_height'] = $file->getHeight();
73 $fields['file_bits'] = $file->getBitDepth();
74 $fields['file_resolution'] =
75 (int)floor( sqrt( $fields['file_width'] * $fields['file_height'] ) );
77 return $fields;
82 /** @deprecated class alias since 1.43 */
83 class_alias( FileContentHandler::class, 'FileContentHandler' );