forked from facebookarchive/hack-hhvm-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquickref.php
157 lines (124 loc) · 4.6 KB
/
quickref.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
<?php
// $Id$
/*
This page is either directly called from the browser, in
which case it will always show the full list of "functions"
in the user's preferred language version of the PHP
documentation.
In other cases this file is included from manual-lookup.php,
which sets $notfound, so we know what function to search for,
and display results for that search.
*/
// Ensure that our environment is set up
$_SERVER['BASE_PAGE'] = 'quickref.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/prepend.inc';
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/errors.inc';
include $_SERVER['DOCUMENT_ROOT'] . '/include/results.inc';
if (empty($notfound)) {
mirror_redirect("/search.php");
}
// Print out the table of found (or all) functions. The HTML comments are
// needed to support MyCroft search (Mozilla browser family and Sherlock for MacOSX)
function quickref_table($functions, $sort = true)
{
global $LANG;
$funcs = "<!-- result list start -->\n";
$funcs .= "<ul id=\"quickref_functions\">\n";
// Prepare the data
if ($sort) {
asort($functions);
}
// Print out all rows
foreach ($functions as $file => $name) {
$funcs .= "<li><a href=\"/manual/$LANG/$file\">$name</a></li>\n";
}
$funcs .= "</ul>\n";
$funcs .= "<!-- result list end -->\n";
return $funcs;
}
function primary_content($notfound_dec) {
global $LANG;
// Could probably use search.php here (add another case to the switch)
// but for now...
$l = $LANG;
$q = $notfound_dec;
echo '<h1>Search results</h1>';
google_cse($q, $l);
}
function sidebar_content($notfound, $maybe) {
$poss_matches = "<h2>Hack and PHP Function List</h2>";
if (!empty($notfound) && count($maybe) > 0) {
$poss_matches .= "<p><b>".$notfound."</b> function doesn't exist. Closest function matches:</p>";
$poss_matches .= quickref_table($maybe, false);
} else {
$poss_matches .= "<p><b>".$notfound."</b> function doesn't exist. No close matches either.</p>";
}
return $poss_matches;
}
// Open directory, fall back to English,
// if there is no dir for that language
$dirh = @opendir($_SERVER['DOCUMENT_ROOT'] . "/manual/$LANG");
if (!$dirh) {
error_noservice();
}
$functions = $maybe = $temp = $parts = array();
$p = 0;
// Get all file names from the directory
while (($entry = readdir($dirh)) !== FALSE) {
// Skip names starting with a dot
if (substr($entry, 0, 1) == ".") { continue; }
// For function and class pages, get the name out
if (preg_match('!^(function|class)\.(.+)\.php$!', $entry, $parts)) {
$funcname = str_replace('-', '_', $parts[2]);
$functions[$entry] = $funcname;
// Compute similarity of the name to the requested one
if (function_exists('similar_text') && !empty($notfound)) {
similar_text($funcname, $notfound, $p);
// If $notfound is a substring of $funcname then overwrite the score
// similar_text() gave it.
if ($p < 70 && ($pos = strpos($funcname, $notfound)) !== FALSE) {
$p = 90 - $pos;
}
$temp[$entry] = $p;
}
}
}
closedir($dirh);
// We have found file names
if (count($temp) > 0) {
// Sort names by percentage
arsort($temp);
// Collect SHOW_CLOSE number of names from the top
foreach ($temp as $file => $p) {
// Stop, if we found enough matches
if (count($maybe) >= 30) { break; }
// If the two are more then 70% similar or $notfound is a substring
// of $funcname, then the match is a very similar one
if ($p >= 70 || (strpos($functions[$file], $notfound) !== FALSE)) {
$maybe[$file] = '<b>' . $functions[$file] . '</b>';
}
// Otherwise it is just similar
else {
$maybe[$file] = $functions[$file];
}
}
unset($matches, $temp);
}
// Do not index page if presented as a search result
if (count($maybe) > 0) { $head_options = array("noindex"); }
else { $head_options = array(); }
site_header("Manual Quick Reference", $head_options+array("current" => "help"));
// Note: $notfound is defined (with htmlspecialchars) inside manual-lookup.php
// Let's decode it for a nice search text box display.
$notfound_dec = htmlspecialchars_decode($notfound);
if ($snippet = is_known_snippet($notfound)) {
echo "<h1>Related snippet found for '{$notfound}'</h1>";
echo "<p>{$snippet}</p>";
}
// Left side content
primary_content($notfound_dec);
// Right side content (sidebar)
$config = array(
"sidebar" => '<p class="panel">'. sidebar_content($notfound, $maybe),
);
site_footer($config);