Add usage echo of GroupWikiTool, tweaks eof handling WebSite, fixes redirect bug ResultsEditor activity, gets rids of caused by BotModel, a=chris

Chris Pollett [2018-05-30 21:May:th]
Add usage echo of GroupWikiTool, tweaks eof handling WebSite, fixes redirect bug ResultsEditor activity, gets rids of caused by BotModel, a=chris
Filename
src/configs/GroupWikiTool.php
src/controllers/components/CrawlComponent.php
src/library/WebSite.php
src/library/media_jobs/RecommendationJob.php
src/models/BotModel.php
src/models/SearchfiltersModel.php
src/models/UserModel.php
src/views/elements/ResultseditorElement.php
diff --git a/src/configs/GroupWikiTool.php b/src/configs/GroupWikiTool.php
index 92120551c..c57282776 100644
--- a/src/configs/GroupWikiTool.php
+++ b/src/configs/GroupWikiTool.php
@@ -28,8 +28,7 @@
  * A description of its usage is given in the $usage global variable
  *
  *
- * @author Ravi Dhillon  ravi.dhillon@yahoo.com, Chris Pollett (modified for n
- *     ngrams)
+ * @author Chris Pollett
  * @license https://www.gnu.org/licenses/ GPL3
  * @link https://www.seekquarry.com/
  * @copyright 2009 - 2018
@@ -42,6 +41,7 @@ use seekquarry\yioop\configs as C;
 use seekquarry\yioop\library as L;
 use seekquarry\yioop\library\VersionManager;
 use seekquarry\yioop\models\Model;
+use seekquarry\yioop\models\GroupModel;

 if (php_sapi_name() != 'cli' ||
     defined("seekquarry\\yioop\\configs\\IS_OWN_WEB_SERVER")) {
@@ -62,22 +62,88 @@ $usage = <<<EOD
 GroupWikiTool.php
 ==============

+GroupWikiTool is used to manage the integrity of resource folders for
+wiki pages of Yioop Groups. The tool has a command to look up what is
+the path for a wiki page of a given group for a locale. To maintain
+previous version of wiki page resources, Yioop writes a .archive folder
+in the wiki page's resource folder and uses it to maintain
+previous versions of this folder. Before changes to the .archive
+folder are made, a LOCK file is written. In the event of a crash before
+completion of an operation, this LOCK file might be present and
+prevent further changes to the resources in a wiki. This tool let's one
+clear this lock. It also allows one to remove the existing .archive folder
+and rebuild it from scratch. Finally, it allows a user to save a new version
+snapshot of the current resource folder.
+
 Usage
 =====
+php GroupWikiTool.php command folder

+php GroupWikiTool.php clear-lock folder
+  if folder is the name of a Group Wiki page resource folder, then this
+  operation will remove any LOCK file on the .archive folder

-php GroupWikiTool.php command folder
+php GroupWikiTool.php path group_name page_name locale_tag
+  returns the resource and thumb folders for the given group, page,
+  and locale.
+
+php GroupWikiTool.php reset folder
+  if folder is the name of a Group Wiki page
+  resource folder, then this will delete the current .archive folder and replace
+  it with a freshly computed one

+php GroupWikiTool.php version folder
+  if folder is the name of a Group Wiki page resource folder, then this
+  will save a save a new version snapshot to the .archive subfolder
 EOD;
 if (empty($argv[2])) {
     $argv[2] = getcwd();
 }
 $num_args = count($argv);
-if ( $num_args != 3 ) {
+if ( $num_args < 3 ) {
     echo $usage;
     exit();
 }
 switch ($argv[1]) {
+    case "clear-lock":
+        $lock_file = $argv[2] ."/.archive/LOCK";
+        if (file_exists($argv[2])) {
+            unlink($lock_file);
+            echo "Group Wiki Page Resource Lock file removed!";
+        }
+        break;
+    case "path":
+        if (empty($argv[4])) {
+            $argv[4] = C\DEFAULT_LOCALE;
+        }
+        if (empty($argv[3])) {
+            $argv[3] = "Main";
+        }
+        if (empty($argv[2])) {
+            $argv[2] = "Public";
+        }
+        $group_model = new GroupModel();
+        $group_id = $group_model->getGroupId($argv[2]);
+        if (!$group_id) {
+            echo "Could not locate that group name!!\n\n";
+            echo $usage;
+            exit();
+        }
+        $page_id = $group_model->getPageId($group_id, $argv[3], $argv[4]);
+        if (!$page_id) {
+            echo "Could not locate that page name in {$argv[2]} wiki!!\n\n";
+            echo $usage;
+            exit();
+        }
+        $folders =
+            $group_model->getGroupPageResourcesFolders($group_id, $page_id);
+        if (empty($folders[1])) {
+            echo "$argv[3] page folders not yet created!!\n\n";
+            echo $usage;
+            exit();
+        }
+        echo "Resource folder: {$folders[0]}\nThumb folder: {$folders[1]}\n";
+        break;
     case "reset":
         if (file_exists($argv[2] . "/.archive")) {
             $model = new Model();
@@ -86,19 +152,10 @@ switch ($argv[1]) {
         }
         $vcs = new VersionManager($argv[2]);
         break;
-    case "info":
-        break;
     case "version":
         $vcs = new VersionManager($argv[2]);
         $vcs->createVersion();
         break;
-    case "clear-lock":
-        $lock_file = $argv[2] ."/.archive/LOCK";
-        if (file_exists($argv[2])) {
-            unlink($lock_file);
-            echo "Group Wiki Page Resource Lock file removed!";
-        }
-        break;
     default:
         echo $usage;
         exit();
diff --git a/src/controllers/components/CrawlComponent.php b/src/controllers/components/CrawlComponent.php
index 9b26b2d29..faecd6d31 100644
--- a/src/controllers/components/CrawlComponent.php
+++ b/src/controllers/components/CrawlComponent.php
@@ -1682,7 +1682,6 @@ class CrawlComponent extends Component implements CrawlConstants
         $filters_model = $parent->model("searchfilters");
         $data["ELEMENT"] = "resultseditor";
         $data['SCRIPT'] = "";
-
         if (isset($_REQUEST['disallowed_sites'])) {
             $sites = $parent->convertStringCleanArray(
                 $_REQUEST['disallowed_sites']);
diff --git a/src/library/WebSite.php b/src/library/WebSite.php
index fe79161bd..3b5452a24 100644
--- a/src/library/WebSite.php
+++ b/src/library/WebSite.php
@@ -1315,12 +1315,7 @@ class WebSite
     protected function processResponseStreams($out_streams_with_data)
     {
         foreach ($out_streams_with_data as $out_stream) {
-            $meta = stream_get_meta_data($out_stream);
             $key = (int)$out_stream;
-            if ($meta['eof']) {
-                $this->shutdownHttpStream($key);
-                continue;
-            }
             $data = $this->out_streams[self::DATA][$key];
             $num_bytes = fwrite($out_stream, $data);
             $remaining_bytes = max(0, strlen($data) - $num_bytes);
@@ -1594,12 +1589,15 @@ class WebSite
             if (in_array($key, $this->immortal_stream_keys)) {
                 continue;
             }
+            $meta = stream_get_meta_data(
+                $this->in_streams[self::CONNECTION][$key]);
             $in_time = empty($this->in_streams[self::MODIFIED_TIME][$key]) ?
                 0 : $this->in_streams[self::MODIFIED_TIME][$key];
             $out_time = empty($this->out_streams[self::MODIFIED_TIME][$key]) ?
                 0 : $this->out_streams[self::MODIFIED_TIME][$key];
             $modified_time = max($in_time, $out_time);
-            if (time() - $modified_time > $this->default_server_globals[
+            if ($meta['eof'] ||
+                time() - $modified_time > $this->default_server_globals[
                 'CONNECTION_TIMEOUT']) {
                 $this->shutdownHttpStream($key);
             }
diff --git a/src/library/media_jobs/RecommendationJob.php b/src/library/media_jobs/RecommendationJob.php
index 5ccf7ff89..3426f231e 100644
--- a/src/library/media_jobs/RecommendationJob.php
+++ b/src/library/media_jobs/RecommendationJob.php
@@ -327,7 +327,7 @@ class RecommendationJob extends MediaJob
             "COUNT(*) AS FREQUENCY, IWF.TERM_ID AS TID ".
             "FROM ITEM_TERM_FREQUENCY IWF, ITEM_IMPRESSION II ".
             "WHERE IWF.ITEM_ID = II.ITEM_ID ".
-            "GROUP BY USER_ID,TERM_ID";
+            "GROUP BY USER_ID, TERM_ID";
         $results = $db->execute($sql);
         $base_insert_sql = "INSERT INTO USER_TERM_FREQUENCY VALUES ";
         $insert_sql = $base_insert_sql;
diff --git a/src/models/BotModel.php b/src/models/BotModel.php
index 21c5ac255..4346ad298 100644
--- a/src/models/BotModel.php
+++ b/src/models/BotModel.php
@@ -73,7 +73,7 @@ class BotModel extends Model
     {
         $db = $this->db;
         $sql = "SELECT COUNT(*) AS NUM FROM CHAT_BOT_PATTERN WHERE USER_ID = ?";
-        $result = $db->execute($sql, [$pattern_id]);
+        $result = $db->execute($sql, [$user_id]);
         if (!$result) {
             return 0;
         }
diff --git a/src/models/SearchfiltersModel.php b/src/models/SearchfiltersModel.php
index 4c873effd..00bf20e42 100644
--- a/src/models/SearchfiltersModel.php
+++ b/src/models/SearchfiltersModel.php
@@ -31,6 +31,7 @@
 namespace seekquarry\yioop\models;

 use seekquarry\yioop\configs as C;
+use seekquarry\yioop\library as L;
 use seekquarry\yioop\library\CrawlConstants;

 /**
@@ -108,7 +109,7 @@ class SearchfiltersModel extends Model
         file_put_contents($this->dir_name."/urls.txt", serialize($urls));
         $hash_urls = [];
         foreach ($urls as $url) {
-            $hash_urls[] = substr(crawlHash($url, true), 1);
+            $hash_urls[] = substr(L\crawlHash($url, true), 1);
         }
         $hash_urls['time'] = time();
         file_put_contents($this->dir_name."/hash_urls.txt",
@@ -132,7 +133,7 @@ class SearchfiltersModel extends Model
         if (file_exists($file_name)) {
             $result_pages = unserialize(file_get_contents($file_name));
         }
-        $hash_url = crawlHash($url, true);
+        $hash_url = L\crawlHash($url, true);
         if ($title == "" && $description == "") {
             unset($result_pages[$hash_url]);
         } else {
diff --git a/src/models/UserModel.php b/src/models/UserModel.php
index b6f7d5150..f58a39a49 100755
--- a/src/models/UserModel.php
+++ b/src/models/UserModel.php
@@ -540,6 +540,10 @@ class UserModel extends Model
             return;
         }
         if ($is_new_bot) {
+            $user['BOT_TOKEN'] = (empty($user['BOT_TOKEN'])) ? "" :
+                $user['BOT_TOKEN'];
+            $user['CALLBACK_URL'] = (empty($user['CALLBACK_URL'])) ? "" :
+                $user['CALLBACK_URL'];
             $sql = "INSERT INTO CHAT_BOT(USER_ID, BOT_TOKEN,
                 CALLBACK_URL) VALUES (?, ?, ?)";
             $result = $db->execute($sql, [$user['USER_ID'], $user['BOT_TOKEN'],
diff --git a/src/views/elements/ResultseditorElement.php b/src/views/elements/ResultseditorElement.php
index e63dfb57f..e9a4a7a44 100644
--- a/src/views/elements/ResultseditorElement.php
+++ b/src/views/elements/ResultseditorElement.php
@@ -103,9 +103,9 @@ class ResultsEditorElement extends Element
         <h2><?= tl('resultseditor_element_filter_websites')?>
          <?=$this->view->helper("helpbutton")->render(
             "Filtering Search Results", $data[C\CSRF_TOKEN]) ?></h2>
-        <form id="searchfiltersForm" method="post" action='?'>
+        <form id="searchfiltersForm" method="post" >
         <input type="hidden" name="c" value="admin" />
-        <input type="hidden" name="<?= C\CSRF_TOKEN ?>" value="<?
+        <input type="hidden" name="<?= C\CSRF_TOKEN ?>" value="<?=
             $data[C\CSRF_TOKEN] ?>" />
         <input type="hidden" name="a" value="resultsEditor" />
         <input type="hidden" name="arg" value="urlfilter" />
ViewGit