2004-02-18 02:15:00 +00:00
|
|
|
<?php
|
2003-11-09 11:45:12 +00:00
|
|
|
|
|
|
|
|
class WatchedItem {
|
2004-07-18 08:48:43 +00:00
|
|
|
var $mTitle, $mUser;
|
2003-11-09 11:45:12 +00:00
|
|
|
|
2004-07-18 08:48:43 +00:00
|
|
|
# Create a WatchedItem object with the given user and title
|
2003-11-09 11:45:12 +00:00
|
|
|
/* static */ function &fromUserTitle( &$user, &$title ) {
|
|
|
|
|
$wl = new WatchedItem;
|
|
|
|
|
$wl->mUser =& $user;
|
|
|
|
|
$wl->mTitle =& $title;
|
|
|
|
|
$wl->id = $user->getId();
|
|
|
|
|
$wl->ns = $title->getNamespace() & ~1;
|
|
|
|
|
$wl->ti = $title->getDBkey();
|
|
|
|
|
return $wl;
|
|
|
|
|
}
|
|
|
|
|
|
2004-07-18 08:48:43 +00:00
|
|
|
# Returns the memcached key for this item
|
2003-11-09 11:45:12 +00:00
|
|
|
function watchKey() {
|
|
|
|
|
global $wgDBname;
|
|
|
|
|
return "$wgDBname:watchlist:user:$this->id:page:$this->ns:$this->ti";
|
|
|
|
|
}
|
|
|
|
|
|
2004-07-18 08:48:43 +00:00
|
|
|
# Is mTitle being watched by mUser?
|
2003-11-09 11:45:12 +00:00
|
|
|
function isWatched()
|
|
|
|
|
{
|
|
|
|
|
# Pages and their talk pages are considered equivalent for watching;
|
|
|
|
|
# remember that talk namespaces are numbered as page namespace+1.
|
|
|
|
|
global $wgMemc;
|
2004-07-18 08:48:43 +00:00
|
|
|
$fname = 'WatchedItem::isWatched';
|
|
|
|
|
|
2003-11-09 11:45:12 +00:00
|
|
|
$key = $this->watchKey();
|
|
|
|
|
$iswatched = $wgMemc->get( $key );
|
2004-01-31 01:53:01 +00:00
|
|
|
if( is_integer( $iswatched ) ) return $iswatched;
|
2003-11-09 11:45:12 +00:00
|
|
|
|
2004-07-18 08:48:43 +00:00
|
|
|
$dbr =& wfGetDB( DB_SLAVE );
|
|
|
|
|
$res = $dbr->select( 'watchlist', 1, array( 'wl_user' => $this->id, 'wl_namespace' => $this->ns,
|
|
|
|
|
'wl_title' => $this->ti ), $fname );
|
|
|
|
|
$iswatched = ($dbr->numRows( $res ) > 0) ? 1 : 0;
|
2003-11-09 11:45:12 +00:00
|
|
|
$wgMemc->set( $key, $iswatched );
|
|
|
|
|
return $iswatched;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function addWatch()
|
|
|
|
|
{
|
2004-07-10 03:09:26 +00:00
|
|
|
$fname = "WatchedItem::addWatch";
|
2003-11-09 11:45:12 +00:00
|
|
|
# REPLACE instead of INSERT because occasionally someone
|
|
|
|
|
# accidentally reloads a watch-add operation.
|
2004-07-18 08:48:43 +00:00
|
|
|
$dbw =& wfGetDB( DB_MASTER );
|
2004-07-10 03:09:26 +00:00
|
|
|
$dbw->replace( 'watchlist', array(array('wl_user', 'wl_namespace', 'wl_title')),
|
|
|
|
|
array(
|
|
|
|
|
'wl_user' => $this->id,
|
|
|
|
|
'wl_namespace' => $this->ns,
|
2004-07-18 08:48:43 +00:00
|
|
|
'wl_title' => $this->ti,
|
2004-07-10 03:09:26 +00:00
|
|
|
), $fname );
|
2004-06-11 15:54:29 +00:00
|
|
|
|
2003-11-09 11:45:12 +00:00
|
|
|
global $wgMemc;
|
|
|
|
|
$wgMemc->set( $this->watchkey(), 1 );
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function removeWatch()
|
|
|
|
|
{
|
2004-07-24 07:27:48 +00:00
|
|
|
$fname = 'WatchedItem::removeWatch';
|
|
|
|
|
|
2004-07-18 08:48:43 +00:00
|
|
|
$dbw =& wfGetDB( DB_MASTER );
|
|
|
|
|
$dbw->delete( 'watchlist',
|
|
|
|
|
array(
|
|
|
|
|
'wl_user' => $this->id,
|
|
|
|
|
'wl_namespace' => $this->ns,
|
|
|
|
|
'wl_title' => $this->ti
|
|
|
|
|
), $fname
|
|
|
|
|
);
|
2003-11-09 11:45:12 +00:00
|
|
|
|
2004-07-18 08:48:43 +00:00
|
|
|
if ( $dbw->affectedRows() ) {
|
|
|
|
|
global $wgMemc;
|
|
|
|
|
$wgMemc->set( $this->watchkey(), 0 );
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2003-11-09 11:45:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* static */ function duplicateEntries( $ot, $nt ) {
|
2004-03-08 09:09:35 +00:00
|
|
|
$fname = "WatchedItem::duplicateEntries";
|
2003-11-09 11:45:12 +00:00
|
|
|
global $wgMemc, $wgDBname;
|
|
|
|
|
$oldnamespace = $ot->getNamespace() & ~1;
|
|
|
|
|
$newnamespace = $nt->getNamespace() & ~1;
|
|
|
|
|
$oldtitle = $ot->getDBkey();
|
|
|
|
|
$newtitle = $nt->getDBkey();
|
|
|
|
|
|
2004-07-18 08:48:43 +00:00
|
|
|
$dbw =& wfGetDB( DB_MASTER );
|
|
|
|
|
$watchlist = $dbw->tableName( 'watchlist' );
|
|
|
|
|
|
|
|
|
|
$res = $dbw->select( 'watchlist', 'wl_user',
|
|
|
|
|
array( 'wl_namespace' => $oldnamespace, 'wl_title' => $oldtitle ),
|
|
|
|
|
$fname, 'FOR UPDATE'
|
|
|
|
|
);
|
|
|
|
|
# Construct array to replace into the watchlist
|
|
|
|
|
$values = array();
|
|
|
|
|
while ( $s = $dbw->fetchObject( $res ) ) {
|
|
|
|
|
$values[] = array(
|
|
|
|
|
'wl_user' => $s->wl_user,
|
|
|
|
|
'wl_namespace' => $newnamespace,
|
|
|
|
|
'wl_title' => $newtitle
|
|
|
|
|
);
|
2003-11-09 11:45:12 +00:00
|
|
|
}
|
2004-07-18 08:48:43 +00:00
|
|
|
$dbw->freeResult( $res );
|
|
|
|
|
|
|
|
|
|
# Perform replace
|
|
|
|
|
# Note that multi-row replace is very efficient for MySQL but may be inefficient for
|
|
|
|
|
# some other DBMSes, mostly due to poor simulation by us
|
|
|
|
|
$dbw->replace( 'watchlist', array(array( 'wl_user', 'wl_namespace', 'wl_title')), $values, $fname );
|
2003-11-09 11:45:12 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
?>
|