From e4ddcd93e291047717843a61d5511719fbec3901 Mon Sep 17 00:00:00 2001 From: Egil Moeller Date: Fri, 12 Mar 2010 21:03:59 +0100 Subject: First attempt to saving tags to db --- trunk/etherpad/src/etherpad/pad/model.js | 36 ++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'trunk') diff --git a/trunk/etherpad/src/etherpad/pad/model.js b/trunk/etherpad/src/etherpad/pad/model.js index 9424f10..ea0d68d 100644 --- a/trunk/etherpad/src/etherpad/pad/model.js +++ b/trunk/etherpad/src/etherpad/pad/model.js @@ -258,6 +258,42 @@ function accessPadGlobal(padId, padFunc, rwMode) { delete meta2.status; sqlbase.putJSON("PAD_META", padId, meta2); + /* Update tags for the pad. Should maybe be in a separate function? */ + var new_tags = pad.text().match(new RegExp("#[^,# \t\n\r][^,# \t\n\r]*", "g")) + if (new_tags == null) new_tags = new Array(); + for (i = 0; i < new_tags.length; i++) + new_tags[i] = new_tags[i].substring(1); + var new_tags_str = new_tags.join('#') + + var old_tags_row = sqlobj.selectSingle("PAD_TAG_CACHE", { id: padId }); + var old_tags_str; + if (old_tags_row !== null) + old_tags_str = old_tags_row['TAGS']; + else + old_tags_str = ''; + + var old_tags = old_tags_str != '' ? old_tags_str.split('#') : new Array(); + + if (new_tags_str != old_tags_str) { + log.info({message: 'Updating tags', new_tags:new_tags, old_tags:old_tags}); + + if (old_tags_row) + sqlobj.update("PAD_TAG_CACHE", { id: padId }, {tags: new_tags.join('#')}); + else + sqlobj.insert("PAD_TAG_CACHE", {id: padId, tags: new_tags.join('#')}); + + sqlobj.deleteRows("PAD_TAG", {pad_id: padId}); + + for (i = 0; i < new_tags.length; i++) { + var tag_row = sqlobj.selectSingle("TAG", { name: new_tags[i] }); + if (tag_row === null) { + sqlobj.insert("TAG", {name: new_tags[i]}); + tag_row = sqlobj.selectSingle("TAG", { name: new_tags[i] }); + } + sqlobj.insert("PAD_TAG", {pad_id: padId, tag_id: tag_row['ID']}); + } + } + _getPadStringArray(padId, "revs").writeToDB(); _getPadStringArray(padId, "revs10").writeToDB(); _getPadStringArray(padId, "revs100").writeToDB(); -- cgit v1.2.3