[Pkg-mozext-commits] [wot] 84/226: Added parsing of update.xml (categories and groups)

David Prévot taffit at moszumanska.debian.org
Fri May 1 00:35:37 UTC 2015


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository wot.

commit 3adb5381997d4b7946ca78faa46458201d69c959
Author: Sergey Andryukhin <sorgoz at yandex.com>
Date:   Tue Jul 23 14:23:56 2013 +0300

    Added parsing of update.xml (categories and groups)
---
 content/api.js        |  36 ++++--------
 content/categories.js | 158 ++++++++++++++++++++++++++++++++++++++++++++++++++
 content/config.js     |   3 +-
 content/core.js       |   8 ++-
 content/overlay.xul   |   1 +
 content/util.js       |  20 +++++++
 6 files changed, 199 insertions(+), 27 deletions(-)

diff --git a/content/api.js b/content/api.js
index c330d84..828b487 100644
--- a/content/api.js
+++ b/content/api.js
@@ -925,16 +925,10 @@ var wot_api_update =
 			}
 
 			var request = event.target;
-
-			if (!request || request.status != 200) {
-				return;
-			}
+			if (!request || request.status != 200) return;
 
 			var response = request.responseXML;
-
-			if (!response) {
-				return;
-			}
+			if (!response) return;
 
 			/* Update the the last check time */
 			wot_prefs.setChar("update_checked", Date.now());
@@ -946,33 +940,27 @@ var wot_api_update =
 				update = tags.item(0);
 			}
 
-			if (!update) {
-				return;
-			}
+			if (!update) return;
 
 			/* Attributes */
-			var interval =
-				update.getAttribute(WOT_SERVICE_XML_UPDATE_INTERVAL);
+			var interval = update.getAttribute(WOT_SERVICE_XML_UPDATE_INTERVAL);
 
 			if (interval && Number(interval) > 0) {
 				wot_prefs.setInt("update_interval", interval * 1000);
 			}
 
-			/* Search rules */
-			var search = response.getElementsByTagName(
-							WOT_SERVICE_XML_UPDATE_SEARCH);
+            /* Categories */
+            var cats = response.getElementsByTagName(WOT_SERVICE_XML_UPDATE_CATEGORIES);
+            if (cats && cats[0]) wot_categories.parse(cats[0]);
 
-			if (search) {
-				wot_search.parse(search);
-			}
+			/* Search rules */
+			var search = response.getElementsByTagName(WOT_SERVICE_XML_UPDATE_SEARCH);
+			if (search) wot_search.parse(search);
 
 			/* Shared domains */
-			var shared = response.getElementsByTagName(
-							WOT_SERVICE_XML_UPDATE_SHARED);
+			var shared = response.getElementsByTagName(WOT_SERVICE_XML_UPDATE_SHARED);
+			if (shared) wot_shared.parse(shared);
 
-			if (shared) {
-				wot_shared.parse(shared);
-			}
 
 			wot_prefs.flush();
 		} catch (e) {
diff --git a/content/categories.js b/content/categories.js
new file mode 100644
index 0000000..2e79fd4
--- /dev/null
+++ b/content/categories.js
@@ -0,0 +1,158 @@
+/*
+ categories.js
+ Copyright © 2013  WOT Services Oy <info at mywot.com>
+
+ This file is part of WOT.
+
+ WOT is free software: you can redistribute it and/or modify it
+ under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ WOT is distributed in the hope that it will be useful, but WITHOUT
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+ License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with WOT. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+var wot_categories = {
+
+    PREF_CATEGORY: "category",
+    PREF_GROUPINGS: "groupings",
+    category_threshold: 3,  // confidence level to show a category as identified
+    inited: false,
+    loading: false,
+    categories: {},
+    grouping: [],   // Groupings for building category selector in Rating Window. Loaded from API server/update.xml.
+    cgroups: {},    // Categories' groups and their mapping to colors and TR/CS.
+
+    load_delayed: function () {
+        if (this.inited) return;
+
+        this.init_categories();
+        this.pbi = wot_prefs.pref.QueryInterface(Components.interfaces.nsIPrefBranch2);
+        this.pbi.addObserver(WOT_PREF + this.PREF_CATEGORY, this, false);
+        this.inited = true;
+    },
+
+    unload: function () {
+        try {
+            if (this.pbi) {
+                this.pbi.removeObserver(WOT_PREF + this.PREF_CATEGORY, this);
+                this.pbi = null;
+            }
+        } catch (e) {
+            wdump("wot_categories.unload: failed with " + e);
+        }
+    },
+
+    observe: function (subject, topic, state) {
+        // see load_delayed(). This function is used as a listener to
+        try {
+            if (!this.loading && topic == "nsPref:changed") {
+                this.init_categories();
+            }
+        } catch (e) {
+            wdump("wot_search.observe: failed with " + e);
+        }
+    },
+
+    parse: function (categories_node) {
+//        wdump("INFO: parse() categories");
+        // process xml dom here and store to prefs
+        this.loading = true;
+        var i, j, gs_obj, cat_obj, res_grouping = [];
+
+        var groupings = categories_node.getElementsByTagName("grouping");
+        wot_prefs.deleteBranch(this.PREF_GROUPINGS + ".");
+        if (groupings) {
+            for (j = 0; j < groupings.length; j++) {
+                gs_obj = wot_util.copy_attrs(groupings[j]);
+
+                // convert some attrs to numbers
+                gs_obj.tmax = gs_obj.tmax !== null ? parseInt(gs_obj.tmax) : null;
+                gs_obj.tmin = gs_obj.tmin !== null ? parseInt(gs_obj.tmin) : null;
+
+                var groups_node = groupings[j].getElementsByTagName("group");
+                if (groups_node) {
+
+                    gs_obj.groups = [];
+
+                    for (i = 0; i < groups_node.length; i++) {
+                        var grp_obj = wot_util.copy_attrs(groups_node[i]);
+                        this.cgroups[grp_obj.name] = { type: grp_obj.type };
+                        gs_obj.groups.push(grp_obj);
+                    }
+                }
+
+                res_grouping.push(gs_obj);
+            }
+            wot_prefs.setChar(this.PREF_GROUPINGS + ".all", JSON.stringify(res_grouping));
+        }
+
+        // remove all categories from prefs
+        wot_prefs.deleteBranch(this.PREF_CATEGORY + ".");
+
+        // Iterate through <category> tags
+        var categories = categories_node.getElementsByTagName("category");
+        for (i = 0; i < categories.length; i++) {
+            cat_obj = wot_util.copy_attrs(categories[i]);
+            if (isNaN(cat_obj.name) || cat_obj.text == null || cat_obj.text.length == 0) {
+                wdump("WARN: wot_categories.parse(): empty malformed category is found. Skipped.");
+                continue;
+            }
+
+            cat_obj.id = parseInt(cat_obj.name);
+            cat_obj.cs = (cat_obj.application == "4");               // set ChildSafety flag
+            cat_obj.type = this.cgroups[cat_obj.group].type; // set type of the category based on parent group
+
+//            this.categories[cat_obj.id] = cat_obj;
+
+            wot_prefs.setChar(this.PREF_CATEGORY + "." + cat_obj.name, JSON.stringify(cat_obj));
+        }
+
+        this.init_categories();
+
+        this.loading = false;
+    },
+
+    init_categories: function () {
+//        wdump("INFO: init_categories()");
+        /* Reads categories info from local preferences */
+
+        try {
+            var branch = wot_prefs.ps.getBranch(WOT_PREF + this.PREF_CATEGORY + ".");
+            var children = branch.getChildList("", {});
+
+            this.categories = {};   // clear categories in memory
+
+            for (var i = 0; i < children.length; i++) {
+                try {
+                    var cat_id = children[i];
+                    var cat_json = wot_prefs.getChar(this.PREF_CATEGORY + "." + cat_id, "{}");
+                    var cat = JSON.parse(cat_json);
+                    if (!wot_util.isEmpty(cat)) this.categories[cat_id] = cat;
+                } catch (e) {
+                    wdump("ERROR: exception in wot_categories.init_categories()" + e);
+                    wdump("ERROR: cat id:" + cat_id);
+                    wdump("ERROR: problematic string: " + cat_json);
+                    continue;
+                }
+            }
+
+            var groupings_json = wot_prefs.getChar(this.PREF_GROUPINGS + ".all", "{}");
+            this.grouping = JSON.parse(groupings_json);
+
+            this.inited = true;
+
+        } catch (e) {
+            wdump("wot_search.init_categories(): failed with " + e);
+        }
+    }
+
+};
+
+wot_modules.push({ name: "wot_categories", obj: wot_categories });
\ No newline at end of file
diff --git a/content/config.js b/content/config.js
index 85e0e01..d5bc2e4 100644
--- a/content/config.js
+++ b/content/config.js
@@ -137,6 +137,7 @@ const WOT_SERVICE_XML_UPDATE_SEARCH_NAME		= "name";
 const WOT_SERVICE_XML_UPDATE_SHARED				= "shared";
 const WOT_SERVICE_XML_UPDATE_SHARED_DOMAINS		= "domains";
 const WOT_SERVICE_XML_UPDATE_SHARED_LEVEL		= "level";
+const WOT_SERVICE_XML_UPDATE_CATEGORIES			= "categories";
 
 /* My */
 //const WOT_MY_URL = "http://www.mywot.com/";
@@ -285,7 +286,7 @@ const wot_prefs_int = [
 	[ "warning_type_1",				WOT_WARNING_DOM ],
 	[ "warning_type_2",				WOT_WARNING_DOM ],
 	[ "warning_type_3",				WOT_WARNING_NONE ],
-	[ "warning_type_4",				WOT_WARNING_NONE ],
+	[ "warning_type_4",				WOT_WARNING_NONE ]
 //	[ "warning_type_5",				WOT_WARNING_NONE ]
 ];
 
diff --git a/content/core.js b/content/core.js
index e183677..a21e4bf 100644
--- a/content/core.js
+++ b/content/core.js
@@ -633,8 +633,12 @@ var wot_core =
 			/* Submit any pending testimonies */
 			wot_pending.submit();
 
-			/* Check for updates */
-			wot_api_update.send(false);
+			/* Check for updates (force update if no categories are loaded yet) */
+            var forced_update = wot_util.isEmpty(wot_categories.categories) ||
+                !wot_categories.grouping ||
+                wot_categories.grouping.length == 0;
+
+			wot_api_update.send(forced_update);
 
 			if (!wot_core.hostname || wot_url.isprivate(wot_core.hostname) ||
 					wot_url.isexcluded(wot_core.hostname)) {
diff --git a/content/overlay.xul b/content/overlay.xul
index 0b147b4..56ae801 100644
--- a/content/overlay.xul
+++ b/content/overlay.xul
@@ -26,6 +26,7 @@
 		xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
 
 	<script type="application/x-javascript" src="chrome://wot/content/config.js"/>
+	<script type="application/x-javascript" src="chrome://wot/content/categories.js"/>
 	<script type="application/x-javascript" src="chrome://wot/content/cache.js"/>
 	<script type="application/x-javascript" src="chrome://wot/content/commands.js"/>
 	<script type="application/x-javascript" src="chrome://wot/content/shared.js"/>
diff --git a/content/util.js b/content/util.js
index 333037d..9c1d52c 100644
--- a/content/util.js
+++ b/content/util.js
@@ -39,6 +39,13 @@ var wot_util =
 		return true;
 	},
 
+    isEmpty: function (obj) {
+        for (var name in obj) {
+            return false;
+        }
+        return true;
+    },
+
 	getstring: function(str, arr)
 	{
 		try {
@@ -57,6 +64,19 @@ var wot_util =
 		return null;
 	},
 
+    copy_attrs: function (node) {
+        var obj = {};
+        if (node) {
+            for (var a in node.attributes) {
+                var attr = node.attributes[a];
+                obj[attr.name] = attr.value;
+            }
+        } else {
+            wdump("wot_utils.copy_attrs() - empty node is provided");
+        }
+        return obj;
+    },
+
 	time_sincefirstrun: function()
 	{
 		try {

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/wot.git



More information about the Pkg-mozext-commits mailing list