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