Clean up user handling in UploadStash
authorChad Horohoe <chadh@wikimedia.org>
Fri, 2 Sep 2016 17:18:12 +0000 (10:18 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Fri, 2 Sep 2016 17:40:49 +0000 (17:40 +0000)
- User parameter is now *required*, remove $wgUser fallback
- We don't actually need the object after construction, don't store it

Change-Id: Id0cc859b70e5d0608ffbfa591bce6a1feb7cc3be

includes/upload/UploadStash.php
maintenance/cleanupUploadStash.php
tests/phpunit/includes/upload/UploadStashTest.php

index c171ded..1c3e13b 100644 (file)
@@ -73,8 +73,8 @@ class UploadStash {
        // fileprops cache
        protected $fileProps = [];
 
-       // current user
-       protected $user, $userId, $isLoggedIn;
+       // current user info
+       protected $userId, $isLoggedIn;
 
        /**
         * Represents a temporary filestore, with metadata in the database.
@@ -82,25 +82,15 @@ class UploadStash {
         * (should replace it eventually).
         *
         * @param FileRepo $repo
-        * @param User $user (default null)
+        * @param User $user
         */
-       public function __construct( FileRepo $repo, $user = null ) {
+       public function __construct( FileRepo $repo, User $user ) {
                // this might change based on wiki's configuration.
                $this->repo = $repo;
 
-               // if a user was passed, use it. otherwise, attempt to use the global.
-               // this keeps FileRepo from breaking when it creates an UploadStash object
-               if ( $user ) {
-                       $this->user = $user;
-               } else {
-                       global $wgUser;
-                       $this->user = $wgUser;
-               }
-
-               if ( is_object( $this->user ) ) {
-                       $this->userId = $this->user->getId();
-                       $this->isLoggedIn = $this->user->isLoggedIn();
-               }
+               // We only need the logged in status and user id.
+               $this->userId = $user->getId();
+               $this->isLoggedIn = $user->isLoggedIn();
        }
 
        /**
index cd7a842..3c768d8 100644 (file)
@@ -74,7 +74,7 @@ class UploadStashCleanup extends Maintenance {
                        // this could be done some other, more direct/efficient way, but using
                        // UploadStash's own methods means it's less likely to fall accidentally
                        // out-of-date someday
-                       $stash = new UploadStash( $repo );
+                       $stash = new UploadStash( $repo, new User() );
 
                        $i = 0;
                        foreach ( $keys as $key ) {
index 9b25505..e1db084 100644 (file)
@@ -55,10 +55,8 @@ class UploadStashTest extends MediaWikiTestCase {
         * @todo give this test a real name explaining what is being tested here
         */
        public function testBug29408() {
-               $this->setMwGlobals( 'wgUser', self::$users['uploader']->getUser() );
-
                $repo = RepoGroup::singleton()->getLocalRepo();
-               $stash = new UploadStash( $repo );
+               $stash = new UploadStash( $repo, self::$users['uploader']->getUser() );
 
                // Throws exception caught by PHPUnit on failure
                $file = $stash->stashFile( $this->bug29408File );