-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
434 lines (364 loc) · 15.3 KB
/
index.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
<?php
// Only valid if PHP7 or greater
declare(strict_types = 1);
namespace Avonture;
require_once 'vendor/autoload.php';
use \Classes\Csv2Md;
use \Classes\DbDocument;
use \Classes\Helper;
// Source of this script on github
define('REPO', 'https://github.com/cavo789/db_documentor');
// Name of the settings file
define('SETTINGS', 'settings.json');
// Enable or not the debug mode
define('DEBUG', true);
/**
* Read the settings.json file.
* If something goes wrong with the file, exit the script and show an error message.
*
* @return array
*/
function readSettings(): array
{
$arr = [];
if (file_exists(SETTINGS)) {
$json = file_get_contents(SETTINGS);
if ('' == trim($json)) {
die('Sorry, the ' . SETTINGS . ' file is empty, please read the documentation at ' . REPO);
}
$arr = json_decode($json, true);
} else {
// die('Sorry, the ' . SETTINGS . ' file is missing, please read the documentation at ' . REPO);
}
return $arr;
}
/**
* A database name has been selected; retrieve informations and generate doc.
*
* @param string $dbName
*
* @return array
*/
function doIt(string $dbName): array
{
// Just in case
if ('' == $dbName) {
return
[
'status' => '0',
'message' => 'Error, no database name provided; something ' .
'goes wrong with the ajax call',
];
}
$dbName = base64_decode($dbName);
$arr = readSettings();
foreach ($arr['databases'] as $db) {
if ($db['name'] == $dbName) {
break;
}
}
// In case of: the search dbname isn't found in the settings file. Should not occurs
// except if the file has been updated after that the form has been displayed.
if ($db['name'] !== $dbName) {
return
[
'status' => '0',
'message' => 'Error, no database called ' . $dbName . ' has been ' .
'retrieved in your ' . SETTINGS . 'file',
];
}
// Ok, we've retrieved our database from the settings file
$arrReturn = [];
// 1 for success
$arrReturn['status'] = 1;
// Instantiate and initialize our class
$csvParser = new Csv2Md();
$dbDoc = new DbDocument($db, $csvParser);
$dbDoc->setDebug(DEBUG);
// Get the configuration coming from the setting file
$dbDoc->setTimeZone($arr['config']['timezone'] ?? 'Europe/Brussels');
$dbDoc->setTimeFormat($arr['config']['timeformat'] ?? 'd/m/Y H:i:s');
$dbDoc->setMaxRows(intval($arr['config']['maxrows'] ?? 5));
$dbDoc->setCreateCSV(boolval($arr['config']['create_csv'] ?? true));
$dbDoc->setCreateMD(boolval($arr['config']['create_md'] ?? true));
$dbDoc->setCreateCustomMD(boolval($db['output']['add_custom_files'] ?? true));
$dbDoc->setCreateMarknotes(boolval($arr['config']['create_marknotes'] ?? false));
$dbDoc->setCreateGitLabWiki(boolval($arr['config']['create_gitlab_wiki'] ?? true));
$dbDoc->setCreateSQL(boolval($arr['config']['create_sql'] ?? true));
$dbDoc->setCSVSeparator($arr['config']['csv_separator'] ?? ',');
// Get templates to use
if (boolval($arr['config']['create_gitlab_wiki'] ?? false)) {
$dbDoc->setTemplates($arr['gitlab_wiki']['templates'] ?? []);
} elseif (boolval($arr['config']['create_marknotes'] ?? false)) {
$dbDoc->setTemplates($arr['marknotes']['templates'] ?? []);
}
// For easiness, return a HTML string
if (boolval($arr['config']['get_credentials'] ?? true)) {
$arrReturn['credentials'] = $dbDoc->getHTMLCredentials();
}
if (!$dbDoc->init()) {
$arrReturn['status'] = 0;
$arrReturn['message'] = 'Fatal error with the database connection, ' .
'invalid credentials, please review your ' . SETTINGS . ' file';
}
// For easiness, return a HTML string
$arrReturn['tables'] = $dbDoc->getListOfTables();
if (boolval($arr['config']['get_detail'] ?? true)) {
$arrReturn['detail'] = $dbDoc->getTablesDetail();
}
if (boolval($arr['config']['createCsv'] ?? true) ||
boolval($arr['config']['createMd'] ?? true)
) {
$arrReturn['conclusion'] = 'Files have been created in folder ' . $db['output']['folder'] . '.';
$url = trim(strval($db['output']['url']) ?? '');
if ('' !== $url) {
$link = '<a href="' . $db['output']['url'] . '" target="_blank">' .
'See documentation online</a>';
$arrReturn['conclusion'] .= ' ' . $link;
}
} else {
$arrReturn['conclusion'] = 'The database has been successfully processed';
}
// No more needed, release the object
unset($dbDoc);
return $arrReturn;
}
Helper::initDebug(DEBUG);
// Get the data sent by Ajax.
$data = json_decode(file_get_contents('php://input'), true);
$task = trim(filter_var(($data['task'] ?? ''), FILTER_SANITIZE_STRING));
$dbName = trim(filter_var(($data['dbName'] ?? ''), FILTER_SANITIZE_STRING));
if ('doIt' === $task) {
$result = doIt($dbName);
// Do the job. doIt() will return an array
die(json_encode($result));
} else {
// Read the settings.json file and initialize the list of databases
$arr = readSettings();
// Get the list of databases
$sDBNames = '';
// Sort databases
if (isset($arr['databases'])) {
sort($arr['databases']);
// $sDBNames will be used for our <select>...</select> for allowing the user to
// select a database
foreach ($arr['databases'] as $db) {
$sDBNames .= '<option value="' . $db['name'] . '">' . $db['name'] . '</option>';
}
}
}
// Get the GitHub corner
$github = '';
if (is_file($cat = __DIR__ . DIRECTORY_SEPARATOR . 'octocat.tmpl')) {
$github = str_replace('%REPO%', REPO, file_get_contents($cat));
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="author" content="Christophe Avonture" />
<meta name="robots" content="noindex, nofollow" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=9; IE=8;" />
<title>Database documentation tool</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.4/css/bulma.min.css" rel="stylesheet" media="screen" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.15.0/themes/prism.min.css" rel="stylesheet" media="screen" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.25.3/css/theme.ice.min.css" rel="stylesheet" media="screen" />
</head>
<body>
<?php echo $github; ?>
<section class="section">
<div class="container">
<h1 class="title is-3">Database documentor</h1>
<p class="subtitle">Document each table of your database; create .csv and multiple .md files so you can easily retrieve these information's for your favorites documentation tool.</p>
<small class="content has-text-info">
The configuration is coming from the
<?php echo __DIR__ . DIRECTORY_SEPARATOR . SETTINGS; ?> file;
if the file isn't there, please copy `settings.json.dist` and name the new file
<?php echo __DIR__ . DIRECTORY_SEPARATOR . SETTINGS; ?> needed please edit it and adapt the program to your needs. Read documentation
on <a href="<?php echo REPO; ?>">GitHub</a> to learn how to do.
</small>
<hr/>
<div id="app">
<div class="field is-horizontal">
<div class="field-label is-normal">
<label class="label" for="select_dbname">Please select your database:</label>
</div>
<div class="field-body">
<div class="field">
<p class="control">
<span class="select">
<select @change="selectDbName" class="select" v-model="name" id="select_dbname" ><?php echo $sDBNames; ?></select>
</span>
</p>
</div>
</div>
</div>
<h2 class="title is-4" v-if="name">{{ name }}</h2>
<errors v-if="errors.length" :errors="errors"></errors>
<loading v-if="name" :loading="loading"></loading>
<credentials v-if="credentials" :html="credentials"></credentials>
<tables v-if="tables" :html="tables"></tables>
<detail v-if="detail" :html="detail"></detail>
<conclusion :html="conclusion"></conclusion>
</div>
</div>
</section>
<script src="https://unpkg.com/vue"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<!-- jQuery need for tablesorter plugin; used in the "List of tables" part -->
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.25.3/js/jquery.tablesorter.combined.min.js"></script>
<!-- Prism - Highlight SQL statements -->
<script type="text/javascript" data-manual src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.15.0/prism.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.15.0/components/prism-sql.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.15.0/components/prism-plsql.min.js"></script>
<script>
Vue.component("loading", {
template:
`<div v-if="loading" class="content has-text-info">
<b>Loading... Please wait... Documenting a database is a slow process</b>
</div>`,
props: {
loading: {
type: Boolean
}
}
});
Vue.component("errors", {
template:
`<div class="content has-text-danger">
<b>Please correct the following error(s):</b>
<ul>
<li v-for="error in errors">{{ error }}</li>
</ul>
</div>`,
props: {
errors: {
type: Array
}
}
});
Vue.component("credentials", {
template:
`<div class="content">
<details>
<summary>Connection information</summary>
<div v-html="html" />
</details>
</div>`,
props: {
html: {
type: String
}
}
});
Vue.component("tables", {
template:
`<div class="content">
<details>
<summary>Summary of tables</summary>
<div v-html="html" />
</details>
</div>`,
props: {
html: {
type: String
}
},
mounted() {
// Make the table sortable thanks to the tableSorter plugin
$("#tbl").tablesorter({
theme: "ice",
widthFixed: false,
sortMultiSortKey: "shiftKey",
sortResetKey: "ctrlKey",
headers: {
0: {sorter: "text"}, // Table name
1: {sorter: "digit"} // Number of records
},
ignoreCase: true,
headerTemplate: "{content} {icon}",
widgets: ["uitheme", "filter"],
initWidgets: true,
widgetOptions: {
uitheme: "ice"
},
sortList: [[0]] // Sort by default on the table name
});
}
});
Vue.component("detail", {
template:
`<div class="content">
<details>
<summary>List of tables</summary>
<div v-html="html" />
</details>
</div>`,
props: {
html: {
type: String
}
}
});
Vue.component("conclusion", {
template:
`<div class="content has-text-success"><div v-html="html" /></div>`,
props: {
html: {
type: String
}
}
});
var app = new Vue({
el: '#app',
data: {
conclusion: '',
credentials: '',
detail: '',
name: '',
errors: [],
status: 0,
tables: '',
loading: false
},
methods: {
selectDbName() {
this.errors = [];
this.conclusion = '';
this.credentials = '';
this.detail = '';
this.tables = '';
this.loading = true;
var $data = {
task: 'doIt',
dbName: window.btoa(this.name)
}
axios.post('<?php echo basename(__FILE__); ?>', $data)
.then(response => {
if (response.data.status==0) {
// status = 0 means errors
this.errors.push(response.data.message);
}
this.loading = false;
this.credentials = response.data.credentials;
this.tables = response.data.tables;
this.detail = response.data.detail;
this.conclusion = response.data.conclusion;
})
.catch(function (error) {console.log(error);})
.then(function() {
if (typeof Prism === 'object') {
// Use prism.js and highlight source code
Prism.highlightAll();
}
});
}
}
});
</script>
</body>
</html>