wiki.techinc.nl/includes/content/FileContentHandler.php
Derick Alangi 21e2d71560 Replace some uses of deprecated wfFindFile() and wfLocalFile()
These global functions were deprecated in 1.34 and services made
available to replace them. See services below;

* wfFindFile() - MediaWikiServices::getInstance()->getRepoGroup()->findFile()
* wfLocalFind() - MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo()->newFile()

NOTES:

* wfFindFile() and wfLocalFind() usages in tests have been ignored
  in this change per @Timo's comments about state of objects.

* includes/upload/UploadBase.php also maintained for now as it causes
  some failures I don't fully understand, will investigate and handle
  it in a follow up patch.

* Also, includes/MovePage.php

Change-Id: I9437494de003f40fbe591321da7b42d16bb732d6
2019-06-11 13:26:37 +00:00

68 lines
2.3 KiB
PHP

<?php
use MediaWiki\MediaWikiServices;
/**
* 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['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
) {
$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;
}
}