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