r10906 - Code Review

From MediaWiki.org

Jump to: navigation, search
Repository:MediaWiki
Revision:r10905 | r10906 (on ViewVC) | r10907 >
Date:06:16, 5 September 2005
Author:vibber
Status:new
Tags:
Comment:* Add 'reupload' and 'reupload-shared' permission keys to restrict new
uploads overwriting existing files; default is the old behavior (allowed).
Modified paths:

Diff [purge]

Index: trunk/phase3/includes/ImagePage.php
===================================================================
--- trunk/phase3/includes/ImagePage.php	(revision 10905)
+++ trunk/phase3/includes/ImagePage.php	(revision 10906)
@@ -286,9 +286,11 @@
 			return;
 
 		$sk = $wgUser->getSkin();
-		$wgOut->addHTML( '<br /><ul><li>' );
-		$wgOut->addWikiText( '<div>'. wfMsg( 'uploadnewversion', $this->getUploadUrl() ) .'</div>' );
-		$wgOut->addHTML( '</li><li>' );
+		$wgOut->addHTML( '<br /><ul>' );
+		if( $wgUser->isAllowed( 'reupload' ) ) {	
+			$wgOut->addWikiText( "<li>\n<div>". wfMsg( 'uploadnewversion', $this->getUploadUrl() ) ."</div>\n</li>\n" );
+		}
+		$wgOut->addHTML( '<li>' );
 		$wgOut->addHTML( $sk->makeKnownLinkObj( $this->mTitle,
 			wfMsg( 'edit-externally' ), "action=edit&externaledit=true&mode=file" ) );
 		$wgOut->addWikiText( '<div>' .  wfMsg('edit-externally-help') . '</div>' );
Index: trunk/phase3/includes/SpecialUpload.php
===================================================================
--- trunk/phase3/includes/SpecialUpload.php	(revision 10905)
+++ trunk/phase3/includes/SpecialUpload.php	(revision 10906)
@@ -201,6 +201,14 @@
 		if( !$nt->userCanEdit() ) {
 			return $this->uploadError( wfMsgWikiHtml( 'protectedpage' ) );
 		}
+		
+		/**
+		 * In some cases we may forbid overwriting of existing files.
+		 */
+		$overwrite = $this->checkOverwrite( $this->mUploadSaveName );
+		if( WikiError::isError( $overwrite ) ) {
+			return $this->uploadError( $overwrite->toString() );
+		}
 
 		/* Don't allow users to override the blacklist (check file extension) */
 		global $wgStrictFileExtensions;
@@ -949,6 +957,45 @@
 			unlink( $this->mUploadTempName );
 		}
 	}
+	
+	/**
+	 * Check if there's an overwrite conflict and, if so, if restrictions
+	 * forbid this user from performing the upload.
+	 *
+	 * @return mixed true on success, WikiError on failure
+	 * @access private
+	 */
+	function checkOverwrite( $name ) {
+		$img = Image::newFromName( $name );
+		if( is_null( $img ) ) {
+			// Uh... this shouldn't happen ;)
+			// But if it does, fall through to previous behavior
+			return false;
+		}
+		
+		$error = '';
+		if( $img->exists() ) {
+			global $wgUser, $wgOut;
+			if( $img->isLocal() ) {
+				if( !$wgUser->isAllowed( 'reupload' ) ) {
+					$error = 'fileexists-forbidden';
+				}
+			} else {
+				if( !$wgUser->isAllowed( 'reupload' ) ||
+				    !$wgUser->isAllowed( 'reupload-shared' ) ) {
+					$error = "fileexists-shared-forbidden";
+				}
+			}
+		}
+		
+		if( $error ) {
+			$errorText = wfMsg( $error, wfEscapeWikiText( $img->getName() ) );
+			return new WikiError( $wgOut->parse( $errorText ) );
+		}
+		
+		// Rockin', go ahead and upload
+		return true;
+	}
 
 }
 ?>
Index: trunk/phase3/includes/Image.php
===================================================================
--- trunk/phase3/includes/Image.php	(revision 10905)
+++ trunk/phase3/includes/Image.php	(revision 10906)
@@ -1484,6 +1484,16 @@
 			$fname
 		);
 	}
+	
+	/**
+	 * Returns true if the image does not come from the shared
+	 * image repository.
+	 *
+	 * @return bool
+	 */
+	function isLocal() {
+		return !$this->fromSharedDirectory;
+	}
 
 } //class
 
Index: trunk/phase3/includes/DefaultSettings.php
===================================================================
--- trunk/phase3/includes/DefaultSettings.php	(revision 10905)
+++ trunk/phase3/includes/DefaultSettings.php	(revision 10906)
@@ -710,6 +710,8 @@
 $wgGroupPermissions['user' ]['read']            = true;
 $wgGroupPermissions['user' ]['edit']            = true;
 $wgGroupPermissions['user' ]['upload']          = true;
+$wgGroupPermissions['user' ]['reupload']        = true;
+$wgGroupPermissions['user' ]['reupload-shared'] = true;
 
 $wgGroupPermissions['bot'  ]['bot']             = true;
 
@@ -724,6 +726,8 @@
 $wgGroupPermissions['sysop']['protect']         = true;
 $wgGroupPermissions['sysop']['rollback']        = true;
 $wgGroupPermissions['sysop']['upload']          = true;
+$wgGroupPermissions['sysop']['reupload']        = true;
+$wgGroupPermissions['sysop']['reupload-shared'] = true;
 
 $wgGroupPermissions['bureaucrat']['userrights'] = true;
 // Used by the Special:Renameuser extension
Index: trunk/phase3/RELEASE-NOTES
===================================================================
--- trunk/phase3/RELEASE-NOTES	(revision 10905)
+++ trunk/phase3/RELEASE-NOTES	(revision 10906)
@@ -82,6 +82,8 @@
 * (bug 3306) Document $wgLocalTZoffset
 * (bug 3304) Language file for Croatian (LanguageHr.php)
 * (bug 2143) Update Vietnamese interface
+* Add 'reupload' and 'reupload-shared' permission keys to restrict new uploads
+  overwriting existing files; default is the old behavior (allowed).
 
 
 === Caveats ===
Index: trunk/phase3/languages/Language.php
===================================================================
--- trunk/phase3/languages/Language.php	(revision 10905)
+++ trunk/phase3/languages/Language.php	(revision 10906)
@@ -1017,6 +1017,8 @@
 'largefileserver' => 'This file is bigger than the server is configured to allow.',
 'emptyfile'		=> 'The file you uploaded seems to be empty. This might be due to a typo in the file name. Please check whether you really want to upload this file.',
 'fileexists'		=> 'A file with this name exists already, please check $1 if you are not sure if you want to change it.',
+'fileexists-forbidden' => 'A file with this name exists already; please go back and upload this file under a new name. [[Image:$1|thumb|center|$1]]',
+'fileexists-shared-forbidden' => 'A file with this name exists already in the shared file repository; lease go back and upload this file under a new name. [[Image:$1|thumb|center|$1]]',
 'successfulupload' => 'Successful upload',
 'fileuploaded'	=> "File $1 uploaded successfully.
 Please follow this link: $2 to the description page and fill
Views
Toolbox