-
Notifications
You must be signed in to change notification settings - Fork 613
Create UserNoteService class #1662
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+255
−261
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,185 +23,7 @@ $PGI = []; $SIDEBAR_DATA = ''; | |
| // ============================================================================= | ||
|
|
||
| use phpweb\I18n\Languages; | ||
| use phpweb\UserNotes\Sorter; | ||
| use phpweb\UserNotes\UserNote; | ||
|
|
||
| /** | ||
| * Print out all user notes for this manual page | ||
| * | ||
| * @param array<string, UserNote> $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, | ||
| "+<small>$addNote</small>", | ||
| ); | ||
|
|
||
| $num_notes = count($notes); | ||
| $noteCountHtml = ''; | ||
| if ($num_notes) { | ||
| $noteCountHtml = "<span class=\"count\">$num_notes note" . ($num_notes == 1 ? '' : 's') . "</span>"; | ||
| } | ||
|
|
||
| $userContributedNotes = autogen('user_contributed_notes', $LANG); | ||
| echo <<<END_USERNOTE_HEADER | ||
| <section id="usernotes"> | ||
| <div class="head"> | ||
| <span class="action">{$addnotesnippet}</span> | ||
| <h3 class="title">$userContributedNotes {$noteCountHtml}</h3> | ||
| </div> | ||
| END_USERNOTE_HEADER; | ||
|
|
||
| // If we have no notes, then inform the user | ||
| if ($num_notes === 0) { | ||
| $noUserContributedNotes = autogen('no_user_notes', $LANG); | ||
| echo "\n <div class=\"note\">$noUserContributedNotes</div>"; | ||
| } else { | ||
| // If we have notes, print them out | ||
| echo '<div id="allnotes">'; | ||
| foreach ($notes as $note) { | ||
| manual_note_display($note); | ||
| } | ||
| echo "</div>\n"; | ||
| echo "<div class=\"foot\">$addnotesnippet</div>\n"; | ||
| } | ||
| echo "</section>"; | ||
| } | ||
|
|
||
| /** | ||
| * Get user notes from the appropriate text dump | ||
| * | ||
| * @return array<string, UserNote> | ||
| */ | ||
| 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 <strong class=\"user\"><em>" . htmlspecialchars($note->user) . "</em></strong>"; | ||
| } else { | ||
| $name = "<strong class=\"user\"><em>Anonymous</em></strong>"; | ||
| } | ||
| $name = ($note->id ? "\n <a href=\"#{$note->id}\" class=\"name\">$name</a><a class=\"genanchor\" href=\"#{$note->id}\"> ¶</a>" : "\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 = <<<VOTEDIV | ||
| <div class="votes"> | ||
| <div id="Vu{$note->id}"> | ||
| <a href="/manual/vote-note.php?id={$note->id}&page={$rredir_filename}&vote=up" title="Vote up!" class="usernotes-voteu">up</a> | ||
| </div> | ||
| <div id="Vd{$note->id}"> | ||
| <a href="/manual/vote-note.php?id={$note->id}&page={$rredir_filename}&vote=down" title="Vote down!" class="usernotes-voted">down</a> | ||
| </div> | ||
| <div class="tally" id="V{$note->id}" title="{$rate}"> | ||
| {$vote} | ||
| </div> | ||
| </div> | ||
| VOTEDIV; | ||
| } else { | ||
| $votediv = null; | ||
| } | ||
|
|
||
| // If the viewer is logged in, show admin options | ||
| if (isset($_COOKIE['IS_DEV']) && $note->id) { | ||
|
|
||
| $admin = "\n <span class=\"admin\">\n " . | ||
|
|
||
| make_popup_link( | ||
| 'https://main.php.net/manage/user-notes.php?action=edit+' . $note->id, | ||
| '<img src="/images/[email protected]" height="12" width="12" alt="edit note">', | ||
| 'admin', | ||
| 'scrollbars=yes,width=650,height=400', | ||
| ) . "\n " . | ||
|
|
||
| make_popup_link( | ||
| 'https://main.php.net/manage/user-notes.php?action=reject+' . $note->id, | ||
| '<img src="/images/[email protected]" height="12" width="12" alt="reject note">', | ||
| 'admin', | ||
| 'scrollbars=no,width=300,height=200', | ||
| ) . "\n " . | ||
|
|
||
| make_popup_link( | ||
| 'https://main.php.net/manage/user-notes.php?action=delete+' . $note->id, | ||
| '<img src="/images/[email protected]" height="12" width="12" alt="delete note">', | ||
| 'admin', | ||
| 'scrollbars=no,width=300,height=200', | ||
| ) . "\n </span>"; | ||
|
|
||
| } else { | ||
| $admin = ''; | ||
| } | ||
|
|
||
| echo <<<USER_NOTE_TEXT | ||
| <div class="note" id="{$note->id}">{$votediv}{$name}{$admin}<div class="date" title="$fdatestr"><strong>{$datestr}</strong></div> | ||
| <div class="text" id="Hcom{$note->id}"> | ||
| {$text} | ||
| </div> | ||
| </div> | ||
| USER_NOTE_TEXT; | ||
|
|
||
| } | ||
| use phpweb\UserNotes\UserNoteService; | ||
|
|
||
| function manual_navigation_breadcrumbs(array $setup) { | ||
| $menu = []; | ||
|
|
@@ -298,7 +120,9 @@ function manual_setup($setup): void { | |
| if (substr($filename, -4) == '.php') { | ||
| $filename = substr($filename, 0, -4); | ||
| } | ||
| $USERNOTES = manual_notes_load($filename); | ||
|
|
||
| $userNoteService = new UserNoteService(); | ||
| $USERNOTES = $userNoteService->load($filename); | ||
| if ($USERNOTES) { | ||
| $note = current($USERNOTES); | ||
| $timestamps[] = $note->ts; | ||
|
|
@@ -422,62 +246,18 @@ function manual_footer($setup): void { | |
| </div> | ||
| CONTRIBUTE; | ||
|
|
||
| manual_notes($USERNOTES); | ||
| $userNoteService = new UserNoteService(); | ||
| $userNoteService->display($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 = "<li>" . implode("</li><li>", $setup["history"]["contributors"]) . "</li>"; | ||
|
|
||
| echo <<<CONTRIBUTORS | ||
| <div class="book"> | ||
| <h1 class="title">Output Buffering Control</h1> | ||
| The following have authored commits that contributed to this page: | ||
| <ul> | ||
| $contributorList | ||
| </ul> | ||
| </div> | ||
| 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]; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Turned out that removing this, broke the manual as it makes use of this function. I had to restore it (quickly) to make the manual not broken again: 4788a14
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I might have removed that accidently during one of the many rebases.