Skip to content

Commit bdbeacc

Browse files
committed
Improved sqlite search. See the comments for @todo items, but removed slow LIKE and increased sqlite hit frequency (e.g., php.net/function.strlen.php as per the 404 miss logs).
1 parent 9a25598 commit bdbeacc

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

include/manual-lookup.inc

+27-3
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,30 @@ function find_manual_page($lang, $keyword)
126126
// Check for all the languages
127127
foreach ($langs as $lang) {
128128

129+
// @todo consider alternative schemas for this data
130+
// @todo utilize phd to generate this data, instead of the current systems/gen-phpweb-sqlite-db.php
131+
132+
/* Example data:
133+
lang = en
134+
name = /manual/en/function.str-replace.php
135+
keyword = str-replace
136+
prefix = function.
137+
prio = 2
138+
139+
Therefore, the query below matches: str-replace, function.str-replace and function.str-replace.php
140+
This also applies to other sections like book.foo, language.foo, example.foo, etc.
141+
Note: $keyword replaces _ with - above, so _ variants also work
142+
*/
129143
if (strpos($keyword, ".") > 0) {
130-
$SQL = "SELECT name from fs WHERE name LIKE ? AND lang = ? ORDER BY prio LIMIT 1";
131-
144+
$SQL = "SELECT name from fs WHERE lang = ? AND (name = ? OR keyword = ?) ORDER BY prio LIMIT 1";
145+
146+
$_keyword = $keyword;
147+
if (pathinfo($keyword, PATHINFO_EXTENSION) !== 'php') {
148+
$_keyword .= '.php';
149+
}
150+
132151
$stm = $dbh->prepare($SQL);
133-
$stm->execute(array("%{$keyword}.php", $lang));
152+
$stm->execute(array($lang, "/manual/{$lang}/{$_keyword}", $keyword));
134153

135154
// Some partially specified URL is used
136155
} else {
@@ -163,6 +182,8 @@ function find_manual_page($lang, $keyword)
163182

164183
// Match found
165184
// But does the file really exist?
185+
// @todo consider redirecting here, instead of including content within the 404
186+
// @todo considering the file path is generated from the manual build, we can probably remove this file_exists() check
166187
if (file_exists($_SERVER["DOCUMENT_ROOT"] . $r[0])) {
167188
return $r[0];
168189
}
@@ -173,6 +194,9 @@ function find_manual_page($lang, $keyword)
173194
}
174195

175196
// No match found
197+
// @todo refactor. find_manual_page_slow() performs many of the same searches already performed above,
198+
// but uses file_exists() instead of sqlite. In other words, if sqlite was used, don't perform
199+
// all of the slow and unnecessary checks.
176200
return find_manual_page_slow($langs[0], $kw);
177201
}
178202

0 commit comments

Comments
 (0)