_binary From 494558406dd5c547036e4b06cd229976cac37f2d Mon Sep 17 00:00:00 2001
From: Parth Patel <parthamrutbhai.patel@sjsu.edu>
Date: Tue, 29 Nov 2022 17:11:09 -0800
Subject: [PATCH] Add RESOURCE_IMPRESSION type to capture wiki resource
 impression and update ManyUserExperiment to create wiki resource impressions,
 r=chris

---
 src/configs/Config.php                        |   4 +
 .../components/SocialComponent.php            |   5 +
 .../media_jobs/DescriptionUpdateJob.php       |   2 +-
 tests/ManyUserExperiment.php                  | 241 +++++++++++++++++-
 4 files changed, 250 insertions(+), 2 deletions(-)

diff --git a/src/configs/Config.php b/src/configs/Config.php
index 07a36625b..e3602044d 100755
--- a/src/configs/Config.php
+++ b/src/configs/Config.php
@@ -1214,6 +1214,10 @@ nsdefine('QUERY_IMPRESSION', 4);
  * Impression type used to record one search cache request view
  */
 nsdefine('CACHE_IMPRESSION', 5);
+/**
+ * Impression type used to record one view of a wiki page resource
+ */
+nsdefine('RESOURCE_IMPRESSION', 6);
 /**
  * Number of ITEM_RECOMMENDATIONs to suggest to a user
  */
diff --git a/src/controllers/components/SocialComponent.php b/src/controllers/components/SocialComponent.php
index ae4ea53ba..073d27670 100644
--- a/src/controllers/components/SocialComponent.php
+++ b/src/controllers/components/SocialComponent.php
@@ -4915,6 +4915,11 @@ EOD;
             $data['SPREADSHEET'] = true;
         }
         $data["PAGE_ID"] = $page_id;
+        $resource_id = unpack('n', md5($group_id . $page_id .
+            $data['RESOURCES_INFO']['thumb_folder'] . "/" .
+            $_REQUEST['n'], true))[1];
+        $parent->model("impression")->add($user_id, $resource_id,
+            C\RESOURCE_IMPRESSION);
     }
     /**
      * Used to initialize arrays for dropdowns in WikiElement as well
diff --git a/src/library/media_jobs/DescriptionUpdateJob.php b/src/library/media_jobs/DescriptionUpdateJob.php
index 2eb0ce83d..16eb810ed 100644
--- a/src/library/media_jobs/DescriptionUpdateJob.php
+++ b/src/library/media_jobs/DescriptionUpdateJob.php
@@ -226,7 +226,7 @@ class DescriptionUpdateJob extends MediaJob
             $resource_detail = trim($resource_detail);
             $resource_name = trim(preg_replace('/\s+/', ' ', $resource_detail));
             $resource_name = pathinfo($resource_name)['filename'];
-            $resource_name = preg_replace('/\s+/', '_', $resource_name);
+            $resource_name = preg_replace('/\s+/', '%20', $resource_name);
             $max_score = 0;
             $details_page_url = "";
             $found_details = false;
diff --git a/tests/ManyUserExperiment.php b/tests/ManyUserExperiment.php
index 761f41c7a..7130062e1 100644
--- a/tests/ManyUserExperiment.php
+++ b/tests/ManyUserExperiment.php
@@ -64,6 +64,7 @@ C\nsconddefine("ME_NUM_GROUPS", 100);
 C\nsconddefine("ME_NUM_THREADS", 100);
 C\nsconddefine("ME_NUM_ROLES", 100);
 C\nsconddefine("ME_NUM_MIXES", 100);
+C\nsconddefine("ME_NUM_RESOURCES_IMPRESSION", 15);
 $user_model = new M\UserModel();
 $group_model = new M\GroupModel();
 //Add lots of users
@@ -89,7 +90,7 @@ $group_ids = [];
 for ($i = 0; $i < C\ME_NUM_GROUPS; $i++) {
     echo "Creating Group $i\n";
     $group_ids[$i] = $group_model->addGroup("Group$i", $user_ids[$i],
-        C\PUBLIC_JOIN, C\GROUP_READ_WRITE);
+        C\PUBLIC_JOIN, C\GROUP_READ_WIKI);
 }
 // add lots of users to groups (everyone belongs to at least group 1)
 for ($i = 0; $i < C\ME_NUM_GROUPS; $i++) {
@@ -138,3 +139,241 @@ for ($i = 0; $i < C\ME_NUM_MIXES; $i++) {
     $mix['NAME'] = "Mix$i";
     $crawl_model->setCrawlMix($mix);
 }
+// add wiki page to group 1
+$page = <<< 'EOD'
+page_type=media_list
+
+page_alias=
+
+page_border=solid-border
+
+toc=true
+
+title=
+
+author=
+
+robots=
+
+description=
+
+alternative_path=
+
+page_header=
+
+page_footer=
+
+sort=aname
+
+update_description=files-and-folders
+
+END_HEAD_VARS
+==This is test page.==
+EOD;
+$page_content = str_replace("'", "&#039;", $page);
+$page_name = "Test";
+echo "Adding wiki page $page_name to group $group_ids[0]...\n";
+$page_id = $group_model->setPageName($user_ids[0], $group_ids[0], $page_name,
+    $page_content, "en-US", "Creating test wiki page",
+    "{$page_name} Wiki Page Created!", "Discuss the page in this thread!");
+echo "Added the wiki page...\n";
+echo "Creating movies folder and adding dummy mp4 resources\n";
+$resources = [];
+$folders_path = [];
+// create movie folder in the wiki page
+$folders = $group_model->getGroupPageResourcesFolders($group_ids[0], $page_id,
+    "movies", true);
+// add resources to the movies subfolder
+$movies = [
+    "The 355.mp4",
+    "The Legend of La Llorona.mp4",
+    "The Commando.mp4",
+    "American Siege.mp4",
+    "Scream.mp4",
+    "Hotel Transylvania Transformania.mp4",
+    "Shattered.mp4",
+    "The Royal Treatment.mp4",
+    "Redeeming Love.mp4",
+    "The Kings Daughter.mp4",
+    "The Tiger Rising.mp4",
+    "Hellblazers.mp4",
+    "The Fallout.mp4",
+    "Home Team.mp4",
+    "The Ice Age Adventures of Buck Wild.mp4",
+    "Clean.mp4",
+    "Sundown.mp4",
+    "Jackass Forever.mp4",
+    "Moonfall.mp4",
+    "Last Looks.mp4",
+    "The Long Night.mp4",
+    "Kimi.mp4",
+    "Shut In.mp4",
+    "Death on the Nile.mp4",
+    "Marry Me.mp4",
+    "Blacklight.mp4",
+    "I Want You Back.mp4",
+    "Tall Girl 2.mp4",
+    "The Sky Is Everywhere.mp4",
+    "The In Between.mp4",
+    "Fistful of Vengeance.mp4",
+    "Uncharted.mp4",
+    "Dog.mp4",
+    "Texas Chainsaw Massacre.mp4",
+    "The Cursed.mp4",
+    "A Fairy Tale After All.mp4",
+    "Cyrano.mp4",
+    "Studio 666.mp4",
+    "A Madea Homecoming.mp4",
+    "No Exit.mp4",
+    "Gasoline Alley.mp4",
+    "The Batman.mp4",
+    "After Yang.mp4",
+    "Fresh.mp4",
+    "A Day to Die.mp4",
+    "The Adam Project.mp4",
+    "Turning Red.mp4",
+    "Tysons Run.mp4",
+    "Rescued by Ruby.mp4",
+    "The Outfit.mp4",
+    "Deep Water.mp4",
+    "Cheaper by the Dozen.mp4",
+    "Windfall.mp4",
+    "X.mp4",
+    "Umma.mp4",
+    "Alice.mp4",
+    "Master.mp4",
+    "Measure of Revenge.mp4",
+    "The Lost City.mp4",
+    "Everything Everywhere All at Once.mp4"
+];
+$resources[] = $movies;
+$folders_path[] = $folders;
+foreach ($movies as $movie) {
+    $group_model->setPageResource($movie, "This is test data", $group_ids[0],
+        $page_id, "movies");
+}
+echo "Creating web_series folder and adding dummy mp4 resources\n";
+// create web_series folder in the wiki page
+$folders = $group_model->getGroupPageResourcesFolders($group_ids[0], $page_id,
+    "web_series", true);
+// add resources to the web_series subfolder
+$web_series = [
+    "Unforgotten Season 2.mp4",
+    "The Devils Hour Season 1.mp4",
+    "Indian Predator Murder in a Courtroom.mp4",
+    "If Only.mp4",
+    "Flames Season 3.mp4",
+    "Jhansi.mp4",
+    "Dubai Bling.mp4",
+    "The Mysterious Benedict Society Season 2.mp4",
+    "Sumo Do, Sumo Don't!.mp4",
+    "Star Wars Tales of the Jedi.mp4",
+    "Shadow Detective.mp4",
+    "Tripling Season 3.mp4",
+    "The Serpent Queen.mp4",
+    "The Peripheral.mp4",
+    "From Scratch.mp4",
+    "Four More Shots Please! Season 3.mp4",
+    "Barbarians Season 2.mp4",
+    "Notre-Dame.mp4",
+    "Shantaram.mp4",
+    "Mismatched Season 2.mp4",
+    "Good Bad Girl.mp4",
+    "Everything Calls for Salvation.mp4",
+    "The Watcher.mp4",
+    "The Playlist.mp4",
+    "Exception Season 1.mp4",
+    "Rookie Cops.mp4",
+    "Big Shot Season 2.mp4",
+    "Belascoarán, Pl Season 1.mp4",
+    "Werewolf by Night.mp4",
+    "The Midnight Club.mp4",
+    "Man on Pause.mp4",
+    "Derry Girls Season 3.mp4",
+    "Conversations with a Killer The Jeffrey Dahmer Tapes.mp4",
+    "Aashiqana Season 2.mp4",
+    "Shipwreck Hunters Australia.mp4"
+];
+$resources[] = $web_series;
+$folders_path[] = $folders;
+foreach ($web_series as $series) {
+    $group_model->setPageResource($series, "This is test data", $group_ids[0],
+        $page_id, "web_series");
+}
+echo "Creating books folder and adding dummy pdf resources\n";
+// create books folder in the wiki page
+$folders = $group_model->getGroupPageResourcesFolders($group_ids[0], $page_id,
+    "books", true);
+// add resources to the books subfolder
+$books = [
+    "Book Lovers.pdf",
+    "Gallant.pdf",
+    "Daughter of the Moon Goddess.pdf",
+    "Book of Night.pdf",
+    "The Girl Who Fell Beneath the Sea.pdf",
+    "I Kissed Shara Wheeler.pdf",
+    "House of Sky and Breath.pdf",
+    "A Magic Steeped in Poison.pdf",
+    "Love on the Brain.pdf",
+    "Only a Monster.pdf",
+    "This Woven Kingdom.pdf",
+    "Delilah Green Doesn't Care.pdf",
+    "Portrait of a Thief.pdf",
+    "The Paris Apartment.pdf",
+    "Foul Lady Fortune.pdf",
+    "Bloodmarked.pdf",
+    "Belladonna.pdf",
+    "Ophelia After All.pdf",
+    "Violet Made of Thorns.pdf",
+    "Carrie Soto Is Back.pdf",
+    "Hook, Line, and Sinker.pdf",
+    "The MaidThe Maid.pdf",
+    "A Far Wilder Magic.pdf",
+    "Sea of Tranquility.pdf",
+    "The Book of Cold Cases.pdf",
+    "All My Rage.pdf",
+    "Kaikeyi.pdf",
+    "How High We Go in the Dark.pdf",
+    "The Sunbearer Trials.pdf",
+    "Hotel Magnifique.pdf",
+    "What Moves the Dead.pdf",
+    "Stuck with You.pdf",
+    "Below Zero.pdf",
+    "The House Across the Lake.pdf",
+    "Nettle & Bone.pdf",
+    "A Million to One.pdf",
+    "An Arrow to the Moon.pdf",
+    "Something Wilder.pdf",
+    "Anatomy: A Love Story.pdf",
+    "The Final Gambit.pdf",
+    "The City of Dusk.pdf",
+    "The Ivory Key.pdf",
+    "So This Is Ever After.pdf",
+    "The Book Eaters.pdf",
+    "The Red Palace.pdf",
+    "The Stardust Thief.pdf",
+    "She Gets the Girl.pdf",
+    "Weather Girl.pdf",
+    "The Bone Spindle.pdf"
+];
+$resources[] = $books;
+$folders_path[] = $folders;
+foreach ($books as $book) {
+    $group_model->setPageResource($book, "This is test data", $group_ids[0],
+        $page_id, "books");
+}
+echo "Creating users impression with random resources\n";
+for ($i = 0; $i < C\ME_NUM_USERS; $i++) {
+    $user_id = $user_ids[$i];
+    for ($j = 0; $j < C\ME_NUM_RESOURCES_IMPRESSION; $j++) {
+        $resource_type = rand(0,2);
+        $resource_index = rand(0, count($resources[$resource_type]) - 1);
+        $resource_name = $resources[$resource_type][$resource_index];
+        echo "Creating RESOURCE_IMPRESSION for user $user_id with resource" .
+            " $resource_name\n";
+        $resource_id = unpack('n', md5($group_ids[0] . $page_id .
+            $folders_path[$resource_type][1] . "/" . $resource_name, true))[1];
+        $impression_model->add($user_id, $resource_id,
+            C\RESOURCE_IMPRESSION);
+    }
+}
-- 
2.37.0 (Apple Git-136)

