Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
DocRelInit.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  * Reinit doc relations
9  */
10 ini_set("max_execution_time", "36000");
11 
12 global $appl, $action;
13 
14 include_once ('FDL/Class.Doc.php');
15 include_once ('FDL/Class.DocFam.php');
16 include_once ('FDL/Class.DocVaultIndex.php');
17 include_once ('VAULT/Class.VaultFile.php');
18 
19 /**
20  * Setup main db connection
21  */
22 $dbaccess = GetParam("FREEDOM_DB");
23 if ($dbaccess == "") {
24  $action->exitError("Database not found : param FREEDOM_DB");
25 }
27 if (!is_object($o)) {
28  $action->exitError(sprintf("Could not get DbObj with dbaccess='%s'", $dbaccess));
29 }
30 /**
31  * Parse arguments
32  */
33 $parms = array();
34 $usage = new ApiUsage();
35 $usage->setText("Re-initialize docrel table");
36 /* --dryrun=no|yes (default 'no') */
37 $parms['dryrun'] = $usage->addOption("dryrun", "Only output SQL queries that would be executed", array(
38  "yes",
39  "no"
40 ) , "no");
41 if ($parms['dryrun'] == 'yes') {
42  $parms['dryrun'] = true;
43 } else {
44  $parms['dryrun'] = false;
45 }
46 /* --famid=<name|id> (default 'all') */
47 $parms['famid'] = $usage->addOption("famid", "Process all families (keyword 'all') or only a specific family (family name or family id)", array() , "all");
48 if ($parms['famid'] != 'all') {
49  if (!is_numeric($parms['famid'])) {
50  $famid = getIdFromName($dbaccess, $parms['famid']);
51  if (!is_numeric($famid)) {
52  $action->exitError(sprintf("Could not find family '%s'", $parms['famid']));
53  }
54  $parms['famid'] = $famid;
55  }
56  $fam = new_Doc($dbaccess, $parms['famid'], true);
57  if (!is_object($fam) || !$fam->isAlive()) {
58  $action->exitError(sprintf("Family with id '%s' is not alive or not a valid family.", $parms['famid']));
59  }
60 }
61 /* --transaction=no|yes (default 'no') */
62 $parms['transaction'] = $usage->addOption("transaction", "Execute whole operation in a single transaction", array(
63  "yes",
64  "no"
65 ) , "no");
66 if ($parms['transaction'] == 'yes') {
67  $parms['transaction'] = true;
68 } else {
69  $parms['transaction'] = false;
70 }
71 /* --realclean=yes|no (default 'yes') */
72 $parms['realclean'] = $usage->addOption("realclean", "Delete everything in docrel at the beginning of the operation", array(
73  "yes",
74  "no"
75 ) , "yes");
76 if ($parms['realclean'] == 'yes' && $parms['famid'] == 'all' ) {
77  $parms['realclean'] = true;
78 } else {
79  $parms['realclean'] = false;
80 }
81 $usage->verify();
82 /**
83  * Load all docid attributes
84  */
85 $q = new QueryDb($dbaccess, "DocAttr");
86 $q->order_by = '(docid, id)';
87 $q->AddQuery("type ~ '^docid'");
88 $q->AddQuery("usefor != 'Q'");
89 $q->AddQuery("id !~ '^:'"); /* Do not process modattr attributes */
90 if (is_numeric($parms['famid'])) {
91  $q->AddQuery(sprintf("docid = %s", pg_escape_string($parms['famid'])));
92 }
93 $attrList = $q->Query(0, 0, "TABLE");
94 /**
95  * Begin transaction if required
96  */
97 if ($parms['transaction']) {
98  sqlexec($o, $parms, "BEGIN;");
99 }
100 /**
101  * Delete all docrels if all families are reindexed
102  * and --realclean=yes as been specified
103  */
104 if ($parms['famid'] == 'all' && $parms['realclean']) {
105  sqlexec($o, $parms, "DELETE FROM docrel");
106 
107  if (!$parms['transaction']) {
108  sqlexec($o, $parms, "VACUUM ANALYSE docrel");
109  }
110  sqlexec($o, $parms, "REINDEX TABLE docrel");
111 }
112 /**
113  * Re-index docid attributes
114  */
115 foreach ($attrList as $i => $attr) {
116  $docid = $attr["docid"];
117  $attrid = $attr["id"];
118 
119  if (!$parms['realclean']) {
120  /**
121  * Delete only docrels that are going to be recomputed
122  */
123  print sprintf("-- Deleting relations for family '%s', attribute '%s'...\n", $docid, $attrid);
124  $sql = sprintf("DELETE FROM docrel WHERE EXISTS (SELECT id FROM doc%s WHERE id = sinitid) AND type = '%s'", pg_escape_string($docid) , pg_escape_string($attrid));
125  sqlexec($o, $parms, $sql);
126  }
127 
128  print sprintf("-- Indexing family '%s', attribute '%s'...\n", $docid, $attrid);
129  $sql = sprintf("SELECT docrelreindex(initid, %s, '%s') FROM doc%s WHERE %s IS NOT NULL AND locked != -1", pg_escape_string($attrid) , pg_escape_string($attrid) , pg_escape_string($docid) , pg_escape_string($attrid));
130  sqlexec($o, $parms, $sql);
131 }
132 /**
133  * Recompute titles and icons
134  */
135 print sprintf("-- Deleting broken relations...\n");
136 sqlexec($o, $parms, "DELETE FROM docrel WHERE NOT EXISTS (SELECT id FROM docread WHERE id = cinitid)");
137 if (!$parms['transaction']) {
138  sqlexec($o, $parms, "VACUUM ANALYSE docrel");
139 }
140 
141 print sprintf("-- Dropping docrel indexes...\n");
142 sqlexec($o, $parms, "DROP INDEX docrel_u");
143 sqlexec($o, $parms, "DROP INDEX i_docrels");
144 sqlexec($o, $parms, "DROP INDEX i_docrelc");
145 
146 print sprintf("-- Recomputing cinitid...\n");
147 sqlexec($o, $parms, "UPDATE docrel SET cinitid = docread.initid FROM docread WHERE cinitid IS NOT NULL AND cinitid > 0 AND cinitid = docread.id AND docread.id != docread.initid");
148 
149 print sprintf("-- Recomputing {stitle, ctitle, sicon, cicon}...\n");
150 sqlexec($o, $parms, "
151 UPDATE docrel SET stitle = s.title, ctitle = c.title, sicon = s.icon, cicon = c.icon, cinitid = c.initid
152 FROM docread AS s, docread AS c
153 WHERE
154  (
155  sinitid = s.initid AND s.locked != -1
156  AND
157  cinitid = c.initid AND c.locked != -1
158  )
159  AND
160  (
161  docrel.stitle IS NULL
162  OR
163  docrel.ctitle IS NULL
164  OR
165  docrel.sicon IS NULL
166  OR
167  docrel.cicon IS NULL
168  )
169 ");
170 
171 print sprintf("-- Re-creating docrel indexes...\n");
172 sqlexec($o, $parms, "CREATE INDEX docrel_u ON docrel (sinitid, cinitid, type)");
173 sqlexec($o, $parms, "CREATE INDEX i_docrels ON docrel (sinitid)");
174 sqlexec($o, $parms, "CREATE INDEX i_docrelc ON docrel (cinitid)");
175 
176 /**
177  * Commit transaction if required
178  */
179 if ($parms['transaction']) {
180  sqlexec($o, $parms, "COMMIT;");
181 }
182 
183 function sqlexec(&$dbobj, &$parms, $sql)
184 {
185  if ($parms['dryrun']) {
186  if (!preg_match('/;\s*$/', $sql)) {
187  $sql = $sql . ';';
188  }
189  str_replace($sql, '\\', '\\\\');
190  print "$sql\n";
191  return '';
192  }
193  $err = $dbobj->exec_query($sql);
194  if ($err != '') {
195  error_log(sprintf("Error executing query [%s]: %s", $sql, $err));
196  }
197  return $err;
198 }
199 ?>
← centre documentaire © anakeen - published under CC License - Dynacase