first init

This commit is contained in:
Metrax
2012-05-09 20:05:43 +02:00
commit e05b7bb8f0
6205 changed files with 395435 additions and 0 deletions
@@ -0,0 +1,110 @@
<?php
/***************************************************************************
* You can find the license in the docs directory
*
* Unicode Reminder メモ
*
* Cleanup the table sys_temptables from entries of dead threads
*
* run it once a day
*
***************************************************************************/
require_once($opt['rootpath'] . 'lib2/logic/gis.class.php');
checkJob(new cache_location());
class cache_location
{
var $name = 'cache_location';
var $interval = 0;
function run()
{
global $opt;
$rsCache = sql("SELECT `caches`.`cache_id`, `caches`.`latitude`, `caches`.`longitude` FROM `caches` LEFT JOIN `cache_location` ON `caches`.`cache_id`=`cache_location`.`cache_id` WHERE ISNULL(`cache_location`.`cache_id`) UNION SELECT `caches`.`cache_id`, `caches`.`latitude`, `caches`.`longitude` FROM `caches` INNER JOIN `cache_location` ON `caches`.`cache_id`=`cache_location`.`cache_id` WHERE `caches`.`last_modified`>`cache_location`.`last_modified`");
while ($rCache = sql_fetch_assoc($rsCache))
{
$sCode = '';
$rsLayers = sql("SELECT `level`, `code`, AsText(`shape`) AS `geometry` FROM `nuts_layer` WHERE WITHIN(GeomFromText('&1'), `shape`) ORDER BY `level` DESC", 'POINT(' . $rCache['longitude'] . ' ' . $rCache['latitude'] . ')');
while ($rLayers = sql_fetch_assoc($rsLayers))
{
if (gis::ptInLineRing($rLayers['geometry'], 'POINT(' . $rCache['longitude'] . ' ' . $rCache['latitude'] . ')'))
{
$sCode = $rLayers['code'];
break;
}
}
sql_free_result($rsLayers);
if ($sCode != '')
{
$adm1 = null; $code1 = null;
$adm2 = null; $code2 = null;
$adm3 = null; $code3 = null;
$adm4 = null; $code4 = null;
if (mb_strlen($sCode) > 5) $sCode = mb_substr($sCode, 0, 5);
if (mb_strlen($sCode) == 5)
{
$code4 = $sCode;
$adm4 = sql_value("SELECT `name` FROM `nuts_codes` WHERE `code`='&1'", null, $sCode);
$sCode = mb_substr($sCode, 0, 4);
}
if (mb_strlen($sCode) == 4)
{
$code3 = $sCode;
$adm3 = sql_value("SELECT `name` FROM `nuts_codes` WHERE `code`='&1'", null, $sCode);
$sCode = mb_substr($sCode, 0, 3);
}
if (mb_strlen($sCode) == 3)
{
$code2 = $sCode;
$adm2 = sql_value("SELECT `name` FROM `nuts_codes` WHERE `code`='&1'", null, $sCode);
$sCode = mb_substr($sCode, 0, 2);
}
if (mb_strlen($sCode) == 2)
{
$code1 = $sCode;
// try to get localised name first
$adm1 = sql_value("SELECT IFNULL(`sys_trans_text`.`text`, `countries`.`name`)
FROM `countries`
LEFT JOIN `sys_trans` ON `countries`.`trans_id`=`sys_trans`.`id` AND `countries`.`name`=`sys_trans`.`text`
LEFT JOIN `sys_trans_text` ON `sys_trans`.`id`=`sys_trans_text`.`trans_id` AND `sys_trans_text`.`lang`='&2'
WHERE `countries`.`short`='&1'",
null,
$sCode,
$opt['template']['default']['locale']);
if ($adm1 == null)
$adm1 = sql_value("SELECT `name` FROM `nuts_codes` WHERE `code`='&1'", null, $sCode);
}
sql("INSERT INTO `cache_location` (`cache_id`, `adm1`, `adm2`, `adm3`, `adm4`, `code1`, `code2`, `code3`, `code4`) VALUES ('&1', '&2', '&3', '&4', '&5', '&6', '&7', '&8', '&9') ON DUPLICATE KEY UPDATE `adm1`='&2', `adm2`='&3', `adm3`='&4', `adm4`='&5', `code1`='&6', `code2`='&7', `code3`='&8', `code4`='&9'", $rCache['cache_id'], $adm1, $adm2, $adm3, $adm4, $code1, $code2, $code3, $code4);
}
else
{
$sCountry = sql_value("SELECT IFNULL(`sys_trans_text`.`text`, `countries`.`name`)
FROM `caches`
INNER JOIN `countries` ON `caches`.`country`=`countries`.`short`
LEFT JOIN `sys_trans` ON `countries`.`trans_id`=`sys_trans`.`id` AND `countries`.`name`=`sys_trans`.`text`
LEFT JOIN `sys_trans_text` ON `sys_trans`.`id`=`sys_trans_text`.`trans_id` AND `sys_trans_text`.`lang`='&2'
WHERE `caches`.`cache_id`='&1'",
null,
$rCache['cache_id'],
$opt['template']['default']['locale']);
$sCode1 = sql_value("SELECT `caches`.`country` FROM `caches` WHERE `caches`.`cache_id`='&1'", null, $rCache['cache_id']);
sql("INSERT INTO `cache_location` (`cache_id`, `adm1`, `code1`) VALUES ('&1', '&2', '&3') ON DUPLICATE KEY UPDATE `adm1`='&2', `adm2`=NULL, `adm3`=NULL, `adm4`=NULL, `code1`='&3', `code2`=NULL, `code3`=NULL, `code4`=NULL", $rCache['cache_id'], $sCountry, $sCode1);
}
}
sql_free_result($rsCache);
}
}
?>
@@ -0,0 +1,60 @@
<?php
/***************************************************************************
* You can find the license in the docs directory
*
* Unicode Reminder メモ
*
* Cleanup the table sys_temptables from entries of dead threads
*
* run it once a day
*
***************************************************************************/
require_once($opt['rootpath'] . 'lib2/logic/gis.class.php');
checkJob(new cache_npa_areas());
class cache_npa_areas
{
var $name = 'cache_npa_areas';
var $interval = 600;
function run()
{
$rsCache = sql("SELECT `cache_id`, `latitude`, `longitude` FROM `caches` WHERE `need_npa_recalc`=1");
while ($rCache = sql_fetch_assoc($rsCache))
{
sql("DELETE FROM `cache_npa_areas` WHERE `cache_id`='&1' AND `calculated`=1", $rCache['cache_id']);
$rsLayers = sql("SELECT `id`, `type_id`, AsText(`shape`) AS `geometry` FROM `npa_areas` WHERE `exclude`=0 AND WITHIN(GeomFromText('&1'), `shape`)", 'POINT(' . $rCache['longitude'] . ' ' . $rCache['latitude'] . ')');
while ($rLayers = sql_fetch_assoc($rsLayers))
{
if (gis::ptInLineRing($rLayers['geometry'], 'POINT(' . $rCache['longitude'] . ' ' . $rCache['latitude'] . ')'))
{
$bExclude = false;
// prüfen, ob in ausgesparter Fläche
$rsExclude = sql("SELECT `id`, AsText(`shape`) AS `geometry` FROM `npa_areas` WHERE `exclude`=1 AND `type_id`='&1' AND WITHIN(GeomFromText('&2'), `shape`)", $rLayers['type_id'], 'POINT(' . $rCache['longitude'] . ' ' . $rCache['latitude'] . ')');
while (($rExclude = sql_fetch_assoc($rsExclude)) && ($bExclude==false))
{
if (gis::ptInLineRing($rExclude['geometry'], 'POINT(' . $rCache['longitude'] . ' ' . $rCache['latitude'] . ')'))
{
$bExclude = true;
}
}
sql_free_result($rsExclude);
if ($bExclude == false)
{
sql("INSERT INTO `cache_npa_areas` (`cache_id`, `npa_id`, `calculated`) VALUES ('&1', '&2', 1) ON DUPLICATE KEY UPDATE `calculated`=1", $rCache['cache_id'], $rLayers['id']);
}
}
}
sql_free_result($rsLayers);
sql("UPDATE `caches` SET `need_npa_recalc`=0 WHERE `cache_id`='&1'", $rCache['cache_id']);
}
sql_free_result($rsCache);
}
}
?>
@@ -0,0 +1,37 @@
<?php
/***************************************************************************
* You can find the license in the docs directory
*
* Unicode Reminder メモ
*
* Cleanup the table sys_temptables from entries of dead threads
*
* run it once a day
*
***************************************************************************/
checkJob(new cleanup_temptables());
class cleanup_temptables
{
var $name = 'cleanup_temptables';
var $interval = 86400;
function run()
{
$nIds = array();
$rs = sqlf("SHOW PROCESSLIST");
while ($r = sql_fetch_assoc($rs))
$nIds[$r['Id']] = $r['Id'];
sql_free_result($rs);
$rs = sqlf("SELECT DISTINCT `threadid` FROM `sys_temptables`");
while ($r = sql_fetch_assoc($rs))
{
if (!isset($nIds[$r['threadid']]))
sqlf("DELETE FROM `sys_temptables` WHERE `threadid`='&1'", $r['threadid']);
}
sql_free_result($rs);
}
}
?>
@@ -0,0 +1,263 @@
<?php
/***************************************************************************
* You can find the license in the docs directory
*
* Unicode Reminder メモ
***************************************************************************/
checkJob(new geokrety());
class geokrety
{
var $name = 'geokrety';
var $interval = 900;
function run()
{
$xmlfile = $this->loadXML();
if ($xmlfile == false) return;
$this->importXML($xmlfile);
$this->removeXML($xmlfile);
}
/* get file from XML interface
* and return path of saved xml
* or false on error
*/
function loadXML()
{
global $opt;
@mkdir($opt['rootpath'] . 'cache2/geokrety');
$path = $opt['rootpath'] . 'cache2/geokrety/import.xml';
$this->removeXML($path);
$modifiedsince = strtotime(getSysConfig('geokrety_lastupdate', '2005-01-01 00:00:00'));
if (!@copy('http://geokrety.org/export.php?modifiedsince=' . date('YmdHis', $modifiedsince - 1), $path))
return false;
return $path;
}
/* remove given file
*/
function removeXML($file)
{
@unlink($file);
}
/* import the given XML file
*/
function importXML($file)
{
global $opt;
$xr = new XMLReader();
if (!$xr->open($file))
{
$xr->close();
return;
}
$xr->read();
if ($xr->nodeType != XMLReader::ELEMENT)
{
echo 'error: First element expected, aborted' . "\n";
return;
}
if ($xr->name != 'gkxml')
{
echo 'error: First element not valid, aborted' . "\n";
return;
}
$startupdate = $xr->getAttribute('date');
if ($startupdate == '')
{
echo 'error: Date attribute not valid, aborted' . "\n";
return;
}
while ($xr->read() && !($xr->name == 'geokret' || $xr->name == 'moves')) ;
$nRecordsCount = 0;
do
{
if ($xr->nodeType == XMLReader::ELEMENT)
{
$element = $xr->expand();
switch ($xr->name)
{
case 'geokret':
$this->importGeoKret($element);
break;
case 'moves':
$this->importMove($element);
break;
}
$nRecordsCount++;
}
}
while ($xr->next());
$xr->close();
setSysConfig('geokrety_lastupdate', date($opt['db']['dateformat'], strtotime($startupdate)));
}
function importGeoKret($element)
{
global $opt;
$id = $element->getAttribute('id');
$name = html_entity_decode($this->GetNodeValue($element, 'name'));
if ($name == '') return;
$userid = $this->GetNodeAttribute($element, 'owner', 'id')+0;
$username = $this->GetNodeValue($element, 'owner');
$this->checkUser($userid, $username);
$typeid = $this->GetNodeAttribute($element, 'type', 'id')+0;
$typename = $this->GetNodeValue($element, 'type');
$this->checkGeoKretType($typeid, $typename);
$description = html_entity_decode($this->GetNodeValue($element, 'description'));
$datecreated = strtotime($this->GetNodeValue($element, 'datecreated'));
$distancetravelled = $this->GetNodeValue($element, 'distancetravelled')+0;
$state = $this->GetNodeValue($element, 'state')+0;
$longitude = $this->GetNodeAttribute($element, 'position', 'longitude')+0;
$latitude = $this->GetNodeAttribute($element, 'position', 'latitude')+0;
sql("INSERT INTO `gk_item` (`id`, `name`, `description`, `userid`, `datecreated`, `distancetravelled`,
`latitude`, `longitude`, `typeid`, `stateid`)
VALUES ('&1', '&2', '&3', '&4', '&5', '&6',
'&7', '&8', '&9', '&10')
ON DUPLICATE KEY UPDATE `name`='&2', `description`='&3', `userid`='&4', `datecreated`='&5',
`distancetravelled`='&6', `latitude`='&7', `longitude`='&8', `typeid`='&9', `stateid`='&10'",
$id, $name, $description, $userid, date($opt['db']['dateformat'], $datecreated), $distancetravelled,
$latitude, $longitude, $typeid, $state);
/* we are using now the moves to update waypoints */
/*
sql("DELETE FROM `gk_item_waypoint` WHERE id='&1'", $id);
// update associated waypoints
$waypoints = $element->getElementsByTagName('waypoints');
if ($waypoints->length == 1)
{
$wpItems = $waypoints->item(0)->getElementsByTagName('waypoint');
for ($i = 0; $i < $wpItems->length; $i++)
{
$wp = $wpItems->item($i)->nodeValue;
if ($wp != '')
sql("INSERT INTO `gk_item_waypoint` (`id`, `wp`) VALUES ('&1', '&2')", $id, $wp);
}
}
*/
}
function importMove($element)
{
global $opt;
$id = $element->getAttribute('id')+0;
$gkid = $this->GetNodeAttribute($element, 'geokret', 'id')+0;
if (sql_value("SELECT COUNT(*) FROM `gk_item` WHERE `id`='&1'", 0, $gkid) == 0) return;
$latitude = $this->GetNodeAttribute($element, 'position', 'latitude')+0;
$longitude = $this->GetNodeAttribute($element, 'position', 'longitude')+0;
$datelogged = strtotime($this->GetNodeAttribute($element, 'date', 'logged'));
$datemoved = strtotime($this->GetNodeAttribute($element, 'date', 'moved'));
$userid = $this->GetNodeAttribute($element, 'user', 'id')+0;
$username = $this->GetNodeValue($element, 'user');
$this->checkUser($userid, $username);
$comment = html_entity_decode($this->GetNodeValue($element, 'comment'));
$logtypeid = $this->GetNodeAttribute($element, 'logtype', 'id')+0;
$logtypename = $this->GetNodeValue($element, 'logtype');
$this->checkMoveType($logtypeid, $logtypename);
sql("INSERT INTO `gk_move` (`id`, `itemid`, `latitude`, `longitude`, `datemoved`, `datelogged`,
`userid`, `comment`, `logtypeid`)
VALUES ('&1', '&2', '&3', '&4', '&5', '&6',
'&7', '&8', '&9')
ON DUPLICATE KEY UPDATE `itemid`='&2', `latitude`='&3', `longitude`='&4', `datemoved`='&5',
`datelogged`='&6', `userid`='&7', `comment`='&8', `logtypeid`='&9'",
$id, $gkid, $latitude, $longitude, date($opt['db']['dateformat'], $datemoved), date($opt['db']['dateformat'], $datelogged),
$userid, $comment, $logtypeid);
sql("DELETE FROM `gk_move_waypoint` WHERE id='&1'", $id);
// update associated waypoints
$waypoints = $element->getElementsByTagName('waypoints');
if ($waypoints->length == 1)
{
$wpItems = $waypoints->item(0)->getElementsByTagName('waypoint');
for ($i = 0; $i < $wpItems->length; $i++)
{
$wp = mb_trim($wpItems->item($i)->nodeValue);
if ($wp != '')
sql("INSERT INTO `gk_move_waypoint` (`id`, `wp`) VALUES ('&1', '&2')", $id, $wp);
}
}
// now update the current gk-waypoints based on the last move
sql("DELETE FROM `gk_item_waypoint` WHERE `id`='&1'", $gkid);
$rs = sql("SELECT * FROM `gk_move` WHERE `itemid`='&1' AND `logtypeid`!=2 ORDER BY `datemoved` DESC LIMIT 1", $gkid);
$r = sql_fetch_assoc($rs);
sql_free_result($rs);
if ($r === false) return;
if ($r['logtypeid'] == 0 || $r['logtypeid'] == 3)
{
sql("INSERT INTO `gk_item_waypoint` (`id`, `wp`) SELECT '&1' AS `id`, `wp` FROM `gk_move_waypoint` WHERE `id`='&2' AND `wp`!=''", $gkid, $id);
}
else
{
// do nothing
}
}
function checkGeoKretType($id, $name)
{
sql("INSERT INTO `gk_item_type` (`id`, `name`) VALUES ('&1', '&2') ON DUPLICATE KEY UPDATE `name`='&2'", $id, $name);
}
function checkUser($id, $name)
{
if ($id == 0) return;
sql("INSERT INTO `gk_user` (`id`, `name`) VALUES ('&1', '&2') ON DUPLICATE KEY UPDATE `name`='&2'", $id, $name);
}
function checkMoveType($id, $name)
{
sql("INSERT INTO `gk_move_type` (`id`, `name`) VALUES ('&1', '&2') ON DUPLICATE KEY UPDATE `name`='&2'", $id, $name);
}
function GetNodeValue(&$domnode, $element)
{
$subnode = $domnode->getElementsByTagName($element);
if ($subnode->length < 1)
return '';
else
return $subnode->item(0)->nodeValue;
}
function GetNodeAttribute(&$domnode, $element, $attr)
{
$subnode = $domnode->getElementsByTagName($element);
if ($subnode->length < 1)
return '';
else
return $subnode->item(0)->getAttribute($attr);
}
}
?>
@@ -0,0 +1,31 @@
<?php
/***************************************************************************
* You can find the license in the docs directory
*
* Unicode Reminder メモ
*
* Publish new geocaches that are marked for timed publish
***************************************************************************/
checkJob(new publish_caches());
class publish_caches
{
var $name = 'publish_caches';
var $interval = 60;
function run()
{
$rsPublish = sql("SELECT `cache_id`, `user_id` FROM `caches` WHERE `status`=5 AND NOT ISNULL(`date_activate`) AND `date_activate`<=NOW()");
while($rPublish = sql_fetch_array($rsPublish))
{
$userid = $rPublish['user_id'];
$cacheid = $rPublish['cache_id'];
// update cache status to active
sql("UPDATE `caches` SET `status`=1, `date_activate`=NULL WHERE `cache_id`='&1'", $cacheid);
}
sql_free_result($rsPublish);
}
}
?>
@@ -0,0 +1,78 @@
<?php
/***************************************************************************
* You can find the license in the docs directory
*
* Unicode Reminder メモ
*
* Publish new geocaches that are marked for timed publish
***************************************************************************/
checkJob(new rating_tops());
class rating_tops
{
var $name = 'rating_tops';
var $interval = 86400;
function run()
{
sql("DELETE FROM `rating_tops`");
sql_temp_table('topLocationCaches');
sql_temp_table('topRatings');
sql_temp_table('topResult');
sql("CREATE TEMPORARY TABLE &topLocationCaches (`cache_id` INT(11) PRIMARY KEY) ENGINE=MEMORY");
sql("CREATE TEMPORARY TABLE &topRatings (`cache_id` INT(11) PRIMARY KEY, `ratings` INT(11)) ENGINE=MEMORY");
sql("CREATE TEMPORARY TABLE &topResult (`idx` INT(11), `cache_id` INT(11) PRIMARY KEY, `ratings` INT(11), `founds` INT(11)) ENGINE=MEMORY");
$rsAdm1 = sql('SELECT SQL_BUFFER_RESULT SQL_SMALL_RESULT DISTINCT `adm1`, `code1` FROM `cache_location` WHERE NOT ISNULL(`adm1`) ORDER BY `adm1` ASC');
while ($rAdm1 = sql_fetch_assoc($rsAdm1))
{
$rsAdm3 = sql("SELECT SQL_BUFFER_RESULT SQL_SMALL_RESULT DISTINCT `adm3` FROM `cache_location` WHERE `code1`='&1' ORDER BY `adm3` ASC", $rAdm1['code1']);
while ($rAdm3 = sql_fetch_assoc($rsAdm3))
{
sql("TRUNCATE TABLE &topLocationCaches");
sql("TRUNCATE TABLE &topRatings");
sql("TRUNCATE TABLE &topResult");
// Alle Caches für diese Gruppe finden
if ($rAdm3['adm3'] == null)
sql("INSERT INTO &topLocationCaches (`cache_id`) SELECT `caches`.`cache_id` FROM `cache_location` INNER JOIN `caches` ON `caches`.`cache_id`=`cache_location`.`cache_id` LEFT JOIN `stat_caches` ON `caches`.`cache_id`=`stat_caches`.`cache_id` WHERE IFNULL(`stat_caches`.`toprating`,0)>0 AND `cache_location`.`adm1`='&1' AND ISNULL(`cache_location`.`adm3`) AND `caches`.`status`=1", $rAdm1['adm1']);
else
sql("INSERT INTO &topLocationCaches (`cache_id`) SELECT `caches`.`cache_id` FROM `cache_location` INNER JOIN `caches` ON `caches`.`cache_id`=`cache_location`.`cache_id` LEFT JOIN `stat_caches` ON `caches`.`cache_id`=`stat_caches`.`cache_id` WHERE IFNULL(`stat_caches`.`toprating`,0)>0 AND `cache_location`.`adm1`='&1' AND `cache_location`.`adm3`='&2' AND `caches`.`status`=1", $rAdm1['adm1'], $rAdm3['adm3']);
sql("INSERT INTO &topRatings (`cache_id`, `ratings`) SELECT `cache_rating`.`cache_id`, COUNT(`cache_rating`.`cache_id`) AS `ratings` FROM `cache_rating` INNER JOIN &topLocationCaches ON `cache_rating`.`cache_id`=&topLocationCaches.`cache_id` INNER JOIN `caches` ON `cache_rating`.`cache_id`=`caches`.`cache_id` WHERE `cache_rating`.`user_id`!=`caches`.`user_id` GROUP BY `cache_rating`.`cache_id`");
sql("INSERT INTO &topResult (`idx`, `cache_id`, `ratings`, `founds`)
SELECT SQL_SMALL_RESULT (&topRatings.`ratings`+1)*(&topRatings.`ratings`+1)/(IFNULL(`stat_caches`.`found`, 0)/10+1)*100 AS `idx`,
&topRatings.`cache_id`,
&topRatings.`ratings`,
IFNULL(`stat_caches`.`found`, 0) AS founds
FROM &topRatings
INNER JOIN `caches` ON &topRatings.`cache_id`=`caches`.`cache_id`
LEFT JOIN `stat_caches` ON `stat_caches`.`cache_id`=`caches`.`cache_id`
ORDER BY `idx` DESC LIMIT 15");
if (sql_value("SELECT COUNT(*) FROM &topResult", 0) > 10)
{
$min_idx = sql_value("SELECT `idx` FROM &topResult ORDER BY idx DESC LIMIT 9, 1", 0);
sql("DELETE FROM &topResult WHERE `idx`<'&1'", $min_idx);
}
sql("INSERT INTO `rating_tops` (`cache_id`, `rating`)
SELECT SQL_BUFFER_RESULT &topResult.`cache_id`,
&topResult.`idx` AS `rating`
FROM &topResult
ORDER BY `rating` DESC");
}
sql_free_result($rsAdm3);
}
sql_free_result($rsAdm1);
sql_drop_temp_table('topLocationCaches');
sql_drop_temp_table('topRatings');
sql_drop_temp_table('topResult');
}
}
?>
@@ -0,0 +1,104 @@
<?php
/***************************************************************************
* You can find the license in the docs directory
*
* Unicode Reminder メモ
*
* - check all slaves and manage table sys_repl_slaves
* - check all slaves and purge master logs
* - send warning when too many logs stay on master (file size)
***************************************************************************/
checkJob(new replication_monitor());
class replication_monitor
{
var $name = 'replication_monitor';
var $interval = 0;
//var $interval = 60;
function run()
{
global $opt;
$known_ids = array();
foreach ($opt['db']['slaves'] AS $k => $v)
{
$this->check_slave($k);
$known_ids[] = "'" . sql_escape($k) . "'";
}
if (count($known_ids) > 0)
sql("DELETE FROM `sys_repl_slaves` WHERE `id` NOT IN (" . implode(',', $known_ids) . ")");
else
sql("DELETE FROM `sys_repl_slaves`");
// now, clean up sys_repl_exclude
sql("DELETE FROM `sys_repl_exclude` WHERE `datExclude`<DATE_SUB(NOW(), INTERVAL '&1' SECOND)", $opt['db']['slave']['max_behind']);
}
function check_slave($id)
{
global $opt;
$nActive = 0;
$nOnline = 0;
$sLogName = '';
$sLogPos = '';
$nTimeDiff = -1;
$slave = $opt['db']['slaves'][$id];
if ($slave['active'] == true)
{
$nActive = 1;
// connect
$dblink = @mysql_connect($slave['server'], $slave['username'], $slave['password']);
if ($dblink !== false)
{
if (mysql_select_db($opt['db']['placeholder']['db'], $dblink))
{
// read slave time
$rs = mysql_query("SELECT `data` FROM `sys_repl_timestamp`", $dblink);
if ($rs !== false)
{
$rTime = mysql_fetch_assoc($rs);
mysql_free_result($rs);
// read current master db time
$nMasterTime = sql_value("SELECT NOW()", null);
$nTimeDiff = strtotime($nMasterTime) - strtotime($rTime['data']);
if ($nTimeDiff < $opt['db']['slave']['max_behind'])
{
$nOnline = 1;
}
}
// update logpos
$rs = mysql_query("SHOW SLAVE STATUS");
$r = mysql_fetch_assoc($rs);
mysql_free_result($rs);
$sLogName = $r['Master_Log_File'];
$sLogPos = $r['Read_Master_Log_Pos'];
}
mysql_close($dblink);
}
}
// only-flag changed?
if ($nOnline != sql_value("SELECT `online` FROM `sys_repl_slaves` WHERE `id`='&1'", 0, $id))
{
mail($opt['db']['error']['mail'], "MySQL Slave Server Id " . $id . " (" . $slave['server'] . ") is now " . (($nOnline!=0)?'Online':'Offline'), '');
}
sql("INSERT INTO `sys_repl_slaves` (`id`, `server`, `active`, `weight`, `online`, `last_check`, `current_log_name`, `current_log_pos`)
VALUES ('&1', '&2', '&3', '&4', '&5', NOW(), '&6', '&7')
ON DUPLICATE KEY UPDATE `server`='&2', `active`='&3', `weight`='&4', `online`='&5', `last_check`=NOW(), `current_log_name`='&6', `current_log_pos`='&7'",
$id, $slave['server'], $nActive, $slave['weight'], $nOnline, $sLogName, $sLogPos);
// update time_diff?
if ($nTimeDiff != -1)
sql("UPDATE `sys_repl_slaves` SET `time_diff`='&1' WHERE `id`='&2'", $nTimeDiff, $id);
}
}
?>
@@ -0,0 +1,143 @@
<?php
/***************************************************************************
* You can find the license in the docs directory
*
* Unicode Reminder メモ
*
* Generate sitemap.xml as specified by http://www.sitemaps.org
* And send ping to search engines
***************************************************************************/
require_once($opt['rootpath'] . 'lib2/logic/sitemapxml.class.php');
checkJob(new sitemaps());
class sitemaps
{
var $name = 'sitemaps';
var $interval = 604800; // once a week
var $oSitemapXML = false;
function run()
{
global $opt;
if ($opt['cron']['sitemaps']['generate'] == true)
{
$this->oSitemapXML = new sitemapxml();
$this->oSitemapXML->open($opt['rootpath'], $opt['page']['absolute_url']);
$this->oSitemapXML->write('index.php', time(), 'always');
$this->write_viewacache_urls();
$this->write_articles_urls();
$this->write_viewlogs_urls();
$this->write_viewprofile_urls();
$this->oSitemapXML->write('tops.php', time()-24*60*60, 'daily');
$this->oSitemapXML->write('newcachesrest.php', time()-24*60*60, 'daily');
$this->write_newcaches_urls();
$this->oSitemapXML->write('newlogs.php', time(), 'always');
$this->oSitemapXML->close();
if ($opt['cron']['sitemaps']['submit'] == true)
{
$this->ping_searchengines();
}
}
}
function ping_searchengines()
{
global $opt;
$url = urlencode($opt['page']['absolute_url'] . 'sitemap.xml');
$this->ping_searchengine('http://www.google.com/webmasters/tools/ping?sitemap=' . $url);
$this->ping_searchengine('http://search.yahooapis.com/SiteExplorerService/V1/updateNotification?appid=USERID&url=' . $url);
$this->ping_searchengine('http://submissions.ask.com/ping?sitemap=' . $url);
$this->ping_searchengine('http://www.bing.com/webmaster/ping.aspx?siteMap=' . $url);
}
function ping_searchengine($url)
{
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_exec($curl);
if (curl_errno($curl) != 0)
{
curl_close($curl);
return false;
}
$respCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($respCode != 200)
{
curl_close($curl);
return false;
}
curl_close($curl);
return true;
}
function write_newcaches_urls()
{
$nCount = sql_value("SELECT COUNT(*) FROM `caches`
INNER JOIN `cache_status` ON `caches`.`status`=`cache_status`.`id`
WHERE `cache_status`.`allow_user_view`=1", 0);
$nIndex = 0;
while ($nIndex < $nCount)
{
$this->oSitemapXML->write('newcaches.php?startat=' . $nIndex, time(), 'always');
$nIndex += 100;
}
}
function write_viewprofile_urls()
{
$rs = sql("SELECT SQL_BUFFER_RESULT `user_id` FROM `user`");
while ($r = sql_fetch_assoc($rs))
{
$this->oSitemapXML->write('viewprofile.php?userid=' . $r['user_id'], time()-31*24*60*60);
}
sql_free_result($rs);
}
function write_viewlogs_urls()
{
$rs = sql("SELECT SQL_BUFFER_RESULT MAX(`last_modified`) AS `d`, `cache_id` FROM `cache_logs` GROUP BY `cache_id`");
while ($r = sql_fetch_assoc($rs))
{
$this->oSitemapXML->write('viewlogs.php?cacheid=' . $r['cache_id'], strtotime($r['d']));
}
sql_free_result($rs);
}
function write_articles_urls()
{
$rs = sql("SELECT `href` FROM `sys_menu` WHERE `href` LIKE 'articles.php?page=%'");
while ($r = sql_fetch_assoc($rs))
{
$this->oSitemapXML->write($r['href'], time()-31*24*60*60);
}
sql_free_result($rs);
}
function write_viewacache_urls()
{
$rs = sql("SELECT SQL_BUFFER_RESULT `caches`.`wp_oc`, `caches`.`cache_id`, `cache_desc`.`language`
FROM `caches`
INNER JOIN `cache_desc` ON `caches`.`cache_id`=`cache_desc`.`cache_id`");
while ($r = sql_fetch_assoc($rs))
{
$dLastMod = sql_value("SELECT MAX(`last_modified`) `last_modified` FROM (SELECT `last_modified` FROM `caches` WHERE `cache_id` ='&1' UNION
SELECT `last_modified` FROM `cache_desc` WHERE `cache_id` ='&1' UNION
SELECT MAX(`last_modified`) AS `last_modified` FROM `cache_logs` WHERE `cache_id` ='&1') `tmp_result`", time(), $r['cache_id']);
$this->oSitemapXML->write('viewcache.php?wp=' . $r['wp_oc'] . '&desclang=' . $r['language'], strtotime($dLastMod));
}
sql_free_result($rs);
}
}
?>
@@ -0,0 +1,84 @@
<?php
/***************************************************************************
* You can find the license in the docs directory
*
* Unicode Reminder メモ
***************************************************************************/
checkJob(new slave_cleanup());
class slave_cleanup
{
var $name = 'slave_cleanup';
var $interval = 300;
function run()
{
global $opt;
$rs = sql("SELECT `id` FROM `sys_repl_slaves` WHERE `active`=1 AND `online`=1 AND (TIMESTAMP(NOW())-TIMESTAMP(`last_check`)+`time_diff`<'&1')", $opt['db']['slave']['max_behind']);
while ($r = sql_fetch_assoc($rs))
{
$this->cleanup_slave($r['id']);
}
sql_free_result($rs);
$this->cleanup_slave(-1);
}
function cleanup_slave($slaveId)
{
// ensure old slave is disconnected
sql_disconnect_slave();
// connect the slave
if ($slaveId == -1)
sql_connect_master_as_slave();
else
sql_connect_slave($slaveId);
$this->cleanup_mapresult($slaveId);
$this->cleanup_mapresult2($slaveId);
// disconnect slave
sql_disconnect_slave();
}
function cleanup_mapresult($slaveId)
{
// table mapresult
sql_slave("DELETE FROM `mapresult` WHERE `date_created`<DATE_SUB(NOW(), INTERVAL 5 MINUTE)");
sql_slave("DELETE `mapresult_data` FROM `mapresult_data` LEFT JOIN `mapresult` ON `mapresult_data`.`query_id`=`mapresult`.`query_id` WHERE ISNULL(`mapresult`.`query_id`)");
}
function cleanup_mapresult2($slaveId)
{
global $opt;
// cleanup old entries
$rs = sql("SELECT SQL_BUFFER_RESULT `result_id` FROM `map2_result` WHERE DATE_ADD(`date_created`, INTERVAL '&1' SECOND)<NOW()", $opt['map']['maxcacheage']);
while ($r = sql_fetch_assoc($rs))
{
sql("DELETE FROM `map2_result` WHERE `result_id`='&1'", $r['result_id']);
}
sql_free_result($rs);
// now reduce table size? (29 bytes is the average row size)
if (sql_value_slave("SELECT COUNT(*) FROM `map2_data`", 0) > $opt['map']['maxcachesize'] / 29)
{
while (sql_value_slave("SELECT COUNT(*) FROM `map2_data`", 0) > $opt['map']['maxcachereducedsize'] / 29)
{
$resultId = sql_value("SELECT `result_id` FROM `map2_result` WHERE `slave_id`='&1' ORDER BY `date_lastqueried` DESC LIMIT 1", 0, $slaveId);
if ($resultId == 0)
return;
sql("DELETE FROM `map2_result` WHERE `result_id`='&1'", $resultId);
}
}
$nMinId = sql_value("SELECT MIN(`result_id`) FROM `map2_result`", 0);
if ($nMinId == 0)
sql("DELETE FROM `map2_data`");
else
sql("DELETE FROM `map2_data` WHERE `result_id`<'&1'", $nMinId);
}
}
?>
@@ -0,0 +1,22 @@
<?php
/***************************************************************************
* You can find the license in the docs directory
*
* Unicode Reminder メモ
*
* Publish new geocaches that are marked for timed publish
***************************************************************************/
checkJob(new user_delete());
class user_delete
{
var $name = 'user_delete';
var $interval = 86400;
function run()
{
sql("DELETE FROM `user` WHERE `date_created`<DATE_ADD(NOW(), INTERVAL -21 DAY) AND `is_active_flag`=0 AND `activation_code`!=''");
}
}
?>