changeset 816:6341de08bd52

Handle alternative location names better;Add unique location ID generation to handle locations with exactly same primary name.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 18 Sep 2009 16:59:10 +0000
parents 42dbde04838e
children eaba088b5424
files www/common.inc.php www/latest.php www/loc.php www/query.php
diffstat 4 files changed, 112 insertions(+), 118 deletions(-) [+]
line wrap: on
line diff
--- a/www/common.inc.php	Fri Sep 18 16:57:57 2009 +0000
+++ b/www/common.inc.php	Fri Sep 18 16:59:10 2009 +0000
@@ -51,42 +51,58 @@
 {
   global $locationTypes;
   
-  $inFile = @fopen(basename($filename.".loc"), "r");
-  if (!$inFile)
-    return FALSE;
+  $file = @fopen(basename($filename.".loc"), "r");
+  if (!$file) return FALSE;
   
-  while (!feof($inFile)) {
-    $inLine = trim(fgets($inFile));
+  while (!feof($file)) {
+    $inLine = trim(fgets($file));
 
-    if (strlen($inLine) == 0 || $inLine[0] == "#") {
-    } else
-    if (preg_match("/^(\d+)\s*;\s*(\d+)\s*;\s*([0-3][A-Za-z%\?\-]*)\s*;\s*([^;]+)\s*;(.+)$/", $inLine, $m)) {
-      $addInfo = preg_split("/\s*;\s*/", $m[5]);
+    // Ignore comments and empty lines
+    if (strlen($inLine) == 0 || $inLine[0] == "#")
+      continue;
+
+    if (preg_match("/^(\d+)\s*;\s*(\d+)\s*;\s*([0-3][A-Za-z%\?\-]*)\s*;\s*([^;]+)\s*;(.+)$/", $inLine, $record)) {
+      // Split fields of the record
+      $addInfo = preg_split("/\s*;\s*/", $record[5]);
       
+      // Split coder names
       if (strlen($addInfo[0]) > 0)
         $coders = preg_split("/\s*,\s*/", $addInfo[0]);
       else
         $coders = array();
       
+      // Get timestamp
       if (strlen($addInfo[1]) > 0) {
         if (sscanf($addInfo[1], "%02d.%02d.%04d", $aday, $amonth, $ayear) == 3) {
           if (($added = mktime(0, 0, 0, $amonth, $aday, $ayear)) === FALSE)
             $added = -1;
         } else {
-          echo "Error: Location <b>".$m[4]."</b> has invalid timestamp '".$addInfo[1]."'"; 
+          echo "Error: Location <b>".$record[4]."</b> has invalid timestamp '".$addInfo[1]."'"; 
           $added = -1;
         }
       } else
         $added = -1;
       
-      if (!strstr($m[3], "-")) {
-        $filterMatch = preg_match("/[".$filter."]/", $m[3]);
+      // Check flags, completely ignore records with "-" flag
+      if (!strstr($record[3], "-")) {
+        $flags = $record[3];
+        $filterMatch = preg_match("/[".$filter."]/", $flags);
+        
         if (($applyFilter && $filterMatch) || (!$applyFilter && !$filterMatch)) {
           
-          $names = preg_split("/\s*\|\s*/", $m[4]);
+          // Get location names
+          $names = preg_split("/\s*\|\s*/", $record[4]);
+
+          // Check for duplicate locations
+          $id = $flags.$names[0];
+          if (isset($locations[$id])) {
+            echo $inLine;
+            echo "Error: Location '<b>".$id."</b>' already defined (".$locations[$id]["continent"]." <=> ".$filename.")<br />\n";
+            return FALSE;
+          }
           
+          // Create primary location name
           $s = "";
-          $flags = $m[3];
           $found = FALSE;
           for ($i = 0; $found === FALSE && $i < strlen($flags); $i++) {
             if (array_key_exists($flags[$i], $locationTypes)) {
@@ -97,24 +113,19 @@
           }
           $s .= $names[0];
           
-          if (isset($locations[$s])) {
-            echo "Error: Location <b>".$names[0]."</b> already defined (".$locations[$s]["continent"]." <=> ".$filename.")\n";
-            return FALSE;
-          }
-          
-          
-          $locations[$s] = array(
+          // Add location to array of locations
+          $locations[$id] = array(
             "name" => $s,
             "names" => $names,
             "continent" => $filename,
-            "x" => $m[1],
-            "y" => $m[2],
-            "flags" => $m[3],
+            "x" => $record[1],
+            "y" => $record[2],
+            "flags" => $record[3],
             "coders" => $coders,
             "added" => $added,
           );
-          if (strlen($addInfo[2]) > 0) { $locations[$s]["url"] = $addInfo[2]; }
-          if (strlen($addInfo[3]) > 0) { $locations[$s]["freeform"] = $addInfo[3]; }
+          if (strlen($addInfo[2]) > 0) { $locations[$id]["url"] = $addInfo[2]; }
+          if (strlen($addInfo[3]) > 0) { $locations[$id]["freeform"] = $addInfo[3]; }
         }
       }
     } else {
@@ -123,7 +134,7 @@
     }
   }
   
-  fclose($inFile);
+  fclose($file);
   return TRUE;
 }
 
@@ -134,79 +145,72 @@
   
   $locations = array();
   
-  reset($continentList);
-  while (list($iKey, $iValue) = each($continentList)) {
-    parseLocFile($iKey, $locations, $applyFilter, $filter);
-  }
+  foreach ($continentList as $id => $data)
+    parseLocFile($id, $locations, $applyFilter, $filter);
   
-  reset($specialList);
-  while (list($iKey, $iValue) = each($specialList)) {
-    parseLocFile($iKey, $locations, $applyFilter, $filter);
-  }
-  
-  reset($specialList);
-  reset($continentList);
-  
+  foreach ($specialList as $id => $data)
+    parseLocFile($id, $locations, $applyFilter, $filter);
+
   return $locations;
 }
 
 
-function submitWZ(&$wizTable, $field, $data)
+function submitWizField(&$table, $field, $data)
 {
   $s = trim($data);
-  if (strlen($s) > 0) $wizTable[$field] = $s;
+  if (strlen($s) > 0) $table[$field] = $s;
 }
 
 
-function submitWizInfo(&$wizTable, $m)
+function submitWizInfo(&$table, $record)
 {
-  $s = trim($m[1]);
-  if (isset($wizTable[$s])) {
+  $s = trim($record[1]);
+  if (isset($table[$s])) {
     echo "Error: <b>".$s."</b> is already set!<br />\n";
+    return FALSE;
   } else {
     $tmp = array("name" => $s);
-    submitWZ($tmp, "homeURL", $m[2]);
-    submitWZ($tmp, "imageURL", $m[3]);
-    submitWZ($tmp, "desc", $m[4]);
+    submitWizField($tmp, "homeURL", $record[2]);
+    submitWizField($tmp, "imageURL", $record[3]);
+    submitWizField($tmp, "desc", $record[4]);
     
     if (count($tmp) > 1)
-      $wizTable[$s] = $tmp;
+      $table[$s] = $tmp;
+    return TRUE;
   }
 }
 
 
-function parseWizInfoFile($filename, &$wizTable)
+function parseWizInfoFile($filename, &$table)
 {
-  $inFile = @fopen($filename, "r");
-  if (!$inFile) {
-    return FALSE;
-  }
+  $file = @fopen($filename, "r");
+  if (!$file) return FALSE;
   
   $contMode = FALSE;
-  while (!feof($inFile)) {
-    $inLine = trim(fgets($inFile));
+  while (!feof($file)) {
+    $line = trim(fgets($file));
     
     if ($contMode) {
-      if (substr($inLine, -1, 1) == "$") {
-        $m[4] .= " ".substr($inLine, 0, -1);
+      if (substr($line, -1, 1) == '$') {
+        $record[4] .= " ".substr($line, 0, -1);
         $contMode = FALSE;
-        submitWizInfo($wizTable, $m);
+        submitWizInfo($table, $record);
       } else
-        $m[4] .= " ".$inLine;
+        $record[4] .= " ".$line;
     } else
-    if (strlen($inLine) == 0 || $inLine[0] == "#") {
+    if (strlen($line) == 0 || $line[0] == "#") {
     } else
-    if (preg_match("/^([A-Z][a-z]+);(http:\/\/[^;]+|bat)?;(http:\/\/[^;]+|bat:\/\/[^;]+|img\/[^;]+)?;([^\$]*)\\\$$/", $inLine, $m)) {
-      submitWizInfo($wizTable, $m);
+    if (preg_match("/^([A-Z][a-z]+);(http:\/\/[^;]+|bat)?;(http:\/\/[^;]+|bat:\/\/[^;]+|img\/[^;]+)?;([^\$]*)\\\$$/", $line, $record)) {
+      submitWizInfo($table, $record);
     } else
-    if (preg_match("/^([A-Z][a-z]+);(http:\/\/[^;]+|bat)?;(http:\/\/[^;]+|bat:\/\/[^;]+|img\/[^;]+)?;(.*)$/", $inLine, $m)) {
+    if (preg_match("/^([A-Z][a-z]+);(http:\/\/[^;]+|bat)?;(http:\/\/[^;]+|bat:\/\/[^;]+|img\/[^;]+)?;(.*)$/", $line, $record)) {
       $contMode = TRUE;
     } else {
-      echo "Error: <b>$inLine</b><br>\n";
+      echo "Error: <b>$line</b><br>\n";
     }
   }
 
-  fclose($inFile);
+  fclose($file);
   return TRUE;
 }
 
@@ -217,12 +221,8 @@
   
   $wizInfo = array();
   
-  reset($wizFileList);
-  while (list($iKey, $iValue) = each($wizFileList)) {
-    parseWizInfoFile($iValue, $wizInfo);
-  }
-  
-  reset($wizFileList);
+  foreach ($wizFileList as $id)
+    parseWizInfoFile($id, $wizInfo);
   
   return $wizInfo;
 }
--- a/www/latest.php	Fri Sep 18 16:57:57 2009 +0000
+++ b/www/latest.php	Fri Sep 18 16:59:10 2009 +0000
@@ -44,6 +44,7 @@
 </div>
 <hr />
 <?
+// Compare two location records by addition date timestamp and primary name
 function locCompare($a, $b)
 {
   if ($a["added"] > $b["added"])
@@ -54,25 +55,21 @@
     return 1;
 }
 
-
-/* Print list of locations
- */
 if (count($locTable) > 0) {
-  /* Sort by timestamp ..
-   */
-  while (list($key, $value) = each($locTable)) {
+  // Sort locations by timestamp ..
+  foreach ($locTable as $location) {
     if (isset($_GET["nodate"])) {
-      if ($value["added"] < 0)
-        $sortTable[] = $value;
+      if ($location["added"] < 0)
+        $sortedLocs[] = $location;
     } else {
-      if ($value["added"] > 0)
-        $sortTable[] = $value;
+      if ($location["added"] > 0)
+        $sortedLocs[] = $location;
     }
   }
   
-  usort($sortTable, "locCompare");
+  usort($sortedLocs, "locCompare");
 
-
+  // Print the list
   $totalLoc = 0;
   echo
   "<table class=\"loc\">\n".
@@ -84,7 +81,7 @@
   "  <th>Information</th>\n".
   " </tr>\n";
   
-  while (list($ik, $iv) = each($sortTable)) {
+  foreach ($sortedLocs as $ik => $iv) {
         
         $totalLoc++;
 
--- a/www/loc.php	Fri Sep 18 16:57:57 2009 +0000
+++ b/www/loc.php	Fri Sep 18 16:59:10 2009 +0000
@@ -2,6 +2,7 @@
 require "config.inc.php";
 require "common.inc.php";
 
+
 function makeURL($afilter, $filter, $coders, $desc, $qname, $qclass)
 {
   $s = "";
@@ -110,12 +111,11 @@
    <td>
     <select name="f">
 <?
-reset($locationTypes);
-while (list($iKey, $iValue) = each($locationTypes)) {
-  echo "    <option value=\"".$iKey."\"";
-  if ($applyFilter && $filter == $iKey)
+foreach ($locationTypes as $id => $type) {
+  echo "    <option value=\"".$id."\"";
+  if ($applyFilter && $filter == $id)
     echo " selected=\"selected\"";
-  echo ">".$iValue[3]."</option>\n";
+  echo ">".$type[3]."</option>\n";
 }
 ?>
     </select>
@@ -123,15 +123,11 @@
    <td>
     <input type="submit" value=" Update " class="isubmit" />
    </td>
+   <td></td>
 <?
-
-echo "   <td></td>\n";
-
 makeURL($applyFilter, $filter, $showCoders, "All continents", "", "");
-
-reset($continentList);
-while (list($iKey, $iValue) = each($continentList)) {
-  makeURL($applyFilter, $filter, $showCoders, $iValue[0], $iKey, $iKey);
+foreach ($continentList as $continent => $data) {
+  makeURL($applyFilter, $filter, $showCoders, $data[0], $continent, $continent);
 }
 ?>
    <td><a href="<? echo $pageIndex ?>">Back to main page</a></td>
@@ -182,19 +178,21 @@
 /* Print list of locations
  */
 if (count($locTable) > 0) {
-  /* Make alphabetical table of locations
+  /* Make alphabetically sorted table of locations
    */
   $alphaTable = array();
-  while (list($key, $value) = each($locTable)) {
+  foreach ($locTable as $id => $data)
+  if (isset($data["name"])) {
+    $idx = $data["name"][0];
     if (isset($_GET["nocoders"])) {
-      if (count($value["coders"]) == 0 && !preg_match("/[SPG]/", $value["flags"]))
-        $alphaTable[$key[0]][] = $value;
+      if (count($data["coders"]) == 0 && !preg_match("/[SPG]/", $data["flags"]))
+        $alphaTable[$idx][] = $data;
     } else {
       if (isset($coderName)) {
-        if (array_search($coderName, $value["coders"]) !== FALSE)
-          $alphaTable[$key[0]][] = $value;
+        if (array_search($coderName, $data["coders"]) !== FALSE)
+          $alphaTable[$idx][] = $data;
       } else
-        $alphaTable[$key[0]][] = $value;
+        $alphaTable[$idx][] = $data;
     }
   }
 
@@ -289,13 +287,12 @@
   
   echo "<p><b>".$totalLoc."</b> locations.</p>\n";
 
-  /* Print out location tooltip HTML
+  /* Print out location tooltip HTML code
    */
-  reset($locationTips);
-  while (list($key, $value) = each($locationTips)) {
-    echo "<div class=\"tooltip\" id=\"tt".$key.
-    "\"><div class=\"holder\"><b>".htmlentities($value["title"]).
-    "</b><br />".$value["desc"]."</div></div>\n";
+  foreach ($locationTips as $tipID => $tip) {
+    echo "<div class=\"tooltip\" id=\"tt".$tipID.
+    "\"><div class=\"holder\"><b>".htmlentities($tip["title"]).
+    "</b><br />".$tip["desc"]."</div></div>\n";
   }
 } else {
   echo "<p><b>No locations known!</b></p>\n";
--- a/www/query.php	Fri Sep 18 16:57:57 2009 +0000
+++ b/www/query.php	Fri Sep 18 16:59:10 2009 +0000
@@ -12,7 +12,7 @@
 }
 
 // Read in location data, etc.
-$locTable = readLocationFiles(FALSE, "C");
+$locTable = readLocationFiles(FALSE, "Q");
 $locExact = FALSE;
 $locFound = FALSE;
 $locJSMap = FALSE;
@@ -44,18 +44,18 @@
 	header("Content-Type: application/json; charset=".$pageCharset);
 	$locResult = array();
 	reset($locTable);
-	while (list($name, $data) = each($locTable)) {
+	foreach ($locTable as $data) {
 		$match = FALSE;
 		foreach ($data["names"] as $n) {
 			if (preg_match($locRegEx, $n)) {
+				$locResult[$n] = $data;
 				$match = TRUE;
-				$locResult[$n] = $data;
 			}
 		}
 		
-		if (preg_match($locRegEx, $name)) {
+		if (preg_match($locRegEx, $data["name"])) {
+			$locResult[$data["name"]] = $data;
 			$match = TRUE;
-			$locResult[$name] = $data;
 		}
 	}
 	echo "[\"".$locName."\",[";
@@ -65,8 +65,8 @@
 } else {
 	// Match location exactly (non-case sensitive tho)
 	reset($locTable);
-	while (!$locFound && (list($name, $data) = each($locTable))) {
-		if (!strcasecmp($locName, $name)) {
+	while (!$locFound && (list($id, $data) = each($locTable))) {
+		if (!strcasecmp($locName, $data["name"])) {
 			$locFound = TRUE;
 		} else {
 			foreach ($data["names"] as $n) {
@@ -80,8 +80,8 @@
 	// If not exact matching mode specified, try non-exact also
 	if (!$locExact) {
 		reset($locTable);
-		while (!$locFound && (list($name, $data) = each($locTable))) {
-			if (preg_match($locRegEx, $name))
+		while (!$locFound && (list($id, $data) = each($locTable))) {
+			if (preg_match($locRegEx, $data["name"]))
 				$locFound = TRUE;
 			else {
 				foreach ($data["names"] as $n) {