Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
migr_IUSER_premigr_3.2.0.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5  */
6 
7 include_once ("FDL/Lib.Attr.php");
8 include_once ("FDL/Class.DocFam.php");
9 
10 global $action;
11 
12 $parent = null;
13 
14 $dbaccess = $action->dbaccess;
15 if ($dbaccess == "") {
16  $action->exitError("Empty dbaccess: action->dbaccess.");
17 }
18 
19 $usage = new ApiUsage();
20 $usage->setDefinitionText("Migrate IUSERS from dynacase 3.1 to 3.2");
21 $dryrun = $usage->addEmptyParameter('dry-run', 'Dry-run mode');
22 $usage->verify();
23 
24 $sqlList = array(
25  'BEGIN;'
26 );
27 /* Get families that inherits from IUSER */
28 $childList = array();
29 $q = sprintf("WITH RECURSIVE q AS ( SELECT id, fromid FROM docfam WHERE fromid = 128 UNION ALL SELECT docfam.id, docfam.fromid FROM docfam, q WHERE q.id = docfam.fromid ) SELECT id FROM q ORDER BY id");
30 simpleQuery($dbaccess, $q, $childList, false, false, true);
31 $childIdList = array();
32 foreach ($childList as $child) {
33  $childIdList[] = $child['id'];
34 }
35 /*
36  * 1. Suppress attributes that do not belongs to IUSER
37 */
38 $resList = array();
39 $q = sprintf("SELECT id, docid FROM docattr WHERE docid IN (120, 128) AND (type IS NULL OR type NOT IN ('frame', 'tab', 'array', 'menu')) AND id !~ '^:' AND id NOT IN ('us_defaultgroup', 'us_lname', 'us_fname', 'us_mail', 'us_extmail', 'us_meid', 'us_login', 'us_whatid', 'us_roles', 'us_rolesorigin', 'us_rolegorigin', 'us_group', 'us_idgroup', 'us_expires', 'us_daydelay', 'us_expiresd', 'us_expirest', 'us_passdelay', 'us_ldapdn', 'us_substitute', 'us_incumbents', 'us_passwd1', 'us_passwd2', 'us_status', 'us_loginfailure', 'us_accexpiredate')");
40 simpleQuery($dbaccess, $q, $resList, false, false, true);
41 $warnNonEmpty = false;
42 $warnColumns = array();
43 foreach ($resList as $attr) {
44  $sqlList[] = sprintf("DELETE FROM docattr WHERE docid = 128 AND id = '%s';", pg_escape_string($attr['id']));
45  /* Check if the column is empty */
46  $count = array();
47  $q = sprintf("SELECT count(%s) AS count FROM doc128 WHERE %s IS NOT NULL", pg_escape_string($attr['id']) , pg_escape_string($attr['id']));
48  simpleQuery($dbaccess, $q, $count, false, false, true);
49 
50  if (!isset($count[0]['count'])) {
51  printf("Error: missing 'count' in result from query '%s'.", $q);
52  exit(1);
53  }
54 
55  if ($count[0]['count'] > 0) {
56  /* Keep non-empty columns and warn user */
57  $warnColumns[] = $attr['id'];
58  $warnNonEmpty = true;
59  continue;
60  }
61  /* Column is empty, so we can safely drop it */
62  $sqlList[] = sprintf('ALTER TABLE doc128 DROP COLUMN "%s" CASCADE;', pg_escape_string($attr['id']));
63 }
64 /*
65  * 2. Suppress MODATTR of deprecated attributes of USER and IUSER
66 */
67 if (count($childIdList) > 0) {
68  $resList = array();
69  $q = sprintf("SELECT id, docid FROM docattr WHERE docid IN (120, 128) AND id !~ '^:' AND id NOT IN ('us_defaultgroup', 'us_lname', 'us_fname', 'us_mail', 'us_extmail', 'us_meid', 'us_login', 'us_whatid', 'us_roles', 'us_rolesorigin', 'us_rolegorigin', 'us_group', 'us_idgroup', 'us_expires', 'us_daydelay', 'us_expiresd', 'us_expirest', 'us_passdelay', 'us_ldapdn', 'us_substitute', 'us_incumbents', 'us_passwd1', 'us_passwd2', 'us_status', 'us_loginfailure', 'us_accexpiredate')");
70  simpleQuery($dbaccess, $q, $resList, false, false, true);
71  foreach ($resList as $attr) {
72  $sqlList[] = sprintf("DELETE FROM docattr WHERE docid IN (%s) AND id = ':%s';", join(', ', $childIdList) , pg_escape_string($attr['id']));
73  }
74 }
75 /*
76  * 3. Suppress arrays/tabs that have no sub-attributes
77 */
78 $sqlList[] = "DELETE FROM docattr WHERE docid = 128 AND type IN ('frame', 'array', 'tab') AND id NOT IN (SELECT DISTINCT dright.id FROM docattr AS dleft, docattr AS dright WHERE dleft.docid = 128 AND dleft.docid = dright.docid AND dleft.frameid = dright.id);";
79 /*
80  * 4. Remove deprecated methods files
81 */
82 $sqlList[] = <<<'EOT'
83 UPDATE docfam SET methods = array_to_string(
84  array(
85  SELECT * FROM (
86  SELECT regexp_split_to_table(methods, E'\n') AS method FROM docfam AS docfam2 WHERE docfam2.id = docfam.id
87  ) AS methodlist WHERE method NOT IN (
88  'Method.DocUser.php',
89  'Method.FAddBook.php',
90  'Method.DocSociety.php',
91  'Method.FAddBookSociety.php',
92  'Method.DocSite.php'
93  )
94  ),
95  E'\n'
96 );
98 
99 $sqlList[] = 'COMMIT;';
100 
101 $sql = join("\n", $sqlList);
102 
103 try {
104  if ($dryrun) {
105  printf("%s\n", $sql);
106  } else {
107  simpleQuery($dbaccess, $sql, $resList, false, false, true);
108  }
109 }
110 catch(Exception $e) {
111  printf("Exception: %s\n", $e->getMessage());
112  printf("Error executing SQL transaction:\n%s\n", $sql);
113  exit(1);
114 }
115 
116 if ($warnNonEmpty) {
117  $columns = join(', ', $warnColumns);
118  $err = <<<"EOT"
119 
120 ===========================================================
121 We have detected that you have families that inherits from
122 IUSER, or have modified the IUSER family, and you are using
123 deprecated attributes that are not present anymore on the
124 IUSER family.
125 
126 The SQL columns and the data have been kept, and you MUST
127 update your families and migrate the data to new
128 attributes.
129 
130 Deprecated columns with data:
131 
132 $columns
133 
134 Press [Continue] to proceed with migration and fix it later
135 ===========================================================
136 
137 EOT;
138 
139 
140 }
141 exit(0);
if($dbaccess=="") $usage
global $action
switch($command) exit
Definition: checkVault.php:46
foreach($childList as $child) $resList
simpleQuery($dbaccess, $query, &$result=array(), $singlecolumn=false, $singleresult=false, $useStrict=null)
Definition: Lib.Common.php:484
if($file) if($subject==""&&$file) if($subject=="") $err
Verify arguments for wsh programs.
← centre documentaire © anakeen