Tidy up PHP code

Signed-off-by: Justin Georgi <justin.georgi@gmail.com>
This commit is contained in:
2024-10-29 16:14:29 -07:00
parent 5d4863f9be
commit 4f1ff35194
3 changed files with 332 additions and 327 deletions

View File

@@ -6,117 +6,119 @@ use Html;
use ParserOutput;
class GlModelHooks {
/**
* MWHook: Add gltf mime types to MimeMagic
*
* @see https://www.mediawiki.org/wiki/Manual:Hooks/MimeMagicInit
*
* @param MimeAnalyzer $mime Instance of MW MimeAnalyzer object
*/
public static function onMimeMagicInit($mime) {
$mime->addExtraTypes('model/gltf-binary glb gltf');
$mime->addExtraInfo('model/gltf-binary [MULTIMEDIA]');
}
/**
* MWHook: Make sure that gltf files get the proper mime assignement on upload
*
* @see https://www.mediawiki.org/wiki/Manual:Hooks/MimeMagicImproveFromExtension
*
* @param MimeAnalyzer $mimeAnalyzer Instance of MW MimeAnalyzer object
* @param string $ext extention of upload file
* @param string &$mime current assigned mime type
*/
public static function onMimeMagicImproveFromExtension( $mimeAnalyzer, $ext, &$mime ) {
if ( $mime !== 'model/gltf-binary' && in_array( $ext, ['glb', 'gltf'] ) ) {
$mime = 'model/gltf-binary';
}
}
/**
* MWHook: Load the js and css modules if model-viewer element is found in the html output
*
* @see https://www.mediawiki.org/wiki/Manual:Hooks/BeforePageDisplay
*
* @param OutputPage $out compiled page html and manipulation methods
*/
public static function onBeforePageDisplay($out) {
preg_match('/(<model-viewer src="\S*?\.(glb|gltf"))/',$out->getHTML(),$findGltf);
if ($findGltf[0]) {
$mvScriptAttr = array(
'src' => 'https://ajax.googleapis.com/ajax/libs/model-viewer/3.5.0/model-viewer.min.js',
'type' => 'module'
);
$out->addHeadItems(Html::rawElement('script',$mvScriptAttr));
$out->addModules('ext.glmv');
}
}
/**
* MWHook: Display model on file page
*
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ImageOpenShowImageInlineBefore
*
* @param ImagePage $imagepage information regarding the page for the image file
* @param OutputPage $out compiled page html and manipulation methods
*/
public static function onImageOpenShowImageInlineBefore( $imagepage, $out ){
$file = $imagepage->getFile();
if ($file->getMimeType() == 'model/gltf-binary') {
$out->clearHTML();
$out->addWikiTextAsContent('[[' . $file->getTitle()->getFullText() . '|800x600px]]');
}
}
/**
* MWHook: Display model when model file page edits are previewed
*
* @see https://www.mediawiki.org/wiki/Manual:Hooks/AlternateEditPreview
*
* @param EditPage $editor access to information about the edit page itself
* @param Content $content access to the current content of the editor
* @param string &$previewHTML by reference access to the resultant compiled preview html
* @param ?ParserOutput &$parserOutput
* @return bool|void True to continue default processing or false to abort for custom processing
*/
public static function onAlternateEditPreview( $editor, $content, string &$previewHTML, ?ParserOutput &$parserOutput ) {
$file = MediaWikiServices::getInstance()->getRepoGroup()->findFile($editor->getTitle());
if (!$file || $file->getMimeType() !== 'model/gltf-binary') {
return true;
/**
* MWHook: Add gltf mime types to MimeMagic
*
* @see https://www.mediawiki.org/wiki/Manual:Hooks/MimeMagicInit
*
* @param MimeAnalyzer $mime Instance of MW MimeAnalyzer object
*/
public static function onMimeMagicInit($mime) {
$mime->addExtraTypes('model/gltf-binary glb gltf');
$mime->addExtraInfo('model/gltf-binary [MULTIMEDIA]');
}
$out = $editor->getContext()->getOutput();
$out->addModules('ext.glmv');
$mvTransform = $file->transform([ 'width' => '800', 'hight' => '600']);
$previewViewer = $mvTransform->toHtml([ 'preview' => true]);
$addButtonAttr = array(
'class' => 'AddHotspot',
'onclick' => 'readyAddHotspot()'
);
$addHsButton = Html::rawElement('button',$addButtonAttr,'Add a new hotspot');
$previewHTML = Html::rawElement('div',NULL,$previewViewer.$addHsButton);
return false;
}
/**
* Small helper function to display information on the browser console
*
* Usage:
* echo '<script>';
* self::console_log('logged string');
* echo '</script>';
*
* @param $data information to display
* @param bool $add_script_tags true to put information is inside complete script tag
*/
public static function console_log($data, $add_script_tags = false) {
$command = 'console.log('. json_encode($data, JSON_HEX_TAG).');';
if ($add_script_tags) {
$command = '<script>'. $command . '</script>';
/**
* MWHook: Make sure that gltf files get the proper mime assignement on upload
*
* @see https://www.mediawiki.org/wiki/Manual:Hooks/MimeMagicImproveFromExtension
*
* @param MimeAnalyzer $mimeAnalyzer Instance of MW MimeAnalyzer object
* @param string $ext extention of upload file
* @param string &$mime current assigned mime type
*/
public static function onMimeMagicImproveFromExtension( $mimeAnalyzer, $ext, &$mime ) {
if ( $mime !== 'model/gltf-binary' && in_array( $ext, ['glb', 'gltf'] ) ) {
$mime = 'model/gltf-binary';
}
}
/**
* MWHook: Load the js and css modules if model-viewer element is found in the html output
*
* @see https://www.mediawiki.org/wiki/Manual:Hooks/BeforePageDisplay
*
* @param OutputPage $out compiled page html and manipulation methods
*/
public static function onBeforePageDisplay($out) {
preg_match('/(<model-viewer src="\S*?\.(glb|gltf"))/',$out->getHTML(),$findGltf);
if ($findGltf[0]) {
$mvScriptAttr = array(
'src' => 'https://ajax.googleapis.com/ajax/libs/model-viewer/3.5.0/model-viewer.min.js',
'type' => 'module'
);
$out->addHeadItems(Html::rawElement('script',$mvScriptAttr));
$out->addModules('ext.glmv');
}
}
/**
* MWHook: Display model on file page
*
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ImageOpenShowImageInlineBefore
*
* @param ImagePage $imagepage information regarding the page for the image file
* @param OutputPage $out compiled page html and manipulation methods
*/
public static function onImageOpenShowImageInlineBefore( $imagepage, $out ){
$file = $imagepage->getFile();
if ($file->getMimeType() == 'model/gltf-binary') {
$out->clearHTML();
$out->addWikiTextAsContent('[[' . $file->getTitle()->getFullText() . '|800x600px]]');
}
}
/**
* MWHook: Display model when model file page edits are previewed
*
* @see https://www.mediawiki.org/wiki/Manual:Hooks/AlternateEditPreview
*
* @param EditPage $editor access to information about the edit page itself
* @param Content $content access to the current content of the editor
* @param string &$previewHTML by reference access to the resultant compiled preview html
* @param ?ParserOutput &$parserOutput
* @return bool|void True to continue default processing or false to abort for custom processing
*/
public static function onAlternateEditPreview( $editor, $content, string &$previewHTML, ?ParserOutput &$parserOutput ) {
$file = MediaWikiServices::getInstance()->getRepoGroup()->findFile($editor->getTitle());
if (!$file || $file->getMimeType() !== 'model/gltf-binary') {
return true;
}
$out = $editor->getContext()->getOutput();
$out->addModules('ext.glmv');
$mvTransform = $file->transform([ 'width' => '800', 'hight' => '600']);
$previewViewer = $mvTransform->toHtml([ 'preview' => true]);
$addButtonAttr = array(
'class' => 'AddHotspot',
'onclick' => 'readyAddHotspot()'
);
$addHsButton = array(
Html::rawElement('button',$addButtonAttr,'Add a new hotspot')
);
$previewHTML = Html::rawElement('div',NULL,$previewViewer.implode($addHsButton));
return false;
}
/**
* Small helper function to display information on the browser console
*
* Usage:
* echo '<script>';
* self::console_log('logged string');
* echo '</script>';
*
* @param $data information to display
* @param bool $add_script_tags true to put information is inside complete script tag
*/
public static function console_log($data, $add_script_tags = false) {
$command = 'console.log('. json_encode($data, JSON_HEX_TAG).');';
if ($add_script_tags) {
$command = '<script>'. $command . '</script>';
}
echo $command;
}
echo $command;
}
}