Fixed bug potentially causing problems if memcached fails for some reason (that is...
[lhc/web/wiklou.git] / includes / WatchedItem.php
1 <?
2
3 class WatchedItem {
4
5 /* static */ function &fromUserTitle( &$user, &$title ) {
6 $wl = new WatchedItem;
7 $wl->mUser =& $user;
8 $wl->mTitle =& $title;
9 $wl->id = $user->getId();
10 $wl->ns = $title->getNamespace() & ~1;
11 $wl->ti = $title->getDBkey();
12 $wl->eti = wfStrencode( $wl->ti );
13 return $wl;
14 }
15
16 function watchKey() {
17 global $wgDBname;
18 return "$wgDBname:watchlist:user:$this->id:page:$this->ns:$this->ti";
19 }
20
21 function isWatched()
22 {
23 # Pages and their talk pages are considered equivalent for watching;
24 # remember that talk namespaces are numbered as page namespace+1.
25 global $wgMemc;
26 $key = $this->watchKey();
27 $iswatched = $wgMemc->get( $key );
28 if( is_integer( $iswatched ) ) return $iswatched;
29
30 $sql = "SELECT 1 FROM watchlist WHERE wl_user=$this->id AND wl_namespace=$this->ns AND wl_title='$this->eti'";
31 $res = wfQuery( $sql, DB_READ );
32 $iswatched = (wfNumRows( $res ) > 0) ? 1 : 0;
33 $wgMemc->set( $key, $iswatched );
34 return $iswatched;
35 }
36
37 function addWatch()
38 {
39 # REPLACE instead of INSERT because occasionally someone
40 # accidentally reloads a watch-add operation.
41 $sql = "REPLACE INTO watchlist (wl_user, wl_namespace,wl_title) VALUES ($this->id,$this->ns,'$this->eti')";
42 $res = wfQuery( $sql, DB_WRITE );
43 if( $res === false ) return false;
44
45 global $wgMemc;
46 $wgMemc->set( $this->watchkey(), 1 );
47 return true;
48 }
49
50 function removeWatch()
51 {
52 $sql = "DELETE FROM watchlist WHERE wl_user=$this->id AND wl_namespace=$this->ns AND wl_title='$this->eti' LIMIT 1";
53 $res = wfQuery( $sql, DB_WRITE );
54 if( $res === false ) return false;
55
56 global $wgMemc;
57 $wgMemc->set( $this->watchkey(), 0 );
58 return true;
59 }
60
61 /* static */ function duplicateEntries( $ot, $nt ) {
62 global $wgMemc, $wgDBname;
63 $oldnamespace = $ot->getNamespace() & ~1;
64 $newnamespace = $nt->getNamespace() & ~1;
65 $oldtitle = $ot->getDBkey();
66 $newtitle = $nt->getDBkey();
67 $eoldtitle = wfStrencode( $oldtitle );
68 $enewtitle = wfStrencode( $newtitle );
69
70 $sql = "SELECT wl_user FROM watchlist
71 WHERE wl_namespace={$oldnamespace} AND wl_title='{$eoldtitle}'";
72 $res = wfQuery( $sql, DB_READ, $fname );
73 if( $s = wfFetchObject( $res ) ) {
74 $sql = "REPLACE INTO watchlist (wl_user,wl_namespace,wl_title)
75 VALUES ({$s->wl_user},{$newnamespace},'{$enewtitle}')";
76 $key = "$wgDBname:watchlist:user:$s->wl_user:page:$newnamespace:$newtitle";
77 $wgMemc->set( $key, 1 );
78 while( $s = wfFetchObject( $res ) ) {
79 $sql .= ",({$s->wl_user},{$newnamespace},'{$enewtitle}')";
80 $key = "$wgDBname:watchlist:user:$s->wl_user:page:$newnamespace:$newtitle";
81 $wgMemc->set( $key, 1 );
82 }
83 $res = wfQuery( $sql, DB_WRITE, $fname );
84 if( $res === false ) return false; # db error?
85 }
86 return true;
87 }
88
89
90 }
91
92 ?>