-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_infopage.php
90 lines (84 loc) · 2.27 KB
/
generate_infopage.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
<?php
$line = array();
$contents = array(); //new stdClass();
$maindir = 'raw-data';
if (! isset($argv[1])) {
DisplayFiles();
}
else {
if ($argv[1] == '--sort') {
$realm = $argv[2];
$sort = true;
}
else {
$realm = $argv[1];
$sort = false;
}
$input_files = GetInputs($realm);
if ($input_files) {
$data='<h1>'.ucfirst($realm).'</h1>'.PHP_EOL;
$total_files = 0;
foreach ($input_files as $file) {
$data.='<h2>'.$file.'</h2>'.PHP_EOL;
$data.=CreateInfo($file, $sort);
}
}
if (isset($data)) {
print $data;
}
}
function DisplayFiles() {
global $maindir;
$files = scandir($maindir);
print PHP_EOL.'Usage: php [--sort] generate_infopage.php [realm]'.PHP_EOL.PHP_EOL;
print 'Available realms: '.PHP_EOL;
foreach ($files as $f) {
if (is_dir($maindir.'/'.$f) &! (preg_match("/^\./", $f))) { //exclude .dirs
print ' '.$f.' ('.GetInputs($f,true).')'.PHP_EOL;
}
}
}
function CreateInfo($file, $sort=false) {
global $maindir;
global $realm;
$output = '';
$input_path = $maindir.'/'.$realm.'/'.$file;
$handle = @fopen($input_path, 'r');
$lines = array();
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
$term = ltrim(chop($buffer));
array_push($lines, $term);
}
if ($sort === true) {
sort($lines);
}
foreach($lines as $term) {
$output.='<li><a href="https://en.wikipedia.org/wiki/'.$term.'">'.$term.'</a></li>'.PHP_EOL;
}
}
return $output;
}
function GetInputs ($realm, $human_readable=false) {
global $maindir;
$output = array();
if (is_dir($maindir.'/'.$realm)) {
$files = scandir($maindir.'/'.$realm);
foreach ($files as $f) {
if (! (preg_match('/^\./', $f))) {
array_push($output, $f);
}
}
$human_array = join (', ', $output);
if ($human_readable) {
return $human_array;
}
else { return $output; }
} //end if is_dir
else {
print 'Not a directory: '.$maindir.'/'.$realm.PHP_EOL;
print PHP_EOL.'Usage: php [--sort] generate_infopage.php [realm]'.PHP_EOL.PHP_EOL;
return false;
}
}
?>