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