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