Platform  3.1
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  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
5  * @package FDL
6 */
7 /**
8  * importation of documents
9  *
10  * @author Anakeen 2002
11  * @version $Id: freedom_import.php,v 1.9 2008/11/13 16:49:16 eric Exp $
12  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
13  * @package FDL
14  * @subpackage WSH
15  */
16 /**
17  */
18 
19 global $action;
20 // refreah for a classname
21 // use this only if you have changed title attributes
22 include_once ("FDL/Class.Doc.php");
23 include_once ("FDL/Class.SearchDoc.php");
24 function color_failure($msg)
25 {
26  if ($msg) $msg = chr(0x1b) . "[1;31m" . $msg . chr(0x1b) . "[0;39m";
27  return $msg;
28 }
29 
30 function color_success($msg)
31 {
32  if ($msg) $msg = chr(0x1b) . "[1;32m" . $msg . chr(0x1b) . "[0;39m";
33  return $msg;
34 }
35 
36 function color_warning($msg)
37 {
38  if ($msg) $msg = chr(0x1b) . "[1;33m" . $msg . chr(0x1b) . "[0;39m";
39  return $msg;
40 }
41 
42 $usage = new ApiUsage();
43 $usage->setText("Refresh documents ");
44 $famId = $usage->addNeeded("famid", "the family identificator used to filter");
45 $method = $usage->addOption("method", "method to use)", array() , "refresh");
46 $arg = $usage->addOption("arg", "optional method argument to set when calling method");
47 $revision = $usage->addOption("revision", "use all revision", array(
48  "yes",
49  "no"
50 ) , "no");
51 $docid = $usage->addOption("docid", "use only for this document id");
52 $start = $usage->addOption("start", "start from offset", array() , 0);
53 $slice = $usage->addOption("slice", "limit from offset", array() , "all");
54 $fldid = $usage->addOption("fldid", "use collection id to limit search");
55 $filter = $usage->addOption("filter", "sql filter to limit search");
56 $save = $usage->addOption("save", "store mode", array(
57  "complete",
58  "light",
59  "none"
60 ) , "light");
61 $usage->verify();
62 
63 $allrev = ($revision == "yes"); // method to use
64 $dbaccess = $action->getParam("FREEDOM_DB");
65 if ($dbaccess == "") {
66  print "Database not found : param FREEDOM_DB";
67  exit();
68 }
69 
70 $famtitle = "";
71 if ($famId) {
72  $f = new_doc($dbaccess, $famId);
73  if (!$f->isAlive()) {
74  $action->exitError(sprintf("family %s not exists", $famId));
75  }
76  if ($f->doctype != 'C') {
77  $action->exitError(sprintf("document %s not a family", $famId));
78  }
79  $famId = $f->id;
80  $famtitle = $f->getTitle();
81 }
82 
84 $s->setObjectReturn();
85 $s->orderby = 'id desc';
86 $s->slice = $slice;
87 $s->start = $start;
88 if ($docid != '') {
89  if (!is_numeric($docid)) {
90  $docName = $docid;
91  $docid = getIdFromName($dbaccess, $docName);
92  if ($docid === false) {
93  $action->exitError(sprintf("document with name '%s' not found", $docName));
94  }
95  }
96  $s->addFilter('id = %d', $docid);
97 }
98 if ($fldid != '') {
99  $s->useCollection($fldid);
100 }
101 if ($allrev) $s->latest = false;
102 if ($filter) {
103  // verify validity and prevent hack
104  if (@pg_prepare(getDbid($dbaccess) , sprintf("select id from doc%d where %s", $s->fromid, $filter)) == false) {
105  $action->exitError(sprintf("filter not valid :%s", pg_last_error()));
106  } else {
107  $s->addFilter($filter);
108  }
109 }
110 $s->search();
111 
112 if ($s->searchError()) {
113  $action->exitError(sprintf("search error : %s", $s->getError()));
114 }
115 $targ = array();
116 if ($arg != "") $targ[] = $arg;
117 $card = $s->count();
118 printf("\n%d %s to update with %s\n", $card, $famtitle, $method);
119 
120 $ret = "";
121 while ($doc = $s->nextDoc()) {
122  $usemethod = ($method && (method_exists($doc, $method)));
123  if ($method && (!method_exists($doc, $method))) {
124  printf("\nmethod not exists %s \n", $method);
125  break;
126  }
127  $doc->_oldvalue = array();
128  $modified = false;
129  $smod = '';
130  if ($usemethod) {
131  $ret = call_user_func_array(array(
132  $doc,
133  $method
134  ) , $targ);
135  if ($doc->isChanged()) {
136  $olds = $doc->getOldValues();
137  foreach ($olds as $k => $v) {
138  $smod.= sprintf("\t- %s [%s]:[%s]\n", $k, $v, $doc->getValue($k));
139  }
140  switch ($save) {
141  case "light":
142  $err = $doc->modify(true);
143  $modified = true;
144  break;
145 
146  case "complete":
147  $err = $doc->store();
148  $modified = true;
149  break;
150  }
151  $ret.= $err;
152  }
153  }
154  $memory = '';
155  //$memory= round(memory_get_usage() / 1024)."Ko";
156  printf("%s)%s[%d] %s %s %s\n", $card, $doc->title, $doc->id, ($modified) ? '-M-' : '', $memory, color_failure($ret));
157  if ($smod) print $smod;
158 
159  $card--;
160 }
161 ?>
← centre documentaire © anakeen - published under CC License - Dynacase