-
Notifications
You must be signed in to change notification settings - Fork 29
/
gen-phpweb-sqlite-db.php
194 lines (160 loc) · 5.06 KB
/
gen-phpweb-sqlite-db.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
<?php
/*
$Id$
This script scans files within the generated PHP Manual (phpweb theme)
and creates shortcut lookups for them with their metaphone based
companions. Mirrors with pdo_sqlite then check against this data. So,
this allows the likes of "php.net/foo" to work.
This script is known to be imperfect, and future plans will use PhD to
generate this data based on XML ids thus including ini settings,
predefined constants, functions, etc.
Usage:
php gen-phpweb-sqlite-db.php database DOCUMENT_ROOT MANUAL_PREFIX [implied_lang]
Example:
php gen-phpweb-sqlite-db.php database /local/Web/sites/phpweb /manual
Each week this information is rendered on the rsync box.
*/
$DOCUMENT_ROOT = $argv[2];
$DOCUMENT_ROOT_LEN = strlen($DOCUMENT_ROOT);
$MANUAL_PREFIX = $argv[3];
$implied_lang = isset($argv[4]) ? $argv[4] : false;
$db_name = $argv[1];
if (file_exists($db_name)) {
unlink($db_name);
}
$dbh = new PDO( "sqlite:$db_name", '', '' );
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "CREATE TABLE fs (
lang char(5) not null,
prefix char(32) not null,
keyword char(128) not null,
name varchar(238) not null,
prio int not null
);";
try {
$res = $dbh->query( $sql );
$res = $dbh->query( 'CREATE INDEX map ON fs (lang,keyword)' );
} catch ( PDOException $e ) {
echo 'Error: Cannot create db table. Here is the error message: ' . $e->getMessage() . PHP_EOL;
exit;
}
if (empty($DOCUMENT_ROOT) || empty($MANUAL_PREFIX) || empty($db_name)) {
echo 'Usage:' . PHP_EOL;
echo ' php gen-phpweb-sqlite-db.php database DOCUMENT_ROOT MANUAL_PREFIX [implied_lang]' . PHP_EOL;
echo ' php gen-phpweb-sqlite-db.php database /local/Web/sites/phpweb /manual' . PHP_EOL;
exit;
}
$dbh->beginTransaction();
if ($implied_lang !== false) {
scan($DOCUMENT_ROOT . $MANUAL_PREFIX, $implied_lang);
} else {
scan_langs($DOCUMENT_ROOT . $MANUAL_PREFIX);
}
$dbh->commit();
exit(0);
function scan($dir, $lang)
{
global $dbh;
global $s;
global $DOCUMENT_ROOT_LEN;
static $sections = array(
'book.', 'ref.', 'function.', 'class.', 'feature-',
'control-structures.', 'language.',
'about.', 'faq.', 'features.',
);
$count = 0;
echo "Lang: $lang\n";
$d = opendir($dir);
if (!$d) {
return;
}
while (($f = readdir($d)) !== false) {
// Directories to skip
$skips = array('.git', '.svn', 'feeds', 'images', 'toc', '.', '..');
if (in_array($f, $skips)) {
continue;
}
$file = $dir . DIRECTORY_SEPARATOR . $f;
if (is_dir($file)) {
scan($file, $lang);
} else {
/* which section/prefix does this fall under ? */
$prefix = "";
$keyword = $f;
// Get filepath relative to the manual/
$doc_rel = substr($file, $DOCUMENT_ROOT_LEN);
// Keyword is filename minus the extension
$x = strrpos($keyword, '.');
if ($x !== false) {
$keyword = substr($keyword, 0, $x);
}
// Skip PHP 4 domxml (book.domxml). It uses function. syntax, unlike book.dom
if (0 === strpos($keyword, 'function.dom') && false === strpos($keyword, 'simplexml')) {
continue;
}
if (0 === strpos($keyword, 'function.xpath') || 0 === strpos($keyword, 'function.xptr')) {
continue;
}
/* Example:
- section: book.
- keyword: about.formats
- f: about.formats.php
- doc_rel: /en/about.formats.php
- x: 13
*/
foreach ($sections as $prio => $section) {
/* Example:
- before: keyword: about.formats prefix: section: about. f: about.formats.php
- after: keyword: formats prefix: about. section: about. f: about.formats.php
*/
if (!strncmp($f, $section, strlen($section))) {
$keyword = substr($keyword, strlen($section));
$prefix = $section;
break;
}
}
// Hack until PhD generates ids from the DocBook files, and error.php uses them
// $hackme contains class [method] prefixes with their function counterparts
$hackme = array(
'mysqli-result.' => 'mysqli-',
'mysqli.' => 'mysqli-',
);
foreach ($hackme as $class => $procedural) {
if (false !== strpos($keyword, $class)) {
$tmp = str_replace($class, $procedural, $keyword);
$dbh->exec("INSERT INTO fs (lang, prefix, keyword, name, prio) values ('$lang', '$prefix', '$tmp', '$doc_rel', " . ($prio+5).")");
$dbh->exec("INSERT INTO fs (lang, prefix, keyword, name, prio) values ('$lang', '$prefix', '". metaphone($tmp) ."', '$doc_rel', " . ($prio+15).")");
break;
}
}
++$count;
$dbh->exec("INSERT INTO fs (lang, prefix, keyword, name, prio) values ('$lang', '$prefix', '$keyword', '$doc_rel', $prio)");
$dbh->exec("INSERT INTO fs (lang, prefix, keyword, name, prio) values ('$lang', '$prefix', '" . metaphone($keyword) . "', '$doc_rel', ".($prio+10).")");
}
}
closedir($d);
echo "Added entries for $count files\n";
echo "\n";
}
function scan_langs($root)
{
global $skip_dirs;
$d = opendir($root);
if (!$d) {
return;
}
readdir($d); readdir($d);
while (($f = readdir($d)) !== false) {
if ($f === '.svn') {
continue;
}
if ($f === '.git') {
continue;
}
$file = $root . DIRECTORY_SEPARATOR . $f;
if (is_dir($file)) {
scan($file, $f);
}
}
closedir($d);
}