Slight refactoring wiki code, a=chris

Chris Pollett [2018-08-11 01:Aug:th]
Slight refactoring wiki code, a=chris
Filename
src/models/GroupModel.php
src/scripts/help.js
diff --git a/src/models/GroupModel.php b/src/models/GroupModel.php
index 375854912..fbb65800b 100644
--- a/src/models/GroupModel.php
+++ b/src/models/GroupModel.php
@@ -1871,12 +1871,6 @@ class GroupModel extends Model implements MediaConstants
                     }
                 }
             }
-            $video_type_extensions = ['video/mp4' => "mp4",
-                'video/ogg' => "ogv",
-                'video/avi' => 'avi', 'video/quicktime' => 'mov',
-                'video/x-flv' => 'flv',
-                'video/x-ms-wmv' => 'wmv', 'video/webm' => 'webm',
-                'application/ogg' => 'ogv'];
             if ($is_dir) {
                 $new_sub_path = ($sub_path) ?
                     "$sub_path/$resource_name" : "$resource_name";
@@ -1888,95 +1882,16 @@ class GroupModel extends Model implements MediaConstants
             } else if (($matches[1][$i] == "-link")) {
                 $replace_string = "<a href='$resource_url' >".
                     "$resource_description</a>";
-                $parsed_page = preg_replace('/'.preg_quote($match_string, '/')
+                $parsed_page = preg_replace('/' . preg_quote($match_string, '/')
                     .'/u',  $replace_string, $parsed_page);
-            } else if (in_array($mime_type, ['image/png', 'image/gif',
-                'image/jpeg', 'image/bmp', 'image/svg+xml'])) {
-                if ($thumb_resource) {
-                    $replace_string = "<a class='image-list' ".
-                        "href='$resource_url' >".
-                        "<img src='$resource_url' ".
-                        " alt='$resource_description' /></a>";
-                } else {
-                    $replace_string = "<img src='$resource_url' ".
-                        " alt='$resource_description' class='photo' />";
-                }
-                $parsed_page = preg_replace('/'.
-                    preg_quote($match_string,'/').'/u',
-                    $replace_string, $parsed_page);
-            } elseif (in_array($mime_type,
-                array_keys($video_type_extensions))) {
-                $replace_string = "<video class='video' ".
-                    "controls='controls' $autoplay id='".
-                    L\crawlHash($resource_name)."' >\n".
-                    "<source src='$resource_url' type='$mime_type'/>\n";
-                $multi_source_types = ["mp4", "webm", "ogg"];
-                $current_extension = $video_type_extensions[$mime_type];
-                $add_sources = [];
-                if (empty($data_resource) &&
-                    !in_array($current_extension, $multi_source_types)) {
-                    $add_sources = array_diff($multi_source_types,
-                        [$current_extension]);
-                }
-                $pre_name = substr($resource_name, 0,
-                    -strlen($current_extension) -1);
-                // add subtitles file if exists
-                $subtitle_file = "$pre_name-subtitles-$locale_tag.vtt";
-                $all_subtitle_files = glob(
-                    "$current_folder/$pre_name-subtitles-*.vtt");
-                if (empty($data_resource) && !empty($all_subtitle_files)) {
-                    foreach ($all_subtitle_files as $sub_file) {
-                        preg_match("@$pre_name-subtitles-(.+).vtt@", $sub_file,
-                            $matches);
-                        if (!empty($matches[1])) {
-                            $resource_url = $this->getGroupPageResourceUrl(
-                                $csrf_token, $group_id, $current_page_id,
-                                $matches[0], $sub_path);
-                            $default = ($sub_file ==
-                                "$current_folder/$subtitle_file") ?
-                                "default" : "";
-                            $tag = $matches[1];
-                            $replace_string .= "<track src='$resource_url' " .
-                                "label='$tag' kind='subtitles' " .
-                                "srclang='$tag' $default />\n";
-                        }
-                    }
-                }
-                $captions_file = "$pre_name-captions-$locale_tag.vtt";
-                $all_captions_files = glob(
-                    "$current_folder/$pre_name-captions-*.vtt");
-                if (empty($data_resource) && !empty($all_captions_files)) {
-                    foreach ($all_captions_files as $cap_file) {
-                        preg_match("@$pre_name-captions-(.+).vtt@", $cap_file,
-                            $matches);
-                        if (!empty($matches[1])) {
-                            $resource_url = $this->getGroupPageResourceUrl(
-                                $csrf_token, $group_id, $current_page_id,
-                                $matches[0], $sub_path);
-                            $default = ($cap_file ==
-                                "$current_folder/$captions_file") ?
-                                "default" : "";
-                            $tag = $matches[1];
-                            $replace_string .= "<track src='$resource_url' " .
-                                "label='$tag' kind='captions' " .
-                                "srclang='$tag' $default />\n";
-                        }
-                    }
-                }
-                foreach ($add_sources as $extension) {
-                    if (file_exists("$current_folder/$pre_name.$extension")) {
-                        $resource_url = $this->getGroupPageResourceUrl(
-                            $csrf_token, $group_id, $current_page_id,
-                            "$pre_name.$extension", $sub_path);
-                        $replace_string .= "<source src='$resource_url' ".
-                            "type='video/$extension'/>\n";
-                    }
-                }
-                $replace_string .= $resource_description."\n"."</video>";
-                $parsed_page = preg_replace('/'.
-                    preg_quote($match_string, '/').'/u',
-                    $replace_string, $parsed_page);
-            } elseif (in_array($mime_type, ['audio/aiff', 'audio/basic',
+            } else if (in_array(substr($mime_type, 0, 5), ['image', 'video']) ||
+                $mime_type == 'application/ogg') {
+                $parsed_page = $this->insertVideoImageResourceParsePage(
+                    $mime_type, $parsed_page, $thumb_resource, $resource_name,
+                    $resource_url, $resource_description, $autoplay,
+                    $current_folder, $data_resource, $match_string, $locale_tag,
+                    $csrf_token, $group_id, $current_page_id);
+            } else if (in_array($mime_type, ['audio/aiff', 'audio/basic',
                 'audio/L24', 'audio/mpeg', 'audio/mpeg3', 'audio/mp4',
                 'audio/ogg', 'audio/opus',
                 'audio/vorbis', 'audio/vnd.rn-realaudio', 'audio/vnd.wave',
@@ -1989,7 +1904,7 @@ class GroupModel extends Model implements MediaConstants
                     "</audio>";
                 $parsed_page = preg_replace('/'.preg_quote($match_string, '/')
                     .'/u', $replace_string, $parsed_page);
-            } elseif (($mime_type =='application/epub+zip' &&
+            } else if (($mime_type =='application/epub+zip' &&
                 file_exists(C\APP_DIR. "/scripts/epub.js")) ||
                 ($mime_type =='application/pdf' &&
                 file_exists(C\APP_DIR. "/scripts/pdf.js"))) {
@@ -2024,12 +1939,12 @@ class GroupModel extends Model implements MediaConstants
                 }
                 $parsed_page = preg_replace('/'.preg_quote($match_string, '/')
                     .'/u',  $replace_string, $parsed_page);
-            } elseif (in_array($mime_type, ['text/html', 'application/pdf'] )) {
+            } else if (in_array($mime_type, ['text/html', 'application/pdf'] )) {
                 $replace_string = "<iframe class='wiki-resource-object' ".
                     "src='$resource_url' >$resource_description</iframe>";
                 $parsed_page = preg_replace('/'.preg_quote($match_string, '/')
                     .'/u',  $replace_string, $parsed_page);
-            }  elseif ($mime_type == 'text/csv') {
+            }  else if ($mime_type == 'text/csv') {
                 if (!$include_charts_and_spreadsheets) {
                     continue;
                 }
@@ -2089,7 +2004,7 @@ class GroupModel extends Model implements MediaConstants
                         preg_quote($match_string, '/')
                         .'/u',  $replace_string, $parsed_page, 1);
                 }
-            } elseif (substr($mime_type, 0, 4) == 'text') {
+            } else if (substr($mime_type, 0, 4) == 'text') {
                 $resource = file_get_contents($file_name);
                 $replace_string = "<pre>\n".htmlentities($resource)."\n</pre>";
                 $parsed_page = preg_replace('/'.preg_quote($match_string, '/')
@@ -2103,6 +2018,106 @@ class GroupModel extends Model implements MediaConstants
         }
         return $parsed_page;
     }
+    /**
+     *
+     */
+    public function insertVideoImageResourceParsePage($mime_type, $parsed_page,
+        $thumb_resource, $resource_name, $resource_url, $resource_description,
+        $autoplay, $current_folder, $data_resource, $match_string, $locale_tag,
+        $csrf_token, $group_id, $current_page_id)
+    {
+        $is_360 =  (preg_match("/360/", $resource_url)) ? true : false;
+        if (substr($mime_type, 0, 5) == 'image') {
+            if ($thumb_resource) {
+                $replace_string = "<a class='image-list' ".
+                    "href='$resource_url' ><img src='$resource_url' ".
+                    " alt='$resource_description' /></a>";
+            } else {
+                $replace_string = "<a ".
+                    "href='$resource_url' ><img src='$resource_url' ".
+                    " alt='$resource_description' class='photo' /></a>";
+            }
+            $parsed_page = preg_replace('/'. preg_quote($match_string,'/').'/u',
+                $replace_string, $parsed_page);
+        } else {
+            $video_type_extensions = ['video/mp4' => "mp4",
+                'video/ogg' => "ogv",
+                'video/avi' => 'avi', 'video/quicktime' => 'mov',
+                'video/x-flv' => 'flv',
+                'video/x-ms-wmv' => 'wmv', 'video/webm' => 'webm',
+                'application/ogg' => 'ogv'];
+            $replace_string = "<video class='video' " .
+                "controls='controls' $autoplay id='" .
+                L\crawlHash($resource_name) . "' >\n".
+                "<source src='$resource_url' type='$mime_type'/>\n";
+            $multi_source_types = ["mp4", "webm", "ogg"];
+            $current_extension = $video_type_extensions[$mime_type];
+            $add_sources = [];
+            if (empty($data_resource) &&
+                !in_array($current_extension, $multi_source_types)) {
+                $add_sources = array_diff($multi_source_types,
+                    [$current_extension]);
+            }
+            $pre_name = substr($resource_name, 0,
+                -strlen($current_extension) -1);
+            // add subtitles file if exists
+            $subtitle_file = "$pre_name-subtitles-$locale_tag.vtt";
+            $all_subtitle_files = glob(
+                "$current_folder/$pre_name-subtitles-*.vtt");
+            if (empty($data_resource) && !empty($all_subtitle_files)) {
+                foreach ($all_subtitle_files as $sub_file) {
+                    preg_match("@$pre_name-subtitles-(.+).vtt@", $sub_file,
+                        $matches);
+                    if (!empty($matches[1])) {
+                        $resource_url = $this->getGroupPageResourceUrl(
+                            $csrf_token, $group_id, $current_page_id,
+                            $matches[0], $sub_path);
+                        $default = ($sub_file ==
+                            "$current_folder/$subtitle_file") ?
+                            "default" : "";
+                        $tag = $matches[1];
+                        $replace_string .= "<track src='$resource_url' " .
+                            "label='$tag' kind='subtitles' " .
+                            "srclang='$tag' $default />\n";
+                    }
+                }
+            }
+            $captions_file = "$pre_name-captions-$locale_tag.vtt";
+            $all_captions_files = glob(
+                "$current_folder/$pre_name-captions-*.vtt");
+            if (empty($data_resource) && !empty($all_captions_files)) {
+                foreach ($all_captions_files as $cap_file) {
+                    preg_match("@$pre_name-captions-(.+).vtt@", $cap_file,
+                        $matches);
+                    if (!empty($matches[1])) {
+                        $resource_url = $this->getGroupPageResourceUrl(
+                            $csrf_token, $group_id, $current_page_id,
+                            $matches[0], $sub_path);
+                        $default = ($cap_file ==
+                            "$current_folder/$captions_file") ?
+                            "default" : "";
+                        $tag = $matches[1];
+                        $replace_string .= "<track src='$resource_url' " .
+                            "label='$tag' kind='captions' " .
+                            "srclang='$tag' $default />\n";
+                    }
+                }
+            }
+            foreach ($add_sources as $extension) {
+                if (file_exists("$current_folder/$pre_name.$extension")) {
+                    $resource_url = $this->getGroupPageResourceUrl(
+                        $csrf_token, $group_id, $current_page_id,
+                        "$pre_name.$extension", $sub_path);
+                    $replace_string .= "<source src='$resource_url' ".
+                        "type='video/$extension'/>\n";
+                }
+            }
+            $replace_string .= $resource_description . "\n</video>";
+            $parsed_page = preg_replace('/'.preg_quote($match_string, '/').'/u',
+                $replace_string, $parsed_page);
+        }
+        return $parsed_page;
+    }
     /**
      * Used to convert a pair of spreadsheet coordinates into a pair
      * of integer rectangular coordinates. For example [A3, B4] into
diff --git a/src/scripts/help.js b/src/scripts/help.js
index 27cfc35f8..2197cdb77 100644
--- a/src/scripts/help.js
+++ b/src/scripts/help.js
@@ -278,7 +278,7 @@ function getPageWithCallback(url, response_type, success_call_back,
  * @param String is_mobile flag to check if the client is mobile
  * or not.
  * @param String target_controller Wiki page's controller name.
- * @param String csrf_token_key teh dynamic name used for CSRF token var.
+ * @param String csrf_token_key the dynamic name used for CSRF token var.
  * @param String csrf_token_value The CSRF token to render edit page.
  * @param String help_group_id  help's group_id required to render resources.
  * @param String api_controller api's controller name.
@@ -357,7 +357,7 @@ function displayHelpForId(help_point, is_mobile, target_controller,
 /*
  * Simple function to construct the Wiki Edit hyperlink with passed in params.
  * @param String target_controller Edit page's controller name.
- * @param String csrf_token_key teh dynamic name used for CSRF token var.
+ * @param String csrf_token_key the dynamic name used for CSRF token var.
  * @param String csrf_token_value The CSRF token to render edit page.
  * @param String group_id GroupId of the group which has the wiki.
  * @param String page_name Page name,unique Identifier for wiki edit page.
ViewGit