00001 <?php
00010 class WatchedItem {
00011 var $mTitle, $mUser, $id, $ns, $ti;
00012
00019 public static function fromUserTitle( $user, $title ) {
00020 $wl = new WatchedItem;
00021 $wl->mUser = $user;
00022 $wl->mTitle = $title;
00023 $wl->id = $user->getId();
00024 # Patch (also) for email notification on page changes T.Gries/M.Arndt 11.09.2004
00025 # TG patch: here we do not consider pages and their talk pages equivalent - why should we ?
00026 # The change results in talk-pages not automatically included in watchlists, when their parent page is included
00027 # $wl->ns = $title->getNamespace() & ~1;
00028 $wl->ns = $title->getNamespace();
00029
00030 $wl->ti = $title->getDBkey();
00031 return $wl;
00032 }
00033
00038 public function isWatched() {
00039 # Pages and their talk pages are considered equivalent for watching;
00040 # remember that talk namespaces are numbered as page namespace+1.
00041
00042 $dbr = wfGetDB( DB_SLAVE );
00043 $res = $dbr->select( 'watchlist', 1, array( 'wl_user' => $this->id, 'wl_namespace' => $this->ns,
00044 'wl_title' => $this->ti ), __METHOD__ );
00045 $iswatched = ($dbr->numRows( $res ) > 0) ? 1 : 0;
00046 return $iswatched;
00047 }
00048
00054 public function addWatch() {
00055 wfProfileIn( __METHOD__ );
00056
00057
00058
00059 $dbw = wfGetDB( DB_MASTER );
00060 $dbw->insert( 'watchlist',
00061 array(
00062 'wl_user' => $this->id,
00063 'wl_namespace' => MWNamespace::getSubject($this->ns),
00064 'wl_title' => $this->ti,
00065 'wl_notificationtimestamp' => NULL
00066 ), __METHOD__, 'IGNORE' );
00067
00068
00069
00070 $dbw->insert( 'watchlist',
00071 array(
00072 'wl_user' => $this->id,
00073 'wl_namespace' => MWNamespace::getTalk($this->ns),
00074 'wl_title' => $this->ti,
00075 'wl_notificationtimestamp' => NULL
00076 ), __METHOD__, 'IGNORE' );
00077
00078 wfProfileOut( __METHOD__ );
00079 return true;
00080 }
00081
00086 public function removeWatch() {
00087 $success = false;
00088 $dbw = wfGetDB( DB_MASTER );
00089 $dbw->delete( 'watchlist',
00090 array(
00091 'wl_user' => $this->id,
00092 'wl_namespace' => MWNamespace::getSubject($this->ns),
00093 'wl_title' => $this->ti
00094 ), __METHOD__
00095 );
00096 if ( $dbw->affectedRows() ) {
00097 $success = true;
00098 }
00099
00100 # the following code compensates the new behaviour, introduced by the
00101 # enotif patch, that every single watched page needs now to be listed
00102 # in watchlist namespace:page and namespace_talk:page had separate
00103 # entries: clear them
00104 $dbw->delete( 'watchlist',
00105 array(
00106 'wl_user' => $this->id,
00107 'wl_namespace' => MWNamespace::getTalk($this->ns),
00108 'wl_title' => $this->ti
00109 ), __METHOD__
00110 );
00111
00112 if ( $dbw->affectedRows() ) {
00113 $success = true;
00114 }
00115 return $success;
00116 }
00117
00125 public static function duplicateEntries( $ot, $nt ) {
00126 WatchedItem::doDuplicateEntries( $ot->getSubjectPage(), $nt->getSubjectPage() );
00127 WatchedItem::doDuplicateEntries( $ot->getTalkPage(), $nt->getTalkPage() );
00128 }
00129
00133 private static function doDuplicateEntries( $ot, $nt ) {
00134 $oldnamespace = $ot->getNamespace();
00135 $newnamespace = $nt->getNamespace();
00136 $oldtitle = $ot->getDBkey();
00137 $newtitle = $nt->getDBkey();
00138
00139 $dbw = wfGetDB( DB_MASTER );
00140 $res = $dbw->select( 'watchlist', 'wl_user',
00141 array( 'wl_namespace' => $oldnamespace, 'wl_title' => $oldtitle ),
00142 __METHOD__, 'FOR UPDATE'
00143 );
00144 # Construct array to replace into the watchlist
00145 $values = array();
00146 while ( $s = $dbw->fetchObject( $res ) ) {
00147 $values[] = array(
00148 'wl_user' => $s->wl_user,
00149 'wl_namespace' => $newnamespace,
00150 'wl_title' => $newtitle
00151 );
00152 }
00153 $dbw->freeResult( $res );
00154
00155 if( empty( $values ) ) {
00156
00157 return true;
00158 }
00159
00160 # Perform replace
00161 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
00162 # some other DBMSes, mostly due to poor simulation by us
00163 $dbw->replace( 'watchlist', array( array( 'wl_user', 'wl_namespace', 'wl_title' ) ), $values, __METHOD__ );
00164 return true;
00165 }
00166 }