Umherirrender has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/179532

Change subject: Add RequestContext::getMainUserAndWarn and use in fallback 
situation
......................................................................

Add RequestContext::getMainUserAndWarn and use in fallback situation

Some methods accept a null user object for backward compability, in this
fallback case now the new method RequestContext::getMainUserAndWarn is
used to get the User object from the main context (which is always the
same as $wgUser) and log the access to allow finding the method calls
and fix them by adding a user object to the method.

Change-Id: I63358d47a7cef832442847513d0d1368f04c3007
---
M includes/FileDeleteForm.php
M includes/Revision.php
M includes/Title.php
M includes/context/RequestContext.php
M includes/filerepo/file/LocalFile.php
M includes/logging/LogEventsList.php
M includes/logging/LogPage.php
M includes/logging/PatrolLog.php
M includes/specialpage/SpecialPageFactory.php
M includes/specials/SpecialUndelete.php
M includes/upload/UploadStash.php
11 files changed, 30 insertions(+), 32 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/32/179532/1

diff --git a/includes/FileDeleteForm.php b/includes/FileDeleteForm.php
index 1c709e6..3c7d471 100644
--- a/includes/FileDeleteForm.php
+++ b/includes/FileDeleteForm.php
@@ -157,8 +157,7 @@
                $suppress, User $user = null
        ) {
                if ( $user === null ) {
-                       global $wgUser;
-                       $user = $wgUser;
+                       $user = RequestContext::getMainUserAndWarn();
                }
 
                if ( $oldimage ) {
diff --git a/includes/Revision.php b/includes/Revision.php
index 8ba79df..4667ab6 100644
--- a/includes/Revision.php
+++ b/includes/Revision.php
@@ -1592,8 +1592,7 @@
 
                if ( $current ) {
                        if ( !$user ) {
-                               global $wgUser;
-                               $user = $wgUser;
+                               $user = RequestContext::getMainUserAndWarn();
                        }
 
                        $row = array(
@@ -1656,8 +1655,7 @@
        ) {
                if ( $bitfield & $field ) { // aspect is deleted
                        if ( $user === null ) {
-                               global $wgUser;
-                               $user = $wgUser;
+                               $user = RequestContext::getMainUserAndWarn();
                        }
                        if ( $bitfield & self::DELETED_RESTRICTED ) {
                                $permissions = array( 'suppressrevision', 
'viewsuppressed' );
diff --git a/includes/Title.php b/includes/Title.php
index cfdba5d..5fc7b87 100644
--- a/includes/Title.php
+++ b/includes/Title.php
@@ -1892,8 +1892,7 @@
         */
        public function userCan( $action, $user = null, $doExpensiveQueries = 
true ) {
                if ( !$user instanceof User ) {
-                       global $wgUser;
-                       $user = $wgUser;
+                       $user = RequestContext::getMainUserAndWarn();
                }
 
                return !count( $this->getUserPermissionsErrorsInternal(
@@ -4438,10 +4437,10 @@
         * @return string|null
         */
        public function getNotificationTimestamp( $user = null ) {
-               global $wgUser, $wgShowUpdatedMarker;
+               global $wgShowUpdatedMarker;
                // Assume current user if none given
                if ( !$user ) {
-                       $user = $wgUser;
+                       $user = RequestContext::getMainUserAndWarn();
                }
                // Check cache first
                $uid = $user->getId();
diff --git a/includes/context/RequestContext.php 
b/includes/context/RequestContext.php
index fe17fde..b8a7088 100644
--- a/includes/context/RequestContext.php
+++ b/includes/context/RequestContext.php
@@ -448,6 +448,19 @@
        }
 
        /**
+        * Get the User object associated with the main request
+        * and gives a warning to the log, to find places, where a user maybe 
is missing.
+        *
+        * @return User
+        * @since 1.25
+        */
+       public static function getMainUserAndWarn() {
+               wfDebugLog( 'GlobalUserFail', __METHOD__ . ' called by ' . 
wfGetAllCallers( 5 ) . ' with no user set.' );
+
+               return self::getMain()->getUser();
+       }
+
+       /**
         * Resets singleton returned by getMain(). Should be called only from 
unit tests.
         */
        public static function resetMain() {
diff --git a/includes/filerepo/file/LocalFile.php 
b/includes/filerepo/file/LocalFile.php
index 249fa01..59131fc 100644
--- a/includes/filerepo/file/LocalFile.php
+++ b/includes/filerepo/file/LocalFile.php
@@ -1206,8 +1206,7 @@
        function recordUpload( $oldver, $desc, $license = '', $copyStatus = '', 
$source = '',
                $watch = false, $timestamp = false, User $user = null ) {
                if ( !$user ) {
-                       global $wgUser;
-                       $user = $wgUser;
+                       $user = RequestContext::getMainUserAndWarn();
                }
 
                $pageText = SpecialUpload::getInitialPageText( $desc, $license, 
$copyStatus, $source );
@@ -1239,8 +1238,7 @@
                wfProfileIn( __METHOD__ );
 
                if ( is_null( $user ) ) {
-                       global $wgUser;
-                       $user = $wgUser;
+                       $user = RequestContext::getMainUserAndWarn();
                }
 
                $dbw = $this->repo->getMasterDB();
@@ -2009,8 +2007,7 @@
                if ( $user ) {
                        $this->user = $user;
                } else {
-                       global $wgUser;
-                       $this->user = $wgUser;
+                       $this->user = RequestContext::getMainUserAndWarn();
                }
                $this->status = $file->repo->newGood();
        }
diff --git a/includes/logging/LogEventsList.php 
b/includes/logging/LogEventsList.php
index f7eaec3..fbc7c45 100644
--- a/includes/logging/LogEventsList.php
+++ b/includes/logging/LogEventsList.php
@@ -439,8 +439,7 @@
        public static function userCanBitfield( $bitfield, $field, User $user = 
null ) {
                if ( $bitfield & $field ) {
                        if ( $user === null ) {
-                               global $wgUser;
-                               $user = $wgUser;
+                               $user = RequestContext::getMainUserAndWarn();
                        }
                        if ( $bitfield & LogPage::DELETED_RESTRICTED ) {
                                $permissions = array( 'suppressrevision', 
'viewsuppressed' );
@@ -656,8 +655,7 @@
                global $wgLogRestrictions;
 
                if ( $audience != 'public' && $user === null ) {
-                       global $wgUser;
-                       $user = $wgUser;
+                       $user = RequestContext::getMainUserAndWarn();
                }
 
                // Reset the array, clears extra "where" clauses when $par is 
used
diff --git a/includes/logging/LogPage.php b/includes/logging/LogPage.php
index d576d74..367f26c 100644
--- a/includes/logging/LogPage.php
+++ b/includes/logging/LogPage.php
@@ -440,8 +440,7 @@
                $this->params = LogPage::makeParamBlob( $params );
 
                if ( $doer === null ) {
-                       global $wgUser;
-                       $doer = $wgUser;
+                       $doer = RequestContext::getMainUserAndWarn();
                } elseif ( !is_object( $doer ) ) {
                        $doer = User::newFromId( $doer );
                }
diff --git a/includes/logging/PatrolLog.php b/includes/logging/PatrolLog.php
index 4f2a565..b352bf9 100644
--- a/includes/logging/PatrolLog.php
+++ b/includes/logging/PatrolLog.php
@@ -52,8 +52,7 @@
                }
 
                if ( !$user ) {
-                       global $wgUser;
-                       $user = $wgUser;
+                       $user = RequestContext::getMainUserAndWarn();
                }
 
                $entry = new ManualLogEntry( 'patrol', 'patrol' );
diff --git a/includes/specialpage/SpecialPageFactory.php 
b/includes/specialpage/SpecialPageFactory.php
index e31ebf6..82fbe18 100644
--- a/includes/specialpage/SpecialPageFactory.php
+++ b/includes/specialpage/SpecialPageFactory.php
@@ -450,8 +450,7 @@
        public static function getUsablePages( User $user = null ) {
                $pages = array();
                if ( $user === null ) {
-                       global $wgUser;
-                       $user = $wgUser;
+                       $user = RequestContext::getMainUserAndWarn();
                }
                foreach ( self::getPageList() as $name => $rec ) {
                        $page = self::getPage( $name );
@@ -495,8 +494,7 @@
        public static function getRestrictedPages( User $user = null ) {
                $pages = array();
                if ( $user === null ) {
-                       global $wgUser;
-                       $user = $wgUser;
+                       $user = RequestContext::getMainUserAndWarn();
                }
                foreach ( self::getPageList() as $name => $rec ) {
                        $page = self::getPage( $name );
diff --git a/includes/specials/SpecialUndelete.php 
b/includes/specials/SpecialUndelete.php
index 2ea1b12..862c75e 100644
--- a/includes/specials/SpecialUndelete.php
+++ b/includes/specials/SpecialUndelete.php
@@ -412,8 +412,7 @@
                }
 
                if ( $user === null ) {
-                       global $wgUser;
-                       $user = $wgUser;
+                       $user = RequestContext::getMainUserAndWarn();
                }
 
                $logEntry = new ManualLogEntry( 'delete', 'restore' );
diff --git a/includes/upload/UploadStash.php b/includes/upload/UploadStash.php
index 52ce4d3..743fbea 100644
--- a/includes/upload/UploadStash.php
+++ b/includes/upload/UploadStash.php
@@ -92,8 +92,7 @@
                if ( $user ) {
                        $this->user = $user;
                } else {
-                       global $wgUser;
-                       $this->user = $wgUser;
+                       $this->user = RequestContext::getMainUserAndWarn();
                }
 
                if ( is_object( $this->user ) ) {

-- 
To view, visit https://gerrit.wikimedia.org/r/179532
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I63358d47a7cef832442847513d0d1368f04c3007
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Umherirrender <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to