-
Notifications
You must be signed in to change notification settings - Fork 563
Introduce UserNote DTO #594
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
kamil-tekiela
merged 7 commits into
php:master
from
kamil-tekiela:Introduce-UserNote-DTO
Sep 16, 2022
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d22a8d6
Introduce UserNote DTO
kamil-tekiela 44aa141
Apply suggestions from code review
kamil-tekiela 69fd8a3
Annotation class as immutable
kamil-tekiela 3a0436b
Adjust tests
kamil-tekiela 2f438ed
Create sort-notes-full.phpt
kamil-tekiela b8893fc
Add return types
kamil-tekiela 20c3cca
CS fixes
kamil-tekiela 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,10 +23,16 @@ $PGI = []; $SIDEBAR_DATA = ''; | |
// ============================================================================= | ||
|
||
require_once __DIR__ . '/../autoload.php'; | ||
use phpweb\UserNotes\Sorter; | ||
|
||
// Print out all user notes for this manual page | ||
function manual_notes($notes) { | ||
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 { | ||
// Get needed values | ||
list($filename) = $GLOBALS['PGI']['this']; | ||
|
||
|
@@ -68,68 +74,63 @@ END_USERNOTE_HEADER; | |
// If we have notes, print them out | ||
echo '<div id="allnotes">'; | ||
foreach ($notes as $note) { | ||
manual_note_display( | ||
$note['xwhen'], $note['user'], $note['note'], $note['id'], $note['votes'] | ||
); | ||
manual_note_display($note); | ||
} | ||
echo "</div>\n"; | ||
echo "<div class=\"foot\">$addnotesnippet</div>\n"; | ||
} | ||
echo "</section>"; | ||
} | ||
// Get user notes from the appropriate text dump | ||
function manual_notes_load($id) | ||
|
||
/** | ||
* Get user notes from the appropriate text dump | ||
* | ||
* @return array<string, UserNote> | ||
*/ | ||
function manual_notes_load(string $id): array | ||
{ | ||
// Initialize values | ||
$notes = []; | ||
$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; | ||
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] = [ | ||
"id" => $id, | ||
"sect" => $sect, | ||
"rate" => $rate, | ||
"xwhen" => $ts, | ||
"user" => $user, | ||
"note" => base64_decode($note, true), | ||
"votes" => ["up" => (int)$up, "down" => (int)$down] | ||
]; | ||
$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($date, $name, $text, $id, $votes = ['up' => 0, 'down' => 0], $voteOption = true) | ||
function manual_note_display(UserNote $note, $voteOption = true) | ||
{ | ||
if ($name) { | ||
$name = "\n <strong class=\"user\"><em>" . htmlspecialchars($name) . "</em></strong>"; | ||
if ($note->user) { | ||
$name = "\n <strong class=\"user\"><em>" . htmlspecialchars($note->user) . "</em></strong>"; | ||
} else { | ||
$name = "<strong class=\"user\"><em>Anonymous</em></strong>"; | ||
} | ||
$name = ($id ? "\n <a href=\"#$id\" class=\"name\">$name</a><a class=\"genanchor\" href=\"#$id\"> ¶</a>" : "\n $name"); | ||
$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 | ||
$datestr = relTime(new DateTime("@{$date}")); | ||
$fdatestr = date("Y-m-d h:i", $date); | ||
$text = clean_note($text); | ||
$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 = $votes['up'] - $votes['down']; | ||
$p = floor(($votes['up'] / (($votes['up'] + $votes['down']) ?: 1)) * 100); | ||
$rate = !$p && !($votes['up'] + $votes['down']) ? "no votes..." : "$p% like this..."; | ||
$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) { | ||
|
@@ -140,13 +141,13 @@ function manual_note_display($date, $name, $text, $id, $votes = ['up' => 0, 'dow | |
$rredir_filename = urlencode($redir_filename); | ||
$votediv = <<<VOTEDIV | ||
<div class="votes"> | ||
<div id="Vu{$id}"> | ||
<a href="/manual/vote-note.php?id={$id}&page={$rredir_filename}&vote=up" title="Vote up!" class="usernotes-voteu">up</a> | ||
<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{$id}"> | ||
<a href="/manual/vote-note.php?id={$id}&page={$rredir_filename}&vote=down" title="Vote down!" class="usernotes-voted">down</a> | ||
<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{$id}" title="{$rate}"> | ||
<div class="tally" id="V{$note->id}" title="{$rate}"> | ||
{$vote} | ||
</div> | ||
</div> | ||
|
@@ -156,26 +157,26 @@ VOTEDIV; | |
} | ||
|
||
// If the viewer is logged in, show admin options | ||
if (isset($_COOKIE['IS_DEV']) && $id) { | ||
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+' . $id, | ||
'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+' . $id, | ||
'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+' . $id, | ||
'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' | ||
|
@@ -187,8 +188,8 @@ VOTEDIV; | |
|
||
echo <<<USER_NOTE_TEXT | ||
|
||
<div class="note" id="$id">{$votediv}{$name}{$admin}<div class="date" title="$fdatestr"><strong>{$datestr}</strong></div> | ||
<div class="text" id="Hcom{$id}"> | ||
<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> | ||
|
@@ -295,7 +296,7 @@ function manual_setup($setup) { | |
$USERNOTES = manual_notes_load($filename); | ||
if ($USERNOTES) { | ||
$note = current($USERNOTES); | ||
$timestamps[] = $note["xwhen"]; | ||
$timestamps[] = $note->ts; | ||
} | ||
|
||
$lastmod = max($timestamps); | ||
|
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
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.
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.
Uh oh!
There was an error while loading. Please reload this page.