wiki.techinc.nl/includes/content/FileContentHandler.php
Erik Bernhardson 3ff25f3fa9 search: Set file_text to null when not available
file_text has previously been set to false or an empty array,
depending on context, when it wasn't available. As part of normalizing
the set of types used in the search index default the value to null
and only set it if the media handler is able to extract text content
from it.

Setting the value to null when not available is done to clear out
historical false/empty array values in storage. Perhaps we need to
think more in the future about if/when default values should be
provided to search updates, everything is a bit ad-hoc today.

Bug: T322327
Change-Id: I1367154b17d9e69c9373e7efee384838aa3b51e8
2022-12-02 18:21:16 +00:00

74 lines
2.5 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 ) {
$fileText = $handler->getEntireText( $file );
if ( $fileText !== false ) {
$fields['file_text'] = $fileText;
}
}
$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;
}
}