X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/mediawiki.git/blobdiff_plain/a4b52d2fe555a507c376e78ee624898c55968364..HEAD:/includes/WatchedItem.php diff --git a/includes/WatchedItem.php b/includes/WatchedItem.php index 1912f540..bfd1d613 100644 --- a/includes/WatchedItem.php +++ b/includes/WatchedItem.php @@ -1,193 +1,200 @@ mUser =& $user; - $wl->mTitle =& $title; - $wl->id = $user->getId(); -# Patch (also) for email notification on page changes T.Gries/M.Arndt 11.09.2004 -# TG patch: here we do not consider pages and their talk pages equivalent - why should we ? -# The change results in talk-pages not automatically included in watchlists, when their parent page is included -# $wl->ns = $title->getNamespace() & ~1; - $wl->ns = $title->getNamespace(); - - $wl->ti = $title->getDBkey(); - return $wl; - } /** - * Returns the memcached key for this item + * @deprecated since 1.27, see User::IGNORE_USER_RIGHTS + */ + const IGNORE_USER_RIGHTS = User::IGNORE_USER_RIGHTS; + + /** + * @deprecated since 1.27, see User::CHECK_USER_RIGHTS + */ + const CHECK_USER_RIGHTS = User::CHECK_USER_RIGHTS; + + /** + * @deprecated Internal class use only + */ + const DEPRECATED_USAGE_TIMESTAMP = -100; + + /** + * @var bool + * @deprecated Internal class use only + */ + public $checkRights = User::CHECK_USER_RIGHTS; + + /** + * @var Title + * @deprecated Internal class use only */ - function watchKey() { - global $wgDBname; - return "$wgDBname:watchlist:user:$this->id:page:$this->ns:$this->ti"; + private $title; + + /** + * @var LinkTarget + */ + private $linkTarget; + + /** + * @var User + */ + private $user; + + /** + * @var null|string the value of the wl_notificationtimestamp field + */ + private $notificationTimestamp; + + /** + * @param User $user + * @param LinkTarget $linkTarget + * @param null|string $notificationTimestamp the value of the wl_notificationtimestamp field + * @param bool|null $checkRights DO NOT USE - used internally for backward compatibility + */ + public function __construct( + User $user, + LinkTarget $linkTarget, + $notificationTimestamp, + $checkRights = null + ) { + $this->user = $user; + $this->linkTarget = $linkTarget; + $this->notificationTimestamp = $notificationTimestamp; + if ( $checkRights !== null ) { + $this->checkRights = $checkRights; + } } - - /** - * Is mTitle being watched by mUser? - */ - function isWatched() { - # Pages and their talk pages are considered equivalent for watching; - # remember that talk namespaces are numbered as page namespace+1. - global $wgMemc; - $fname = 'WatchedItem::isWatched'; - - $key = $this->watchKey(); - $iswatched = $wgMemc->get( $key ); - if( is_integer( $iswatched ) ) return $iswatched; - - $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; - $wgMemc->set( $key, $iswatched ); - return $iswatched; + + /** + * @return User + */ + public function getUser() { + return $this->user; } /** - * @todo document - */ - function addWatch() { - $fname = 'WatchedItem::addWatch'; - wfProfileIn( $fname ); - # REPLACE instead of INSERT because occasionally someone - # accidentally reloads a watch-add operation. - $dbw =& wfGetDB( DB_MASTER ); - $dbw->replace( 'watchlist', array(array('wl_user', 'wl_namespace', 'wl_title', 'wl_notificationtimestamp')), - array( - 'wl_user' => $this->id, - 'wl_namespace' => ($this->ns & ~1), - 'wl_title' => $this->ti, - 'wl_notificationtimestamp' => '0' - ), $fname ); - - # the following code compensates the new behaviour, introduced by the enotif patch, - # that every single watched page needs now to be listed in watchlist - # namespace:page and namespace_talk:page need separate entries: create them - $dbw->replace( 'watchlist', array(array('wl_user', 'wl_namespace', 'wl_title', 'wl_notificationtimestamp')), - array( - 'wl_user' => $this->id, - 'wl_namespace' => ($this->ns | 1 ), - 'wl_title' => $this->ti, - 'wl_notificationtimestamp' => '0' - ), $fname ); - - global $wgMemc; - $wgMemc->set( $this->watchkey(), 1 ); - wfProfileOut( $fname ); - return true; + * @return LinkTarget + */ + public function getLinkTarget() { + return $this->linkTarget; } - function removeWatch() { - global $wgMemc; - $fname = 'WatchedItem::removeWatch'; - - $success = false; - $dbw =& wfGetDB( DB_MASTER ); - $dbw->delete( 'watchlist', - array( - 'wl_user' => $this->id, - 'wl_namespace' => ($this->ns & ~1), - 'wl_title' => $this->ti - ), $fname - ); - if ( $dbw->affectedRows() ) { - $success = true; + /** + * Get the notification timestamp of this entry. + * + * @return bool|null|string + */ + public function getNotificationTimestamp() { + // Back compat for objects constructed using self::fromUserTitle + if ( $this->notificationTimestamp === self::DEPRECATED_USAGE_TIMESTAMP ) { + // wfDeprecated( __METHOD__, '1.27' ); + if ( $this->checkRights && !$this->user->isAllowed( 'viewmywatchlist' ) ) { + return false; + } + $item = MediaWikiServices::getInstance()->getWatchedItemStore() + ->loadWatchedItem( $this->user, $this->linkTarget ); + if ( $item ) { + $this->notificationTimestamp = $item->getNotificationTimestamp(); + } else { + $this->notificationTimestamp = false; + } } + return $this->notificationTimestamp; + } - # the following code compensates the new behaviour, introduced by the - # enotif patch, that every single watched page needs now to be listed - # in watchlist namespace:page and namespace_talk:page had separate - # entries: clear them - $dbw->delete( 'watchlist', - array( - 'wl_user' => $this->id, - 'wl_namespace' => ($this->ns | 1), - 'wl_title' => $this->ti - ), $fname - ); - - if ( $dbw->affectedRows() ) { - $success = true; - } - if ( $success ) { - $wgMemc->set( $this->watchkey(), 0 ); + /** + * Back compat pre 1.27 with the WatchedItemStore introduction + * @todo remove in 1.28/9 + * ------------------------------------------------- + */ + + /** + * @return Title + * @deprecated Internal class use only + */ + public function getTitle() { + if ( !$this->title ) { + $this->title = Title::newFromLinkTarget( $this->linkTarget ); } - return $success; + return $this->title; } /** - * Check if the given title already is watched by the user, and if so - * add watches on a new title. To be used for page renames and such. - * - * @param Title $ot Page title to duplicate entries from, if present - * @param Title $nt Page title to add watches on - * @static + * @deprecated since 1.27 Use the constructor, WatchedItemStore::getWatchedItem() + * or WatchedItemStore::loadWatchedItem() */ - function duplicateEntries( $ot, $nt ) { - WatchedItem::doDuplicateEntries( $ot->getSubjectPage(), $nt->getSubjectPage() ); - WatchedItem::doDuplicateEntries( $ot->getTalkPage(), $nt->getTalkPage() ); + public static function fromUserTitle( $user, $title, $checkRights = User::CHECK_USER_RIGHTS ) { + wfDeprecated( __METHOD__, '1.27' ); + return new self( $user, $title, self::DEPRECATED_USAGE_TIMESTAMP, (bool)$checkRights ); } - - /** - * @static - * @access private - */ - function doDuplicateEntries( $ot, $nt ) { - $fname = "WatchedItem::duplicateEntries"; - global $wgMemc, $wgDBname; - $oldnamespace = $ot->getNamespace(); - $newnamespace = $nt->getNamespace(); - $oldtitle = $ot->getDBkey(); - $newtitle = $nt->getDBkey(); - - $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 - ); - } - $dbw->freeResult( $res ); - - if( empty( $values ) ) { - // Nothing to do - return true; - } - # 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 ); + /** + * @deprecated since 1.27 Use User::addWatch() + * @return bool + */ + public function addWatch() { + wfDeprecated( __METHOD__, '1.27' ); + $this->user->addWatch( $this->getTitle(), $this->checkRights ); return true; } + /** + * @deprecated since 1.27 Use User::removeWatch() + * @return bool + */ + public function removeWatch() { + wfDeprecated( __METHOD__, '1.27' ); + if ( $this->checkRights && !$this->user->isAllowed( 'editmywatchlist' ) ) { + return false; + } + $this->user->removeWatch( $this->getTitle(), $this->checkRights ); + return true; + } -} + /** + * @deprecated since 1.27 Use User::isWatched() + * @return bool + */ + public function isWatched() { + wfDeprecated( __METHOD__, '1.27' ); + return $this->user->isWatched( $this->getTitle(), $this->checkRights ); + } -?> + /** + * @deprecated since 1.27 Use WatchedItemStore::duplicateAllAssociatedEntries() + */ + public static function duplicateEntries( Title $oldTitle, Title $newTitle ) { + wfDeprecated( __METHOD__, '1.27' ); + $store = MediaWikiServices::getInstance()->getWatchedItemStore(); + $store->duplicateAllAssociatedEntries( $oldTitle, $newTitle ); + } + +}