Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
46.51% covered (danger)
46.51%
20 / 43
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
FileContentHandler
47.62% covered (danger)
47.62%
20 / 42
50.00% covered (danger)
50.00%
1 / 2
14.04
0.00% covered (danger)
0.00%
0 / 1
 getFieldsForSearchIndex
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
1
 getDataForSearchIndex
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3namespace MediaWiki\Content;
4
5use MediaWiki\MediaWikiServices;
6use MediaWiki\Parser\ParserOutput;
7use MediaWiki\Revision\RevisionRecord;
8use SearchEngine;
9use SearchIndexField;
10use WikiPage;
11
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.
17 */
18class FileContentHandler extends WikitextContentHandler {
19
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;
41    }
42
43    public function getDataForSearchIndex(
44        WikiPage $page,
45        ParserOutput $parserOutput,
46        SearchEngine $engine,
47        ?RevisionRecord $revision = null
48    ) {
49        $fields = [];
50
51        $title = $page->getTitle();
52        if ( NS_FILE != $title->getNamespace() ) {
53            return [];
54        }
55        $file = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo()
56            ->newFile( $title );
57        if ( !$file || !$file->exists() ) {
58            return [];
59        }
60
61        $handler = $file->getHandler();
62        if ( $handler ) {
63            $fileText = $handler->getEntireText( $file );
64            if ( $fileText !== false ) {
65                $fields['file_text'] = $fileText;
66            }
67        }
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'] ) );
76
77        return $fields;
78    }
79
80}
81
82/** @deprecated class alias since 1.43 */
83class_alias( FileContentHandler::class, 'FileContentHandler' );