Avoid select/writes for anon user in WatchedItem
authorumherirrender <umherirrender_de.wp@web.de>
Thu, 23 May 2013 18:38:05 +0000 (20:38 +0200)
committerumherirrender <umherirrender_de.wp@web.de>
Thu, 23 May 2013 18:38:05 +0000 (20:38 +0200)
For a page view of a anon user the following select is executed:
SELECT  wl_notificationtimestamp  FROM `watchlist`  WHERE wl_user = '0'
AND wl_namespace = '0' AND wl_title = 'Hauptseite'  LIMIT 1

Change-Id: I1cf77a2d024956ed7c150bdf1687eb80c7eccc6f

includes/WatchedItem.php

index 01376a0..45aa822 100644 (file)
@@ -89,6 +89,12 @@ class WatchedItem {
                }
                $this->loaded = true;
 
+               // Only loggedin user can have a watchlist
+               if ( $this->mUser->isAnon() ) {
+                       $this->watched = false;
+                       return;
+               }
+
                # Pages and their talk pages are considered equivalent for watching;
                # remember that talk namespaces are numbered as page namespace+1.
 
@@ -135,6 +141,11 @@ class WatchedItem {
         *        page is not watched or the notification timestamp is already NULL.
         */
        public function resetNotificationTimestamp( $force = '' ) {
+               // Only loggedin user can have a watchlist
+               if ( wfReadOnly() || $this->mUser->isAnon() ) {
+                       return;
+               }
+
                if ( $force != 'force' ) {
                        $this->load();
                        if ( !$this->watched || $this->timestamp === null ) {
@@ -153,11 +164,17 @@ class WatchedItem {
        /**
         * Given a title and user (assumes the object is setup), add the watch to the
         * database.
-        * @return bool (always true)
+        * @return bool
         */
        public function addWatch() {
                wfProfileIn( __METHOD__ );
 
+               // Only loggedin user can have a watchlist
+               if ( wfReadOnly() || $this->mUser->isAnon() ) {
+                       wfProfileOut( __METHOD__ );
+                       return false;
+               }
+
                // Use INSERT IGNORE to avoid overwriting the notification timestamp
                // if there's already an entry for this page
                $dbw = wfGetDB( DB_MASTER );
@@ -192,6 +209,12 @@ class WatchedItem {
        public function removeWatch() {
                wfProfileIn( __METHOD__ );
 
+               // Only loggedin user can have a watchlist
+               if ( wfReadOnly() || $this->mUser->isAnon() ) {
+                       wfProfileOut( __METHOD__ );
+                       return false;
+               }
+
                $success = false;
                $dbw = wfGetDB( DB_MASTER );
                $dbw->delete( 'watchlist',