summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2009-08-08 14:30:21 -0700
committerBharat Mediratta <bharat@menalto.com>2009-08-08 14:30:21 -0700
commit7863aa16f9304e3c457898f22bce035bf4fedfd8 (patch)
treed05aa44709fa2c75137892dc937339d9623be327 /modules
parente4eec71efa5f7b1902155a34f8655cebe523c358 (diff)
Update tags module to notify modules when items related to a tag are
affected. Practically speaking this means that we'll reindex items when tags are added or removed from them. API change: Remove item_related_updated_batch event. Rationale: While this is an efficient event, it requires module developers to support two event APIs for staying up to date and increases the likelihood that they'll forget one and have data corruption. Force them all through the slower but more reliable pipe, for now. We can always try to improve efficiency by using the batch_start and batch_stop events.
Diffstat (limited to 'modules')
-rw-r--r--modules/comment/helpers/comment_installer.php13
-rw-r--r--modules/search/helpers/search_event.php5
-rw-r--r--modules/tag/models/tag.php48
3 files changed, 58 insertions, 8 deletions
diff --git a/modules/comment/helpers/comment_installer.php b/modules/comment/helpers/comment_installer.php
index f54913c3..edf2427c 100644
--- a/modules/comment/helpers/comment_installer.php
+++ b/modules/comment/helpers/comment_installer.php
@@ -52,8 +52,8 @@ class comment_installer {
}
static function upgrade($version) {
+ $db = Database::instance();
if ($version == 1) {
- $db = Database::instance();
$db->query("ALTER TABLE {comments} CHANGE `state` `state` varchar(15) default 'unpublished'");
module::set_version("comment", 2);
}
@@ -61,9 +61,16 @@ class comment_installer {
static function uninstall() {
$db = Database::instance();
- $sql = "SELECT `item_id` FROM {comments}";
- module::event("item_related_update_batch", $sql);
+ // Notify listeners that we're deleting some data. This is probably going to be very
+ // inefficient for large uninstalls, and we could make it better by doing things like passing
+ // a SQL fragment through so that the listeners could use subselects. But by using a single,
+ // simple event API we lighten the load on module developers.
+ foreach (ORM::factory("item")
+ ->join("comments", "items.id", "comments.item_id")
+ ->find_all() as $item) {
+ module::event("item_related_update", $item);
+ }
$db->query("DROP TABLE IF EXISTS {comments};");
}
}
diff --git a/modules/search/helpers/search_event.php b/modules/search/helpers/search_event.php
index b65763af..836bbe15 100644
--- a/modules/search/helpers/search_event.php
+++ b/modules/search/helpers/search_event.php
@@ -35,9 +35,4 @@ class search_event_Core {
static function item_related_update($item) {
search::update($item);
}
-
- static function item_related_update_batch($sql) {
- $db = Database::instance();
- $db->query("UPDATE {search_records} SET `dirty` = 1 WHERE item_id IN ($sql)");
- }
}
diff --git a/modules/tag/models/tag.php b/modules/tag/models/tag.php
index e910a8ee..d9488e1c 100644
--- a/modules/tag/models/tag.php
+++ b/modules/tag/models/tag.php
@@ -54,4 +54,52 @@ class Tag_Model extends ORM {
}
return $model->count_all();
}
+
+ /**
+ * Overload ORM::save() to trigger an item_related_update event for all items that are related
+ * to this tag. Since items can be added or removed as part of the save, we need to trigger an
+ * event for the union of all related items before and after the save.
+ */
+ public function save() {
+ $db = Database::instance();
+ $related_item_ids = array();
+ foreach ($db->getwhere("items_tags", array("tag_id" => $this->id)) as $row) {
+ $related_item_ids[$row->item_id] = 1;
+ }
+
+ $result = parent::save();
+
+ foreach ($db->getwhere("items_tags", array("tag_id" => $this->id)) as $row) {
+ $related_item_ids[$row->item_id] = 1;
+ }
+
+ if ($related_item_ids) {
+ foreach (ORM::factory("item")->in("id", array_keys($related_item_ids))->find_all() as $item) {
+ module::event("item_related_update", $item);
+ }
+ }
+
+ return $result;
+ }
+
+ /**
+ * Overload ORM::delete() to trigger an item_related_update event for all items that are
+ * related to this tag.
+ */
+ public function delete() {
+ $related_item_ids = array();
+ $db = Database::Instance();
+ foreach ($db->getwhere("items_tags", array("tag_id" => $this->id)) as $row) {
+ $related_item_ids[$row->item_id] = 1;
+ }
+
+ $result = parent::delete();
+
+ if ($related_item_ids) {
+ foreach (ORM::factory("item")->in("id", array_keys($related_item_ids))->find_all() as $item) {
+ module::event("item_related_update", $item);
+ }
+ }
+ return $result;
+ }
} \ No newline at end of file