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: Called when the parser initializes for the first time * * @param Parser $parser: Parser object being initialized */ static function onParserFirstCallInit( $parser ) { $parser->setHook('mvconfig', array( __CLASS__, 'renderConfigTag')); } /** * Render the config toml in a
tag
*
* @param $input The text inside the custom tag
* @param array $args Any attributes given in the tag
* @param Parser $parser The parser object
* @param PPFrame $frame The parent frame calling the parser
* @return string HTML string of output
*/
static function renderConfigTag( $input, array $args, $parser, $frame ) {
return '' . $input . '
';
}
/**
* 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('/(getHTML(),$findGltf);
if ($findGltf[0]) {
$out->addHeadItems(
Html::rawElement(
'script',
array(
'src' => 'https://ajax.googleapis.com/ajax/libs/model-viewer/3.5.0/model-viewer.min.js',
'type' => 'module'
)
)
);
$out->addModules('ext.glmv');
}
}
/**
* 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 ) {
$title = $editor->getTitle();
$file = MediaWikiServices::getInstance()->getRepoGroup()->findFile($title);
if (!$file || $file->getMimeType() !== 'model/gltf-binary') {
return true;
}
$out = $editor->getContext()->getOutput();
$out->addModules('ext.glmv.prev');
$mvTransform = $file->transform([ 'width' => '800', 'hight' => '600', 'view' => 'test']);
$previewViewer = $mvTransform->toHtml([ 'preview' => true]);
$previewHTML = Html::rawElement('div',NULL,$previewViewer);
return false;
}
/**
* MWHook: Occurs after the descriptor for the upload form as been assembled.
*
* @param $descriptor the HTMLForm descriptor
*/
public static function onUploadFormInitDescriptor( $descriptor ) {
$context = RequestContext::getMain();
$out = $context->getOutput();
$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.upl');
}
/**
* Perform upload verification, based on both file properties like MIME type
* (same as UploadVerifyFile) and the information entered by the user (upload
* comment, file page contents etc.).
*
* @param UploadBase $upload Instance of UploadBase, with all info about the upload
* @param User $user User uploading this file
* @param array | null $props File properties, as returned by MWFileProps::getPropsFromPath()
* @param string $comment Upload log comment (also used as edit summary)
* @param string | false $pageText File description page text (only used for new uploads)
* @param array | MessageSpecifier | null &$error Output: If the file upload should be prevented, set this to the reason
* @return bool|void True or no return value to continue or false to abort
*/
public static function onUploadVerifyUpload ($upload, $user, ?array $props, $comment, $pageText, &$error) {
if ($props['mime'] != 'model/gltf-binary') {
return true;
} else {
$path = $upload->getTempPath();
$gltf = fopen($path, 'r');
$glHeader = fread($gltf, 4);
fclose($gltf);
if ($glHeader == 'glTF') {
return true;
} else {
$error = array(
"glmv-load-error-format",
$upload->getTitle()->getRootText()
);
return false;
}
}
}
/**
* MWHook: Called just before the upload data, like wpUploadDescription, are processed, so extensions get a chance to manipulate them.
*
* @param SpecialUpload $uploadFormObj current SpecialUpload object
*/
public static function onUploadFormBeforeProcessing( $uploadFormObj ) {
preg_match('/(glb|gltf)$/', $uploadFormObj->mUpload->getTitle(), $isGltf);
if ($isGltf) {
$uploadFormObj->mComment .= <<
[viewerConfig.default]
camera-controls = true
CONF;
}
return true;
}
/**
* Small helper function to display information on the browser console
*
* Usage:
* echo '';
*
* @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 = '';
}
echo $command;
}
}