]> git.openstreetmap.org Git - nominatim.git/blobdiff - utils/setup.php
Merge branch 'master' of github.com:twain47/Nominatim
[nominatim.git] / utils / setup.php
index 4b60815cd89fad0f41ef15fd7a9a175e677c8e0e..8254b6cc974440f7d962c787b7f7e160b912e207 100755 (executable)
                if (file_exists($sWikiArticlesFile))
                {
                        echo "Importing wikipedia articles...";
-                       pgsqlRunRestoreData($sWikiArticlesFile);
+                       pgsqlRunDropAndRestore($sWikiArticlesFile);
                        echo "...done\n";
                }
                else
                if (file_exists($sWikiRedirectsFile))
                {
                        echo "Importing wikipedia redirects...";
-                       pgsqlRunRestoreData($sWikiRedirectsFile);
+                       pgsqlRunDropAndRestore($sWikiRedirectsFile);
                        echo "...done\n";
                }
                else
 
                proc_close($hProcess);
        }
+
+       function pgsqlRunDropAndRestore($sDumpFile)
+       {
+               // Convert database DSN to psql paramaters
+               $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;
+
+               $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]);
+
+               proc_close($hProcess);
+       }