cvs: phpweb /include manual-lookup.inc

From: Date: Thu, 29 May 2003 18:01:30 +0000
Subject: cvs: phpweb /include manual-lookup.inc
Groups: php.mirrors 
Request: Send a blank email to php-mirrors+get-18210@lists.php.net to get a copy of this message
goba Thu May 29 14:01:30 2003 EDT Modified files: /phpweb/include manual-lookup.inc Log: Use $_SERVER['DOCUMENT_ROOT'] instead of $DOCUMENT_ROOT in the slow lookup function Also use this for loading the sqlite DB, instead of a relative path value Reformat the sqlite lookup function to be indented with spaces instead of tabs Add many comments to this part Add prefix detection code, so /function.echo type of lookups will work with the sqlite lookup method too. This hopefully fixes #23652. Remove ending ?> for consistency Changes tested on my local mirror with sqlite and without it Index: phpweb/include/manual-lookup.inc diff -u phpweb/include/manual-lookup.inc:1.20 phpweb/include/manual-lookup.inc:1.21 --- phpweb/include/manual-lookup.inc:1.20 Fri May 16 15:35:16 2003 +++ phpweb/include/manual-lookup.inc Thu May 29 14:01:30 2003 @@ -1,12 +1,10 @@ <?php // -*- C++ -*- -// $Id: manual-lookup.inc,v 1.20 2003/05/16 19:35:16 wez Exp $ +// $Id: manual-lookup.inc,v 1.21 2003/05/29 18:01:30 goba Exp $ // Try to find some variations of keyword with $prefix in the $lang manual function tryprefix($lang, $keyword, $prefix) { - global $DOCUMENT_ROOT; - // Replace all underscores with hyphens (phpdoc convention) $keyword = str_replace("_", "-", $keyword); @@ -15,35 +13,36 @@ // Try the keyword with the prefix $try = "/manual/${lang}/${prefix}${keyword}.php"; - if (@file_exists("$DOCUMENT_ROOT$try")) { return $try; } + if (@file_exists($_SERVER['DOCUMENT_ROOT'] . $try)) { return $try; } // Drop out spaces, and try that keyword (if different) $nosp = str_replace(" ", "", $keyword); if ($nosp != $keyword) { $try = "/manual/${lang}/${prefix}${nosp}.php"; - if (@file_exists("$DOCUMENT_ROOT$try")) { return $try; } + if (@file_exists($_SERVER['DOCUMENT_ROOT'] . $try)) { return $try; } } // Replace spaces with hyphens, and try that (if different) $dasp = str_replace(" ", "-", $keyword); if ($dasp != $keyword) { $try = "/manual/${lang}/${prefix}${dasp}.php"; - if (@file_exists("$DOCUMENT_ROOT$try")) { return $try; } + if (@file_exists($_SERVER['DOCUMENT_ROOT'] . $try)) { return $try; } } // Remove hyphens (and underscores), and try that (if different) $noul = str_replace("-", "", $keyword); if ($noul != $keyword) { $try = "/manual/${lang}/${prefix}${noul}.php"; - if (@file_exists("$DOCUMENT_ROOT$try")) { return $try; } + if (@file_exists($_SERVER['DOCUMENT_ROOT'] . $try)) { return $try; } } // Nothing found return ""; } -// Try to find a manual page in a specified -// language for the specified "keyword" +// Try to find a manual page in a specified language +// for the specified "keyword". Using the sqlite is +// better because then stat() calls are eliminated. function find_manual_page_slow($lang, $keyword) { // Possible prefixes to test @@ -71,51 +70,78 @@ return ""; } +// If sqlite is available on this mirror use that for manual +// page shortcuts, so we avoid stat() calls on the server function find_manual_page($lang, $keyword) { - // Use open rather than popen to avoid any chance of confusion - // when rsync updates the database - if (!function_exists("sqlite_open") || !($s = @sqlite_open(dirname(__FILE__) . '/../backend/manual-lookup.sqlite'))) { - return find_manual_page_slow($lang, $keyword); - } - - $keyword = str_replace('_', '-', $keyword); - if (strpos($keyword, '(') !== false) { - $keyword = preg_replace("!\\(.*\\)!", "-", $keyword); - } - - if (strlen($keyword) == 0) { - return ""; - } - - // NB: sqlite wants ' quoted as '' - $keyword = sqlite_escape_string($keyword); - - $keywords = array_unique(array( - $keyword, - str_replace(' ', '', $keyword), - str_replace(' ', '-', $keyword), - str_replace('-', '', $keyword), - metaphone($keyword) - )); - - $words = "'" . implode("','", $keywords) . "'"; - - $langs = ($lang != 'en') ? - array(sqlite_escape_string($lang), 'en') : - array('en'); - - foreach ($langs as $lang) { - $q = sqlite_query($s, "SELECT name from fs where lang='" . $lang . "' and keyword in (" . $words . ") order by prio LIMIT 1"); - if ($q) { - $r = sqlite_fetch_array($q, SQLITE_NUM); - if (isset($r[0])) { - return $r[0]; - } - } - } - return ""; + // If there is no sqlite support, or we are unable to + // open the database, fall back to normal search. Use + // open rather than popen to avoid any chance of confusion + // when rsync updates the database + if (!function_exists("sqlite_open") || + !($s = @sqlite_open($_SERVER['DOCUMENT_ROOT'] . '/backend/manual-lookup.sqlite'))) { + return find_manual_page_slow($lang, $keyword); + } -} + // Reformat keyword, drop anything in parenthesis + $keyword = str_replace('_', '-', $keyword); + if (strpos($keyword, '(') !== FALSE) { + $keyword = preg_replace("!\\(.*\\)!", "-", $keyword); + } + + // No keyword to search for + if (strlen($keyword) == 0) { + return ""; + } -?> + // NB: sqlite wants ' quoted as '' + $keyword = sqlite_escape_string($keyword); + + // If there is a dot in the $keyword, then the prefix + // is spefied, so we need to carry that on into the SQL + // search [eg. function.echo or function.mysql-close] + if (strpos($keyword, ".") > 0) { + list($prefix, $keyword) = explode('.', $keyword); + $prefix_SQL = "prefix = '" . $prefix . ".' AND"; + } else { + $prefix_SQL = ""; + } + + // List a few variations, plus a metaphone version + $keywords = array_unique(array( + $keyword, + str_replace(' ', '', $keyword), + str_replace(' ', '-', $keyword), + str_replace('-', '', $keyword), + metaphone($keyword) + )); + + // List of all the keywords in single quotes + $words = "'" . implode("','", $keywords) . "'"; + + // Try the preferred language first, then the + // English one in case no page is found + $langs = ($lang != 'en') ? + array(sqlite_escape_string($lang), 'en') : + array('en'); + + // Check for all the languages + foreach ($langs as $lang) { + $q = sqlite_query($s, + "SELECT name FROM fs WHERE $prefix_SQL lang = '" . $lang . + "' AND keyword IN (" . $words . ") ORDER BY prio LIMIT 1" + ); + + // Successful query + if ($q) { + $r = sqlite_fetch_array($q, SQLITE_NUM); + if (isset($r[0])) { + // Match found + return $r[0]; + } + } + } + + // No match found + return ""; +}

« previous php.mirrors (#18210) next »