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" page content
14 *
15 * TODO: this handler is not used directly now,
16 * but instead manually called by WikitextHandler.
17 * This should be fixed in the future.
18 *
19 * @ingroup Content
20 */
21class FileContentHandler extends WikitextContentHandler {
22
23    public function getFieldsForSearchIndex( SearchEngine $engine ) {
24        $fields = [];
25        $fields['file_media_type'] =
26            $engine->makeSearchFieldMapping( 'file_media_type', SearchIndexField::INDEX_TYPE_KEYWORD );
27        $fields['file_media_type']->setFlag( SearchIndexField::FLAG_CASEFOLD );
28        $fields['file_mime'] =
29            $engine->makeSearchFieldMapping( 'file_mime', SearchIndexField::INDEX_TYPE_SHORT_TEXT );
30        $fields['file_mime']->setFlag( SearchIndexField::FLAG_CASEFOLD );
31        $fields['file_size'] =
32            $engine->makeSearchFieldMapping( 'file_size', SearchIndexField::INDEX_TYPE_INTEGER );
33        $fields['file_width'] =
34            $engine->makeSearchFieldMapping( 'file_width', SearchIndexField::INDEX_TYPE_INTEGER );
35        $fields['file_height'] =
36            $engine->makeSearchFieldMapping( 'file_height', SearchIndexField::INDEX_TYPE_INTEGER );
37        $fields['file_bits'] =
38            $engine->makeSearchFieldMapping( 'file_bits', SearchIndexField::INDEX_TYPE_INTEGER );
39        $fields['file_resolution'] =
40            $engine->makeSearchFieldMapping( 'file_resolution', SearchIndexField::INDEX_TYPE_INTEGER );
41        $fields['file_text'] =
42            $engine->makeSearchFieldMapping( 'file_text', SearchIndexField::INDEX_TYPE_TEXT );
43        return $fields;
44    }
45
46    public function getDataForSearchIndex(
47        WikiPage $page,
48        ParserOutput $parserOutput,
49        SearchEngine $engine,
50        ?RevisionRecord $revision = null
51    ) {
52        $fields = [];
53
54        $title = $page->getTitle();
55        if ( NS_FILE != $title->getNamespace() ) {
56            return [];
57        }
58        $file = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo()
59            ->newFile( $title );
60        if ( !$file || !$file->exists() ) {
61            return [];
62        }
63
64        $handler = $file->getHandler();
65        if ( $handler ) {
66            $fileText = $handler->getEntireText( $file );
67            if ( $fileText !== false ) {
68                $fields['file_text'] = $fileText;
69            }
70        }
71        $fields['file_media_type'] = $file->getMediaType();
72        $fields['file_mime'] = $file->getMimeType();
73        $fields['file_size'] = $file->getSize();
74        $fields['file_width'] = $file->getWidth();
75        $fields['file_height'] = $file->getHeight();
76        $fields['file_bits'] = $file->getBitDepth();
77        $fields['file_resolution'] =
78            (int)floor( sqrt( $fields['file_width'] * $fields['file_height'] ) );
79
80        return $fields;
81    }
82
83}
84
85/** @deprecated class alias since 1.43 */
86class_alias( FileContentHandler::class, 'FileContentHandler' );