New global config setting $wgMaxTocLevel: Maximum indent level of toc.
[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 $dbw =& wfGetDB( DB_MASTER );
64 $dbw->delete( 'watchlist',
65 array(
66 'wl_user' => $this->id,
67 'wl_namespace' => $this->ns,
68 'wl_title' => $this->ti
69 ), $fname
70 );
71
72 if ( $dbw->affectedRows() ) {
73 global $wgMemc;
74 $wgMemc->set( $this->watchkey(), 0 );
75 return true;
76 } else {
77 return false;
78 }
79 }
80
81 /* static */ function duplicateEntries( $ot, $nt ) {
82 $fname = "WatchedItem::duplicateEntries";
83 global $wgMemc, $wgDBname;
84 $oldnamespace = $ot->getNamespace() & ~1;
85 $newnamespace = $nt->getNamespace() & ~1;
86 $oldtitle = $ot->getDBkey();
87 $newtitle = $nt->getDBkey();
88
89 $dbw =& wfGetDB( DB_MASTER );
90 $watchlist = $dbw->tableName( 'watchlist' );
91
92 $res = $dbw->select( 'watchlist', 'wl_user',
93 array( 'wl_namespace' => $oldnamespace, 'wl_title' => $oldtitle ),
94 $fname, 'FOR UPDATE'
95 );
96 # Construct array to replace into the watchlist
97 $values = array();
98 while ( $s = $dbw->fetchObject( $res ) ) {
99 $values[] = array(
100 'wl_user' => $s->wl_user,
101 'wl_namespace' => $newnamespace,
102 'wl_title' => $newtitle
103 );
104 }
105 $dbw->freeResult( $res );
106
107 # Perform replace
108 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
109 # some other DBMSes, mostly due to poor simulation by us
110 $dbw->replace( 'watchlist', array(array( 'wl_user', 'wl_namespace', 'wl_title')), $values, $fname );
111 return true;
112 }
113
114
115 }
116
117 ?>