4 require_once(dirname(dirname(__FILE__)).'/lib/init-cmd.php');
5 ini_set('memory_limit', '800M');
8 "Create and setup nominatim search system",
9 array('help', 'h', 0, 1, 0, 0, false, 'Show Help'),
10 array('quiet', 'q', 0, 1, 0, 0, 'bool', 'Quiet output'),
11 array('verbose', 'v', 0, 1, 0, 0, 'bool', 'Verbose output'),
13 array('all', '', 0, 1, 1, 1, 'realpath', 'Do the complete process'),
15 array('create-db', '', 0, 1, 0, 0, 'bool', 'Create nominatim db'),
16 array('setup-db', '', 0, 1, 0, 0, 'bool', 'Build a blank nominatim db'),
17 array('import-data', '', 0, 1, 1, 1, 'realpath', 'Import a osm file'),
18 array('create-functions', '', 0, 1, 0, 0, 'bool', 'Create functions'),
19 array('create-tables', '', 0, 1, 0, 0, 'bool', 'Create main tables'),
20 array('create-partitions', '', 0, 1, 0, 0, 'bool', 'Create required partition tables and triggers'),
21 array('load-data', '', 0, 1, 0, 0, 'bool', 'Copy data to live tables from import table'),
23 getCmdOpt($_SERVER['argv'], $aCMDOptions, $aCMDResult, true, true);
25 $bDidSomething = false;
27 if ($aCMDResult['create-db'] || isset($aCMDResult['all']))
29 $bDidSomething = true;
30 $oDB =& DB::connect(CONST_Database_DSN, false);
31 if (!PEAR::isError($oDB))
33 fail('database already exists');
35 passthru('createdb nominatim');
38 if ($aCMDResult['create-db'] || isset($aCMDResult['all']))
40 $bDidSomething = true;
41 // TODO: path detection, detection memory, etc.
44 passthru('createlang plpgsql nominatim');
45 pgsqlRunScriptFile(CONST_Path_Postgresql_Contrib.'/_int.sql');
46 pgsqlRunScriptFile(CONST_Path_Postgresql_Contrib.'/hstore.sql');
47 pgsqlRunScriptFile(CONST_Path_Postgresql_Postgis.'/postgis.sql');
48 pgsqlRunScriptFile(CONST_Path_Postgresql_Postgis.'/spatial_ref_sys.sql');
49 pgsqlRunScriptFile(CONST_BasePath.'/data/country_name.sql');
50 pgsqlRunScriptFile(CONST_BasePath.'/data/country_osm_grid.sql');
51 pgsqlRunScriptFile(CONST_BasePath.'/data/gb_postcode.sql');
52 pgsqlRunScriptFile(CONST_BasePath.'/data/us_statecounty.sql');
53 pgsqlRunScriptFile(CONST_BasePath.'/data/us_state.sql');
54 pgsqlRunScriptFile(CONST_BasePath.'/data/worldboundaries.sql');
57 if (isset($aCMDResult['all']) && !isset($aCMDResult['import-data'])) $aCMDResult['import-data'] = $aCMDResult['all'];
58 if (isset($aCMDResult['import-data']) && $aCMDResult['import-data'])
60 $bDidSomething = true;
61 passthru(CONST_BasePath.'/osm2pgsql/osm2pgsql -lsc -O gazetteer -C 10000 --hstore -d nominatim '.$aCMDResult['import-data']);
64 if ($aCMDResult['create-functions'] || isset($aCMDResult['all']))
66 $bDidSomething = true;
67 $sTemplate = file_get_contents(CONST_BasePath.'/sql/functions.sql');
68 $sTemplate = str_replace('{modulepath}',CONST_BasePath.'/module', $sTemplate);
69 pgsqlRunScript($sTemplate);
72 if ($aCMDResult['create-tables'] || isset($aCMDResult['all']))
74 $bDidSomething = true;
75 pgsqlRunScriptFile(CONST_BasePath.'/sql/tables.sql');
78 if ($aCMDResult['create-partitions'] || isset($aCMDResult['all']))
80 $bDidSomething = true;
82 $sSQL = 'select distinct country_code from country_name order by country_code';
83 $aPartitions = $oDB->getCol($sSQL);
84 if (PEAR::isError($aPartitions))
86 fail($aPartitions->getMessage());
88 $aPartitions[] = 'none';
90 $sTemplate = file_get_contents(CONST_BasePath.'/sql/partitions.src.sql');
91 preg_match_all('#^-- start(.*?)^-- end#ms', $sTemplate, $aMatches, PREG_SET_ORDER);
92 foreach($aMatches as $aMatch)
95 foreach($aPartitions as $sPartitionName)
97 $sResult .= str_replace('-partition-', $sPartitionName, $aMatch[1]);
99 $sTemplate = str_replace($aMatch[0], $sResult, $sTemplate);
101 pgsqlRunScript($sTemplate);
104 if ($aCMDResult['load-data'] || isset($aCMDResult['all']))
106 $bDidSomething = true;
107 pgsqlRunScriptFile(CONST_BasePath.'/sql/loaddata.sql');
112 showUsage($aCMDOptions, true);
115 function pgsqlRunScriptFile($sFilename)
117 if (!file_exists($sFilename)) fail('unable to find '.$sFilename);
119 // Convert database DSN to psql paramaters
120 $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
121 $sCMD = 'psql -f '.$sFilename.' '.$aDSNInfo['database'];
123 $aDescriptors = array(
124 0 => array('pipe', 'r'),
125 1 => array('pipe', 'w'),
126 2 => array('file', '/dev/null', 'a')
129 $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes);
130 if (!is_resource($hProcess)) fail('unable to start pgsql');
134 // TODO: error checking
135 while(!feof($ahPipes[1]))
137 echo fread($ahPipes[1], 4096);
141 proc_close($hProcess);
144 function pgsqlRunScript($sScript)
146 // Convert database DSN to psql paramaters
147 $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
148 $sCMD = 'psql '.$aDSNInfo['database'];
150 $aDescriptors = array(
151 0 => array('pipe', 'r'),
152 1 => array('pipe', 'w'),
153 2 => array('file', '/dev/null', 'a')
156 $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes);
157 if (!is_resource($hProcess)) fail('unable to start pgsql');
159 fwrite($ahPipes[0], $sScript);
162 // TODO: error checking
163 while(!feof($ahPipes[1]))
165 echo fread($ahPipes[1], 4096);
169 proc_close($hProcess);