Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.UpdateAttributeStatus.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * update attribut status management
8  *
9  * @author Anakeen
10  * @package FDL
11  */
12 /**
13  * Format document list to be easily used in
14  * @class UpdateAttributeStatus
15  * @code
16  $newValue=4;
17  $attrid='TST_ENUM';
18  $s = new \SearchDoc('', 'TST_UPDTATTR');
19  $s->setObjectReturn();
20  $s->setSlice(34);
21  $dl = new \DocumentList($s);
22  $ua = new \UpdateAttribute();
23  $ua->useCollection($dl);
24  $comment = "coucoux";
25  $ua->addHistoryComment($comment);
26  $ua->revise('REvX');
27  $statusFile= $ua->bgSetValue($attrid, $newValue);
28  $sua= new UpdateAttributeStatus($statusFile);
29  while (! $sua->isFinished()) {
30  print $sua->getStatus();
31  print ".";
32  sleep(1);
33  }
34  * @endcode
35  */
37 {
38 
39  private $statusFile = '';
40  private $content = null;
41  const statusRunning = 1;
42  const StatusFinished = 2;
43  const statusUnknown = 3;
44 
45  public function __construct($statusFile)
46  {
47  $this->statusFile = $statusFile;
48  if (!file_exists($this->statusFile)) throw new Dcp\Upat\Exception("UPAT0003", $statusFile);
49  }
50 
51  private function readStatus()
52  {
53  $this->content = file($this->statusFile);
54  }
55  /**
56  * return file status content
57  * one line by array items
58  * @return string[]
59  */
60  public function getContent()
61  {
62  if ($this->content === null) $this->readStatus();
63  return $this->content;
64  }
65  /**
66  * return global status
67  * update file status content
68  * @return int StatusFinished, StatusRunning or statusUnknown
69  */
70  public function getStatus()
71  {
72  $this->readStatus();
73  if ($this->content) {
74  $last = end($this->content);
75  if (strpos($last, 'END') > 0) return self::StatusFinished;
76  $first = $this->content[0];
77  if (strpos($first, 'BEGIN') > 0) return self::statusRunning;
78  }
79  return self::statusUnknown;
80  }
81  /**
82  * get lines which match code
83  * @param string $code
84  * @return string[]
85  */
86  public function getCodeLines($code)
87  {
88  if ($this->content === null) $this->readStatus();
89  $lines = array();
90  foreach ($this->content as $line) {
91  if (preg_match(sprintf("/^[0-9T:-]{19} [\w-]* ?%s/u", preg_quote($code, "/")) , $line)) $lines[] = $line;
92  }
93  return $lines;
94  }
95  /**
96  * get last message from file status
97  * @return UpdateAttributeStatusLine
98  */
99  public function getLastMessage()
100  {
101  $l = new UpdateAttributeStatusLine();
102  list($l->date, $l->processCode, $l->message) = explode(' ', trim(end($this->content)) , 3);
103 
104  return $l;
105  }
106  /**
107  * return error messages
108  * empty string if not
109  * @return string error messages
110  */
111  public function getError()
112  {
113  $r = $this->getCodeLines("ERROR");
114  if ($r) {
115 
116  return implode("\n", $r);
117  }
118  return '';
119  }
120  /**
121  * get all status for each documents
122  * return null is no processing doduments
123  * @return null|UpdateAttributeResults[]
124  */
125  public function getResults()
126  {
127  $r = $this->getCodeLines("logStatusReport PHP");
128  if ($r) {
129  $pos = mb_strpos($r[0], ':', 20);
130  $s = mb_substr($r[0], $pos + 1);
131  return unserialize($s);
132  }
133  return null;
134  }
135  /**
136  * return true is status file contains message that indicates end of processing
137  * @return bool
138  */
139  public function isFinished()
140  {
141  return $this->getStatus() == self::StatusFinished;
142  }
143 }
145 {
146  public $date;
147  public $processCode;
148  public $message;
149  public function __toString()
150  {
151  return $this->date . " " . $this->processCode . " " . $this->message;
152  }
153 }
if($famId) $s
← centre documentaire © anakeen