+ // Convert database DSN to psql parameters
+ $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
+ if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
+ $sCMD = 'psql -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'];
+
+ $ahGzipPipes = null;
+ if (preg_match('/\\.gz$/', $sFilename))
+ {
+ $aDescriptors = array(
+ 0 => array('pipe', 'r'),
+ 1 => array('pipe', 'w'),
+ 2 => array('file', '/dev/null', 'a')
+ );
+ $hGzipProcess = proc_open('zcat '.$sFilename, $aDescriptors, $ahGzipPipes);
+ if (!is_resource($hGzipProcess)) fail('unable to start zcat');
+ $aReadPipe = $ahGzipPipes[1];
+ fclose($ahGzipPipes[0]);
+ }
+ else
+ {
+ $sCMD .= ' -f '.$sFilename;
+ $aReadPipe = array('pipe', 'r');
+ }
+
+ $aDescriptors = array(
+ 0 => $aReadPipe,
+ 1 => array('pipe', 'w'),
+ 2 => array('file', '/dev/null', 'a')
+ );
+ $ahPipes = null;
+ $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes);
+ if (!is_resource($hProcess)) fail('unable to start pgsql');
+
+
+ // TODO: error checking
+ while(!feof($ahPipes[1]))
+ {
+ echo fread($ahPipes[1], 4096);
+ }
+ fclose($ahPipes[1]);
+
+ $iReturn = proc_close($hProcess);
+ if ($iReturn > 0)
+ {
+ fail("pgsql returned with error code ($iReturn)");
+ }
+ if ($ahGzipPipes)
+ {
+ fclose($ahGzipPipes[1]);
+ proc_close($hGzipProcess);
+ }
+
+ }
+
+ function pgsqlRunScript($sScript)
+ {
+ // Convert database DSN to psql parameters
+ $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
+ if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
+ $sCMD = 'psql -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'];
+ $sCMD .= ' -v ON_ERROR_STOP=1';
+ $aDescriptors = array(
+ 0 => array('pipe', 'r'),
+ 1 => STDOUT,
+ 2 => STDERR
+ );
+ $ahPipes = null;
+ $hProcess = @proc_open($sCMD, $aDescriptors, $ahPipes);
+ if (!is_resource($hProcess)) fail('unable to start pgsql');
+
+ while(strlen($sScript))
+ {
+ $written = fwrite($ahPipes[0], $sScript);
+ $sScript = substr($sScript, $written);
+ }
+ fclose($ahPipes[0]);
+ $iReturn = proc_close($hProcess);
+ if ($iReturn > 0)
+ {
+ fail("pgsql returned with error code ($iReturn)");
+ }
+ }
+
+ function pgsqlRunRestoreData($sDumpFile)
+ {
+ // Convert database DSN to psql parameters