From e19a4391b5b861d60e9b3accafc62e9e2a87ef4c Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Sun, 15 May 2005 10:37:56 +0000 Subject: [PATCH] * (bug 2178) Use TMPDIR, TMP, or TEMP environment variable for temp dir before trying /tmp; new wfTempDir() function to centralize this * Fix parser test database usage; runs on a single connection again by not creating new LoadBalancer instances all the time --- RELEASE-NOTES | 2 ++ includes/GlobalFunctions.php | 23 ++++++++++++++++++++++- includes/ParserXML.php | 4 ++-- maintenance/parserTests.php | 7 ++++--- tests/GlobalTest.php | 2 +- 5 files changed, 31 insertions(+), 7 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 102f183845..3f93e911f3 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -185,6 +185,8 @@ Various bugfixes, small features, and a few experimental things: revisions only or complete histories. * (bug 2150) Fix tab indexes on edit form * (bug 2152) Add missing bgcolor to attribute whitelist for and +* Group table renamed.....? +* (bug 2178) Use temp dir from environment in parser tests === Caveats === diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 3f9026a2e5..aa3f6d310b 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -822,7 +822,7 @@ function wfMerge( $old, $mine, $yours, &$result ){ } # Make temporary files - $td = '/tmp/'; + $td = wfTempDir(); $oldtextFile = fopen( $oldtextName = tempnam( $td, 'merge-old-' ), 'w' ); $mytextFile = fopen( $mytextName = tempnam( $td, 'merge-mine-' ), 'w' ); $yourtextFile = fopen( $yourtextName = tempnam( $td, 'merge-your-' ), 'w' ); @@ -1229,4 +1229,25 @@ function wfElementClean( $element, $attribs = array(), $contents = '') { return wfElement( $element, $attribs, UtfNormal::cleanUp( $contents ) ); } +/** + * Tries to get the system directory for temporary files. + * The TMPDIR, TMP, and TEMP environment variables are checked in sequence, + * and if none are set /tmp is returned as the generic Unix default. + * + * NOTE: When possible, use the tempfile() function to create temporary + * files to avoid race conditions on file creation, etc. + * + * @return string + */ +function wfTempDir() { + foreach( array( 'TMPDIR', 'TMP', 'TEMP' ) as $var ) { + $tmp = getenv( 'TMPDIR' ); + if( $tmp && file_exists( $tmp ) && is_dir( $tmp ) && is_writable( $tmp ) ) { + return $tmp; + } + } + # Hope this is Unix of some kind! + return '/tmp'; +} + ?> diff --git a/includes/ParserXML.php b/includes/ParserXML.php index 4b97a5beb8..f8b3b9b888 100644 --- a/includes/ParserXML.php +++ b/includes/ParserXML.php @@ -585,7 +585,7 @@ class ParserXML extends Parser { $html2xml = implode('/', $a); $a = array (); - $tmpfname = tempnam('/tmp', 'FOO'); + $tmpfname = tempnam( wfTempDir(), 'FOO' ); $handle = fopen($tmpfname, 'w'); fwrite($handle, utf8_encode($text)); fclose($handle); @@ -600,7 +600,7 @@ class ParserXML extends Parser { $this->html2xml($text); - $tmpfname = tempnam('/tmp', 'FOO'); + $tmpfname = tempnam( wfTempDir(), 'FOO'); $handle = fopen($tmpfname, 'w'); fwrite($handle, $text); fclose($handle); diff --git a/maintenance/parserTests.php b/maintenance/parserTests.php index 184f8761c0..9de5751da9 100644 --- a/maintenance/parserTests.php +++ b/maintenance/parserTests.php @@ -294,7 +294,6 @@ class ParserTest { 'wgDBprefix' => 'parsertest', 'wgDefaultUserOptions' => array(), - 'wgLoadBalancer' => LoadBalancer::newFromParams( $GLOBALS['wgDBservers'] ), 'wgLang' => new LanguageUtf8(), 'wgContLang' => new LanguageUtf8(), 'wgNamespacesWithSubpages' => array( 0 => preg_match('/\\bsubpage\\b/i', $opts)), @@ -344,6 +343,8 @@ class ParserTest { # Make sure we don't mess with the live DB if (!$setupDB && $wgDBprefix === 'parsertest') { + # oh teh horror + $GLOBALS['wgLoadBalancer'] = LoadBalancer::newFromParams( $GLOBALS['wgDBservers'] ); $db =& wfGetDB( DB_MASTER ); $tables = $this->listTables(); @@ -438,7 +439,7 @@ class ParserTest { function setupUploadDir() { global $IP; - $dir = "/tmp/mwParser-" . mt_rand() . "-images"; + $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images"; mkdir( $dir ); mkdir( $dir . '/3' ); mkdir( $dir . '/3/3a' ); @@ -546,7 +547,7 @@ class ParserTest { * @access private */ function quickDiff( $input, $output ) { - $prefix = "/tmp/mwParser-" . mt_rand(); + $prefix = wfTempDir() . "/mwParser-" . mt_rand(); $infile = "$prefix-expected"; $this->dumpToFile( $input, $infile ); diff --git a/tests/GlobalTest.php b/tests/GlobalTest.php index 9cb398df11..a7da66a069 100644 --- a/tests/GlobalTest.php +++ b/tests/GlobalTest.php @@ -18,7 +18,7 @@ class GlobalTest extends PHPUnit_TestCase { $this->save[$var] = $GLOBALS[$var]; } } - $GLOBALS['wgReadOnlyFile'] = '/tmp/testReadOnly-' . mt_rand(); + $GLOBALS['wgReadOnlyFile'] = wfTempDir() . '/testReadOnly-' . mt_rand(); } function tearDown() { -- 2.20.1