| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Content; |
| 4 |
|
| 5 |
use MediaWiki\MediaWikiServices; |
| 6 |
use MediaWiki\Parser\ParserOutput; |
| 7 |
use MediaWiki\Revision\RevisionRecord; |
| 8 |
use SearchEngine; |
| 9 |
use SearchIndexField; |
| 10 |
use WikiPage; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class 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 |
|
| 83 |
class_alias( FileContentHandler::class, 'FileContentHandler' ); |