Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.UpdateAttribute.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * update attribut for a document set
8  *
9  * @author Anakeen
10  * @version $Id: $
11  * @package FDL
12  */
13 /**
14  * Format document list to be easily used in
15  * @class UpdateAttribute
16  * @code
17  * $s = new \SearchDoc('', "MY_FAMILY");
18  $s->setObjectReturn();
19  $dl=new DocumentList($s);
20  $fc = new \UpdateAttribute();
21  $fc->useCollection($dl);
22  * @endcode
23  */
25 {
26  /*
27  * @var DocumentList $dl
28  */
29  public $dl = null;
30  private $dbaccess;
31  private $useRevision = false;
32  private $revisionComment = false;
33  private $historyComment = '';
34  private $statusFile = '';
35  private $famid = '';
36  private $transaction = false;
37  private $useProfiling = false;
38  private $error = '';
39  /**
40  * @var DbObj
41  */
42  private $transactionObject = null;
43  /**
44  * @var UpdateAttributeResults[]
45  */
46  private $results = array();
47 
48  public function __construct()
49  {
50  $this->dbaccess = getDbAccess();
51  }
52 
53  private function getFamily()
54  {
55  $search = $this->dl->getSearchDocument();
56  $this->famid = '';
57  if ($search) {
58  $famName = $search->fromid;
59  if (!is_numeric($famName)) $this->famid = getFamIdFromName($this->dbaccess, $famName);
60  else $this->famid = $famName;
61  }
62  return $this->famid;
63  }
64  /**
65  * document list to process
66  * @param DocumentList $l
67  * @throws Dcp\Upat\Exception
68  * @return UpdateAttribute
69  */
70  public function useCollection(DocumentList & $l)
71  {
72  $this->dl = $l;
73  $s = $l->getSearchDocument();
74  if (!$s->isObjectReturn()) throw new \Dcp\Upat\Exception(ErrorCode::getError("UPAT0002"));
75  return $this;
76  }
77  /**
78  * active revision before update documents
79  * @param string $revisionComment
80  * @return UpdateAttribute
81  */
82  public function addRevision($revisionComment = '')
83  {
84  $this->useRevision = true;
85  $this->revisionComment = $revisionComment;
86  return $this;
87  }
88  /**
89  * active revision before update documents
90  * @param string $historyComment
91  * @return UpdateAttribute
92  */
93  public function addHistoryComment($historyComment = '')
94  {
95  $this->historyComment = $historyComment;
96  return $this;
97  }
98  /**
99  * all modification are done in a sql transaction
100  * if only one request failed, none documents are modified
101  * default no use transaction
102  * @param bool $transaction
103  * @return UpdateAttribute
104  */
105  public function useTransaction($transaction = true)
106  {
107  $this->transaction = $transaction;
108  return $this;
109  }
110  /**
111  * use status file when use it in background processing
112  * @param string $statusFile file path where partial status are written
113  * @return UpdateAttribute
114  */
115  public function setStatusFile($statusFile)
116  {
117  $this->statusFile = $statusFile;
118  return $this;
119  }
120  /**
121  * add post action to recompute profiling for each modified documents in case of dynamic attribute references
122  * @param bool $profiling
123  * @return UpdateAttribute
124  */
125  public function useProfileUpdating($profiling = true)
126  {
127  $this->useProfiling = $profiling;
128  return $this;
129  }
130 
131  public function getError()
132  {
133  return $this->error;
134  }
135 
136  protected function executeProfiling(array $ids)
137  {
138  $dl = new DocumentList();
139  $dl->addDocumentIdentifiers($ids);
140  $c = 0;
141  $nbR = count($ids);
142  /*
143  * @var Doc $doc
144  */
145  foreach ($dl as $doc) {
146  $err = $doc->computeDProfil();
147  /*
148  * @var UpdateAttributeStatus $r
149  */
150  $r = & $this->results[$doc->initid];
151  $r->profiled = ($err == '');
152  $r->profilingError = $err;
153  $this->error.= $err;
154  $c++;
155  if ($c % 10 == 0) $this->logStatus(sprintf(_("%d/%d profiling done") , $c, $nbR));
156  }
157  $this->logStatus(sprintf(_("%d/%d profiling done") , $c, $nbR));
158  }
159  /**
160  * process a revision for each documents
161  */
162  protected function executeRevision(array $ids)
163  {
164  $this->logStatus(sprintf(_("process revision for %d documents") , $this->dl->length));
165  /*
166  * @var Doc $doc
167  */
168  $c = 0;
169  $nbR = count($ids);
170  foreach ($this->dl as $doc) {
171  if (isset($ids[$doc->id])) {
172  $err = $doc->revise($this->revisionComment);
173  /*
174  * @var UpdateAttributeStatus $r
175  */
176  $r = & $this->results[$doc->initid];
177  $r->revised = ($err == '');
178  $r->revisionError = $err;
179  $this->error.= $err;
180  $c++;
181  if ($c % 10 == 0) $this->logStatus(sprintf(_("%d/%d revision done") , $c, $nbR));
182  }
183  }
184  $this->logStatus(sprintf(_("%d/%d revision done") , $c, $nbR));
185  $this->logStatus(sprintf(_("process revision done")));
186  }
187  /**
188  * add history item for each document
189  * @param int[] $ids document identicators to process
190  * @return void
191  */
192  protected function executeHistory($ids)
193  {
194 
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 ";
197  $vs = array();
198  $u = getCurrentUser();
199  $uid = $u->id;
200  $uname = trim($u->firstname . ' ' . $u->lastname);
201  $date = Doc::getTimeDate(0, true);
202  $level = HISTO_MESSAGE;
203  $code = "UPDATE";
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));
206  }
207  $sql.= implode(',', $vs);
208  simpleQuery($this->dbaccess, $sql);
209 
210  foreach ($ids as $id => $initid) {
211  $this->results[$initid]->historyUpdated = true;
212  }
213  $this->logStatus(sprintf(_("Update history done")));
214  }
215 
216  private function executeSetValue(array $ids, $attrid, $newValue)
217  {
218  $this->logStatus(sprintf(_("process setValue for %d documents") , count($ids)));
219  if (is_array($newValue)) $newValue = Doc::arrayToRawValue($newValue);
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));
222  simpleQuery($this->dbaccess, $sql);
223  foreach ($ids as $id => $initid) {
224  $this->results[$initid]->changed = true;
225  }
226 
227  $this->logStatus(sprintf(_("%d documents are updated") , count($ids)));
228  }
229 
230  private function executeRemoveValue(array $ids, $attrid, $valueToRemove)
231  {
232  $this->logStatus(sprintf(_("process removeValue for %d documents") , count($ids)));
233 
234  if (is_array($valueToRemove)) {
235  foreach ($valueToRemove as $k => $v) {
236  $valueToRemove[$k] = pg_escape_string($v);
237  }
238  $searchValue = implode('|', $valueToRemove);
239  } else {
240  $searchValue = pg_escape_string($valueToRemove);
241  }
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));
244  simpleQuery($this->dbaccess, $sql);
245 
246  $oa = $this->getFamilyAttribute($attrid);
247  $singleMultiple = $doubleMultiple = false;
248  if ($oa->isMultipleInArray()) {
249  $doubleMultiple = true;
250  } elseif ($oa->isMultiple()) {
251  $singleMultiple = true;
252  }
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));
256  simpleQuery($this->dbaccess, $sql);
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));
258  simpleQuery($this->dbaccess, $sql);
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));
261  simpleQuery($this->dbaccess, $sql);
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));
263  simpleQuery($this->dbaccess, $sql);
264  }
265  }
266  foreach ($ids as $id => $initid) {
267  $this->results[$initid]->changed = true;
268  }
269 
270  $this->logStatus(sprintf(_("%d documents are updated") , count($ids)));
271  }
272  private function executeAddValue(array $ids, $attrid, $valueToAdd)
273  {
274  $pattrid = pg_escape_string(strtolower($attrid));
275  $this->logStatus(sprintf(_("process addValue for %d documents") , count($ids)));
276  if (is_array($valueToAdd)) $valueToAdd = Doc::arrayToRawValue($valueToAdd);
277 
278  $this->logStatus(sprintf(_("argument %s=>%s") , $attrid, $valueToAdd));
279  $oa = $this->getFamilyAttribute($attrid);
280  if ($oa->isMultipleInArray()) {
281  //double multiple
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));
283 
284  simpleQuery($this->dbaccess, $sql);
285  // trim when is first
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));
287 
288  simpleQuery($this->dbaccess, $sql);
289  } else {
290  // add if not null
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));
292 
293  simpleQuery($this->dbaccess, $sql);
294  // set when is null
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));
296 
297  simpleQuery($this->dbaccess, $sql);
298  }
299  foreach ($ids as $id => $initid) {
300  $this->results[$initid]->changed = true;
301  }
302 
303  $this->logStatus(sprintf(_("%d documents are updated") , count($ids)));
304  }
305 
306  private function executeReplaceValue(array $ids, $attrid, $oldvalue, $newValue)
307  {
308  $this->logStatus(sprintf(_("process replaceValue for %d documents") , count($ids)));
309 
310  if (is_array($newValue)) $newValue = Doc::arrayToRawValue($newValue);
311 
312  $this->logStatus(sprintf(_("argument %s=>%s") , $attrid, $oldvalue . '/' . $newValue));
313  $this->getFamily();
314  // replace for multiple
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));
316  simpleQuery($this->dbaccess, $sql);
317 
318  foreach ($ids as $id => $initid) {
319  $this->results[$initid]->changed = true;
320  }
321 
322  $this->logStatus(sprintf(_("%d documents are updated") , count($ids)));
323  }
324  /**
325  * change value of an attribute for docment list
326  * @param string $attrid attribute id
327  * @param string|string[] $newValue new value to set for attribute
328  * @throws Dcp\Upat\Exception
329  */
330  public function setValue($attrid, $newValue)
331  {
332  $oa = $this->getFamilyAttribute($attrid);
333  $attrid = $oa->id;
334  $this->getFamilyAttribute($attrid);
335  $this->logStatus("BEGIN");
336  if (!CheckAttr::checkAttrSyntax($attrid)) {
337  $this->logStatus("END");
338  throw new \Dcp\Upat\Exception(ErrorCode::getError("UPAT0001", $attrid));
339  }
340 
341  $newValue = $this->name2id($oa, $newValue);
342  $ids = array();
343  $upToDateIds = array();
344  /*
345  * @var Doc $doc
346  */
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);
350  $this->results[$doc->initid] = new UpdateAttributeResults();
351  }
352  foreach ($upToDateIds as $id => $initid) {
353  $this->results[$initid]->changed = false;
354  }
355 
356  $this->logStatus(sprintf(_("process %s") , json_encode(array_keys($ids))));
357  if ($ids) {
358  $this->beginTransaction();
359  if ($this->historyComment) $this->executeHistory($ids + $upToDateIds);
360  if ($this->useRevision) $this->executeRevision($ids);
361  $this->executeSetValue($ids, $attrid, $newValue);
362 
363  if ($this->useProfiling) $this->executeProfiling($ids);
364  $this->endTransaction();
365  }
366  $this->logStatusReport();
367  $this->logStatus("END");
368  }
369  /**
370  * replace value by another for an document list attribute
371  * @param string $attrid attribute id
372  * @param string $oldValue value to replace
373  * @param string|string[] $newValue new value to set for attribute
374  * @throws Dcp\Upat\Exception
375  */
376  public function replaceValue($attrid, $oldValue, $newValue)
377  {
378 
379  $oa = $this->getFamilyAttribute($attrid);
380  $attrid = $oa->id;
381 
382  $this->getFamilyAttribute($attrid);
383  $this->logStatus("BEGIN");
384  if (!CheckAttr::checkAttrSyntax($attrid)) {
385  $this->logStatus("END");
386  throw new \Dcp\Upat\Exception(ErrorCode::getError("UPAT0001", $attrid));
387  }
388  $ids = array();
389  $upToDateIds = array();
390  $oldValue = $this->name2id($oa, $oldValue);
391  $newValue = $this->name2id($oa, $newValue);
392  /*
393  * @var Doc $doc
394  */
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);
398  $this->results[$doc->initid] = new UpdateAttributeResults();
399  }
400  foreach ($upToDateIds as $id => $initid) {
401  $this->results[$initid]->changed = false;
402  }
403 
404  $this->logStatus(sprintf(_("process %s") , json_encode(array_keys($ids))));
405  if ($ids) {
406  $this->beginTransaction();
407  if ($this->historyComment) $this->executeHistory($ids + $upToDateIds);
408  if ($this->useRevision) $this->executeRevision($ids);
409  $this->executeReplaceValue($ids, $attrid, $oldValue, $newValue);
410  if ($this->useProfiling) $this->executeProfiling($ids);
411  $this->endTransaction();
412  }
413  $this->logStatusReport();
414  $this->logStatus("END");
415  }
416  /**
417  * @param $attrid
418  * @throws Dcp\Upat\Exception
419  * @return NormalAttribute
420  */
421  private function getFamilyAttribute($attrid)
422  {
423  $famid = $this->getFamily();
424  if (!$famid) throw new \Dcp\Upat\Exception(ErrorCode::getError("UPAT0006"));
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());
429  return $oa;
430  }
431  /**
432  * replace value by another for an document list attribute
433  * @param string $attrid attribute id
434  * @param string|\string[] $valueToAdd
435  * @throws Dcp\Upat\Exception
436  */
437  public function addValue($attrid, $valueToAdd)
438  {
439  $oa = $this->getFamilyAttribute($attrid);
440  $attrid = $oa->id;
441  $singleMultiple = $doubleMultiple = false;
442  if ($oa->isMultipleInArray()) {
443  $doubleMultiple = true;
444  } elseif ($oa->isMultiple()) {
445  $singleMultiple = true;
446  }
447 
448  if (!$singleMultiple && !$doubleMultiple) {
449  throw new \Dcp\Upat\Exception("UPAT0007", $attrid, $oa->docname);
450  }
451 
452  $valueToAdd = $this->name2id($oa, $valueToAdd);
453  $ids = array();
454  $upToDateIds = array();
455  if ($oa->inArray() && $singleMultiple) {
456  /*
457  * @var Doc $doc
458  */
459  foreach ($this->dl as $doc) {
460  $ids[$doc->id] = intval($doc->initid);
461 
462  $this->results[$doc->initid] = new UpdateAttributeResults();
463  }
464  foreach ($upToDateIds as $initid) {
465  $this->results[$initid]->changed = false;
466  }
467  } elseif ((!$oa->inArray()) && $singleMultiple) {
468  if (is_array($valueToAdd)) {
469  throw new \Dcp\Upat\Exception("UPAT0008", $attrid, $oa->docname);
470  }
471  /*
472  * @var Doc $doc
473  */
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);
477 
478  $this->results[$doc->initid] = new UpdateAttributeResults();
479  }
480  foreach ($upToDateIds as $initid) {
481  $this->results[$initid]->changed = false;
482  }
483  } elseif ($doubleMultiple) {
484  if (is_array($valueToAdd)) {
485  throw new \Dcp\Upat\Exception("UPAT0008", $attrid, $oa->docname);
486  }
487  /*
488  * @var Doc $doc
489  */
490  foreach ($this->dl as $doc) {
491  $ids[$doc->id] = intval($doc->initid);
492 
493  $this->results[$doc->initid] = new UpdateAttributeResults();
494  }
495  foreach ($upToDateIds as $initid) {
496  $this->results[$initid]->changed = false;
497  }
498  }
499  $this->logStatus("BEGIN");
500 
501  $this->logStatus(sprintf(_("process %s") , json_encode(array_keys($ids))));
502  if ($ids) {
503  $this->beginTransaction();
504  if ($this->historyComment) $this->executeHistory($ids + $upToDateIds);
505  if ($this->useRevision) $this->executeRevision($ids);
506  $this->executeAddValue($ids, $attrid, $valueToAdd);
507  if ($this->useProfiling) $this->executeProfiling($ids);
508  $this->endTransaction();
509  }
510  $this->logStatusReport();
511  $this->logStatus("END");
512  }
513 
514  protected function logStatusReport()
515  {
516  $this->logStatus("JSON:" . json_encode($this->getResults()));
517  $this->logStatus("PHP:" . serialize($this->getResults()));
518  }
519  /**
520  * replace value by another for an document list attribute
521  * @param string $attrid attribute id
522  * @param string|\string[] $valueToRemove
523  * @throws Dcp\Upat\Exception
524  */
525  public function removeValue($attrid, $valueToRemove)
526  {
527  $oa = $this->getFamilyAttribute($attrid);
528  $attrid = $oa->id;
529  $singleMultiple = $doubleMultiple = false;
530  if ($oa->isMultipleInArray()) {
531  $doubleMultiple = true;
532  } elseif ($oa->isMultiple()) {
533  $singleMultiple = true;
534  }
535 
536  if (!$singleMultiple && !$doubleMultiple) {
537  throw new \Dcp\Upat\Exception("UPAT0009", $attrid, $oa->docname);
538  }
539  $valueToRemove = $this->name2id($oa, $valueToRemove);
540 
541  $ids = array();
542  $upToDateIds = array();
543  /*
544  * @var Doc $doc
545  */
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);
553 
554  $this->results[$doc->initid] = new UpdateAttributeResults();
555  }
556  foreach ($upToDateIds as $initid) {
557  $this->results[$initid]->changed = false;
558  }
559 
560  $this->logStatus("BEGIN");
561 
562  $this->logStatus(sprintf(_("process %s") , json_encode(array_keys($ids))));
563  if ($ids) {
564  $this->beginTransaction();
565  if ($this->historyComment) $this->executeHistory($ids + $upToDateIds);
566  if ($this->useRevision) $this->executeRevision($ids);
567  $this->executeRemoveValue($ids, $attrid, $valueToRemove);
568  if ($this->useProfiling) $this->executeProfiling($ids);
569  $this->endTransaction();
570  }
571  $this->logStatusReport();
572  $this->logStatus("END");
573  }
574 
575  private function beginTransaction()
576  {
577  if ($this->transaction) {
578  if (!$this->transactionObject) {
579  $this->transactionObject = new DbObj($this->dbaccess);
580  }
581  $this->transactionObject->savePoint("dcp:updateattr");
582  }
583  }
584 
585  private function endTransaction()
586  {
587  if ($this->transaction) {
588  if ($this->error) {
589  $this->transactionObject->rollbackPoint("dcp:updateattr");
590  } else {
591  $this->transactionObject->commitPoint("dcp:updateattr");
592  }
593  }
594  }
595 
596  private function name2id($oa, $values)
597  {
598  if ($oa->type == "docid" || $oa->type == "account") {
599  if (is_array($values)) {
600  foreach ($values as $ka => $aValue) {
601  $aValue = $this->getInitIdFromName($aValue);
602  $values[$ka] = $aValue;
603  }
604  } else {
605  if (!is_numeric($values)) $values = $this->getInitIdFromName($values);
606  }
607  }
608 
609  return $values;
610  }
611  /**
612  * fet initial document identifier from name
613  * @param string $name
614  * @return int
615  */
616  public static function getInitIdFromName($name)
617  {
618  simpleQuery(getDbAccess() , sprintf("select initid from docread where name = '%s' limit 1", pg_escape_string($name)) , $initid, true, true);
619  return $initid;
620  }
621  private function linearize($v)
622  {
623  $t = explode("\n", str_replace('<BR>', "\n", $v));
624  foreach ($t as $k => $vt) {
625  if (!$vt) unset($t[$k]);
626  }
627  return $t;
628  }
629  public function logStatus($s)
630  {
631  if ($this->statusFile) {
632  $st = getDebugStack(2);
633 
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);
637  }
638  }
639  /**
640  * call ::setValue() in a background process
641  * the return filename can be use by UpdateAttributeStatus to see information trace from background process
642  * @see UpdateAttributeStatus
643  * @param string $attrid
644  * @param string|string[] $newValue
645  * @return string filename status
646  */
647  public function bgSetValue($attrid, $newValue)
648  {
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()));
653  $wsh = getWshCmd(true, getCurrentUser()->id);
654  $tmpStatus = tempnam(getTmpDir() , 'statusSetValue');
655 
656  $cmd[] = sprintf("$wsh --api=updateAttribute --objectFile=%s --argsFile=%s --statusFile=%s --method=setValue", escapeshellarg($tmpThis) , escapeshellarg($tmpArgs) , escapeshellarg($tmpStatus));
657 
658  bgexec($cmd, $result, $err);
659  return $tmpStatus;
660  }
661  /**
662  * call ::addValue() in a background process
663  * the return filename can be use by UpdateAttributeStatus to see information trace from background process
664  * @see UpdateAttributeStatus
665  * @param string $attrid attribute id
666  * @param string|\string[] $valueToAdd
667  * @return string filename status
668  */
669  public function bgAddValue($attrid, $valueToAdd)
670  {
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()));
675  $wsh = getWshCmd(true, getCurrentUser()->id);
676  $tmpStatus = tempnam(getTmpDir() , 'statusAddValue');
677 
678  $cmd[] = sprintf("$wsh --api=updateAttribute --objectFile=%s --argsFile=%s --statusFile=%s --method=addValue", escapeshellarg($tmpThis) , escapeshellarg($tmpArgs) , escapeshellarg($tmpStatus));
679 
680  bgexec($cmd, $result, $err);
681  return $tmpStatus;
682  }
683  /**
684  * call ::removeValue() in a background process
685  * the return filename can be use by UpdateAttributeStatus to see information trace from background process
686  * @see UpdateAttributeStatus
687  * @param string $attrid attribute id
688  * @param string|\string[] $valueToRemove
689  * @return string filename status
690  */
691  public function bgRemoveValue($attrid, $valueToRemove)
692  {
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()));
697  $wsh = getWshCmd(true, getCurrentUser()->id);
698  $tmpStatus = tempnam(getTmpDir() , 'statusRemoveValue');
699 
700  $cmd[] = sprintf("$wsh --api=updateAttribute --objectFile=%s --argsFile=%s --statusFile=%s --method=removeValue", escapeshellarg($tmpThis) , escapeshellarg($tmpArgs) , escapeshellarg($tmpStatus));
701 
702  bgexec($cmd, $result, $err);
703  return $tmpStatus;
704  }
705  /**
706  * call ::replaceValue() in a background process
707  * the return filename can be use by UpdateAttributeStatus to see information trace from background process
708  * @see UpdateAttributeStatus
709  * @param string $attrid attribute id
710  * @param string $oldValue value to replace
711  * @param string|string[] $newValue new value to set for attribute
712  * @return string filename status
713  */
714  public function bgReplaceValue($attrid, $oldValue, $newValue)
715  {
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()));
720  $wsh = getWshCmd(true, getCurrentUser()->id);
721  $tmpStatus = tempnam(getTmpDir() , 'statusReplValue');
722 
723  $cmd[] = sprintf("$wsh --api=updateAttribute --objectFile=%s --argsFile=%s --statusFile=%s --method=replaceValue", escapeshellarg($tmpThis) , escapeshellarg($tmpArgs) , escapeshellarg($tmpStatus));
724 
725  bgexec($cmd, $result, $err);
726  return $tmpStatus;
727  }
728  /**
729  * @return null|UpdateAttributeResults[]
730  */
731  public function getResults()
732  {
733  return $this->results;
734  }
735 }
736 
738 {
739  /**
740  * @var bool true if document has been modified
741  */
742  public $changed = null;
743  public $revisionError = '';
744  public $profilingError = '';
745  /**
746  * @var bool true if document has been profiled
747  */
748  public $profiled = false;
749  /**
750  * @var bool true if document has been revised
751  */
752  public $revised = false;
753  /**
754  * @var bool true if history has been updated
755  */
756  public $historyUpdated = false;
757 }
758 
static getInitIdFromName($name)
const HISTO_MESSAGE
bgSetValue($attrid, $newValue)
removeValue($attrid, $valueToRemove)
addValue($attrid, $valueToAdd)
replaceValue($attrid, $oldValue, $newValue)
if($famId) $s
static getError($code, $args=null)
Definition: ErrorCode.php:27
bgAddValue($attrid, $valueToAdd)
setValue($attrid, $newValue)
useTransaction($transaction=true)
foreach($argv as $arg) $cmd
$search
getWshCmd($nice=false, $userid=0, $sudo=false)
Definition: Lib.Common.php:594
static checkAttrSyntax($attrid)
Definition: checkAttr.php:609
useProfileUpdating($profiling=true)
getDebugStack($slice=1)
Definition: Lib.Common.php:325
getFamIdFromName($dbaccess, $name)
addRevision($revisionComment= '')
getTmpDir($def= '/tmp')
Definition: Lib.Common.php:150
getCurrentUser()
Definition: Lib.Common.php:250
addHistoryComment($historyComment= '')
getDbAccess()
Definition: Lib.Common.php:368
static getTimeDate($hourdelta=0, $second=false)
Definition: Class.Doc.php:8826
static arrayToRawValue($v, $br= '< BR >')
Definition: Class.Doc.php:6252
simpleQuery($dbaccess, $query, &$result=array(), $singlecolumn=false, $singleresult=false, $useStrict=null)
Definition: Lib.Common.php:484
bgReplaceValue($attrid, $oldValue, $newValue)
bgexec($tcmd, &$result, &$err)
Definition: Lib.Common.php:621
if($file) if($subject==""&&$file) if($subject=="") $err
useCollection(DocumentList &$l)
bgRemoveValue($attrid, $valueToRemove)
← centre documentaire © anakeen