Toolserver:User:HiW/Codesnipets/Para

<?php ob_start("ob_gzhandler"); header('Content-type: application/vnd.google-earth.kml+xml');

// get the time $timesnap = date("H:i:s");

include "marks-styles.php";

$response = ""; $coords[0]=-10; $coords[1]=-90; $coords[2]=10; $coords[3]=90;

function wfImageDir( $fname,$w ) { $wgUploadDirectory="http://upload.wikimedia.org/wikipedia/commons/thumb"; $hash = md5( $fname ); $dest = $wgUploadDirectory. '/' . $hash{0}. '/' . substr( $hash, 0, 2 ).'/' .$fname."/".$w."-".$fname; return $dest; }

function microtime_float {   list($usec, $sec) = explode(" ", microtime); return ((float)$usec + (float)$sec); }

function placemarks ($coords0,$coords1,$coords2,$coords3)

{ Global $lang; Global $pop; Global $style; Global $photo; Global $region; Global $notregion; Global $source; Global $notsource; Global $styles; Global $thumbs; Global $smem; Global $s; Global $typen; Global $timebegin; Global $timebetween; Global $sql; Global $ssl;

$loclang=$lang; $red_table=0; if (abs($coords1-$coords3)>1) {$red_table=2;} if (abs($coords1-$coords3)>10) {$red_table=3;} if (abs($coords1-$coords3)>100) {$red_table=4;}
 * 1) select red_table

if ($style<>""){$styleinsert=" AND style like '$style'";} if ($photo=="yes"){$photoinsert=" AND imagejpg IS NOT NULL";} if ($photo=="no"){$photoinsert=" AND imagejpg IS NULL";}
 * 1) if ($pop<>""){$popinsert=" AND pop>$pop";}
 * 2) if ($pop==-1){$popinsert=" AND pop='0' AND style NOT LIKE 'city%'";}


 * 1) if    ($region<>""){list ($reg, $sub) = split('-', $region);
 * 2)       $regioninsert=" AND `Country`='$reg'";
 * 3)       if ($sub<>"") {$regioninsert.=" AND `Subregion`='$sub'"; }
 * 4)   }


 * 1) if ($notregion<>""){list ($reg, $sub) = split('-', $notregion);
 * 2)       $regioninsert=" AND `Country`<>'$reg'";
 * 3)       if ($sub<>"") {$regioninsert.=" AND `Subregion`<>'$sub'"; }
 * 4)   }

if ($source<>""){$sourceinsert=" AND lang like '$source'";} if ($notsource<>""){$sourceinsert=" AND lang not like '$notsource'";}

$lonmid = (($coords2 - $coords0)/2) + $coords0; $latmid = (($coords3 - $coords1)/2) + $coords1; $distan=$coords2 - $coords0; if ($lang==""){$Titel_Column='"Titel"';} else {$Titel_Column='"T_'.$lang.'"';}

$sql='(SELECT '.$Titel_Column.' as title, pop,lon,lat,style,lang,image,imagejpg,name'       ." FROM wp_coords_red".$red_table."  WHERE the_geom && ST_SetSRID(ST_MakeBox2D(ST_Point($coords0,$coords1),             ST_Point($coords2,$coords3)),4326)". ' and '.$Titel_Column.' IS NOT NULL'. " $popinsert$styleinsert$photoinsert$regioninsert$sourceinsert ORDER BY psize DESC LIMIT 80) ";

$timebegin= microtime_float;
 * 1) $sql=$sqlleft.$coords0." AND ".$lonmid." AND `lat` BETWEEN ".$coords1." AND ".$latmid.$sqlright." UNION ";
 * 2) $sql.=$sqlleft.$lonmid." AND ".$coords2." AND `lat` BETWEEN ".$coords1." AND ".$latmid.$sqlright." UNION ";
 * 3) $sql.=$sqlleft.$coords0." AND ".$coords2." AND `lat` BETWEEN ".$latmid." AND ".$coords3.$sqlright." UNION ";
 * 4) $sql.=$sqlleft.$lonmid." AND ".$coords2." AND `lat` BETWEEN ".$latmid." AND ".$coords3.$sqlright;
 * 1) echo $sql;

// query the database $res = pg_query($sql); // check for query error if($e = pg_last_error) {echo $sql;die($e);}
 * 1) if ($ssl) {$proto='https://secure.wikimedia.org/wikipedia/'.$loclang.'/wiki/';} else
 * 2)      {$proto='http://'.$loclang.'.wikipedia.org/wiki/';}

while($row = pg_fetch_assoc($res)) { if ($lang==""){$loclang=$row[lang];} $proto = $ssl ? 'https://secure.wikimedia.org/wikipedia/'.$loclang.'/wiki/' : 'http://'.$loclang.'.wikipedia.org/wiki/';
 * 1) print_r ($row);

$row[title]=str_replace("?","ÃŸ",$row[title]); $row[title]=str_replace("&","&amp;",$row[title]); $row[imagejpg]=str_replace("?","ÃŸ",$row[imagejpg]); $row[imagejpg]=str_replace("&","&amp;",$row[imagejpg]);

$name=utf8_decode ($row[title]);

$forbitten = array("&","<", ">", "'",'"'); $row[name]=str_replace($forbitten,"",$row[name]);

$subname=$row[name]; if ($subname=="") {$sub="";} else {$sub="#$subname";} $url=substr((rawurlencode($row[title])),0,250);

$row[4]=$row[style]; $type=$row[style]; if ($row[4]=="") {$type="without";} if ($row[4]=="adm1st") {$type="without";} if ($row[4]=="adm2nd") {$type="without";} if (substr($row[4], 0, 4)=="moun") {$type="mountain";} if ($row[4]=="volcano") {$type="mountain";} if ($row[4]=="tunnel") {$type="street";} if ($row[4]=="shopping") {$type="economy";} if ($row[4]=="company") {$type="economy";}
 * 1) if (substr($row[4], 0, 4)=="city") {$type="city";}

$ersetze=",cemetery,zoo,theme park,wildlife san,forest,world natura,natural monu,botanic gard,oase"; if (strrpos($ersetze, $row[4])>0) {$type="park";}

$ersetze=",education,tower,gastronomy,theater,nuclear powe,hotel,hydroelectri,power plant,library,jail,nuclear faci,hall,cultural cen,habour,court,event center,cinema,boat lift,casino"; if (strrpos($ersetze, $row[4])>0) {$type="building";}

$ersetze=",reservoir,bay,strait,lake,waterfall,sea,water well,canal,geysers,canyon,river,ocean,geyser"; if (strrpos($ersetze, $row[4])>0) {$type="waterbody";}

$ersetze=",castle,manor-house,world cultur,monument,monastery,archaeologic,government,fortress,historical c,world herita,palace,pyramid,archipelago"; if (strrpos($ersetze, $row[4])>0) {$type="castle";}

$ersetze=",religious bu,temple,synagogue,mosque"; if (strrpos($ersetze, $row[4])>0) {$type="church";}

$ersetze=",mining,military,science,glacier,quarter,impact crate,dam,cape,cave,peninsula,festival,valley,concentratio,lock,cablecar"; if (strrpos($ersetze, $row[4])>0) {$type="landmark";}

if (stripos($smem,"$type,") === false) {

$typen=$typen.$s[str_replace(" ","_",$type)]."\n"; $smem=$smem.$type.",";}

$image=urlencode($row[image]); $imagejpg=urlencode($row[imagejpg]); if (($imagejpg<>"") AND ($thumbs=="yes") AND ($sub=="")){$insertstyle=substr(md5($row[title]), 0, 4 );} else   {$insertstyle=$type;} if ($sub<>"") {$showname=$sub;$shownameballon=$row[title];} else {$showname=$row[title];$shownameballon=$loclang.".Wikipedia";}

$response = ' '; $response .= " $showname "; $response .= ' 1 '; $response .= ' <![CDATA[ '.$shownameballon.' ';

if (($imagejpg<>"") AND ($thumbs<>"yes")) {$response .= '  ';} $response .= " Source: $row[lang]"; $response .= " style: $insertstyle ";

$response .= ']]> '; $response .= "    #$insertstyle"; $response .= '   '; $response .= " $row[lon],$row[lat],0 \n"; $response .= "  \n";

if (($imagejpg<>"") AND ($thumbs=="yes")){ $styles .= "\n         2.5 ". " ".wfImageDir($row[imagejpg],"120px")." ". ''. '<LabelStyle> ffb5b5b5 0.8 </LabelStyle>'. '</Style>'; }

$response_all=$response.$response_all; } $timebetween= microtime_float;

$logfile="log5.txt"; if (date("d")=="01") {{:Toolserver:Template:$counter=fopen($logfile,"a");} $log=($timebetween-$timebegin).",".$distan.",".$lonmid.",".$latmid.",".$lang.",".$red_table.",".date("m")."\n";    #",".$dis.",".$lonmid.","$latmid.",".$sql. fputs($counter,$log); fclose($counter); }
 * 1) logfile

return $response_all; }

// split the client's BBOX return by commas and spaces to obtain an array of coordinates $BOX = pg_escape_string(urldecode( $_GET["BBOX"] )); if ($BOX==""){$BOX = pg_escape_string(urldecode( $_GET["bbox"] ));}

$lang=addslashes($_GET[LANG]);
 * 1) if ($_SERVER['REMOTE_ADDR']=="88.251.219.128"){exit;}
 * 2) if ($lang=="en"){exit;}

$style=addslashes($_GET[style]); $pop=addslashes($_GET[pop]); $photo=addslashes($_GET[photo]); $region=addslashes($_GET[region]); $notregion=addslashes($_GET[notregion]); $source=addslashes($_GET[source]); $notsource=addslashes($_GET[notsource]); $ssl = isset($_REQUEST['secure']) && $_REQUEST['secure'];

$coords = preg_split('/,|\s/', $BOX);

$thumbs=addslashes($_GET[thumbs]);

$time_start = microtime_float;

// open psql connection $pg = pg_connect('host=sql-mapnik dbname=u_kolossos'); // check for connection error if($e = pg_last_error) die($e);

// for clarity, place each coordinate into a clearly marked bottom_left or top_right variable $bl_lon = $coords[0]; $bl_lat = $coords[1]; $tr_lon = $coords[2]; $tr_lat = $coords[3];

// calculate the approx center of the view -- note that this is innaccurate if the user is not looking straight down $lonmid = (($coords[2] - $coords[0])/2) + $coords[0]; $latmid = (($coords[3] - $coords[1])/2) + $coords[1];

$response= placemarks ($coords[0],$coords[1],$coords[2],$coords[3]);
 * 1) $response= placemarks ($coords[0],$coords[1],$lonmid,$latmid);
 * 2) $response.= placemarks ($coords[0],$latmid,$lonmid,$coords[3]);
 * 3) $response.= placemarks ($lonmid,$coords[1],$coords[2],$latmid);
 * 4) $response.= placemarks ($lonmid,$latmid,$coords[2],$coords[3]);

pg_close($pg); $time_end = microtime_float; $time1=round($timebegin-$time_start,3); $time2=round($timebetween-$timebegin,3); $time3 = round($time_end - $timebetween,3); ?>

<kml xmlns="http://www.opengis.net/kml/2.2"> <Document> cities.kml 0  1

<?php echo $typen; echo $styles; echo $response; ?>

 Info 1      <![CDATA[This is a extract from <a href="http://en.wikipedia.org/wiki/Hauptseite">Wikipedia</a> from 42 languages, it use the Interwikilinks to generarte all the other languages. See for more infos: <a href="http://de.wikipedia.org/wiki/Wikipedia:WikiProjekt_Georeferenzierung/Wikipedia-World/en">Wikipedia-World</a> Created in <?php echo $time1."+".$time2."+".$time3." ".$sql;?> sec. ]]> </Placemark>

</Document>