$notes
*/
function manual_notes($notes):void {
global $LANG;
// Get needed values
list($filename) = $GLOBALS['PGI']['this'];
// Drop file extension from the name
if (substr($filename, -4) == '.php') {
$filename = substr($filename, 0, -4);
}
$sorter = new Sorter();
$sorter->sort($notes);
$repo = strtolower($LANG);
$addNote = autogen('add_a_note', $LANG);
// Link target to add a note to the current manual page,
// and it's extended form with a [+] image
$addnotelink = '/manual/add-note.php?sect=' . $filename .
'&repo=' . $repo .
'&redirect=' . $_SERVER['BASE_HREF'];
$addnotesnippet = make_link(
$addnotelink,
"+$addNote",
);
$num_notes = count($notes);
$noteCountHtml = '';
if ($num_notes) {
$noteCountHtml = "$num_notes note" . ($num_notes == 1 ? '' : 's') . "";
}
$userContributedNotes = autogen('user_contributed_notes', $LANG);
echo <<
{$addnotesnippet}
$userContributedNotes {$noteCountHtml}
END_USERNOTE_HEADER;
// If we have no notes, then inform the user
if ($num_notes === 0) {
$noUserContributedNotes = autogen('no_user_notes', $LANG);
echo "\n
$noUserContributedNotes
";
} else {
// If we have notes, print them out
echo '
';
foreach ($notes as $note) {
manual_note_display($note);
}
echo "
\n";
echo "
$addnotesnippet
\n";
}
echo "";
}
/**
* Get user notes from the appropriate text dump
*
* @return array
*/
function manual_notes_load(string $id): array
{
$hash = substr(md5($id), 0, 16);
$notes_file = $_SERVER['DOCUMENT_ROOT'] . "/backend/notes/" .
substr($hash, 0, 2) . "/$hash";
// Open the note file for reading and get the data (12KB)
// ..if it exists
if (!file_exists($notes_file)) {
return [];
}
$notes = [];
if ($fp = @fopen($notes_file, "r")) {
while (!feof($fp)) {
$line = chop(fgets($fp, 12288));
if ($line == "") { continue; }
@list($id, $sect, $rate, $ts, $user, $note, $up, $down) = explode("|", $line);
$notes[$id] = new UserNote($id, $sect, $rate, $ts, $user, base64_decode($note, true), (int) $up, (int) $down);
}
fclose($fp);
}
return $notes;
}
// Print out one user note entry
function manual_note_display(UserNote $note, $voteOption = true): void
{
if ($note->user) {
$name = "\n " . htmlspecialchars($note->user) . "";
} else {
$name = "Anonymous";
}
$name = ($note->id ? "\n id}\" class=\"name\">$nameid}\"> ¶" : "\n $name");
// New date style will be relative time
$date = new DateTime("@{$note->ts}");
$datestr = relTime($date);
$fdatestr = $date->format("Y-m-d h:i");
$text = clean_note($note->text);
// Calculate note rating by up/down votes
$vote = $note->upvotes - $note->downvotes;
$p = floor(($note->upvotes / (($note->upvotes + $note->downvotes) ?: 1)) * 100);
$rate = !$p && !($note->upvotes + $note->downvotes) ? "no votes..." : "$p% like this...";
// Vote User Notes Div
if ($voteOption) {
list($redir_filename) = $GLOBALS['PGI']['this'];
if (substr($redir_filename, -4) == '.php') {
$redir_filename = substr($redir_filename, 0, -4);
}
$rredir_filename = urlencode($redir_filename);
$votediv = <<
CONTRIBUTE;
manual_notes($USERNOTES);
site_footer([
'related_menu' => $__RELATED['toc'],
'related_menu_deprecated' => $__RELATED['toc_deprecated'],
]);
}
// This function takes a DateTime object and returns a formated string of the time difference relative to now
function relTime(DateTime $date) {
$current = new DateTime();
$diff = $current->diff($date);
$units = ["year" => $diff->format("%y"),
"month" => $diff->format("%m"),
"day" => $diff->format("%d"),
"hour" => $diff->format("%h"),
"minute" => $diff->format("%i"),
"second" => $diff->format("%s"),
];
$out = "just now...";
foreach ($units as $unit => $amount) {
if (empty($amount)) {
continue;
}
$out = $amount . " " . ($amount == 1 ? $unit : $unit . "s") . " ago";
break;
}
return $out;
}
function contributors($setup) {
if (!isset($_GET["contributors"])
|| !isset($setup["history"]["contributors"])
|| count($setup["history"]["contributors"]) < 1) {
return;
}
$contributorList = "
" . implode("
", $setup["history"]["contributors"]) . "
";
echo <<
Output Buffering Control
The following have authored commits that contributed to this page:
$contributorList
CONTRIBUTORS;
manual_footer($setup);
exit;
}
function autogen(string $text, string $lang) {
static $translations = [];
$lang = ($lang === "") ? "en" : $lang;
$lang = strtolower($lang);
if (isset($translations[$lang])) {
if (isset($translations[$lang][$text]) && $translations[$lang][$text] !== "") {
return $translations[$lang][$text];
}
if ($lang !== "en") {
// fall back to English if text is not defined for the given language
return autogen($text, "en");
}
// we didn't find the English text either
throw new \InvalidArgumentException("Cannot autogenerate text for '$text'");
}
$translationFile = __DIR__ . \DIRECTORY_SEPARATOR . "ui_translation" . \DIRECTORY_SEPARATOR . $lang . ".ini";
if (!\file_exists($translationFile)) {
if ($lang !== "en") {
// fall back to English if translation file is not found
return autogen($text, "en");
}
// we didn't find the English file either
throw new \Exception("Cannot find translation files");
}
$translations[$lang] = \parse_ini_file($translationFile);
return autogen($text, $lang);
}