/**
* @see RuleCollection::replayRulesOnExistingDB()
**/
function replayRulesOnExistingDB($offset = 0, $maxtime = 0, $items = array(), $params = array())
{
global $DB;
if (isCommandLine()) {
echo "replayRulesOnExistingDB started : " . date("r") . "\n";
}
$nb = 0;
$i = $offset;
if (count($items) == 0) {
//Select all the differents software
$sql = "SELECT DISTINCT `glpi_softwares`.`name`,\n `glpi_manufacturers`.`name` AS manufacturer,\n `glpi_softwares`.`manufacturers_id` AS manufacturers_id,\n `glpi_softwares`.`entities_id` AS entities_id,\n `glpi_softwares`.`is_helpdesk_visible` AS helpdesk\n FROM `glpi_softwares`\n LEFT JOIN `glpi_manufacturers`\n ON (`glpi_manufacturers`.`id` = `glpi_softwares`.`manufacturers_id`)";
// Do not replay on dustbin and templates
$sql .= "WHERE `glpi_softwares`.`is_deleted` = '0'\n AND `glpi_softwares`.`is_template` = '0' ";
if (isset($params['manufacturer']) && $params['manufacturer']) {
$sql .= " AND `glpi_softwares`.`manufacturers_id` = '" . $params['manufacturer'] . "'";
}
if ($offset) {
$sql .= " LIMIT " . intval($offset) . ",999999999";
}
$res = $DB->query($sql);
$nb = $DB->numrows($res) + $offset;
$step = $nb > 1000 ? 50 : ($nb > 20 ? floor($DB->numrows($res) / 20) : 1);
while ($input = $DB->fetch_assoc($res)) {
if (!($i % $step)) {
if (isCommandLine()) {
printf(__('%1$s - replay rules on existing database: %2$s/%3$s (%4$s Mio)') . "\n", date("H:i:s"), $i, $nb, round(memory_get_usage() / (1024 * 1024), 2));
} else {
Html::changeProgressBarPosition($i, $nb, "{$i} / {$nb}");
}
}
//If manufacturer is set, then first run the manufacturer's dictionnary
if (isset($input["manufacturer"])) {
$input["manufacturer"] = Manufacturer::processName(addslashes($input["manufacturer"]));
}
//Replay software dictionnary rules
$res_rule = $this->processAllRules($input, array(), array());
if (isset($res_rule["name"]) && $res_rule["name"] != $input["name"] || isset($res_rule["version"]) && $res_rule["version"] != '' || isset($res_rule['new_entities_id']) && $res_rule['new_entities_id'] != $input['entities_id'] || isset($res_rule['is_helpdesk_visible']) && $res_rule['is_helpdesk_visible'] != $input['helpdesk'] || isset($res_rule['manufacturer']) && $res_rule['manufacturer'] != $input['manufacturer']) {
$IDs = array();
//Find all the softwares in the database with the same name and manufacturer
$sql = "SELECT `id`\n FROM `glpi_softwares`\n WHERE `name` = '" . $input["name"] . "'\n AND `manufacturers_id` = '" . $input["manufacturers_id"] . "'";
$res_soft = $DB->query($sql);
if ($DB->numrows($res_soft) > 0) {
//Store all the software's IDs in an array
while ($result = $DB->fetch_assoc($res_soft)) {
$IDs[] = $result["id"];
}
//Replay dictionnary on all the softwares
$this->replayDictionnaryOnSoftwaresByID($IDs, $res_rule);
}
}
$i++;
if ($maxtime) {
$crt = explode(" ", microtime());
if ($crt[0] + $crt[1] > $maxtime) {
break;
}
}
}
// each distinct software
if (isCommandLine()) {
printf(__('Replay rules on existing database: %1$s/%2$s') . " \n", $i, $nb);
} else {
Html::changeProgressBarPosition($i, $nb, "{$i} / {$nb}");
}
} else {
$this->replayDictionnaryOnSoftwaresByID($items);
return true;
}
if (isCommandLine()) {
printf(__('Replay rules on existing database ended on %s') . "\n", date("r"));
}
return $i == $nb ? -1 : $i;
}