=====Watchlist for Wikka Pages===== >>**See also:** UserAccountModules>>::c:: This patch is based on the code [[http://web.archive.org/web/20040823065854/http://www.wakkawiki.com/NodeWatching | published at wakkawiki]] by [[http://web.archive.org/web/20040823065833/http://www.wakkawiki.com/MattPeperell | MattPeperell]] and [[http://web.archive.org/web/20040823064948/http://www.wakkawiki.com/SilBaer | SilBaer]]. registered users can put any page on a watchlist and can optionally choose to get an email on changes. first we need a separate database table that holds the watches: %%CREATE TABLE `wikka_watches` ( `user` varchar(80) NOT NULL default '', `tag` varchar(50) NOT NULL default '', `mail` enum('N','Y') NOT NULL default 'N', `time` datetime NOT NULL default '0000-00-00 00:00:00', PRIMARY KEY (`user`, `tag`) ) TYPE=MyISAM;%% additional functions to be used when saving pages (notifywatchers()) and in the page footer... or header, of course (watchlink()) **wikka.php** inside ##class Wakka {## %%(php)LoadSingle("select * from ".$this->config["table_prefix"]."watches where user = '".mysql_real_escape_string($user)."' and tag = '".mysql_real_escape_string($tag)."'"); } function WatchLink($delimiter = "::", $unwatch = "unwatch", $watch = "watch") { if ($this->GetUser()) { echo ($this->IsWatched($this->GetUserName(), $this->tag) ? $delimiter." ".$unwatch."" : $delimiter.":: ".$watch.""); } } function NotifyWatchers($tag) { $w = $this->config["table_prefix"]."watches"; $u = $this->config["table_prefix"]."users"; $sql = "select $w.user, $u.email from $w, $u". " where $w.user = $u.name and $w.mail = 'Y'". " and $w.tag = '".mysql_real_escape_string($tag)."'"; $notify = $this->LoadAll($sql); foreach ($notify as $watcher) $watchers[$notify["user"]] = $notify["email"]; //needs group management //add chief editors, a group that always will be notified // if ($chiefeditors = $this->GetMembers($this->config["always_notify"]) { // foreach ($chiefeditors as $chiefeditor) { // if ($editor = $this->LoadSingle("select * from $u where user='".mysql_real_escape_string($chiefeditor)."'")) { // $watchers[$editor["user"]] = $editor["email"]; // } // } // } foreach ($watchers as $watcher => $email) { if ($watcher != $user && preg_match("/^.+@.+$/", $email)) { $subject = $this->config["wakka_name"].": watched page ".$tag." changed"; $message = "Hello ". $watcher."\n\n"; $message .= "The following page has been changed: ".$this->Href("",$tag)."\n\n"; $message .= "You can remove this page from your watchlist with the following link:\n\n"; $message .= $this->Href("watch", $tag)."\n\n"; $message .= "Regards\n\nYour Wikka watchdog"; $headers = "From: Wikka watchdog <".$this->config["admin_mail"].">\n"; mail($email, $subject, $message, $headers); } } } ?>%% the watchlist itself is designed as an action. every user will see another list that contains only his own watches. **actions/watches.php** %%(php)config["table_prefix"]."pages"; $w = $this->config["table_prefix"]."watches"; $sql = "select $p.tag, $w.user, $p.time, $p.user, $p.changelog from $p, $w ". "where $p.tag = $w.tag and $p.latest='Y' and $w.user='".$this->GetUserName()."' ". "order by $p.time desc"; if ($pages = $this->LoadAll($sql)) { $s = ""; $cur = ""; foreach ($pages as $page) { list($day, $time) = explode(" ", $page["time"]); if ($day != $curday) { print("
\n".$day."
\n"); $curday = $day; } print("".$time." (".$this->Link($page["tag"], "revisions", "history", 0).") ".$this->Link($page["tag"], "", "", 0)." ⇒ ".$this->Link($page["user"], "", "", 0)." (".$page["note"].")
\n"); } } else print("None
\n"); ?>%% the handler controls the watchlist and toggles page watching when called. **handlers/page/watch.php** %%(php)GetPageTag(); $mail = $_POST["mail"] == "Y" ? "Y" : "N"; if ($this->GetUser()) { $user = $this->GetUserName(); if ($this->IsWatched($user, $tag)) { return $this->Query("delete from ".$this->config["table_prefix"]."watches where user = '".mysql_real_escape_string($user)."' and tag = '".mysql_real_escape_string($tag)."'"); $this->Redirect($this->href("", $tag)); } else { if ($_POST) { if ($_POST["submit"] == "watch") { return $this->Query("insert into ".$this->config["table_prefix"]."watches set user='".mysql_real_escape_string($user)."', tag='".mysql_real_escape_string($tag)."', mail='".$mail."'"); } $this->Redirect($this->href("", $tag)); } else { print("
".$this->FormOpen("watch")); print($this->Format("You have chosen to put the page **".$tag."** onto your [[watchlist]].\n")); print("If you don't want to watch this page, hit cancel\n"); print("

Aditionally you can be notified by mail every time this page is changed until you put it off your watchlist.
"); print(" send mail on changes
"); print("\n"); print($this->FormClose()."

"); } } } else $this->Redirect($this->href("", $tag)); ?>%% finally hook in the notify feature in the savepage() function by calling %%NotifyWatchers($tag); ?>%% at the end of that function and place a call to %%WatchLink(); ?>%% wherever you want in the header or footer. Back Links ---- {{backlinks}} ---- CategoryDevelopmentActions CategoryDevelopmentHandlers