Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
migrDates2iso.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * Migrate date which are in arrays
8  */
9 
10 include_once ('FDL/Class.Doc.php');
11 include_once ("FDL/Lib.Dir.php");
12 global $action;
13 
14 define("REDCOLOR", "\033" . '[01;31m');
15 define("GREENCOLOR", "\033" . '[01;32m');
16 define("STOPCOLOR", "\033" . '[0m');
17 
18 $usage = new ApiUsage();
19 
20 $usage->setDefinitionText("Update date from french to iso");
21 $force = ($usage->addOptionalParameter("force", "force conversion although the configuration is good", array(
22  "yes",
23  "no"
24 ) , "no") == "yes");
25 $onlyAnalyze = ($usage->addOptionalParameter("analyze", "only analyze ", array(
26  "yes",
27  "no"
28 ) , "no") == "yes");
29 
30 $usage->verify();
31 
32 $locale = substr(getParam("CORE_LCDATE") , 0, 3);
33 simpleQuery($action->dbaccess, "show datestyle", $dbDate, true, true);
34 simpleQuery($action->dbaccess, "SELECT current_database();", $databaseName, true, true);
35 
36 if ((!$force) && $dbDate == "ISO, DMY" && $locale == "iso") {
37  print "Database '$databaseName' datestyle is 'ISO, DMY'.\n\tDatabase is clean.\n";
38  print "Inspect family parameters and default values only.\n";
40  return;
41 }
42 
43 if (($force) || ($dbDate == "SQL, DMY" && $locale == "dmy")) {
44  if (!$force) {
45  print "Database datestyle is 'SQL, DMY'.\nDatabase '$databaseName' needs to be migrated\n";
46  } else {
47  print "Force mode enable.\n";
48  }
49  print "Conversion dates to iso starts ...\n";
50 
52 
54 } else {
55 
56  $err = sprintf("Wrong Config Date detected : CORE_LCDATE= '%s', database datestyle='%s'\n", $locale, $dbDate);
57  $action->exitError($err);
58 }
59 /**
60  * Send date conversion dd/mm/yyyy to yyyy-mm-dd
61  * @param Action $action
62  * @param bool $onlyAnalyze
63  */
65 {
66  $sql = "SELECT a1.id, a1.docid, a1.type from docattr a1, docattr a2 where a1.usefor != 'Q' and (a1.type ~ 'date' or a1.type ~ 'timestamp') and a1.frameid=a2.id and a2.type ~ 'array' and a1.id !~ ':';";
67 
68  simpleQuery($action->dbaccess, $sql, $res);
69 
70  $b = microtime(true);
71  foreach ($res as $attr) {
72  $famid = $attr["docid"];
73  $famName = getNameFromId($action->dbaccess, $famid);
74  $attrName = pg_escape_string($attr["id"]);
75 
76  $sql = sprintf("select count(%s) from doc%d where %s ~ '/';", $attrName, $famid, $attrName);
77  simpleQuery($action->dbaccess, $sql, $count, true, true);
78  if ($count > 0) {
79  printf("Family %s - Attribute %s : %d documents to %supdate%s\n", $famName, $attrName, $count, REDCOLOR, STOPCOLOR);
80  } else {
81 
82  printf("Family %s - Attribute %s : %sOK%s\n", $famName, $attrName, GREENCOLOR, STOPCOLOR);
83  }
84  if ((!$onlyAnalyze) && $count > 0) {
85  $b1 = microtime(true);
86  $sql = sprintf("UPDATE doc%d set %s=regexp_replace(%s, E'(..)/(..)/(....)', E'\\\\3-\\\\2-\\\\1','g') where %s ~ '/';", $famid, $attrName, $attrName, $attrName);
87  //print "$sql\n";
88  simpleQuery($action->dbaccess, $sql);
89  $partDelay = microtime(true) - $b1;
90  printf("\t%s updated in %ds.\n", $attrName, $partDelay);
91  }
92  }
93 
94  if (!$onlyAnalyze) {
95  simpleQuery($action->dbaccess, "SELECT current_database();", $databaseName, true, true);
96  $sql = sprintf("ALTER DATABASE %s set datestyle = 'ISO, DMY'", pg_escape_identifier($databaseName));
97  simpleQuery($action->dbaccess, $sql);
98  print "Change database datestyle to 'ISO, DMY'\n";
99  $sql = "update paramv set val='iso' where name = 'CORE_LCDATE'";
100  simpleQuery($action->dbaccess, $sql);
101  print "Change CORE_LCDATE to 'iso'\n";
102  }
103  $e = microtime(true) - $b;
104  printf("End of conversion in %dmin %ds.\n", $e / 60, $e % 60);
105 }
106 /**
107  * Send date conversion for parameters and default values dd/mm/yyyy to yyyy-mm-dd
108  * @param Action $action
109  * @param bool $onlyAnalyze
110  */
112 {
113 
114  $b = microtime(true);
115  $s = new SearchDoc($action->dbaccess, -1);
116  $s->setObjectReturn(true);
117  $s->addFilter("param ~ E'([0-9]{2})/([0-9]{2})/([0-9]{4})' or defval ~ E'([0-9]{2})/([0-9]{2})/([0-9]{4})'");
118  $s->search();
119 
120  if ($s->count() == 0) {
121  print "\tFamily parameters and default date are clean.\n";
122  return;
123  }
124  /*
125  * @var DocFam $docfam
126  */
127  while ($docfam = $s->getNextDoc()) {
128  $defVal = $docfam->getOwnDefValues();
129  $params = $docfam->getOwnParams();
130  $la = $docfam->getAttributes();
131  foreach ($la as $attrid => $oAttr) {
132 
133  if ($oAttr->type == "date" || $oAttr->type == "timestamp") {
134 
135  if (!empty($defVal[$attrid])) {
136  $date = $defVal[$attrid];
137 
138  $isoDate = preg_replace('/(..)\/(..)\/(....)/', '$3-$2-$1', $date);
139  if ($isoDate != $date) {
140  $docfam->setDefValue($attrid, $isoDate);
141 
142  print "\tChange default value for $attrid : $date to $isoDate\n";
143  if (!$onlyAnalyze) {
144  $err = $docfam->modify();
145  if ($err) {
146  printf("\t%s$err%s", REDCOLOR, STOPCOLOR);
147  } else {
148  printf("\t%s updated.\n", $attrid);
149  }
150  }
151  }
152  }
153  if ($oAttr->usefor == "Q") {
154  if (!empty($params[$attrid])) {
155  $date = $params[$attrid];
156 
157  $isoDate = preg_replace('/(..)\/(..)\/(....)/', '$3-$2-$1', $date);
158  if ($isoDate != $date) {
159  $docfam->setParam($attrid, $isoDate);
160  print "\tChange parameter value for $attrid : $date to $isoDate\n";
161  if (!$onlyAnalyze) {
162  $err = $docfam->modify();
163  if ($err) {
164  printf("\t%s$err%s", REDCOLOR, STOPCOLOR);
165  } else {
166  printf("\t%s updated.\n", $attrid);
167  }
168  }
169  }
170  }
171  }
172  }
173  }
174  }
175 
176  $e = microtime(true) - $b;
177  printf("Family parameters and default date conversion in %dmin %ds.\n", $e / 60, $e % 60);
178 }
179 ?>
$locale
convertFamilyDateToIso(Action $action, $onlyAnalyze=false)
if($famId) $s
$force
$onlyAnalyze
$usage
getParam($name, $def="")
must be in core or global type
Definition: Lib.Common.php:193
const STOPCOLOR
print
Definition: checklist.php:49
convertDateToIso(Action $action, $onlyAnalyze=false)
getNameFromId($dbaccess, $id)
const GREENCOLOR
simpleQuery($dbaccess, $query, &$result=array(), $singlecolumn=false, $singleresult=false, $useStrict=null)
Definition: Lib.Common.php:484
if($file) if($subject==""&&$file) if($subject=="") $err
global $action
const REDCOLOR
Verify arguments for wsh programs.
← centre documentaire © anakeen