function &getDB($bNew = false, $bPersistent = false)
{
// Get the database object
- $oDB = DB::connect(CONST_Database_DSN.($bNew?'?new_link=true':''), $bPersistent);
- if (PEAR::IsError($oDB))
- {
- var_dump(CONST_Database_DSN);
- var_Dump($oDB);
- fail($oDB->getMessage());
- }
+ $oDB = chksql(DB::connect(CONST_Database_DSN.($bNew?'?new_link=true':''), $bPersistent),
+ "Failed to establish database connection");
$oDB->setFetchMode(DB_FETCHMODE_ASSOC);
$oDB->query("SET DateStyle TO 'sql,european'");
$oDB->query("SET client_encoding TO 'utf-8'");
<?php
- function failInternalError($sError, $sSQL = false, $vDumpVar = false)
- {
- header('HTTP/1.0 500 Internal Server Error');
- header('Content-type: text/html; charset=utf-8');
- echo "<html><body><h1>Internal Server Error</h1>";
- echo '<p>Nominatim has encountered an internal error while processing your request. This is most likely because of a bug in the software.</p>';
- echo "<p><b>Details:</b> ".$sError,"</p>";
- echo '<p>Feel free to report the bug in the <a href="http://trac.openstreetmap.org">OSM bug database</a>. Please include the error message above and the URL you used.</p>';
- if (CONST_Debug)
- {
- echo "<hr><h2>Debugging Information</h2><br>";
- if ($sSQL)
- {
- echo "<h3>SQL query</h3><code>".$sSQL."</code>";
- }
- if ($vDumpVar)
- {
- echo "<h3>Result</h3> <code>";
- var_dump($vDumpVar);
- echo "</code>";
- }
- }
- echo "\n</body></html>\n";
- exit;
- }
-
-
- function userError($sError)
- {
- header('HTTP/1.0 400 Bad Request');
- header('Content-type: text/html; charset=utf-8');
- echo "<html><body><h1>Bad Request</h1>";
- echo '<p>Nominatim has encountered an error with your request.</p>';
- echo "<p><b>Details:</b> ".$sError,"</p>";
- echo '<p>If you feel this error is incorrect feel free to report the bug in the <a href="http://trac.openstreetmap.org">OSM bug database</a>. Please include the error message above and the URL you used.</p>';
- echo "\n</body></html>\n";
- exit;
- }
-
function fail($sError, $sUserError = false)
{
if (!$sUserError) $sUserError = $sError;
<?php
+/***************************************************************************
+ *
+ * Error handling functions
+ *
+ */
+ function chksql($oSql, $sMsg = "Database request failed")
+ {
+ if (!PEAR::isError($oSql)) return $oSql;
+
+ header('HTTP/1.0 500 Internal Server Error');
+ header('Content-type: text/html; charset=utf-8');
+
+ $sSqlError = $oSql->getMessage();
+
+ echo <<<INTERNALFAIL
+ <html>
+ <head><title>Internal Server Error</title></head>
+ <body>
+ <h1>Internal Server Error</h1>
+ <p>Nominatim has encountered an internal error while accessing the database.
+ This may happen because the database is broken or because of a bug in
+ the software. If you think it is a bug, feel free to report
+ it over on <a href="https://github.com/twain47/Nominatim/issues">
+ Github</a>. Please include the URL that caused the problem and the
+ complete error details below.</p>
+ <p><b>Message:</b> $sMsg</p>
+ <p><b>SQL Error:</b> $sSqlError</p>
+ <p><b>Details:</b> <pre>
+INTERNALFAIL;
+
+ if (CONST_Debug)
+ {
+ var_dump($oSql);
+ }
+ else
+ {
+ echo "<pre>\n".$oSql->getUserInfo()."</pre>";
+ }
+
+ echo "</pre></p></body></head></html>";
+ exit;
+ }
+
+ function failInternalError($sError, $sSQL = false, $vDumpVar = false)
+ {
+ header('HTTP/1.0 500 Internal Server Error');
+ header('Content-type: text/html; charset=utf-8');
+ echo "<html><body><h1>Internal Server Error</h1>";
+ echo '<p>Nominatim has encountered an internal error while processing your request. This is most likely because of a bug in the software.</p>';
+ echo "<p><b>Details:</b> ".$sError,"</p>";
+ echo '<p>Feel free to file an issue on <a href="https://github.com/twain47/Nominatim/issues">Github</a>. Please include the error message above and the URL you used.</p>';
+ if (CONST_Debug)
+ {
+ echo "<hr><h2>Debugging Information</h2><br>";
+ if ($sSQL)
+ {
+ echo "<h3>SQL query</h3><code>".$sSQL."</code>";
+ }
+ if ($vDumpVar)
+ {
+ echo "<h3>Result</h3> <code>";
+ var_dump($vDumpVar);
+ echo "</code>";
+ }
+ }
+ echo "\n</body></html>\n";
+ exit;
+ }
+
+
+ function userError($sError)
+ {
+ header('HTTP/1.0 400 Bad Request');
+ header('Content-type: text/html; charset=utf-8');
+ echo "<html><body><h1>Bad Request</h1>";
+ echo '<p>Nominatim has encountered an error with your request.</p>';
+ echo "<p><b>Details:</b> ".$sError,"</p>";
+ echo '<p>If you feel this error is incorrect feel file an issue on <a href="https://github.com/twain47/Nominatim/issues">Github</a>. Please include the error message above and the URL you used.</p>';
+ echo "\n</body></html>\n";
+ exit;
+ }
+
+
/***************************************************************************
*
* Functions for parsing URL parameters
$oDB =& getDB();
$sSQL = "select placex.place_id, calculated_country_code as country_code, name->'name' as name, i.* from placex, import_polygon_delete i where placex.osm_id = i.osm_id and placex.osm_type = i.osm_type and placex.class = i.class and placex.type = i.type";
- $aPolygons = $oDB->getAll($sSQL);
- if (PEAR::isError($aPolygons))
+ $aPolygons = chksql($oDB->getAll($sSQL),
+ "Could not get list of deleted OSM elements.");
+
+ if (CONST_DEBUG)
{
- failInternalError("Could not get list of deleted OSM elements.", $sSQL, $aPolygons);
+ var_dump($aPolygons);
+ exit;
}
-
-//var_dump($aPolygons);
?>
<!DOCTYPE html>
<html>
$iOsmId = getParamInt('osmid', -1);
if ($sOsmType && $iOsmId > 0)
{
- $sPlaceId = $oDB->getOne("select place_id from placex where osm_type = '".$sOsmType."' and osm_id = ".$iOsmId." order by type = 'postcode' asc");
+ $sPlaceId = chksql($oDB->getOne("select place_id from placex where osm_type = '".$sOsmType."' and osm_id = ".$iOsmId." order by type = 'postcode' asc"));
// Be nice about our error messages for broken geometry
if (!$sPlaceId)
{
- $aPointDetails = $oDB->getRow("select osm_type, osm_id, errormessage, class, type, get_name_by_language(name,$sLanguagePrefArraySQL) as localname, ST_AsText(prevgeometry) as prevgeom, ST_AsText(newgeometry) as newgeom from import_polygon_error where osm_type = '".$sOsmType."' and osm_id = ".$iOsmId." order by updated desc limit 1");
+ $aPointDetails = chksql($oDB->getRow("select osm_type, osm_id, errormessage, class, type, get_name_by_language(name,$sLanguagePrefArraySQL) as localname, ST_AsText(prevgeometry) as prevgeom, ST_AsText(newgeometry) as newgeom from import_polygon_error where osm_type = '".$sOsmType."' and osm_id = ".$iOsmId." order by updated desc limit 1"));
if (!PEAR::isError($aPointDetails) && $aPointDetails) {
if (preg_match('/\[(-?\d+\.\d+) (-?\d+\.\d+)\]/', $aPointDetails['errormessage'], $aMatches))
{
if (CONST_Use_US_Tiger_Data)
{
- $iParentPlaceID = $oDB->getOne('select parent_place_id from location_property_tiger where place_id = '.$iPlaceID);
+ $iParentPlaceID = chksql($oDB->getOne('select parent_place_id from location_property_tiger where place_id = '.$iPlaceID));
if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
}
if (CONST_Use_Aux_Location_data)
{
- $iParentPlaceID = $oDB->getOne('select parent_place_id from location_property_aux where place_id = '.$iPlaceID);
+ $iParentPlaceID = chksql($oDB->getOne('select parent_place_id from location_property_aux where place_id = '.$iPlaceID));
if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
}
$hLog = logStart($oDB, 'details', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
- // Make sure the point we are reporting on is fully indexed
- //$sSQL = "UPDATE placex set indexed = true where indexed = false and place_id = $iPlaceID";
- //$oDB->query($sSQL);
-
// Get the details for this point
$sSQL = "select place_id, osm_type, osm_id, class, type, name, admin_level, housenumber, street, isin, postcode, calculated_country_code as country_code, importance, wikipedia,";
$sSQL .= " to_char(indexed_date, 'YYYY-MM-DD HH24:MI') as indexed_date, parent_place_id, rank_address, rank_search, get_searchrank_label(rank_search) as rank_search_label, get_name_by_language(name,$sLanguagePrefArraySQL) as localname, ";
$sSQL .= " case when importance = 0 OR importance IS NULL then 0.75-(rank_search::float/40) else importance end as calculated_importance, ";
$sSQL .= " ST_AsText(CASE WHEN ST_NPoints(geometry) > 5000 THEN ST_SimplifyPreserveTopology(geometry, 0.0001) ELSE geometry END) as outlinestring";
$sSQL .= " from placex where place_id = $iPlaceID";
- $aPointDetails = $oDB->getRow($sSQL);
- if (PEAR::IsError($aPointDetails))
- {
- failInternalError("Could not get details of place object.", $sSQL, $aPointDetails);
- }
+ $aPointDetails = chksql($oDB->getRow($sSQL),
+ "Could not get details of place object.");
$aPointDetails['localname'] = $aPointDetails['localname']?$aPointDetails['localname']:$aPointDetails['housenumber'];
$aClassType = getClassTypesWithImportance();
if ($sOutputFormat=='html')
{
- $sDataDate = $oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1");
+ $sDataDate = chksql($oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1"));
$sTileURL = CONST_Map_Tile_URL;
$sTileAttribution = CONST_Map_Tile_Attribution;
}
$iOsmId = getParamInt('osmid', -1);
if ($sOsmType && $iOsmId > 0)
{
- $sPlaceId = $oDB->getOne("select place_id from placex where osm_type = '".$sOsmType."' and osm_id = ".$iOsmId." order by type = 'postcode' asc");
+ $sPlaceId = chksql($oDB->getOne("select place_id from placex where osm_type = '".$sOsmType."' and osm_id = ".$iOsmId." order by type = 'postcode' asc"));
// Be nice about our error messages for broken geometry
if (!$sPlaceId)
{
- $aPointDetails = $oDB->getRow("select osm_type, osm_id, errormessage, class, type, get_name_by_language(name,$sLanguagePrefArraySQL) as localname, ST_AsText(prevgeometry) as prevgeom, ST_AsText(newgeometry) as newgeom from import_polygon_error where osm_type = '".$sOsmType."' and osm_id = ".$iOsmId." order by updated desc limit 1");
- if (!PEAR::isError($aPointDetails) && $aPointDetails) {
+ $aPointDetails = chksql($oDB->getRow("select osm_type, osm_id, errormessage, class, type, get_name_by_language(name,$sLanguagePrefArraySQL) as localname, ST_AsText(prevgeometry) as prevgeom, ST_AsText(newgeometry) as newgeom from import_polygon_error where osm_type = '".$sOsmType."' and osm_id = ".$iOsmId." order by updated desc limit 1"));
+ if ($aPointDetails) {
if (preg_match('/\[(-?\d+\.\d+) (-?\d+\.\d+)\]/', $aPointDetails['errormessage'], $aMatches))
{
$aPointDetails['error_x'] = $aMatches[1];
if (CONST_Use_US_Tiger_Data)
{
- $iParentPlaceID = $oDB->getOne('select parent_place_id from location_property_tiger where place_id = '.$iPlaceID);
+ $iParentPlaceID = chksql($oDB->getOne('select parent_place_id from location_property_tiger where place_id = '.$iPlaceID));
if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
}
if (CONST_Use_Aux_Location_data)
{
- $iParentPlaceID = $oDB->getOne('select parent_place_id from location_property_aux where place_id = '.$iPlaceID);
+ $iParentPlaceID = chksql($oDB->getOne('select parent_place_id from location_property_aux where place_id = '.$iPlaceID));
if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
}
exit;
}
- $aRelatedPlaceIDs = $oDB->getCol($sSQL = "select place_id from placex where linked_place_id = $iPlaceID or place_id = $iPlaceID");
+ $aRelatedPlaceIDs = chksql($oDB->getCol($sSQL = "select place_id from placex where linked_place_id = $iPlaceID or place_id = $iPlaceID"));
$sSQL = "select obj.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') as isarea, st_area(geometry) as area, ";
$sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) as localname, length(name::text) as namelength ";
$sSQL .= " from (select placex.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, rank_search, geometry, name from placex ";
$sSQL .= " where parent_place_id in (".join(',',$aRelatedPlaceIDs).") and name is not null order by rank_address asc,rank_search asc limit 500) as obj";
$sSQL .= " order by rank_address asc,rank_search asc,localname,class, type,housenumber";
- $aParentOfLines = $oDB->getAll($sSQL);
+ $aParentOfLines = chksql($oDB->getAll($sSQL));
if (sizeof($aParentOfLines))
{
$bReduced = getParamBool('reduced', false);
$sClass = getParamString('class', false);
- $iTotalBroken = (int) $oDB->getOne('select count(*) from import_polygon_error');
+ $iTotalBroken = (int) chksql($oDB->getOne('select count(*) from import_polygon_error'));
$aPolygons = array();
while($iTotalBroken && !sizeof($aPolygons))
if ($bReduced) $sSQL .= " and errormessage like 'Area reduced%'";
if ($sClass) $sSQL .= " and class = '".pg_escape_string($sClass)."'";
$sSQL .= " order by updated desc limit 1000";
- $aPolygons = $oDB->getAll($sSQL);
+ $aPolygons = chksql($oDB->getAll($sSQL));
+ }
+
+ if (CONST_Debug)
+ {
+ var_dump($aPolygons);
+ exit;
}
-//var_dump($aPolygons);
?>
<!DOCTYPE html>
<html>
if ($sOutputFormat=='html')
{
- $sDataDate = $oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1");
+ $sDataDate = chksql($oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1"));
$sTileURL = CONST_Map_Tile_URL;
$sTileAttribution = CONST_Map_Tile_Attribution;
}
if ($sOutputFormat=='html')
{
- $sDataDate = $oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1");
+ $sDataDate = chksql($oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1"));
}
logEnd($oDB, $hLog, sizeof($aSearchResults));