Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
VaultExamine.php
Go to the documentation of this file.
1 <?php
2 /*
3  * Examine 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.DocVaultIndex.php');
16 include_once ('VAULT/Class.VaultFile.php');
17 
18 $appl = new Application();
19 $appl->Set("FDL", $core);
20 $dbaccess = $appl->GetParam("FREEDOM_DB");
21 if ($dbaccess == "") {
22  print "Database not found : param FREEDOM_DB";
23  exit;
24 }
25 /**
26  * Parse arguments
27  */
28 $usage = new ApiUsage();
29 $usage->setText("Examine vault files");
30 /* --vaultname */
31 $vaultname = $usage->addOption("vault", "Name of the vault to examine", null, "FREEDOM");
32 /* --test */
33 $test = $usage->addOption("test", "Enable/disable test mode: do not delete anything, just print what would be done", array(
34  "yes",
35  "no"
36 ) , "no");
37 $test = ($test == "yes" ? true : false);
38 /* --cmd=check */
39 $command = $usage->addNeeded("cmd", "Examine command", array(
40  "check-all",
41  "check-noref",
42  "check-nofile",
43  "clean-unref"
44 ) , null);
45 /* --csv */
46 $csv = $usage->addOption("csv", "Output in CSV format", array(
47  1,
48  0,
49  "yes",
50  "no"
51 ) , "no");
52 $csv = ($csv == 1 || $csv == "yes") ? true : false;
53 
54 $usage->verify();
55 
56 switch ($command) {
57  case "check-all":
59  printres($t, $csv);
60  break;
61 
62  case "check-noref":
63  $t = view($dbaccess, $vaultname, array(
64  "unref" => true
65  ));
66  printres($t, $csv);
67  break;
68 
69  case "check-nofile":
70  $t = view($dbaccess, $vaultname, array(
71  "unread" => true
72  ));
73  printres($t, $csv);
74  break;
75 
76  case "clean-unref":
77  $t = view($dbaccess, $vaultname, array(
78  "unref" => true
79  ));
81  break;
82 
83  default:
84  print sprintf("Unknown command '%s'.\n", $command);
85  exit;
86 }
87 exit;
88 
89 function printres($t, $csv = false)
90 {
91  $filel = 30;
92  if (!$csv) $fmt = " %-5s | %-10s | %-" . $filel . "s | %s\n";
93  else $fmt = "%s;%s;%s;%s\n";
94  if (!is_array($t) || count($t) == 0) return;
95  if (!$csv) {
96  $s = sprintf($fmt, "Vid", "Access", "Filename", "Doc Id's");
97  echo $s;
98  echo "---------------------------------------------------------------------------------------------\n";
99  }
100  foreach ($t as $k => $v) {
101  $ds = "";
102  $first = true;
103  if (is_array($v["docs"]) && count($v["docs"]) > 0) {
104  foreach ($v["docs"] as $kk => $vv) {
105  if ($vv != - 1) {
106  $ds.= ($first ? "" : "|") . $vv;
107  $first = false;
108  }
109  }
110  }
111  if (!$csv && $ds == "") $ds = "(none)";
112  if (strlen($v["file"]) > $filel && !$csv) $f = "..." . substr($v["file"], -($filel - 3));
113  else $f = $v["file"];
114  $s = sprintf($fmt, $v["vid"], ($v["access"] ? "Ok" : "No") , $f, $ds);
115  echo $s;
116  }
117 }
118 
119 function view($dbaccess, $vaultname, $filter = array())
120 {
121 
123 
124  $vault = new VaultFile($dbaccess, $vaultname);
125  $vault->ListFiles($alls);
126 
127  $unref = false;
128  if (isset($filter["unref"])) $unref = true;
129  $unread = false;
130  if (isset($filter["unread"])) $unread = true;
131 
132  $all = array();
133  $if = 0;
134 
135  foreach ($alls as $k => $v) {
136  $vid = $v["id_file"];
137  $file = "";
138  $access = false;
139  $docs = array();
140  $docids = $dvi->GetDocIds($vid);
141  if (is_array($docids) && count($docids) > 0) {
142  foreach ($docids as $kk => $vv) if ($vv->docid != - 1) $docs[] = $vv->docid;
143  } else {
144  $dvi->docid = - 1;
145  $dvi->vaultid = $vid;
146  $dvi->Add();
147  }
148  $vault->Show($vid, $inf);
149  $file = $inf->path;
150  if (is_readable($file)) $access = true;
151  if (((!$unref && !$unread) || ($unref && count($docs) == 0) || ($unread && !$access))) {
152  $all[$if]["vid"] = $vid;
153  $all[$if]["file"] = $file;
154  $all[$if]["access"] = $access;
155  $all[$if]["docs"] = $docs;
156  $if++;
157  }
158  }
159  return $all;
160 }
161 
162 function loclog($s)
163 {
164  echo "SetDocVaultIndex> $s\n";
165 }
166 
168 {
169  if (!is_array($vt) || count($vt) == 0) return;
170 
171  $pref = "";
172  if ($test) $pref = " [test] ";
174  $vault = new VaultFile($dbaccess, $vaultname);
175 
176  foreach ($vt as $k => $v) {
177  $vid = $v["vid"];
178  $vname = $v["file"];
179  $used = (is_array($v["docs"]) && count($v["docs"]) > 0 ? true : false);
180  if (!$used) {
181  loclog("$pref Suppress vault id $vid, filename $vname");
182  if (!$test) {
183  $vault->Destroy($vid);
184  $dvi->DeleteVaultId($vid);
185  }
186  } else loclog(" *** ERROR *** $vid used (referenced in doc(s))");
187  }
188 }
189 ?>
← centre documentaire © anakeen - published under CC License - Dynacase