Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
cleanFamily.php
Go to the documentation of this file.
1 <?php
2 /*
3  * Clean parasite attributes
4  *
5  * @author Anakeen
6  * @package FDL
7 */
8 
9 include_once ("FDL/Class.DocFam.php");
10 
11 $usage = new ApiUsage();
12 $usage->setDefinitionText("Delete attributes parameters and values which are not defined in family");
13 $docid = $usage->addRequiredParameter("famid", "family identifier - 0 means all families");
14 $verifyOnly = $usage->addEmptyParameter("verify-only", "only verify, do not changes");
15 $withoutConfirm = $usage->addEmptyParameter("without-confirm", "ask confirmation before operating");
16 $usage->verify();
17 
19 {
20  /**
21  * @var DocFam
22  */
23  public $doc = null;
24  public $messages = array();
25  public $dryrun = false;
26 
27  public function clean(DocFam $fam)
28  {
29  $this->doc = $fam;
30  $this->messages = array();
32  $this->cleanStructure();
33  }
34  public function justModify($justModify)
35  {
36  $this->dryrun = $justModify;
37  }
38  public function cleanStructure()
39  {
40  if (!$this->doc) return;
41 
42  $orphanAttributes = CheckDb::getOrphanAttributes($this->doc->id);
43  if ($orphanAttributes) {
44 
45  $sql = array();
46  foreach ($orphanAttributes as $orphanAttrId) {
47  $sql[] = sprintf("alter table doc%d drop column %s cascade; ", $this->doc->id, $orphanAttrId);
48 
49  $this->messages[].= "Destroy values for \"$orphanAttrId\".";
50  }
51  $sql[] = sprintf("create view family.\"%s\" as select * from doc%d", strtolower($this->doc->name) , $this->doc->id);
52  if (!$this->dryrun) {
53  foreach ($sql as $aSql) {
54  simpleQuery('', $aSql);
55  }
56  }
57  }
58  }
59 
61  {
62  $defs = $this->doc->getOwnDefValues();
63  foreach ($defs as $aid => $v) {
64  if (!$this->doc->getAttribute($aid)) {
65  $this->doc->setDefValue($aid, '', false);
66  $this->messages[].= "Clear default value \"$aid\".";
67  }
68  }
69  $defs = $this->doc->getOwnParams();
70  foreach ($defs as $aid => $v) {
71  $oa = $this->doc->getAttribute($aid);
72  if (!$oa || $oa->usefor != 'Q') {
73  $this->doc->setParam($aid, '', false);
74  $this->messages[].= "Clear parameter value \"$aid\".";
75  }
76  }
77 
78  if (!$this->dryrun) {
79  $this->doc->modify();
80  }
81  }
82 
83  public function getMessage($sep = "\n")
84  {
85  return implode($sep, $this->messages);
86  }
87 }
88 /*
89  * @var Action $action
90  */
91 // First Part: Workflow
92 print "\t === Deleting parasite attributes ===\n";
93 
94 $s = new SearchDoc($action->dbaccess, "-1");
95 $s->setObjectReturn(true);
96 $s->setOrder("initid");
97 $fam = null;
98 if ($docid) {
99  $fam = new_Doc($action->dbaccess, $docid);
100  if (!$fam->isAlive()) {
101  $action->exitError(sprintf("%s is not referenced", $docid));
102  }
103  if ($fam->doctype != 'C') {
104  $action->exitError(sprintf("%s is not a family reference", $docid));
105  }
106  $s->addFilter("id = %d", $fam->id);
107 }
108 
109 if ($verifyOnly) {
110  print "\nJust Verify...\n";
111 } elseif (!$withoutConfirm) {
112  printf("The suppression of attributes is irreversible.\nConfirm deleting parasites for %s [Y|N] ? ", $docid ? $fam->name : "all families");
113  $confirm = strtolower(trim(fgets(STDIN)));
114  if ($confirm != "y" && $confirm != "yes") {
115  print "\nAborted.\n";
116  exit(0);
117  }
118  print "\nCleaning in progress...\n";
119 }
120 
121 $s->search();
122 $deleting = array();
123 
124 $c = new cleanFamily();
125 $c->justModify($verifyOnly);
126 /*
127  * @var DocFam $fam
128  */
129 while ($fam = $s->getNextDoc()) {
130  $c->clean($fam);
131  $msg = $c->getMessage("\n\t");
132  if ($msg) {
133  printf("\n\"%s\" %s cleaning\n", $fam->getTitle() , $fam->name);
134  print "\t$msg\n";
135  } else {
136  printf("\"%s\" %s is clean. Nothing to do.\n", $fam->getTitle() , $fam->name);
137  }
138 }
139 print "\n";
$usage
Definition: cleanFamily.php:11
global $action
justModify($justModify)
Definition: cleanFamily.php:34
getMessage($sep="\n")
Definition: cleanFamily.php:83
$withoutConfirm
Definition: cleanFamily.php:15
if($famId) $s
clean(DocFam $fam)
Definition: cleanFamily.php:27
$docid
Definition: cleanFamily.php:13
$verifyOnly
Definition: cleanFamily.php:14
cleanDefaultAndParametersValues()
Definition: cleanFamily.php:60
print
Definition: checklist.php:49
switch($command) exit
Definition: checkVault.php:46
new_Doc($dbaccess, $id= '', $latest=false)
simpleQuery($dbaccess, $query, &$result=array(), $singlecolumn=false, $singleresult=false, $useStrict=null)
Definition: Lib.Common.php:484
Verify arguments for wsh programs.
← centre documentaire © anakeen