Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
refreshDocuments.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * importation of documents
8  *
9  * @author Anakeen
10  * @version $Id: freedom_import.php,v 1.9 2008/11/13 16:49:16 eric Exp $
11  * @package FDL
12  * @subpackage WSH
13  */
14 /**
15  */
16 
17 global $action;
18 // refreah for a classname
19 // use this only if you have changed title attributes
20 include_once ("FDL/Class.Doc.php");
21 include_once ("FDL/Class.SearchDoc.php");
22 function color_failure($msg)
23 {
24  if ($msg) $msg = chr(0x1b) . "[1;31m" . $msg . chr(0x1b) . "[0;39m";
25  return $msg;
26 }
27 
28 function color_success($msg)
29 {
30  if ($msg) $msg = chr(0x1b) . "[1;32m" . $msg . chr(0x1b) . "[0;39m";
31  return $msg;
32 }
33 
34 function color_warning($msg)
35 {
36  if ($msg) $msg = chr(0x1b) . "[1;33m" . $msg . chr(0x1b) . "[0;39m";
37  return $msg;
38 }
39 
40 $usage = new ApiUsage();
41 $usage->setDefinitionText("Refresh documents ");
42 $famId = $usage->addRequiredParameter("famid", "the family identifier used to filter");
43 $method = $usage->addOptionalParameter("method", "method to use", function ($value)
44 {
45  if ($value === ApiUsage::GET_USAGE) {
46  return '';
47  }
48  if (!is_string($value)) {
49  return sprintf("Invalid %s value for option --method=<methodName>", gettype($value));
50  }
51  if (strlen($value) <= 0) {
52  return sprintf("Invalid empty value for option --method=<methodName>");
53  }
54  return '';
55 }
56 , "refresh");
57 $arg = $usage->addOptionalParameter("arg", "optional method argument to set when calling method");
58 $revision = $usage->addOptionalParameter("revision", "use all revision", array(
59  "yes",
60  "no"
61 ) , "no");
62 $docid = $usage->addOptionalParameter("docid", "use only for this document id");
63 $start = $usage->addOptionalParameter("start", "start from offset", array() , 0);
64 $slice = $usage->addOptionalParameter("slice", "limit from offset", array() , "all");
65 $fldid = $usage->addOptionalParameter("fldid", "use collection id to limit search");
66 $filter = $usage->addOptionalParameter("filter", "sql filter to limit search");
67 $save = $usage->addOptionalParameter("save", "store mode", array(
68  "complete",
69  "light",
70  "none"
71 ) , "light");
72 $statusFile = $usage->addOptionalParameter("status-file", "refresh's status file or '-' for STDOUT", function ($value)
73 {
74  if ($value === ApiUsage::GET_USAGE) {
75  return '';
76  }
77  if (!is_string($value)) {
78  return sprintf("Invalid %s value for option --status-file=<statusFile>|'-'", gettype($value));
79  }
80  if (strlen($value) <= 0) {
81  return sprintf("Invalid empty value for option --status-file=<statusFile>|'-'");
82  }
83  return '';
84 });
85 $stopOnError = ($usage->addEmptyParameter("stop-on-error", "Stop processing when a document returns an error or throws an exception") !== false);
86 $usage->verify();
87 
88 $allrev = ($revision == "yes"); // method to use
89 $dbaccess = $action->dbaccess;
90 if ($dbaccess == "") {
91  print "Database not found : action->dbaccess";
92  exit();
93 }
94 
95 if ($statusFile === '') {
96  $statusFile = null;
97 }
98 
99 $famtitle = "";
100 if ($famId) {
101  $f = new_doc($dbaccess, $famId);
102  if (!$f->isAlive()) {
103  $action->exitError(sprintf("family %s not exists", $famId));
104  }
105  if ($f->doctype != 'C') {
106  $action->exitError(sprintf("document %s not a family", $famId));
107  }
108  $famId = $f->id;
109  $famtitle = $f->getTitle();
110 }
111 
113 $s->setObjectReturn();
114 $s->orderby = 'id desc';
115 $s->slice = $slice;
116 $s->start = $start;
117 if ($docid != '') {
118  if (!is_numeric($docid)) {
119  $docName = $docid;
120  $docid = getIdFromName($dbaccess, $docName);
121  if ($docid === false) {
122  $action->exitError(sprintf("document with name '%s' not found", $docName));
123  }
124  }
125  $s->addFilter('id = %d', $docid);
126 }
127 if ($fldid != '') {
128  $s->useCollection($fldid);
129 }
130 if ($allrev) $s->latest = false;
131 if ($filter) {
132  // verify validity and prevent hack
133  if (@pg_prepare(getDbid($dbaccess) , 'refreshDocument', sprintf("select id from doc%d where %s", $s->fromid, $filter)) == false) {
134  $action->exitError(sprintf("filter not valid :%s", pg_last_error()));
135  } else {
136  $s->addFilter($filter);
137  }
138 }
139 $s->search();
140 
141 if ($s->searchError()) {
142  $action->exitError(sprintf("search error : %s", $s->getError()));
143 }
144 $targ = array();
145 if ($arg != "") $targ[] = $arg;
146 $card = $s->count();
147 printf("\n%d %s to update with %s\n", $card, $famtitle, $method);
148 
149 $ret = "";
155 while ($doc = $s->getNextDoc()) {
156  if (!method_exists($doc, $method)) {
157  printf("\nmethod not exists '%s' \n", $method);
158  $exitcode = 1;
159  break;
160  }
161 
162  $countProcessed++;
163 
164  $ret = '';
165  $err = '';
166  $modified = false;
167  $smod = '';
168  try {
169  $ret = call_user_func_array(array(
170  $doc,
171  $method
172  ) , $targ);
173  if ($doc->isChanged()) {
174  $olds = $doc->getOldRawValues();
175  foreach ($olds as $k => $v) {
176  $smod.= sprintf("\t- %s [%s]:[%s]\n", $k, $v, $doc->getRawValue($k));
177  }
178  switch ($save) {
179  case "light":
180  $err = $doc->modify(true);
181  $modified = true;
182  break;
183 
184  case "complete":
185  $err = $doc->store();
186  $modified = true;
187  break;
188  }
189  }
190  }
191  catch(\Exception $e) {
192  $err.= $e->getMessage();
193  }
194  $memory = '';
195  //$memory= round(memory_get_usage() / 1024)."Ko";
196  if ($err != '') {
197  printf("%s)%s[%d] %s %s %s\n", $card, $doc->title, $doc->id, ($modified) ? '-M-' : '', $memory, color_failure($ret . $err));
198  $countFailure++;
199  $exitcode = 1;
200  } else {
201  printf("%s)%s[%d] %s %s %s\n", $card, $doc->title, $doc->id, ($modified) ? '-M-' : '', $memory, color_success($ret));
202  $countSuccess++;
203  }
204  if ($smod) print $smod;
205  if ($err != '' && $stopOnError) {
206  break;
207  }
208  $card--;
209 }
210 
212 {
213  $err = '';
214  $fh = STDOUT;
215  if ($statusFile !== '-') {
216  if (file_exists($statusFile)) {
217  unlink($statusFile);
218  }
219  if (($fh = fopen($statusFile, 'x')) === false) {
220  $err = sprintf("Error creating refresh's status file '%s'.", $statusFile);
221  return $err;
222  }
223  }
224  if (fwrite($fh, $status) === false) {
225  $err = sprintf("Error writing refresh's status to file '%s'.", $statusFile);
226  fclose($fh);
227  return $err;
228  }
229  if ($fh !== STDOUT) {
230  fclose($fh);
231  }
232  return $err;
233 };
234 
235 if ($statusFile !== null) {
236  $status = '';
237  $status.= sprintf("ALL: %d\n", $countAll);
238  $status.= sprintf("PROCESSED: %d\n", $countProcessed);
239  $status.= sprintf("FAILURE: %d\n", $countFailure);
240  $status.= sprintf("SUCCESS: %d\n", $countSuccess);
242  if ($err !== '') {
243  printf("%s\n", $err);
244  }
245 }
246 
247 exit($exitcode);
if($s->searchError()) $targ
$status
Definition: index.php:30
if($dbaccess=="") if($statusFile=== '') $famtitle
if($famId) $s
const GET_USAGE
$countSuccess
$countProcessed
$countFailure
while($doc=$s->getNextDoc()) $writeStatus
$statusFile
color_failure($msg)
global $action
getDbid($dbaccess)
Definition: Lib.Common.php:353
color_warning($msg)
print
Definition: checklist.php:49
switch($command) exit
Definition: checkVault.php:46
getIdFromName($dbaccess, $name)
if($file) if($subject==""&&$file) if($subject=="") $err
$stopOnError
color_success($msg)
$value
Verify arguments for wsh programs.
← centre documentaire © anakeen