[MediaWiki-commits] [Gerrit] mediawiki/core[master]: FSFile and TempFSFile cleanups

2016-09-20 Thread jenkins-bot (Code Review)
jenkins-bot has submitted this change and it was merged.

Change subject: FSFile and TempFSFile cleanups
..


FSFile and TempFSFile cleanups

* Remove wf* function dependencies. This includes wfTempDir().
  Callers now should specify the directory, though it will try to do
  most of the wfTempDir() logic anyway if they do not.
* Update callers to inject wfTempDir() so $wgTmpDirectory is used by
  TempFSFile instead of it probing to find a valid directory itself.
* Move most of the wfTempDir() logic to TempFSFile::getUsableTempDirectory().
* Remove unused getMimeType() method.

Change-Id: Idd55936b07f9448a6c90577708722b7b52b8fe66
---
M includes/GlobalFunctions.php
M includes/api/ApiImageRotate.php
M includes/filebackend/FSFile.php
M includes/filebackend/TempFSFile.php
M includes/filerepo/file/File.php
M includes/upload/UploadFromChunks.php
M includes/upload/UploadFromUrl.php
M tests/phpunit/includes/filebackend/FileBackendTest.php
M tests/phpunit/includes/filerepo/MigrateFileRepoLayoutTest.php
M tests/phpunit/mocks/filebackend/MockFSFile.php
10 files changed, 57 insertions(+), 62 deletions(-)

Approvals:
  Catrope: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php
index 90bba53..e5f518f 100644
--- a/includes/GlobalFunctions.php
+++ b/includes/GlobalFunctions.php
@@ -2078,35 +2078,7 @@
return $wgTmpDirectory;
}
 
-   $tmpDir = array_map( "getenv", [ 'TMPDIR', 'TMP', 'TEMP' ] );
-   $tmpDir[] = sys_get_temp_dir();
-   $tmpDir[] = ini_get( 'upload_tmp_dir' );
-
-   foreach ( $tmpDir as $tmp ) {
-   if ( $tmp && file_exists( $tmp ) && is_dir( $tmp ) && 
is_writable( $tmp ) ) {
-   return $tmp;
-   }
-   }
-
-   /**
-* PHP on Windows will detect C:\Windows\Temp as not writable even 
though PHP can write to it
-* so create a directory within that called 'mwtmp' with a suffix of 
the user running the
-* current process.
-* The user is included as if various scripts are run by different 
users they will likely
-* not be able to access each others temporary files.
-*/
-   if ( wfIsWindows() ) {
-   $tmp = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'mwtmp' . '-' 
. get_current_user();
-   if ( !file_exists( $tmp ) ) {
-   mkdir( $tmp );
-   }
-   if ( file_exists( $tmp ) && is_dir( $tmp ) && is_writable( $tmp 
) ) {
-   return $tmp;
-   }
-   }
-
-   throw new MWException( 'No writable temporary directory could be found. 
' .
-   'Please set $wgTmpDirectory to a writable directory.' );
+   return TempFSFile::getUsableTempDirectory();
 }
 
 /**
diff --git a/includes/api/ApiImageRotate.php b/includes/api/ApiImageRotate.php
index 2b99353..966bcbf 100644
--- a/includes/api/ApiImageRotate.php
+++ b/includes/api/ApiImageRotate.php
@@ -108,7 +108,7 @@
continue;
}
$ext = strtolower( pathinfo( "$srcPath", 
PATHINFO_EXTENSION ) );
-   $tmpFile = TempFSFile::factory( 'rotate_', $ext );
+   $tmpFile = TempFSFile::factory( 'rotate_', $ext, 
wfTempDir() );
$dstPath = $tmpFile->getPath();
$err = $handler->rotate( $file, [
'srcPath' => $srcPath,
diff --git a/includes/filebackend/FSFile.php b/includes/filebackend/FSFile.php
index 8aa11b6..221fe9f 100644
--- a/includes/filebackend/FSFile.php
+++ b/includes/filebackend/FSFile.php
@@ -86,15 +86,6 @@
}
 
/**
-* Guess the MIME type from the file contents alone
-*
-* @return string
-*/
-   public function getMimeType() {
-   return MimeMagic::singleton()->guessMimeType( $this->path, 
false );
-   }
-
-   /**
 * Get an associative array containing information about
 * a file with the given storage path.
 *
@@ -117,8 +108,6 @@
 * @return array
 */
public function getProps( $ext = true ) {
-   wfDebug( __METHOD__ . ": Getting file info for $this->path\n" );
-
$info = self::placeholderProps();
$info['fileExists'] = $this->exists();
 
@@ -131,7 +120,7 @@
}
 
# MIME type according to file contents
-   $info['file-mime'] = $this->getMimeType();
+   $info['file-mime'] = $magic->guessMimeType( 
$this->path, false );
# logical MIME type
$info['mime'] = $magic->improveTypeFromExtension( 
$info['file-mime'], $ext );
 
@@ -145,17 +134,15 @@
$handler = MediaHandler::getHandler( $info[

[MediaWiki-commits] [Gerrit] mediawiki/core[master]: FSFile and TempFSFile cleanups

2016-09-18 Thread Aaron Schulz (Code Review)
Aaron Schulz has uploaded a new change for review.

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

Change subject: FSFile and TempFSFile cleanups
..

FSFile and TempFSFile cleanups

* Remove wf* function dependencies. This includes wfTempDir().
  Callers now should specify the directory, though it will try to do
  most of the wfTempDir() logic anyway if they do not.
* Update callers to inject wfTempDir() so $wgTmpDirectory is used by
  TempFSFile instead of it proping to find a valid directory itself.
* Move most of the wfTempDir() logic to TempFSFile::getUsableTempDirectory().
* Remove unused getMimeType() method.

Change-Id: Idd55936b07f9448a6c90577708722b7b52b8fe66
---
M includes/GlobalFunctions.php
M includes/api/ApiImageRotate.php
M includes/filebackend/FSFile.php
M includes/filebackend/TempFSFile.php
M includes/filerepo/file/File.php
M includes/upload/UploadFromChunks.php
M includes/upload/UploadFromUrl.php
M tests/phpunit/includes/filebackend/FileBackendTest.php
M tests/phpunit/includes/filerepo/MigrateFileRepoLayoutTest.php
M tests/phpunit/mocks/filebackend/MockFSFile.php
10 files changed, 57 insertions(+), 62 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/62/311362/1

diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php
index 90bba53..e5f518f 100644
--- a/includes/GlobalFunctions.php
+++ b/includes/GlobalFunctions.php
@@ -2078,35 +2078,7 @@
return $wgTmpDirectory;
}
 
-   $tmpDir = array_map( "getenv", [ 'TMPDIR', 'TMP', 'TEMP' ] );
-   $tmpDir[] = sys_get_temp_dir();
-   $tmpDir[] = ini_get( 'upload_tmp_dir' );
-
-   foreach ( $tmpDir as $tmp ) {
-   if ( $tmp && file_exists( $tmp ) && is_dir( $tmp ) && 
is_writable( $tmp ) ) {
-   return $tmp;
-   }
-   }
-
-   /**
-* PHP on Windows will detect C:\Windows\Temp as not writable even 
though PHP can write to it
-* so create a directory within that called 'mwtmp' with a suffix of 
the user running the
-* current process.
-* The user is included as if various scripts are run by different 
users they will likely
-* not be able to access each others temporary files.
-*/
-   if ( wfIsWindows() ) {
-   $tmp = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'mwtmp' . '-' 
. get_current_user();
-   if ( !file_exists( $tmp ) ) {
-   mkdir( $tmp );
-   }
-   if ( file_exists( $tmp ) && is_dir( $tmp ) && is_writable( $tmp 
) ) {
-   return $tmp;
-   }
-   }
-
-   throw new MWException( 'No writable temporary directory could be found. 
' .
-   'Please set $wgTmpDirectory to a writable directory.' );
+   return TempFSFile::getUsableTempDirectory();
 }
 
 /**
diff --git a/includes/api/ApiImageRotate.php b/includes/api/ApiImageRotate.php
index 2b99353..966bcbf 100644
--- a/includes/api/ApiImageRotate.php
+++ b/includes/api/ApiImageRotate.php
@@ -108,7 +108,7 @@
continue;
}
$ext = strtolower( pathinfo( "$srcPath", 
PATHINFO_EXTENSION ) );
-   $tmpFile = TempFSFile::factory( 'rotate_', $ext );
+   $tmpFile = TempFSFile::factory( 'rotate_', $ext, 
wfTempDir() );
$dstPath = $tmpFile->getPath();
$err = $handler->rotate( $file, [
'srcPath' => $srcPath,
diff --git a/includes/filebackend/FSFile.php b/includes/filebackend/FSFile.php
index 8aa11b6..221fe9f 100644
--- a/includes/filebackend/FSFile.php
+++ b/includes/filebackend/FSFile.php
@@ -86,15 +86,6 @@
}
 
/**
-* Guess the MIME type from the file contents alone
-*
-* @return string
-*/
-   public function getMimeType() {
-   return MimeMagic::singleton()->guessMimeType( $this->path, 
false );
-   }
-
-   /**
 * Get an associative array containing information about
 * a file with the given storage path.
 *
@@ -117,8 +108,6 @@
 * @return array
 */
public function getProps( $ext = true ) {
-   wfDebug( __METHOD__ . ": Getting file info for $this->path\n" );
-
$info = self::placeholderProps();
$info['fileExists'] = $this->exists();
 
@@ -131,7 +120,7 @@
}
 
# MIME type according to file contents
-   $info['file-mime'] = $this->getMimeType();
+   $info['file-mime'] = $magic->guessMimeType( 
$this->path, false );
# logical MIME type
$info['mime'] = $magic->improveTypeFromExtension( 
$info['file-mime'], $ext );
 
@@ -145,17 +134,15 @@