Extend FileRepo::findFile private option to accept a User object
authorBrad Jorsch <bjorsch@wikimedia.org>
Fri, 17 Jan 2014 16:42:45 +0000 (11:42 -0500)
committerBrad Jorsch <bjorsch@wikimedia.org>
Fri, 17 Jan 2014 16:43:22 +0000 (11:43 -0500)
Callers may want to test visibility for a user other than $wgUser when
specifying the 'private' option. It seems a natural extension to allow
'private' to be a User object, with boolean true retaining its old
meaning of $wgUser.

Change-Id: Idbed0f3055c0135b5c11068de1bf1ef668e13456

includes/filerepo/FileRepo.php
includes/filerepo/LocalRepo.php

index cab5690..81529e7 100644 (file)
@@ -416,7 +416,7 @@ class FileRepo {
         *   ignoreRedirect: If true, do not follow file redirects
         *   private:        If true, return restricted (deleted) files if the current
         *                   user is allowed to view them. Otherwise, such files will not
-        *                   be found.
+        *                   be found. If a User object, use that user instead of the current.
         * @return File|bool False on failure
         */
        public function findFile( $title, $options = array() ) {
@@ -439,7 +439,11 @@ class FileRepo {
                        if ( $img && $img->exists() ) {
                                if ( !$img->isDeleted( File::DELETED_FILE ) ) {
                                        return $img; // always OK
-                               } elseif ( !empty( $options['private'] ) && $img->userCan( File::DELETED_FILE ) ) {
+                               } elseif ( !empty( $options['private'] ) &&
+                                       $img->userCan( File::DELETED_FILE,
+                                               $options['private'] instanceof User ? $options['private'] : null
+                                       )
+                               ) {
                                        return $img;
                                }
                        }
@@ -536,7 +540,11 @@ class FileRepo {
                        if ( $img && $img->exists() ) {
                                if ( !$img->isDeleted( File::DELETED_FILE ) ) {
                                        return $img; // always OK
-                               } elseif ( !empty( $options['private'] ) && $img->userCan( File::DELETED_FILE ) ) {
+                               } elseif ( !empty( $options['private'] ) &&
+                                       $img->userCan( File::DELETED_FILE,
+                                               $options['private'] instanceof User ? $options['private'] : null
+                                       )
+                               ) {
                                        return $img;
                                }
                        }
index 1399c2b..99c0e09 100644 (file)
@@ -260,6 +260,9 @@ class LocalRepo extends FileRepo {
 
                $fileMatchesSearch = function( File $file, array $search ) {
                        // Note: file name comparison done elsewhere (to handle redirects)
+                       $user = ( !empty( $search['private'] ) && $search['private'] instanceof User )
+                               ? $search['private']
+                               : null;
                        return (
                                $file->exists() &&
                                (
@@ -267,7 +270,7 @@ class LocalRepo extends FileRepo {
                                        ( !empty( $search['time'] ) && $search['time'] === $file->getTimestamp() )
                                ) &&
                                ( !empty( $search['private'] ) || !$file->isDeleted( File::DELETED_FILE ) ) &&
-                               $file->userCan( File::DELETED_FILE )
+                               $file->userCan( File::DELETED_FILE, $user )
                        );
                };