+function pgsqlRunScript($sScript, $bfatal = true)
+{
+ global $aCMDResult;
+ runSQLScript(
+ $sScript,
+ $bfatal,
+ $aCMDResult['verbose'],
+ $aCMDResult['ignore-errors']
+ );
+}
+
+function pgsqlRunPartitionScript($sTemplate)
+{
+ global $aCMDResult;
+ $oDB =& getDB();
+
+ $sSQL = 'select distinct partition from country_name';
+ $aPartitions = chksql($oDB->getCol($sSQL));
+ if (!$aCMDResult['no-partitions']) $aPartitions[] = 0;
+
+ preg_match_all('#^-- start(.*?)^-- end#ms', $sTemplate, $aMatches, PREG_SET_ORDER);
+ foreach ($aMatches as $aMatch) {
+ $sResult = '';
+ foreach ($aPartitions as $sPartitionName) {
+ $sResult .= str_replace('-partition-', $sPartitionName, $aMatch[1]);
+ }
+ $sTemplate = str_replace($aMatch[0], $sResult, $sTemplate);
+ }
+
+ pgsqlRunScript($sTemplate);
+}
+
+function pgsqlRunRestoreData($sDumpFile)
+{
+ // Convert database DSN to psql parameters
+ $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
+ if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
+ $sCMD = 'pg_restore -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'].' -Fc -a '.$sDumpFile;
+
+ $aDescriptors = array(
+ 0 => array('pipe', 'r'),
+ 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 pg_restore');
+
+ fclose($ahPipes[0]);
+
+ // TODO: error checking
+ while (!feof($ahPipes[1])) {
+ echo fread($ahPipes[1], 4096);
+ }
+ fclose($ahPipes[1]);
+
+ $iReturn = proc_close($hProcess);
+}
+
+function pgsqlRunDropAndRestore($sDumpFile)
+{
+ // Convert database DSN to psql parameters
+ $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
+ if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
+ $sCMD = 'pg_restore -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'].' -Fc --clean '.$sDumpFile;
+ if (isset($aDSNInfo['hostspec']) && $aDSNInfo['hostspec']) {
+ $sCMD .= ' -h ' . $aDSNInfo['hostspec'];
+ }
+ if (isset($aDSNInfo['username']) && $aDSNInfo['username']) {
+ $sCMD .= ' -U ' . $aDSNInfo['username'];
+ }
+ $aProcEnv = null;
+ if (isset($aDSNInfo['password']) && $aDSNInfo['password']) {
+ $aProcEnv = array_merge(array('PGPASSWORD' => $aDSNInfo['password']), $_ENV);
+ }
+
+ $iReturn = runWithEnv($sCMD, $aProcEnv);
+}
+
+function passthruCheckReturn($sCmd)
+{
+ $iResult = -1;
+ passthru($sCmd, $iResult);
+}
+
+function replace_tablespace($sTemplate, $sTablespace, $sSql)
+{
+ if ($sTablespace) {
+ $sSql = str_replace($sTemplate, 'TABLESPACE "'.$sTablespace.'"', $sSql);
+ } else {
+ $sSql = str_replace($sTemplate, '', $sSql);
+ }
+
+ return $sSql;
+}
+
+function create_sql_functions($aCMDResult)
+{
+ global $sModulePath;
+ $sTemplate = file_get_contents(CONST_BasePath.'/sql/functions.sql');
+ $sTemplate = str_replace('{modulepath}', $sModulePath, $sTemplate);
+ if ($aCMDResult['enable-diff-updates']) {
+ $sTemplate = str_replace('RETURN NEW; -- %DIFFUPDATES%', '--', $sTemplate);
+ }
+ if ($aCMDResult['enable-debug-statements']) {
+ $sTemplate = str_replace('--DEBUG:', '', $sTemplate);
+ }
+ if (CONST_Limit_Reindexing) {
+ $sTemplate = str_replace('--LIMIT INDEXING:', '', $sTemplate);
+ }
+ if (!CONST_Use_US_Tiger_Data) {
+ $sTemplate = str_replace('-- %NOTIGERDATA% ', '', $sTemplate);
+ }
+ if (!CONST_Use_Aux_Location_data) {
+ $sTemplate = str_replace('-- %NOAUXDATA% ', '', $sTemplate);
+ }
+ pgsqlRunScript($sTemplate);
+}
+
+function checkModulePresence()
+{
+ // Try accessing the C module, so we know early if something is wrong
+ // and can simply error out.
+ global $sModulePath;
+ $sSQL = "CREATE FUNCTION nominatim_test_import_func(text) RETURNS text AS '";
+ $sSQL .= $sModulePath."/nominatim.so', 'transliteration' LANGUAGE c IMMUTABLE STRICT";
+ $sSQL .= ';DROP FUNCTION nominatim_test_import_func(text);';
+
+ $oDB =& getDB();
+ $oResult = $oDB->query($sSQL);
+
+ $bResult = true;
+
+ if (PEAR::isError($oResult)) {
+ echo "\nERROR: Failed to load nominatim module. Reason:\n";
+ echo $oResult->userinfo."\n\n";
+ $bResult = false;
+ }
+
+ return $bResult;
+}