Merge "wpUndidRevision should be passed along during 'preview' and 'diff'"
[lhc/web/wiklou.git] / includes / WatchedItem.php
index 45aa822..1e07e7c 100644 (file)
  * @ingroup Watchlist
  */
 class WatchedItem {
-       var $mTitle, $mUser;
+       /**
+        * Constant to specify that user rights 'editmywatchlist' and
+        * 'viewmywatchlist' should not be checked.
+        * @since 1.22
+        */
+       const IGNORE_USER_RIGHTS = 0;
+
+       /**
+        * Constant to specify that user rights 'editmywatchlist' and
+        * 'viewmywatchlist' should be checked.
+        * @since 1.22
+        */
+       const CHECK_USER_RIGHTS = 1;
+
+       var $mTitle, $mUser, $mCheckRights;
        private $loaded = false, $watched, $timestamp;
 
        /**
         * Create a WatchedItem object with the given user and title
+        * @since 1.22 $checkRights parameter added
         * @param $user User: the user to use for (un)watching
         * @param $title Title: the title we're going to (un)watch
+        * @param $checkRights int: Whether to check the 'viewmywatchlist' and 'editmywatchlist' rights.
+        *     Pass either WatchedItem::IGNORE_USER_RIGHTS or WatchedItem::CHECK_USER_RIGHTS.
         * @return WatchedItem object
         */
-       public static function fromUserTitle( $user, $title ) {
+       public static function fromUserTitle( $user, $title, $checkRights = WatchedItem::CHECK_USER_RIGHTS ) {
                $wl = new WatchedItem;
                $wl->mUser = $user;
                $wl->mTitle = $title;
+               $wl->mCheckRights = $checkRights;
 
                return $wl;
        }
@@ -110,11 +128,23 @@ class WatchedItem {
                }
        }
 
+       /**
+        * Check permissions
+        * @param $what string: 'viewmywatchlist' or 'editmywatchlist'
+        */
+       private function isAllowed( $what ) {
+               return !$this->mCheckRights || $this->mUser->isAllowed( $what );
+       }
+
        /**
         * Is mTitle being watched by mUser?
         * @return bool
         */
        public function isWatched() {
+               if ( !$this->isAllowed( 'viewmywatchlist' ) ) {
+                       return false;
+               }
+
                $this->load();
                return $this->watched;
        }
@@ -126,6 +156,10 @@ class WatchedItem {
         *         the wl_notificationtimestamp field otherwise
         */
        public function getNotificationTimestamp() {
+               if ( !$this->isAllowed( 'viewmywatchlist' ) ) {
+                       return false;
+               }
+
                $this->load();
                if ( $this->watched ) {
                        return $this->timestamp;
@@ -142,7 +176,7 @@ class WatchedItem {
         */
        public function resetNotificationTimestamp( $force = '' ) {
                // Only loggedin user can have a watchlist
-               if ( wfReadOnly() || $this->mUser->isAnon() ) {
+               if ( wfReadOnly() || $this->mUser->isAnon() || !$this->isAllowed( 'editmywatchlist' ) ) {
                        return;
                }
 
@@ -170,7 +204,7 @@ class WatchedItem {
                wfProfileIn( __METHOD__ );
 
                // Only loggedin user can have a watchlist
-               if ( wfReadOnly() || $this->mUser->isAnon() ) {
+               if ( wfReadOnly() || $this->mUser->isAnon() || !$this->isAllowed( 'editmywatchlist' ) ) {
                        wfProfileOut( __METHOD__ );
                        return false;
                }
@@ -210,7 +244,7 @@ class WatchedItem {
                wfProfileIn( __METHOD__ );
 
                // Only loggedin user can have a watchlist
-               if ( wfReadOnly() || $this->mUser->isAnon() ) {
+               if ( wfReadOnly() || $this->mUser->isAnon() || !$this->isAllowed( 'editmywatchlist' ) ) {
                        wfProfileOut( __METHOD__ );
                        return false;
                }