-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.php
206 lines (197 loc) · 9.14 KB
/
admin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
/* _DRY_RUN = false | true - dry run won't change the database, but will
mime all the actions that would be done: use _DEBUG true to see these */
define("_DRY_RUN",false);
define("_ALLOWUNINSTALL",false); // NOT YET ACTIVE
require_once("headerDB.inc.php");
require_once('admin.inc.php');
define("_DEBUG",true && ($config['debug'] & _GTD_DEBUG));
/*
TOFIX: scan for available installations
TOFIX: Use a javascript onsubmit for the delete verification, and fallback to POST if no javascript
TOFIX: move DELETE from install.php to here
------------------------------------------------------------
TOFIX: LOCK TABLES if possible, while doing admin.
NB: you cannot use a locked table multiple times in a single query.
Use aliases instead, in which case you must obtain a lock for each alias separately.
mysql> LOCK TABLE t WRITE, t AS t1 WRITE;
mysql> INSERT INTO t SELECT * FROM t;
ERROR 1100: Table 't' was not locked with LOCK TABLES
mysql> INSERT INTO t SELECT * FROM t AS t1;
------------------------------------------------------------
*/
$action=(isset($_GET['action']))?$_GET['action']:'validate';
$showInstallations=true;
$showCommands=true;
$prefix=(isset($_GET['prefix']))?$_GET['prefix']:$config['prefix'];
if (!checkPrefix($prefix)) $prefix='';
$availableActions=array('validate','repair','backup');
if (_ALLOWUNINSTALL) $availableActions[]='delete';
switch ($action) {
case 'backup':
$backup=backupData($prefix);
break;
case 'delete':
if (!_ALLOWUNINSTALL) break;
break;
case 'none':
break;
case 'repair':
$toterrs=0;
$pre=checkErrors($prefix);
fixData($prefix);
$post=checkErrors($prefix);
$repair="<h2>Results of repairs on installation with prefix '$prefix'</h2>\n";
$repair.="<p>Repair complete.</p>";
if ($post['totals']['orphans'])
$repair.="<p>Now check <a href='orphans.php'>orphans</a>.</p>\n";
$repair.="<p>Check for <a href='listItems.php?type=p'>projects</a> that have no actions, or no next actions.</p>\n";
$repair.="<table summary='result of repairs'><thead>\n<tr><th>Before</th><th>After</th><th> </th></tr></thead><tbody>";
foreach($post['totals'] as $key=>$val)
$repair .="<tr><td>{$pre['totals'][$key]}</td><td>$val</td><th>$key</th></tr>\n";
foreach($post['errors'] as $key=>$val) {
$toterrs+=(int) $val;
$preval=$pre['errors'][$key];
$class1=($preval)?" class='warnresult' ":" class='goodresult' ";
if ($val)
$class2=" class='warnresult' ";
else if ($preval)
$class2=" class='goodresult' ";
else {
$class1='';
$class2='';
}
$repair .= "<tr><td $class1>{$preval}</td><td $class2>$val</td><td $class2>$key</td></tr>\n";
}
$repair .="</tbody></table>\n";
$action=($toterrs)?'repair':'backup';
break;
case 'validate':
$result=checkErrors($prefix);
$validate="<h2>Validation checks on installation with prefix $prefix</h2>";
if ($result===false) {
$validate.="<p class='error'>No database with prefix '$prefix'</p>\n";
$prefix=$config['prefix'];
} else {
$toterrs=0;
$validate.="<p>Number of inconsistencies in the gtd-php data-set. NB some errors may overlap.</p>\n"
."<table summary='validation checks'><thead>\n";
foreach($result['totals'] as $key=>$val)
$validate .="<tr><td>$val</td><th>$key</th></tr>\n";
$validate .="</thead><tbody>\n";
foreach($result['errors'] as $key=>$val) {
$class=($val)?" class='warnresult' ":" class='goodresult' ";
$validate .= "<tr><td $class>$val</td><td $class>$key</td></tr>\n";
$toterrs+=(int) $val;
}
$validate .="</tbody></table>\n";
}
$action=($toterrs)?'repair':'backup';
break;
}
/* ------------------------------------------------------------------------
output begins here
------------------------------------------------------------------------*/
?>
<?php require_once("headerHtml.inc.php"); ?>
</head><body><div id='container'>
<?php require_once("headerMenu.inc.php"); ?>
<div id='main'>
<h1>gtd-php Admin Tasks</h1>
<?php if ($action==='delete') { ?>
<h2>Delete installation</h2>
<?php }
if (!empty($validate)) echo $validate;
if ($showInstallations || $showCommands) { ?>
<h2>Action</h2>
<form action='admin.php'>
<?php if ($showInstallations) { ?>
<h3>Detected installations in this database</h3>
<p>Pick one to operate on:</p>
<div class='formrow'>
<label class='left first' for='prefix'>prefix</label><input id='prefix' type='text' name='prefix'
value='<?php echo $prefix; ?>' />
</div>
<?php } if ($showCommands) { ?>
<h3>Action to take:</h3>
<div class='formrow'>
<?php foreach ($availableActions as $doit) { ?>
<label class='notfirst left'><?php echo $doit; ?></label>
<input type='radio' name='action' value=<?php echo "'$doit'",($doit===$action)?" checked='checked' ":''; ?> />
<?php } ?>
<input type='submit' name='submit' value='Go' />
</div>
<h3>Delete completed references:</h3>
<div class='formrow'>
<a href="admin.php?comprefs=delete">Go</a>
<?php
$q="SELECT * FROM `{$prefix}items` AS `i`
JOIN `{$prefix}itemattributes` AS `ia` USING (`itemId`)
JOIN `{$prefix}itemstatus` AS `its` USING (`itemId`)
WHERE `its`.`dateCompleted` IS NOT NULL
AND ia.`type` = 'r'";
$refscomp = query($q);
echo count($refscomp);
if (isset($_GET['comprefs']) && $_GET['comprefs'] ==='delete') {
foreach ($refscomp as $ref) {
$values['itemId'] = $ref['itemId'];
query("deleteitemstatus",$config,$values);
query("deleteitemattributes",$config,$values);
query("deleteitem",$config,$values);
query("deletelookup",$config,$values);
query("deletelookupparents",$config,$values);
//removeNextAction();
query("deletenextactionparents",$config,$values);
$values['itemType'] = $ref['type'];
query("deletequalities",$config,$values);
}
}
?>
</div>
<h3>Delete orphaned actions/refs:</h3>
<div class='formrow'>
<a href="admin.php?orphans=delete">Go</a>
<?php
$q="SELECT * FROM `{$prefix}items` AS `i`
JOIN `{$prefix}itemattributes` AS `ia` USING (`itemId`)
JOIN `{$prefix}itemstatus` AS `its` USING (`itemId`)
WHERE (ia.`type` IN ('a','r','w')
AND `i`.`itemId` NOT IN (SELECT `itemId` FROM `{$prefix}lookup`
) OR ia.`type`='' OR ia.`type` IS NULL)";
$orphs = query($q);
echo count($orphs);
if (isset($_GET['orphans']) && $_GET['orphans'] ==='delete') {
foreach ($orphs as $orph) {
$values['itemId'] = $orph['itemId'];
query("deleteitemstatus",$config,$values);
query("deleteitemattributes",$config,$values);
query("deleteitem",$config,$values);
query("deletelookup",$config,$values);
query("deletelookupparents",$config,$values);
//removeNextAction();
query("deletenextactionparents",$config,$values);
$values['itemType'] = $orph['type'];
query("deletequalities",$config,$values);
}
}
?>
</div>
<?php } ?>
</form>
<?php
}
if (!empty($repair)) echo $repair;
if (!empty($backup)) {
?><h2>Backup of installation with prefix '<?php echo $prefix; ?>'</h2>
<textarea cols="120" id="cursorHere"rows="10"><?php echo $backup; ?></textarea>
<?php } ?>
<h2> </h2>
<p>Note that because this report counts all items (of all types) without parents
regardless of whether they'd normally appear in the orphans report, the
orphan count in the table will rarely match the total shown on the <a href='orphans.php'>orphans report</a>.</p>
<p>The count of next actions also includes items marked as next actions in the
<a href='listItems.php?type=a&tickler=true'>tickler file</a>, and on the
<a href='listItems.php?type=w'>waiting-on list</a>, and so will rarely match the
total shown on the <a href='listItems.php?type=a&nextonly=true'>next-actions report</a>.</p>
<script type='text/javascript'>focusOnForm('cursorHere');</script>
<?php require_once('footer.php'); ?>