diff --git a/GlModelHandler.i18n.magic.php b/GlModelHandler.i18n.magic.php new file mode 100644 index 0000000..16c6330 --- /dev/null +++ b/GlModelHandler.i18n.magic.php @@ -0,0 +1,7 @@ + [ 1, "view=$1" ], + 'glmv_hsset' => [1, "set=$1"] +]; \ No newline at end of file diff --git a/extension.json b/extension.json index 9f0f786..f2fcf2c 100644 --- a/extension.json +++ b/extension.json @@ -19,6 +19,9 @@ "BeforePageDisplay": "MediaWiki\\Extension\\GlModelViewer\\GlModelHooks::onBeforePageDisplay", "AlternateEditPreview": "MediaWiki\\Extension\\GlModelViewer\\GlModelHooks::onAlternateEditPreview" }, + "ExtensionMessagesFiles": { + "GlModelHandlerMagic": "GlModelHandler.i18n.magic.php" + }, "ResourceFileModulePaths": { "localBasePath": "modules", "remoteExtPath": "GlModelViewer/modules" diff --git a/includes/GlModelHandler.php b/includes/GlModelHandler.php index 7777894..5707e35 100644 --- a/includes/GlModelHandler.php +++ b/includes/GlModelHandler.php @@ -59,9 +59,6 @@ class GlModelHandler extends ImageHandler { * @return bool */ public function validateParam( $name, $value ) { - echo ''; if (in_array($name, ['width', 'height'])) { return $value > 0; } else if (in_array($name, ['view', 'hsset'])) { @@ -71,6 +68,68 @@ class GlModelHandler extends ImageHandler { } } + /** + * Merge a parameter array into a string appropriate for inclusion in filenames. + * + * @param array $params array of parameters that have been through normaliseParams. + * @return string concatenated, formatted string of parameter values + */ + public function makeParamString( $params ) { + $res = parent::makeParamString( $params ); + if ( $res && isset( $params['hsset'] ) ) { + $res = "hs{$params['hsset']}-$res"; + } + if ( $res && isset( $params['view'] ) ) { + $res = "v{$params['view']}-$res"; + } + return $res; + } + + /** + * Parse a param string made with makeParamString back into an array. + * + * @param string $str the parameter string without file name + * @return array|false Array of parameters or false on failure + */ + public function parseParamString( $str ) { + preg_match( '/v([^-]*?)-/', $str, $view ); + preg_match( '/hs([^-]*?)-/', $str, $hsset ); + $otherParams = preg_replace('/(?:(?:hs[^-]*?-)|(?:v[^-]*?-))*(.*)$/', '/$1/', $str); + $parsedParams = parent::parseParamString( $otherParams ); + $parsedParams['view'] = $view; + $parsedParams['hsset'] = $hsset; + return $res; + } + + /** + * ? + * + * @param array $params input parameters + * @return array same array as input? + */ + protected function getScriptParams( $params ) { + $res = parent::getScriptParams( $params ); + if ( isset( $params['view'] ) ) { + $res['view'] = $params['view']; + } + if ( isset( $params['hsset'] ) ) { + $res['hsset'] = $params['hsset']; + } + return $res; + } + + /** + * Get an associative array mapping magic word IDs to parameter names. + * @return string[] + */ + public function getParamMap() { + return [ + 'img_width' => 'width', + 'glmv_view' => 'view', + 'glmv_hsset' => 'hsset' + ]; + } + /** * Small helper function to display information on the browser console * diff --git a/includes/GlModelTransformOutput.php b/includes/GlModelTransformOutput.php index 23b75ff..48ec87c 100644 --- a/includes/GlModelTransformOutput.php +++ b/includes/GlModelTransformOutput.php @@ -20,6 +20,8 @@ class GlModelTransformOutput extends MediaTransformOutput { $this->parameters = $parameters; $this->width = $parameters['width']; $this->height = $parameters['height']; + $this->view = (isset($parameters['view'])) ? $parameters['view'] : 'default'; + $this->hsset = (isset($parameters['hsset'])) ? $parameters['hsset'] : 'all'; $this->url = $file->getFullUrl(); $this->thumb = isset($parameters['isFilePageThumb']) && $parameters['isFilePageThumb']; } @@ -79,15 +81,15 @@ class GlModelTransformOutput extends MediaTransformOutput { //Gather basic data preg_match('/
([\S\s]*?)<\/pre>/',$inText,$modelDescript);
         $metadata = json_decode($modelDescript[1], true);
-        $viewClass = self::extractClassParams('view',$viewParams['class']);
-        $hsSetClass = self::extractClassParams('hsset',$viewParams['class']);
+        $hsSet = (isset($metadata['annotationSets']) && isset($metadata['annotationSets'][$this->hsset])) ? $this->hsset : 'default';
+        $view = (isset($metadata['viewerConfig']) && isset($metadata['viewerConfig'][$this->view])) ? $this->view : 'default';
 
         //Handle annotations and annotation sets
         if (isset($metadata['annotations'])) {
             $hotspots = [];
             $annotations = [];
-            if ($hsSetClass != 'default' && isset($metadata['annotationSets'])) {
-                foreach($metadata['annotationSets'][$hsSetClass] as $includeAn) {
+            if ($hsSet != 'default') {
+                foreach($metadata['annotationSets'][$hsSet] as $includeAn) {
                     if (isset($metadata['annotations'][$includeAn])) {
                         $annotations[$includeAn] = $metadata['annotations'][$includeAn];
                     }
@@ -114,8 +116,8 @@ class GlModelTransformOutput extends MediaTransformOutput {
         }
 
         //Set viewer configurations or use basic default if none defined
-        if (isset($metadata['viewerConfig']) && isset($metadata['viewerConfig'][$viewClass])) {
-        $attrModelView = $metadata['viewerConfig'][$viewClass];
+        if (isset($metadata['viewerConfig']) && isset($metadata['viewerConfig'][$view])) {
+        $attrModelView = $metadata['viewerConfig'][$view];
         } else {
         $attrModelView = array('camera-controls' => true);
         }
@@ -139,32 +141,6 @@ class GlModelTransformOutput extends MediaTransformOutput {
         return Html::rawElement('div', $attrContainer, $elModel . $elMenu);
     }
 
-    /**
-     * Detect and return model user params from link classes
-     * 
-     * The best way to send information in a file link is by adding classes to the
-     * link. This function parses the array of those classes and returns the information
-     * or 'default'
-     * 
-     * @param string $paramType the type of parameter to be extracted 'view'|'hsset'
-     * @param string $classList a string of class names
-     * @return string parameter value or 'default'
-     */
-    private static function extractClassParams(string $paramType, ? string $classList) {
-        if (!in_array($paramType, ['view', 'hsset'])) {
-            return 'default';
-        }
-
-        if (!isset($classList)) {
-            return 'default';
-        }
-
-        $searchString = '/' . $paramType . '-(\S*)/';
-        preg_match($searchString,$classList,$extractInfo);
-
-        return ($extractInfo[1]) ? $extractInfo[1] : 'default';
-    }
-
     /**
      * Build the button menu used for viewer actions
      *