Completed
Branch master (939199)
by
unknown
39:35
created

includes/content/FileContentHandler.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Content handler for File: files
5
 * TODO: this handler s not used directly now,
6
 * but instead manually called by WikitextHandler.
7
 * This should be fixed in the future.
8
 */
9
class FileContentHandler extends WikitextContentHandler  {
10
11
	public function getFieldsForSearchIndex( SearchEngine $engine ) {
12
		$fields['file_media_type'] =
0 ignored issues
show
Coding Style Comprehensibility introduced by
$fields was never initialized. Although not strictly required by PHP, it is generally a good practice to add $fields = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
13
			$engine->makeSearchFieldMapping( 'file_media_type', SearchIndexField::INDEX_TYPE_KEYWORD );
14
		$fields['file_media_type']->setFlag( SearchIndexField::FLAG_CASEFOLD );
15
		$fields['file_mime'] =
16
			$engine->makeSearchFieldMapping( 'file_mime', SearchIndexField::INDEX_TYPE_SHORT_TEXT );
17
		$fields['file_mime']->setFlag( SearchIndexField::FLAG_CASEFOLD );
18
		$fields['file_size'] =
19
			$engine->makeSearchFieldMapping( 'file_size', SearchIndexField::INDEX_TYPE_INTEGER );
20
		$fields['file_width'] =
21
			$engine->makeSearchFieldMapping( 'file_width', SearchIndexField::INDEX_TYPE_INTEGER );
22
		$fields['file_height'] =
23
			$engine->makeSearchFieldMapping( 'file_height', SearchIndexField::INDEX_TYPE_INTEGER );
24
		$fields['file_bits'] =
25
			$engine->makeSearchFieldMapping( 'file_bits', SearchIndexField::INDEX_TYPE_INTEGER );
26
		$fields['file_resolution'] =
27
			$engine->makeSearchFieldMapping( 'file_resolution', SearchIndexField::INDEX_TYPE_INTEGER );
28
		$fields['file_text'] =
29
			$engine->makeSearchFieldMapping( 'file_text', SearchIndexField::INDEX_TYPE_TEXT );
30
		return $fields;
31
	}
32
33
	public function getDataForSearchIndex( WikiPage $page, ParserOutput $parserOutput,
34
	                                       SearchEngine $engine ) {
35
		$fields = [];
36
37
		$title = $page->getTitle();
38
		if ( NS_FILE != $title->getNamespace() ) {
39
			return [];
40
		}
41
		$file = wfLocalFile( $title );
42
		if ( !$file || !$file->exists() ) {
43
			return [];
44
		}
45
46
		$handler = $file->getHandler();
47
		if ( $handler ) {
48
			$fields['file_text'] = $handler->getEntireText( $file );
49
		}
50
		$fields['file_media_type'] = $file->getMediaType();
51
		$fields['file_mime'] = $file->getMimeType();
52
		$fields['file_size'] = $file->getSize();
53
		$fields['file_width'] = $file->getWidth();
54
		$fields['file_height'] = $file->getHeight();
55
		$fields['file_bits'] = $file->getBitDepth();
56
		$fields['file_resolution'] =
57
			(int)floor( sqrt( $fields['file_width'] * $fields['file_height'] ) );
58
59
		return $fields;
60
	}
61
62
}
63