Watchlist for Wikka Pages


 

This patch is based on the code published at wakkawiki by MattPeperell and 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 {
<?
    // WATCHES
    function IsWatched($user, $tag) { return $this->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." <a href='".$this->href("watch")."'>".$unwatch."</a>" :
                $delimiter.":: <a href='".$this->href("watch")."'>".$watch."</a>");
        }
    }
    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
$p = $this->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("<br />\n<b>".$day."</b><br />\n");
            $curday = $day;
        }
        print("<span class='indent'>".$time." (".$this->Link($page["tag"], "revisions", "history", 0).") ".$this->Link($page["tag"], "", "", 0)." &rArr; ".$this->Link($page["user"], "", "", 0)." (".$page["note"].")</span><br />\n");
    }
} else print("<i>None</i><br>\n");
?>


the handler controls the watchlist and toggles page watching when called.

handlers/page/watch.php
<?php
    $tag = $this->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("<div class='page'>".$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("<p>Aditionally you can be notified by mail every time this page is changed until you put it off your watchlist.<br />");
                print("<input type='checkbox' name='mail' value='Y' /> send mail on changes<br />");
                print("<input type='submit' name='submit' value='watch' /><input type='submit' name='submit' value='cancel' />\n");
                print($this->FormClose()."</div>");
            }
        }
    } else $this->Redirect($this->href("", $tag));
?>


finally hook in the notify feature in the savepage() function by calling

<? $this->NotifyWatchers($tag); ?>


at the end of that function and place a call to

<? $this->WatchLink(); ?>


wherever you want in the header or footer.

Back Links

DreckFehler
LukaV
PeeJay
RemovingUsers
RyeBreadDraftsEventNotification
UserAccountModules



CategoryDevelopmentActions CategoryDevelopmentHandlers
There are 4 comments on this page. [Show comments]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki