* (bug 2178) Use TMPDIR, TMP, or TEMP environment variable for temp dir
authorBrion Vibber <brion@users.mediawiki.org>
Sun, 15 May 2005 10:37:56 +0000 (10:37 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Sun, 15 May 2005 10:37:56 +0000 (10:37 +0000)
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
includes/GlobalFunctions.php
includes/ParserXML.php
maintenance/parserTests.php
tests/GlobalTest.php

index 102f183..3f93e91 100644 (file)
@@ -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 <td> and <th>
+* Group table renamed.....?
+* (bug 2178) Use temp dir from environment in parser tests
 
 
 === Caveats ===
index 3f9026a..aa3f6d3 100644 (file)
@@ -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';
+}
+
 ?>
index 4b97a5b..f8b3b9b 100644 (file)
@@ -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);
index 184f876..9de5751 100644 (file)
@@ -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 );
index 9cb398d..a7da66a 100644 (file)
@@ -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() {