wiki.techinc.nl/includes/content/FileContentHandler.php
David Causse 9fbd8f500f Make the doc building for search aware of the revision
Added an optional RevisionRecord param to:
- ContentHandler::getParserOutputForIndexing
- ContentHandler::getDataForSearchIndex
- the SearchDataForIndex hook

So that they have a chance to build the content related to a specific
revision.

Ultimately we'd like to make this parameter mandatory.

Bug: T317309
Depends-On: I8b220cd6c4aeeca1d924bdd527409b8602318944
Depends-On: I8616b611caab3f5fa97ff0e655b19c3034304597
Change-Id: I3298ce7591069eb32f624b2c9fbb6de58ae04a29
2022-10-25 18:45:23 +02:00

71 lines
2.4 KiB
PHP

<?php
use MediaWiki\MediaWikiServices;
use MediaWiki\Revision\RevisionRecord;
/**
* Content handler for File: files
* TODO: this handler s not used directly now,
* but instead manually called by WikitextHandler.
* This should be fixed in the future.
*/
class FileContentHandler extends WikitextContentHandler {
public function getFieldsForSearchIndex( SearchEngine $engine ) {
$fields = [];
$fields['file_media_type'] =
$engine->makeSearchFieldMapping( 'file_media_type', SearchIndexField::INDEX_TYPE_KEYWORD );
$fields['file_media_type']->setFlag( SearchIndexField::FLAG_CASEFOLD );
$fields['file_mime'] =
$engine->makeSearchFieldMapping( 'file_mime', SearchIndexField::INDEX_TYPE_SHORT_TEXT );
$fields['file_mime']->setFlag( SearchIndexField::FLAG_CASEFOLD );
$fields['file_size'] =
$engine->makeSearchFieldMapping( 'file_size', SearchIndexField::INDEX_TYPE_INTEGER );
$fields['file_width'] =
$engine->makeSearchFieldMapping( 'file_width', SearchIndexField::INDEX_TYPE_INTEGER );
$fields['file_height'] =
$engine->makeSearchFieldMapping( 'file_height', SearchIndexField::INDEX_TYPE_INTEGER );
$fields['file_bits'] =
$engine->makeSearchFieldMapping( 'file_bits', SearchIndexField::INDEX_TYPE_INTEGER );
$fields['file_resolution'] =
$engine->makeSearchFieldMapping( 'file_resolution', SearchIndexField::INDEX_TYPE_INTEGER );
$fields['file_text'] =
$engine->makeSearchFieldMapping( 'file_text', SearchIndexField::INDEX_TYPE_TEXT );
return $fields;
}
public function getDataForSearchIndex(
WikiPage $page,
ParserOutput $parserOutput,
SearchEngine $engine,
?RevisionRecord $revision = null
) {
$fields = [];
$title = $page->getTitle();
if ( NS_FILE != $title->getNamespace() ) {
return [];
}
$file = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo()
->newFile( $title );
if ( !$file || !$file->exists() ) {
return [];
}
$handler = $file->getHandler();
if ( $handler ) {
$fields['file_text'] = $handler->getEntireText( $file );
}
$fields['file_media_type'] = $file->getMediaType();
$fields['file_mime'] = $file->getMimeType();
$fields['file_size'] = $file->getSize();
$fields['file_width'] = $file->getWidth();
$fields['file_height'] = $file->getHeight();
$fields['file_bits'] = $file->getBitDepth();
$fields['file_resolution'] =
(int)floor( sqrt( $fields['file_width'] * $fields['file_height'] ) );
return $fields;
}
}