get_parms(
"sbas_id"
, "period"
, 'status0'
, 'coll0'
, 'status1'
, 'coll1'
);
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
if( $dom->loadXML($oldxml) )
{
$xmlchanged = false;
// foreach($parm2 as $pname=>$pvalue)
foreach(array(
"str:sbas_id",
"str:period",
'str:status0',
'str:coll0',
'str:status1',
'str:coll1',
) as $pname)
{
$ptype = substr($pname, 0, 3);
$pname = substr($pname, 4);
$pvalue = $parm2[$pname];
if( $ns = $dom->getElementsByTagName($pname)->item(0) )
{
// le champ existait dans le xml, on supprime son ancienne valeur (tout le contenu)
while( ($n = $ns->firstChild) )
$ns->removeChild($n);
}
else
{
// le champ n'existait pas dans le xml, on le cree
$dom->documentElement->appendChild($dom->createTextNode("\t"));
$ns = $dom->documentElement->appendChild($dom->createElement($pname));
$dom->documentElement->appendChild($dom->createTextNode("\n"));
}
// on fixe sa valeur
switch($ptype)
{
case "str":
$ns->appendChild($dom->createTextNode($pvalue));
break;
case "boo":
$ns->appendChild($dom->createTextNode($pvalue ? '1':'0'));
break;
}
$xmlchanged = true;
}
}
return($dom->saveXML());
}
// ====================================================================
// xml2graphic : must fill the graphic form (using js) from xml
// ====================================================================
public function xml2graphic($xml, $form)
{
if( ($sxml = simplexml_load_string($xml)) ) // in fact XML IS always valid here...
{
// ... but we could check for safe values
if( (int)($sxml->period) < 10 )
$sxml->period = 10;
elseif( (int)($sxml->period) > 1440 ) // 1 jour
$sxml->period = 1440;
if( (string)($sxml->delay) == '')
$sxml->delay = 0;
?>
0 ORDER BY ord';
if($rs = $conn->query($sql))
{
?>
cmd
array("set"=>false, "values"=>array(), "usage"=>" : usage du truc")
);
// ======================================================================================================
// ===== help() : text displayed if --help (optional)
// ======================================================================================================
function help()
{
return(_("task::outofdate:deplacement de docs suivant valeurs de champs 'date'"));
}
// ======================================================================================================
// ===== run() : le code d'execution de la tache proprement dite
// ======================================================================================================
private $sxTaskSettings = null; // les settings de la tache en simplexml
private $connbas = null; // cnx a la base
private $running = false;
private $msg = "";
function run()
{
if( ($this->sxTaskSettings = simplexml_load_string($this->taskSettings)) )
{
}
else
{
// Settings illisibles
return(false);
}
return($this->run2());
}
private function run2()
{
$ret = '';
$this->sxBasePrefs = null;
$sbas_id = (int)($this->sxTaskSettings->sbas_id);
$conn = connection::getInstance();
$this->connbas = connection::getInstance($sbas_id);
$this->running = true;
$this->tmask = array();
$this->tmaskgrp = array();
$this->period = 60;
// ici la t�che tourne tant qu'elle est active
$last_exec = 0;
$loop = 0;
while($conn && $this->running)
{
$sql = "UPDATE task2 SET last_exec_time=NOW() WHERE task_id=" . $this->taskid ;
$conn->query($sql);
// // on lit les prefs de cette base
$this->sxBasePrefs = databox::get_sxml_structure($sbas_id);//$rowbas["struct"]);
$sql = "SELECT * FROM task2 WHERE task_id=" . $this->taskid ;
if($rs = $conn->query($sql))
{
$row = $conn->fetch_assoc($rs);
$conn->free_result($rs);
if($row)
{
if($row['status'] == 'tostop')
{
$ret = 'stopped';
$this->running = false;
}
else
{
if( $this->sxTaskSettings = simplexml_load_string($row['settings']) )
{
$period = (int)($this->sxTaskSettings->period);
if($period <= 0 || $period >= 24*60)
$period = 60;
}
else
{
$period = 60;
}
$this->connbas = connection::getInstance($sbas_id);
$now = time();
if($now-$last_exec >= $period*60) // period est en minutes
{
$r = $this->doRecords();
// printf("line %s r=%s\n", __LINE__, $r);
if($r == 'NORECSTODO')
{
$last_exec = $now;
}
else
{
// on a trait� des records, on restart (si on a �t� lanc� par le scheduler)
if($row['status'] == 'started')
{
// ask for wakeup by scheduler
$ret = 'torestart';
$this->running = false;
}
}
if($loop > 5 || memory_get_usage()>>20 >= 15)
{
$ret = 'torestart';
$this->running = false;
}
}
else
{
$conn->close();
$this->connbas->close();
unset($conn);
sleep(5);
$conn = connection::getInstance();
$this->connbas = connection::getInstance($sbas_id);
}
}
}
else
{
$ret = 'stopped';
$this->running = false;
}
}
else
{
$ret = 'stopped';
$this->running = false;
}
$loop++;
}
return($ret);
}
function doRecords()
{
$ndone = 0;
$ret = 'NORECSTODO';
$nchanged = 0;
$coll = array();
$stat = array();
for($i=0; $i<=1; $i++)
{
$coll[$i] = trim((string)($this->sxTaskSettings->{'coll'.$i}));
$stat[$i] = trim((string)($this->sxTaskSettings->{'status'.$i}));
}
if(($coll[0] || $stat[0]) && ($coll[1] || $stat[1]))
{
$u = '';
if($coll[1])
$u .= ($u?', ':'') . 'coll_id=\'' . $this->connbas->escape_string($coll[1]) . '\'';
if($stat[1])
{
$x = explode('_', $stat[1]);
if($x[1]=='0')
$u .= ($u?', ':'') . 'status=status&~(1<<' . $x[0] . ')';
else
$u .= ($u?', ':'') . 'status=status|(1<<' . $x[0] . ')';
}
$w = '';
if($coll[0])
$w .= ($w?' AND ':'') . 'coll_id=\'' . $this->connbas->escape_string($coll[0]) . '\'';
if($stat[0])
{
$x = explode('_', $stat[0]);
if($x[1]=='0')
$w .= ($w?' AND ':'') . '(status>>' . $x[0] . ')&1=0';
else
$w .= ($w?' AND ':'') . '(status>>' . $x[0] . ')&1=1';
}
$sql = 'UPDATE record SET ' . $u . ' WHERE ' . $w;
var_dump($sql);
$this->connbas->query($sql);
$nchanged += ($n=$this->connbas->affected_rows());
if($n > 0)
$this->log(sprintf(("SQL=%s\n - %s changes"), $sql, $n));
}
$ret = ($nchanged > 0 ? $nchanged : 'NORECSTODO');
return($ret);
}
public function facility()
{
//global $parm;
$request = httpRequest::getInstance();
$parm2 = $request->get_parms(
"bid"
);
header("Content-Type: text/xml; charset=UTF-8");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
$ret = new DOMDocument("1.0", "UTF-8");
$ret->standalone = true;
$ret->preserveWhiteSpace = false;
$element = $ret->createElement('result');
$root = $ret->appendChild($element);
$root->appendChild($ret->createCDATASection( var_export($parm2, true) ));
$dfields = $root->appendChild($ret->createElement("date_fields"));
$statbits = $root->appendChild($ret->createElement("status_bits"));
$coll = $root->appendChild($ret->createElement("collections"));
$xml = NULL;
if($parm2["bid"] !== null)
{
$conn = connection::getInstance();
$connbas = connection::getInstance($parm2['bid']);
if($connbas->isok())
{
$sxBasePrefs = databox::get_sxml_structure($parm2['bid']);
if($sxBasePrefs)
{
foreach($sxBasePrefs->description->children() as $fname=>$field)
{
if(mb_strtolower($field['type']) == 'date')
$dfields->appendChild($ret->createElement("field"))->appendChild($ret->createTextNode($fname));
}
foreach($sxBasePrefs->statbits->bit as $sbit)
{
if( ($n=(int)($sbit['n'])) >= 4)
{
$labelOn = (string)$sbit;
if($sbit['labelOn'] != '')
$labelOn = $sbit['labelOn'];
$labelOff = 'non-' . $labelOn;
if($sbit['labelOff'] != '')
$labelOff = $sbit['labelOff'];
$node = $statbits->appendChild($ret->createElement("bit"));
$node->setAttribute('n', $n);
$node->setAttribute('value', '0');
$node->setAttribute('label', $labelOff);
$node->appendChild($ret->createTextNode($labelOff));
$node = $statbits->appendChild($ret->createElement("bit"));
$node->setAttribute('n', $n);
$node->setAttribute('value', '1');
$node->setAttribute('label', $labelOn);
$node->appendChild($ret->createTextNode($labelOn));
}
}
}
$sql = "SELECT coll_id, asciiname FROM coll";
if($rsbas = $connbas->query($sql))
{
while($rowbas = $conn->fetch_assoc($rsbas))
{
$node = $coll->appendChild($ret->createElement("collection"));
$node->setAttribute('id', $rowbas['coll_id']);
$name = trim($rowbas['asciiname']) != '' ? $rowbas['asciiname'] : 'Untitled';
$node->appendChild($ret->createTextNode($name));
}
$connbas->free_result($rsbas);
}
}
}
print($ret->saveXML());
}
}
?>