get_parms( "sbas_id" , "period" , 'field1' , 'fieldDs1' , 'fieldDv1' , 'field2' , 'fieldDs2' , 'fieldDv2' , 'status0' , 'coll0' , 'status1' , 'coll1' , 'status2' , 'coll2' ); $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:field1', 'str:fieldDs1', 'str:fieldDv1', 'str:field2', 'str:fieldDs2', 'str:fieldDv2', 'str:status0', 'str:coll0', 'str:status1', 'str:coll1', 'str:status2', 'str:coll2' ) 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 cr�e $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; ?> get_session(); $user = User_Adapter::getInstance($session->get_usr_id(), $appbox); ob_start(); $sbas_list = $user->ACL()->get_granted_sbas(array('bas_manage')); ?>
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'�x�cution de la t�che proprement dite // ====================================================================================================== private $sxTaskSettings = null; // les settings de la tache en simplexml private $connbas = null; // cnx � la base private $msg = ""; protected function run2() { $ret = ''; $sbas_id = (int) ($this->sxTaskSettings->sbas_id); $conn = connection::getPDOConnection(); $this->connbas = connection::getPDOConnection($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 ($this->running) { $this->set_last_exec_time(); $databox = databox::get_instance($sbas_id); $sql = "SELECT * FROM task2 WHERE task_id = :task_id"; $stmt = $conn->prepare($sql); $stmt->execute(array(':task_id' => $this->get_task_id())); $row = $stmt->fetch(PDO::FETCH_ASSOC); $stmt->closeCursor(); 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::getPDOConnection($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::getPDOConnection(); $this->connbas = connection::getPDOConnection($sbas_id); } } } else { $ret = 'stopped'; $this->running = false; } $loop++; } return($ret); } function doRecords() { $ndone = 0; $ret = 'NORECSTODO'; $date1 = $date2 = time(); $field1 = $field2 = ''; // test : DATE 1 if (($field1 = trim($this->sxTaskSettings->field1)) != '') { if (($delta = (int) ($this->sxTaskSettings->fieldDv1)) > 0) { if ($this->sxTaskSettings->fieldDs1 == '-') $date1 += 86400 * $delta; else $date1 -= 86400 * $delta; } } // test : DATE 2 if (($field2 = trim($this->sxTaskSettings->field2)) != '') { if (($delta = (int) ($this->sxTaskSettings->fieldDv2)) > 0) { if ($this->sxTaskSettings->fieldDs2 == '-') $date2 += 86400 * $delta; else $date2 -= 86400 * $delta; } } $date1 = date("YmdHis", $date1); $date2 = date("YmdHis", $date2); $sqlset = $params = $tmp_params = array(); for ($i = 0; $i <= 2; $i++) { $sqlwhere[$i] = ''; $sqlset[$i] = ''; $x = 'status' . $i; @list($tostat, $statval) = explode('_', (string) ($this->sxTaskSettings->{$x})); if ($tostat >= 4 && $tostat <= 63) { if ($statval == '0') { $sqlset[$i] = 'status=status & ~(1<<' . $tostat . ')'; $sqlwhere[$i] .= '(status & (1<<' . $tostat . ') = 0)'; } elseif ($statval == '1') { $sqlset[$i] = 'status=status|(1<<' . $tostat . ')'; $sqlwhere[$i] .= '(status & (1<<' . $tostat . ') != 0)'; } } $x = 'coll' . $i; if (($tocoll = (string) ($this->sxTaskSettings->{$x})) != '') { $sqlset[$i] .= ( $sqlset[$i] ? ', ' : '') . ('coll_id = :coll_id_set'. $i); $sqlwhere[$i] .= ( $sqlwhere[$i] ? ' AND ' : '') . '(coll_id = :coll_id_where'. $i.')'; $tmp_params[':coll_id_set' . $i] = $tocoll; $tmp_params[':coll_id_where' . $i] = $tocoll; } } for ($i = 0; $i <= 2; $i++) { if (!$sqlwhere[$i]) $sqlwhere[$i] = '1'; } $nchanged = 0; // $sqlupd = 'UPDATE record INNER JOIN prop ON prop.record_id=record.record_id'; if ($date1) { $sql = "UPDATE prop AS p1 INNER JOIN record USING(record_id) SET " . $sqlset[0] . " WHERE p1.name = :name1 AND :date1 <= p1.value AND (" . $sqlwhere[1] . " OR " . $sqlwhere[2] . ")"; $params = array(); $params[':name1'] = $field1; $params[':date1'] = $date1; $params[':coll_id_set0'] = $tmp_params[':coll_id_set0']; $params[':coll_id_where1'] = $tmp_params[':coll_id_where1']; $params[':coll_id_where2'] = $tmp_params[':coll_id_where2']; $stmt = $this->connbas->prepare($sql); $stmt->execute($params); $n = $stmt->rowCount(); $stmt->closeCursor(); $nchanged += $n; if ($n > 0) $this->log(sprintf(("SQL=%s\n - %s changes"), $sql, $n)); } if ($date2 && $date1) { $sql = "UPDATE (prop AS p1 INNER JOIN prop AS p2 USING(record_id)) INNER JOIN record USING(record_id) SET " . $sqlset[1] . " WHERE p1.name = :name1 AND p2.name = :name2 AND :date1 > p1.value AND :date2 <= p2.value AND (" . $sqlwhere[0] . " OR " . $sqlwhere[2] . ")"; $params = array(); $params[':name1'] = $field1; $params[':name2'] = $field2; $params[':date1'] = $date1; $params[':date2'] = $date2; $params[':coll_id_set1'] = $tmp_params[':coll_id_set1']; $params[':coll_id_where0'] = $tmp_params[':coll_id_where0']; $params[':coll_id_where2'] = $tmp_params[':coll_id_where2']; $stmt = $this->connbas->prepare($sql); $stmt->execute($params); $n = $stmt->rowCount(); $stmt->closeCursor(); $nchanged += $n; if ($n > 0) $this->log(sprintf(("SQL=%s\n - %s changes"), $sql, $n)); } if ($date2) { $sql = "UPDATE prop AS p2 INNER JOIN record USING(record_id) SET " . $sqlset[2] . " WHERE p2.name = :name2 AND :date2 > p2.value AND (" . $sqlwhere[0] . " OR " . $sqlwhere[1] . ")"; $params = array(); $params[':name2'] = $field2; $params[':date2'] = $date2; $params[':coll_id_set2'] = $tmp_params[':coll_id_set2']; $params[':coll_id_where0'] = $tmp_params[':coll_id_where0']; $params[':coll_id_where1'] = $tmp_params[':coll_id_where1']; $stmt = $this->connbas->prepare($sql); $stmt->execute($params); $n = $stmt->rowCount(); $stmt->closeCursor(); $nchanged += $n; 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 = http_request::getInstance(); $parm2 = $request->get_parms( "bid" ); phrasea::headers(200, true, 'text/xml','UTF-8', false); $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; $sbas_id = (int) $parm2['bid']; try { $databox = databox::get_instance($sbas_id); $meta_struct = $databox->get_meta_structure(); foreach ($meta_struct as $meta) { if (mb_strtolower($meta->get_type()) == 'date') $dfields->appendChild($ret->createElement("field"))->appendChild($ret->createTextNode($meta->get_name())); } $status = $databox->get_statusbits(); foreach ($status as $n => $stat) { $node = $statbits->appendChild($ret->createElement("bit")); $node->setAttribute('n', $n); $node->setAttribute('value', '0'); $node->setAttribute('label', $stat['labeloff']); $node->appendChild($ret->createTextNode($stat['labeloff'])); $node = $statbits->appendChild($ret->createElement("bit")); $node->setAttribute('n', $n); $node->setAttribute('value', '1'); $node->setAttribute('label', $stat['labelon']); $node->appendChild($ret->createTextNode($stat['labelon'])); } foreach ($databox->get_collections() as $collection) { $node = $coll->appendChild($ret->createElement("collection")); $node->setAttribute('id', $collection->get_coll_id()); $node->appendChild($ret->createTextNode($collection->get_name())); } } catch (Exception $e) { } print($ret->saveXML()); } } ?>