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'] = |
|
|
|
|
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
|
|
|
|
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:
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 thebar
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.