Record and report memory usage change in profiling.
[lhc/web/wiklou.git] / includes / WatchedItem.php
1 <?php
2
3 class WatchedItem {
4 var $mTitle, $mUser;
5
6 # Create a WatchedItem object with the given user and title
7 /* static */ function &fromUserTitle( &$user, &$title ) {
8 $wl = new WatchedItem;
9 $wl->mUser =& $user;
10 $wl->mTitle =& $title;
11 $wl->id = $user->getId();
12 $wl->ns = $title->getNamespace() & ~1;
13 $wl->ti = $title->getDBkey();
14 return $wl;
15 }
16
17 # Returns the memcached key for this item
18 function watchKey() {
19 global $wgDBname;
20 return "$wgDBname:watchlist:user:$this->id:page:$this->ns:$this->ti";
21 }
22
23 # Is mTitle being watched by mUser?
24 function isWatched()
25 {
26 # Pages and their talk pages are considered equivalent for watching;
27 # remember that talk namespaces are numbered as page namespace+1.
28 global $wgMemc;
29 $fname = 'WatchedItem::isWatched';
30
31 $key = $this->watchKey();
32 $iswatched = $wgMemc->get( $key );
33 if( is_integer( $iswatched ) ) return $iswatched;
34
35 $dbr =& wfGetDB( DB_SLAVE );
36 $res = $dbr->select( 'watchlist', 1, array( 'wl_user' => $this->id, 'wl_namespace' => $this->ns,
37 'wl_title' => $this->ti ), $fname );
38 $iswatched = ($dbr->numRows( $res ) > 0) ? 1 : 0;
39 $wgMemc->set( $key, $iswatched );
40 return $iswatched;
41 }
42
43 function addWatch()
44 {
45 $fname = "WatchedItem::addWatch";
46 # REPLACE instead of INSERT because occasionally someone
47 # accidentally reloads a watch-add operation.
48 $dbw =& wfGetDB( DB_MASTER );
49 $dbw->replace( 'watchlist', array(array('wl_user', 'wl_namespace', 'wl_title')),
50 array(
51 'wl_user' => $this->id,
52 'wl_namespace' => $this->ns,
53 'wl_title' => $this->ti,
54 ), $fname );
55
56 global $wgMemc;
57 $wgMemc->set( $this->watchkey(), 1 );
58 return true;
59 }
60
61 function removeWatch()
62 {
63 $fname = 'WatchedItem::removeWatch';
64
65 $dbw =& wfGetDB( DB_MASTER );
66 $dbw->delete( 'watchlist',
67 array(
68 'wl_user' => $this->id,
69 'wl_namespace' => $this->ns,
70 'wl_title' => $this->ti
71 ), $fname
72 );
73
74 if ( $dbw->affectedRows() ) {
75 global $wgMemc;
76 $wgMemc->set( $this->watchkey(), 0 );
77 return true;
78 } else {
79 return false;
80 }
81 }
82
83 /* static */ function duplicateEntries( $ot, $nt ) {
84 $fname = "WatchedItem::duplicateEntries";
85 global $wgMemc, $wgDBname;
86 $oldnamespace = $ot->getNamespace() & ~1;
87 $newnamespace = $nt->getNamespace() & ~1;
88 $oldtitle = $ot->getDBkey();
89 $newtitle = $nt->getDBkey();
90
91 $dbw =& wfGetDB( DB_MASTER );
92 $watchlist = $dbw->tableName( 'watchlist' );
93
94 $res = $dbw->select( 'watchlist', 'wl_user',
95 array( 'wl_namespace' => $oldnamespace, 'wl_title' => $oldtitle ),
96 $fname, 'FOR UPDATE'
97 );
98 # Construct array to replace into the watchlist
99 $values = array();
100 while ( $s = $dbw->fetchObject( $res ) ) {
101 $values[] = array(
102 'wl_user' => $s->wl_user,
103 'wl_namespace' => $newnamespace,
104 'wl_title' => $newtitle
105 );
106 }
107 $dbw->freeResult( $res );
108
109 # Perform replace
110 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
111 # some other DBMSes, mostly due to poor simulation by us
112 $dbw->replace( 'watchlist', array(array( 'wl_user', 'wl_namespace', 'wl_title')), $values, $fname );
113 return true;
114 }
115
116
117 }
118
119 ?>