Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
VaultIndexInit.php
Go to the documentation of this file.
1 <?php
2 /*
3  * Reinit vault files
4  *
5  * @author Anakeen
6  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
7  * @package FDL
8 */
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  * Setup main db connection
20  */
21 $dbaccess = GetParam("FREEDOM_DB");
22 if ($dbaccess == "") {
23  $action->exitError("Database not found : param FREEDOM_DB");
24 }
26 if (!is_object($o)) {
27  $action->exitError(sprintf("Could not get DbObj with dbaccess='%s'", $dbaccess));
28 }
29 /**
30  * Parse arguments
31  */
32 $parms = array();
33 $usage = new ApiUsage();
34 $usage->setText("Re-initialize docvaultindex table");
35 /* --dryrun=no|yes (default 'no') */
36 $parms['dryrun'] = $usage->addOption("dryrun", "Only output SQL queries that would be executed", array(
37  "yes",
38  "no"
39 ) , "no");
40 if ($parms['dryrun'] == 'yes') {
41  $parms['dryrun'] = true;
42 } else {
43  $parms['dryrun'] = false;
44 }
45 /* --famid=<name|id> (default 'all') */
46 $parms['famid'] = $usage->addOption("famid", "Process all families (keyword 'all') or only a specific family (family name or family id)", null, "all");
47 if ($parms['famid'] != 'all') {
48  if (!is_numeric($parms['famid'])) {
49  $famid = getIdFromName($dbaccess, $parms['famid']);
50  if (!is_numeric($famid)) {
51  $action->exitError(sprintf("Could not find family '%s'", $parms['famid']));
52  }
53  $parms['famid'] = $famid;
54  }
55  $fam = new_Doc($dbaccess, $parms['famid'], true);
56  if (!is_object($fam) || !$fam->isAlive()) {
57  $action->exitError(sprintf("Family with id '%s' is not alive or not a valid family.", $parms['famid']));
58  }
59 }
60 /* --transaction=no|yes (default 'no') */
61 $parms['transaction'] = $usage->addOption("transaction", "Execute whole operation in a single transaction", array(
62  "yes",
63  "no"
64 ) , "no");
65 if ($parms['transaction'] == 'yes') {
66  $parms['transaction'] = true;
67 } else {
68  $parms['transaction'] = false;
69 }
70 /* --realclean=yes|no (default 'yes') */
71 $parms['realclean'] = $usage->addOption("realclean", "Delete everything in docvaultindex at the beginning of the operation", array(
72  "yes",
73  "no"
74 ) , "yes");
75 if ($parms['realclean'] == 'yes' && $parms['famid'] == 'all') {
76  $parms['realclean'] = true;
77 } else {
78  $parms['realclean'] = false;
79 }
80 $usage->verify();
81 /**
82  * Load family list
83  */
84 $q = new QueryDb($dbaccess, "DocFam");
85 $q->order_by = 'id';
86 $q->AddQuery("icon ~ E'^[^\\\\|]*\\\\|\\\\d+(\\\\|[^\\\\|]*)?$'");
87 if (is_numeric($parms['famid'])) {
88  $q->AddQuery(sprintf("id = %s", pg_escape_string($parms['famid'])));
89 }
90 $famIconList = $q->Query(0, 0, "TABLE");
91 /**
92  * Load all file attributes
93  */
94 $q = new QueryDb($dbaccess, "DocAttr");
95 $q->order_by = 'docid, id';
96 $q->AddQuery("type = 'file' OR type = 'image'");
97 $q->AddQuery("usefor != 'Q'");
98 $q->AddQuery("id !~ '^:'"); /* Do not process modattr attributes */
99 if (is_numeric($parms['famid'])) {
100  $q->AddQuery(sprintf("docid = %s", pg_escape_string($parms['famid'])));
101 }
102 $attrList = $q->Query(0, 0, "TABLE");
103 /**
104  * Load all file parameters
105  */
106 $q = new QueryDb($dbaccess, "DocAttr");
107 $q->order_by = 'docid, id';
108 $q->AddQuery("(type = 'file' OR type = 'image')");
109 $q->AddQuery("usefor = 'Q'");
110 $q->AddQuery("id !~ '^:'");
111 if (is_numeric($parms['famid'])) {
112  $q->AddQuery(sprintf("docid = %s", pg_Escape_string($parms['famid'])));
113 }
114 $paramList = $q->Query(0, 0, "TABLE");
115 /**
116  * Begin transaction if required
117  */
118 if ($parms['transaction']) {
119  sqlexec($o, $parms, "BEGIN;");
120 }
121 /**
122  * Delete all docvaultindex if all families are reindexed
123  * and --realclean=yes
124  */
125 if ($parms['famid'] == 'all' && $parms['realclean']) {
126  sqlexec($o, $parms, "DELETE FROM docvaultindex");
127 
128  if (!$parms['transaction']) {
129  sqlexec($o, $parms, "VACUUM ANALYSE docvaultindex");
130  }
131  sqlexec($o, $parms, "REINDEX TABLE docvaultindex");
132 }
133 /**
134  * Re-index docvaultindex file attributes
135  */
136 $deletedFam = array();
137 foreach ($attrList as $i => $attr) {
138  $docid = $attr['docid'];
139  $attrid = $attr['id'];
140 
141  if (!$parms['realclean'] && !isset($deletedFam[$docid])) {
142  print sprintf("-- Deleting attributes vault indexes for family '%s'...\n", $docid);
143  $sql = sprintf("DELETE FROM docvaultindex WHERE EXISTS (SELECT id FROM doc%s WHERE id = docid)", pg_escape_string($docid));
144  sqlexec($o, $parms, $sql);
145  $deletedFam[$docid] = 1;
146  }
147 
148  print sprintf("-- Indexing family '%s', attribute '%s'...\n", $docid, $attrid);
149  $sql = sprintf("SELECT vaultreindex(id, %s) FROM doc%s WHERE %s IS NOT NULL", pg_escape_string($attrid) , pg_escape_string($docid) , pg_escape_string($attrid));
150  sqlexec($o, $parms, $sql);
151 }
152 /**
153  * Re-index docvaultindex file parameters
154  */
155 $deletedFam = array();
156 foreach ($paramList as $i => $param) {
157  $docid = $param['docid'];
158  $paramid = $param['id'];
159 
160  if (!isset($deletedFam[$docid]) && !$parms['realclean']) {
161  print sprintf("-- Deleting icons and parameters vault indexes for family '%s'...\n", $docid);
162  $sql = sprintf("DELETE FROM docvaultindex WHERE docid = %s", $docid);
163  sqlexec($o, $parms, $sql);
164  $deletedFam[$docid] = 1;
165  }
166 
167  print sprintf("-- Indexing family '%s', parameter '%s'...\n", $docid, $paramid);
168  $sql = sprintf("SELECT vaultreindexparam(id, param, '%s') FROM docfam WHERE id = %s", pg_escape_string($paramid) , pg_escape_string($docid) , pg_escape_string($paramid));
169  sqlexec($o, $parms, $sql);
170 }
171 /**
172  * Re-index family icons
173  */
174 foreach ($famIconList as $i => $fam) {
175  $famid = $fam['id'];
176 
177  print sprintf("-- Indexing icon for family '%s'...\n", $famid);
178  $sql = sprintf("SELECT vaultreindex(id, icon) FROM docfam WHERE id = %s", $famid);
179  sqlexec($o, $parms, $sql);
180 }
181 /**
182  * Commit transaction if required
183  */
184 if ($parms['transaction']) {
185  sqlexec($o, $parms, "COMMIT;");
186 }
187 
188 function sqlexec(&$dbobj, &$parms, $sql)
189 {
190  if ($parms['dryrun']) {
191  if (!preg_match('/;\s*$/', $sql)) {
192  $sql = $sql . ';';
193  }
194  str_replace($sql, '\\', '\\\\');
195  print "$sql\n";
196  return '';
197  }
198  $err = $dbobj->exec_query($sql);
199  if ($err != '') {
200  error_log(sprintf("Error executing query [%s]: %s", $sql, $err));
201  }
202  return $err;
203 }
204 ?>
← centre documentaire © anakeen - published under CC License - Dynacase