31 private $useRevision =
false;
32 private $revisionComment =
false;
33 private $historyComment =
'';
34 private $statusFile =
'';
36 private $transaction =
false;
37 private $useProfiling =
false;
42 private $transactionObject = null;
46 private $results = array();
53 private function getFamily()
55 $search = $this->dl->getSearchDocument();
59 if (!is_numeric($famName)) $this->famid =
getFamIdFromName($this->dbaccess, $famName);
60 else $this->famid = $famName;
84 $this->useRevision =
true;
85 $this->revisionComment = $revisionComment;
95 $this->historyComment = $historyComment;
107 $this->transaction = $transaction;
117 $this->statusFile = $statusFile;
127 $this->useProfiling = $profiling;
139 $dl->addDocumentIdentifiers($ids);
146 $err = $doc->computeDProfil();
150 $r = & $this->results[$doc->initid];
151 $r->profiled = (
$err ==
'');
152 $r->profilingError =
$err;
155 if (
$c % 10 == 0) $this->
logStatus(sprintf(_(
"%d/%d profiling done") ,
$c, $nbR));
157 $this->
logStatus(sprintf(_(
"%d/%d profiling done") ,
$c, $nbR));
164 $this->
logStatus(sprintf(_(
"process revision for %d documents") , $this->dl->length));
170 foreach ($this->dl as
$doc) {
171 if (isset($ids[$doc->id])) {
172 $err = $doc->revise($this->revisionComment);
176 $r = & $this->results[$doc->initid];
177 $r->revised = (
$err ==
'');
181 if (
$c % 10 == 0) $this->
logStatus(sprintf(_(
"%d/%d revision done") ,
$c, $nbR));
184 $this->
logStatus(sprintf(_(
"%d/%d revision done") ,
$c, $nbR));
185 $this->
logStatus(sprintf(_(
"process revision done")));
195 $this->
logStatus(sprintf(_(
"process history for %d documents") , count($ids)));
196 $sql =
"insert into dochisto (id,initid,uid,uname,date,level,code,comment ) values ";
200 $uname = trim($u->firstname .
' ' . $u->lastname);
204 foreach ($ids as $id => $initid) {
205 $vs[] = sprintf(
"(%d,%d,%d,'%s','%s',%d,'%s','%s')", $id, $initid, $uid, pg_escape_string($uname) , pg_escape_string($date) , $level, pg_escape_string($code) , pg_escape_string($this->historyComment));
207 $sql.= implode(
',', $vs);
210 foreach ($ids as $id => $initid) {
211 $this->results[$initid]->historyUpdated =
true;
213 $this->
logStatus(sprintf(_(
"Update history done")));
216 private function executeSetValue(array $ids, $attrid, $newValue)
218 $this->
logStatus(sprintf(_(
"process setValue for %d documents") , count($ids)));
220 $this->
logStatus(sprintf(_(
"argument %s=>%s") , $attrid, $newValue));
221 $sql = sprintf(
"update doc%s set \"%s\"=E'%s' where locked != -1 and initid in (%s)", $this->famid, ($attrid) , pg_escape_string($newValue) , implode(
',', $ids));
223 foreach ($ids as $id => $initid) {
224 $this->results[$initid]->changed =
true;
227 $this->
logStatus(sprintf(_(
"%d documents are updated") , count($ids)));
230 private function executeRemoveValue(array $ids, $attrid, $valueToRemove)
232 $this->
logStatus(sprintf(_(
"process removeValue for %d documents") , count($ids)));
234 if (is_array($valueToRemove)) {
235 foreach ($valueToRemove as $k => $v) {
236 $valueToRemove[$k] = pg_escape_string($v);
238 $searchValue = implode(
'|', $valueToRemove);
240 $searchValue = pg_escape_string($valueToRemove);
242 $this->
logStatus(sprintf(_(
"argument %s=>%s") , $attrid, print_r($valueToRemove,
true)));
243 $sql = sprintf(
"update doc%s set \"%s\"=regexp_replace(\"%s\",E'(\\\\A|\\n|<BR>)(%s)(\\\\Z|\n|<BR>)',E'\\\\1\\\\3','g') where locked != -1 and initid in (%s)", $this->famid, $attrid, $attrid, $searchValue, implode(
',', $ids));
246 $oa = $this->getFamilyAttribute($attrid);
247 $singleMultiple = $doubleMultiple =
false;
248 if ($oa->isMultipleInArray()) {
249 $doubleMultiple =
true;
250 } elseif ($oa->isMultiple()) {
251 $singleMultiple =
true;
253 if ($oa->getOption(
"multiple") ==
"yes") {
254 if ($singleMultiple) {
255 $sql = sprintf(
"update doc%s set \"%s\"=regexp_replace(\"%s\", E'(\\\\A\\n)|(\\n\\\\Z)', '','g') where locked != -1 and initid in (%s)", $this->famid, $attrid, $attrid, implode(
',', $ids));
257 $sql = sprintf(
"update doc%s set \"%s\"=regexp_replace(\"%s\", E'([\\n]+)', E'\n','g') where locked != -1 and initid in (%s)", $this->famid, $attrid, $attrid, implode(
',', $ids));
259 } elseif ($doubleMultiple) {
260 $sql = sprintf(
"update doc%s set \"%s\"=regexp_replace(\"%s\", '(\\\\A<BR>)|(<BR>\\\\Z)', '','g') where locked != -1 and initid in (%s)", $this->famid, $attrid, $attrid, implode(
',', $ids));
262 $sql = sprintf(
"update doc%s set \"%s\"=regexp_replace(\"%s\", '(<BR>)+', '<BR>','g') where locked != -1 and initid in (%s)", $this->famid, $attrid, $attrid, implode(
',', $ids));
266 foreach ($ids as $id => $initid) {
267 $this->results[$initid]->changed =
true;
270 $this->
logStatus(sprintf(_(
"%d documents are updated") , count($ids)));
272 private function executeAddValue(array $ids, $attrid, $valueToAdd)
274 $pattrid = pg_escape_string(strtolower($attrid));
275 $this->
logStatus(sprintf(_(
"process addValue for %d documents") , count($ids)));
278 $this->
logStatus(sprintf(_(
"argument %s=>%s") , $attrid, $valueToAdd));
279 $oa = $this->getFamilyAttribute($attrid);
280 if ($oa->isMultipleInArray()) {
282 $sql = sprintf(
"update doc%s set \"%s\"=regexp_replace(\"%s\",E'\$',E'<BR>%s','gn') where locked != -1 and \"%s\" is not null and initid in (%s)", $this->famid, $pattrid, $pattrid, pg_escape_string($valueToAdd) , $pattrid, implode(
',', $ids));
286 $sql = sprintf(
"update doc%s set \"%s\"=regexp_replace(\"%s\",E'^<BR>','','gn') where locked != -1 and \"%s\" is not null and initid in (%s)", $this->famid, $pattrid, $pattrid, $pattrid, implode(
',', $ids));
291 $sql = sprintf(
"update doc%s set \"%s\"=\"%s\" || E'\\n%s' where locked != -1 and \"%s\" is not null and initid in (%s)", $this->famid, $pattrid, $pattrid, pg_escape_string($valueToAdd) , $pattrid, implode(
',', $ids));
295 $sql = sprintf(
"update doc%s set \"%s\"= E'%s' where locked != -1 and \"%s\" is null and initid in (%s)", $this->famid, $pattrid, pg_escape_string($valueToAdd) , $pattrid, implode(
',', $ids));
299 foreach ($ids as $id => $initid) {
300 $this->results[$initid]->changed =
true;
303 $this->
logStatus(sprintf(_(
"%d documents are updated") , count($ids)));
306 private function executeReplaceValue(array $ids, $attrid, $oldvalue, $newValue)
308 $this->
logStatus(sprintf(_(
"process replaceValue for %d documents") , count($ids)));
312 $this->
logStatus(sprintf(_(
"argument %s=>%s") , $attrid, $oldvalue .
'/' . $newValue));
315 $sql = sprintf(
"update doc%s set \"%s\"= regexp_replace(\"%s\",E'(\\\\A|\\n)%s(\\\\Z|\\n)',E'\\\\1%s\\\\2','g') where locked != -1 and initid in (%s)", $this->famid, strtolower($attrid) , strtolower($attrid) , pg_escape_string($oldvalue) , pg_escape_string($newValue) , implode(
',', $ids));
318 foreach ($ids as $id => $initid) {
319 $this->results[$initid]->changed =
true;
322 $this->
logStatus(sprintf(_(
"%d documents are updated") , count($ids)));
332 $oa = $this->getFamilyAttribute($attrid);
334 $this->getFamilyAttribute($attrid);
341 $newValue = $this->name2id($oa, $newValue);
343 $upToDateIds = array();
347 foreach ($this->dl as
$doc) {
348 if ($doc->getRawValue($attrid) != $newValue) $ids[$doc->id] = intval($doc->initid);
349 else $upToDateIds[$doc->id] = intval($doc->initid);
352 foreach ($upToDateIds as $id => $initid) {
353 $this->results[$initid]->changed =
false;
356 $this->
logStatus(sprintf(_(
"process %s") , json_encode(array_keys($ids))));
358 $this->beginTransaction();
359 if ($this->historyComment) $this->
executeHistory($ids + $upToDateIds);
361 $this->executeSetValue($ids, $attrid, $newValue);
364 $this->endTransaction();
379 $oa = $this->getFamilyAttribute($attrid);
382 $this->getFamilyAttribute($attrid);
389 $upToDateIds = array();
390 $oldValue = $this->name2id($oa, $oldValue);
391 $newValue = $this->name2id($oa, $newValue);
395 foreach ($this->dl as
$doc) {
396 if (preg_match(sprintf(
'/\b%s\b/', preg_quote($oldValue,
"/")) , $doc->getRawValue($attrid))) $ids[$doc->id] = intval($doc->initid);
397 else $upToDateIds[$doc->id] = intval($doc->initid);
400 foreach ($upToDateIds as $id => $initid) {
401 $this->results[$initid]->changed =
false;
404 $this->
logStatus(sprintf(_(
"process %s") , json_encode(array_keys($ids))));
406 $this->beginTransaction();
407 if ($this->historyComment) $this->
executeHistory($ids + $upToDateIds);
409 $this->executeReplaceValue($ids, $attrid, $oldValue, $newValue);
411 $this->endTransaction();
421 private function getFamilyAttribute($attrid)
423 $famid = $this->getFamily();
425 $fam = new_doc($this->dbaccess, $famid);
426 if (!$fam->isAlive())
throw new \Dcp\Upat\Exception(
ErrorCode::getError(
"UPAT0005", $this->getFamily()));
427 $oa = $fam->getAttribute($attrid);
428 if (!$oa)
throw new \Dcp\Upat\Exception(
"UPAT0004", $attrid, $fam->getTitle());
439 $oa = $this->getFamilyAttribute($attrid);
441 $singleMultiple = $doubleMultiple =
false;
442 if ($oa->isMultipleInArray()) {
443 $doubleMultiple =
true;
444 } elseif ($oa->isMultiple()) {
445 $singleMultiple =
true;
448 if (!$singleMultiple && !$doubleMultiple) {
449 throw new \Dcp\Upat\Exception(
"UPAT0007", $attrid, $oa->docname);
452 $valueToAdd = $this->name2id($oa, $valueToAdd);
454 $upToDateIds = array();
455 if ($oa->inArray() && $singleMultiple) {
459 foreach ($this->dl as
$doc) {
460 $ids[$doc->id] = intval($doc->initid);
464 foreach ($upToDateIds as $initid) {
465 $this->results[$initid]->changed =
false;
467 } elseif ((!$oa->inArray()) && $singleMultiple) {
468 if (is_array($valueToAdd)) {
469 throw new \Dcp\Upat\Exception(
"UPAT0008", $attrid, $oa->docname);
474 foreach ($this->dl as
$doc) {
475 if (!in_array($valueToAdd, $doc->getMultipleRawValues($attrid))) $ids[$doc->id] = intval($doc->initid);
476 else $upToDateIds[$doc->id] = intval($doc->initid);
480 foreach ($upToDateIds as $initid) {
481 $this->results[$initid]->changed =
false;
483 } elseif ($doubleMultiple) {
484 if (is_array($valueToAdd)) {
485 throw new \Dcp\Upat\Exception(
"UPAT0008", $attrid, $oa->docname);
490 foreach ($this->dl as
$doc) {
491 $ids[$doc->id] = intval($doc->initid);
495 foreach ($upToDateIds as $initid) {
496 $this->results[$initid]->changed =
false;
501 $this->
logStatus(sprintf(_(
"process %s") , json_encode(array_keys($ids))));
503 $this->beginTransaction();
504 if ($this->historyComment) $this->
executeHistory($ids + $upToDateIds);
506 $this->executeAddValue($ids, $attrid, $valueToAdd);
508 $this->endTransaction();
527 $oa = $this->getFamilyAttribute($attrid);
529 $singleMultiple = $doubleMultiple =
false;
530 if ($oa->isMultipleInArray()) {
531 $doubleMultiple =
true;
532 } elseif ($oa->isMultiple()) {
533 $singleMultiple =
true;
536 if (!$singleMultiple && !$doubleMultiple) {
537 throw new \Dcp\Upat\Exception(
"UPAT0009", $attrid, $oa->docname);
539 $valueToRemove = $this->name2id($oa, $valueToRemove);
542 $upToDateIds = array();
546 foreach ($this->dl as
$doc) {
547 if (is_array($valueToRemove)) {
548 if (array_intersect($this->linearize($doc->getRawValue($attrid)) , $valueToRemove)) {
549 $ids[$doc->id] = intval($doc->initid);
550 }
else $upToDateIds[$doc->id] = intval($doc->initid);
551 } elseif (in_array($valueToRemove, $this->linearize($doc->getRawValue($attrid)))) $ids[$doc->id] = intval($doc->initid);
552 else $upToDateIds[$doc->id] = intval($doc->initid);
556 foreach ($upToDateIds as $initid) {
557 $this->results[$initid]->changed =
false;
562 $this->
logStatus(sprintf(_(
"process %s") , json_encode(array_keys($ids))));
564 $this->beginTransaction();
565 if ($this->historyComment) $this->
executeHistory($ids + $upToDateIds);
567 $this->executeRemoveValue($ids, $attrid, $valueToRemove);
569 $this->endTransaction();
575 private function beginTransaction()
577 if ($this->transaction) {
578 if (!$this->transactionObject) {
579 $this->transactionObject =
new DbObj($this->dbaccess);
581 $this->transactionObject->savePoint(
"dcp:updateattr");
585 private function endTransaction()
587 if ($this->transaction) {
589 $this->transactionObject->rollbackPoint(
"dcp:updateattr");
591 $this->transactionObject->commitPoint(
"dcp:updateattr");
596 private function name2id($oa, $values)
598 if ($oa->type ==
"docid" || $oa->type ==
"account") {
599 if (is_array($values)) {
600 foreach ($values as $ka => $aValue) {
602 $values[$ka] = $aValue;
618 simpleQuery(
getDbAccess() , sprintf(
"select initid from docread where name = '%s' limit 1", pg_escape_string($name)) , $initid,
true,
true);
621 private function linearize($v)
623 $t = explode(
"\n", str_replace(
'<BR>',
"\n", $v));
624 foreach ($t as $k => $vt) {
625 if (!$vt) unset($t[$k]);
631 if ($this->statusFile) {
634 $f = $st[0][
"function"];
635 if ($f ==
"include") $f =
'-';
636 file_put_contents($this->statusFile, sprintf(
"%s %s %s\n", date(
"Y-m-d\\TH:i:s") , $f,
$s) , FILE_APPEND);
649 $tmpThis = tempnam(
getTmpDir() ,
'uptSetValue');
650 file_put_contents($tmpThis, serialize($this));
651 $tmpArgs = tempnam(
getTmpDir() ,
'argSetValue');
652 file_put_contents($tmpArgs, serialize(func_get_args()));
654 $tmpStatus = tempnam(
getTmpDir() ,
'statusSetValue');
656 $cmd[] = sprintf(
"$wsh --api=updateAttribute --objectFile=%s --argsFile=%s --statusFile=%s --method=setValue", escapeshellarg($tmpThis) , escapeshellarg($tmpArgs) , escapeshellarg($tmpStatus));
671 $tmpThis = tempnam(
getTmpDir() ,
'uptAddValue');
672 file_put_contents($tmpThis, serialize($this));
673 $tmpArgs = tempnam(
getTmpDir() ,
'argAddValue');
674 file_put_contents($tmpArgs, serialize(func_get_args()));
676 $tmpStatus = tempnam(
getTmpDir() ,
'statusAddValue');
678 $cmd[] = sprintf(
"$wsh --api=updateAttribute --objectFile=%s --argsFile=%s --statusFile=%s --method=addValue", escapeshellarg($tmpThis) , escapeshellarg($tmpArgs) , escapeshellarg($tmpStatus));
693 $tmpThis = tempnam(
getTmpDir() ,
'uptRemoveValue');
694 file_put_contents($tmpThis, serialize($this));
695 $tmpArgs = tempnam(
getTmpDir() ,
'argRemoveValue');
696 file_put_contents($tmpArgs, serialize(func_get_args()));
698 $tmpStatus = tempnam(
getTmpDir() ,
'statusRemoveValue');
700 $cmd[] = sprintf(
"$wsh --api=updateAttribute --objectFile=%s --argsFile=%s --statusFile=%s --method=removeValue", escapeshellarg($tmpThis) , escapeshellarg($tmpArgs) , escapeshellarg($tmpStatus));
716 $tmpThis = tempnam(
getTmpDir() ,
'uptReplValue');
717 file_put_contents($tmpThis, serialize($this));
718 $tmpArgs = tempnam(
getTmpDir() ,
'argReplValue');
719 file_put_contents($tmpArgs, serialize(func_get_args()));
721 $tmpStatus = tempnam(
getTmpDir() ,
'statusReplValue');
723 $cmd[] = sprintf(
"$wsh --api=updateAttribute --objectFile=%s --argsFile=%s --statusFile=%s --method=replaceValue", escapeshellarg($tmpThis) , escapeshellarg($tmpArgs) , escapeshellarg($tmpStatus));
733 return $this->results;
static getInitIdFromName($name)
bgSetValue($attrid, $newValue)
removeValue($attrid, $valueToRemove)
addValue($attrid, $valueToAdd)
replaceValue($attrid, $oldValue, $newValue)
executeProfiling(array $ids)
static getError($code, $args=null)
bgAddValue($attrid, $valueToAdd)
setValue($attrid, $newValue)
useTransaction($transaction=true)
foreach($argv as $arg) $cmd
getWshCmd($nice=false, $userid=0, $sudo=false)
static checkAttrSyntax($attrid)
useProfileUpdating($profiling=true)
getFamIdFromName($dbaccess, $name)
addRevision($revisionComment= '')
addHistoryComment($historyComment= '')
executeRevision(array $ids)
setStatusFile($statusFile)
static getTimeDate($hourdelta=0, $second=false)
static arrayToRawValue($v, $br= '< BR >')
simpleQuery($dbaccess, $query, &$result=array(), $singlecolumn=false, $singleresult=false, $useStrict=null)
bgReplaceValue($attrid, $oldValue, $newValue)
bgexec($tcmd, &$result, &$err)
if($file) if($subject==""&&$file) if($subject=="") $err
useCollection(DocumentList &$l)
bgRemoveValue($attrid, $valueToRemove)