@@ -23,10 +23,16 @@ $PGI = []; $SIDEBAR_DATA = '';
2323// =============================================================================
2424
2525require_once __DIR__ . ' /../autoload.php' ;
26- use phpweb\UserNotes\Sorter;
2726
28- // Print out all user notes for this manual page
29- function manual_notes ($notes) {
27+ use phpweb\UserNotes\Sorter;
28+ use phpweb\UserNotes\UserNote;
29+
30+ /* *
31+ * Print out all user notes for this manual page
32+ *
33+ * @param array<string, UserNote> $notes
34+ */
35+ function manual_notes ($notes):void {
3036 // Get needed values
3137 list ($filename) = $GLOBALS[' PGI' ][' this' ];
3238
@@ -68,68 +74,63 @@ END_USERNOTE_HEADER;
6874 // If we have notes, print them out
6975 echo ' <div id="allnotes">' ;
7076 foreach ($notes as $note) {
71- manual_note_display (
72- $note[' xwhen' ], $note[' user' ], $note[' note' ], $note[' id' ], $note[' votes' ]
73- );
77+ manual_note_display ($note);
7478 }
7579 echo " </div>\n " ;
7680 echo " <div class=\" foot\" >$addnotesnippet</div>\n " ;
7781 }
7882 echo " </section>" ;
7983}
80- // Get user notes from the appropriate text dump
81- function manual_notes_load ($id)
84+
85+ /* *
86+ * Get user notes from the appropriate text dump
87+ *
88+ * @return array<string, UserNote>
89+ */
90+ function manual_notes_load (string $id): array
8291{
83- // Initialize values
84- $notes = [];
8592 $hash = substr (md5 ($id), 0 , 16 );
8693 $notes_file = $_SERVER[' DOCUMENT_ROOT' ] . " /backend/notes/" .
8794 substr ($hash, 0 , 2 ) . " /$hash" ;
8895
8996 // Open the note file for reading and get the data (12KB)
9097 // ..if it exists
9198 if (!file_exists ($notes_file)) {
92- return $notes ;
99+ return [] ;
93100 }
101+ $notes = [];
94102 if ($fp = @fopen ($notes_file, " r" )) {
95103 while (!feof ($fp)) {
96104 $line = chop (fgets ($fp, 12288 ));
97105 if ($line == " " ) { continue ; }
98106 @list ($id, $sect, $rate, $ts, $user, $note, $up, $down) = explode (" |" , $line);
99- $notes[$id] = [
100- " id" => $id,
101- " sect" => $sect,
102- " rate" => $rate,
103- " xwhen" => $ts,
104- " user" => $user,
105- " note" => base64_decode ($note, true ),
106- " votes" => [" up" => (int )$up, " down" => (int )$down]
107- ];
107+ $notes[$id] = new UserNote ($id, $sect, $rate, $ts, $user, base64_decode ($note, true ), (int ) $up, (int ) $down);
108108 }
109109 fclose ($fp);
110110 }
111111 return $notes;
112112}
113113
114114// Print out one user note entry
115- function manual_note_display ($date, $name, $text, $id, $votes = [ ' up ' => 0 , ' down ' => 0 ] , $voteOption = true )
115+ function manual_note_display (UserNote $note , $voteOption = true )
116116{
117- if ($name ) {
118- $name = " \n <strong class=\" user\" ><em>" . htmlspecialchars ($name ) . " </em></strong>" ;
117+ if ($note-> user ) {
118+ $name = " \n <strong class=\" user\" ><em>" . htmlspecialchars ($note-> user ) . " </em></strong>" ;
119119 } else {
120120 $name = " <strong class=\" user\" ><em>Anonymous</em></strong>" ;
121121 }
122- $name = ($id ? " \n <a href=\" #$id \" class=\" name\" >$name</a><a class=\" genanchor\" href=\" #$id \" > ¶</a>" : " \n $name" );
122+ $name = ($note-> id ? " \n <a href=\" #{$note->id} \" class=\" name\" >$name</a><a class=\" genanchor\" href=\" #{$note->id} \" > ¶</a>" : " \n $name" );
123123
124124 // New date style will be relative time
125- $datestr = relTime (new DateTime (" @{$date}" ));
126- $fdatestr = date (" Y-m-d h:i" , $date);
127- $text = clean_note ($text);
125+ $date = new DateTime (" @{$note->ts}" );
126+ $datestr = relTime ($date);
127+ $fdatestr = $date->format (" Y-m-d h:i" );
128+ $text = clean_note ($note->text );
128129
129130 // Calculate note rating by up/down votes
130- $vote = $votes[ ' up ' ] - $votes[ ' down ' ] ;
131- $p = floor (($votes[ ' up ' ] / (($votes[ ' up ' ] + $votes[ ' down ' ] ) ?: 1 )) * 100 );
132- $rate = !$p && !($votes[ ' up ' ] + $votes[ ' down ' ] ) ? " no votes..." : " $p% like this..." ;
131+ $vote = $note-> upvotes - $note-> downvotes ;
132+ $p = floor (($note-> upvotes / (($note-> upvotes + $note-> downvotes ) ?: 1 )) * 100 );
133+ $rate = !$p && !($note-> upvotes + $note-> downvotes ) ? " no votes..." : " $p% like this..." ;
133134
134135 // Vote User Notes Div
135136 if ($voteOption) {
@@ -140,13 +141,13 @@ function manual_note_display($date, $name, $text, $id, $votes = ['up' => 0, 'dow
140141 $rredir_filename = urlencode ($redir_filename);
141142 $votediv = <<<VOTEDIV
142143 <div class =" votes" >
143- <div id=" Vu{$id}" >
144- <a href=" /manual/vote-note.php?id={$id}&page={$rredir_filename}&vote=up" title=" Vote up!" class =" usernotes-voteu" >up</a>
144+ <div id=" Vu{$note-> id}" >
145+ <a href=" /manual/vote-note.php?id={$note-> id}&page={$rredir_filename}&vote=up" title=" Vote up!" class =" usernotes-voteu" >up</a>
145146 </div>
146- <div id=" Vd{$id}" >
147- <a href=" /manual/vote-note.php?id={$id}&page={$rredir_filename}&vote=down" title=" Vote down!" class =" usernotes-voted" >down</a>
147+ <div id=" Vd{$note-> id}" >
148+ <a href=" /manual/vote-note.php?id={$note-> id}&page={$rredir_filename}&vote=down" title=" Vote down!" class =" usernotes-voted" >down</a>
148149 </div>
149- <div class =" tally" id=" V{$id}" title=" {$rate}" >
150+ <div class =" tally" id=" V{$note-> id}" title=" {$rate}" >
150151 {$vote}
151152 </div>
152153 </div>
@@ -156,26 +157,26 @@ VOTEDIV;
156157 }
157158
158159 // If the viewer is logged in, show admin options
159- if (isset ($_COOKIE[' IS_DEV' ]) && $id) {
160+ if (isset ($_COOKIE[' IS_DEV' ]) && $note-> id ) {
160161
161162 $admin = " \n <span class=\" admin\" >\n " .
162163
163164 make_popup_link (
164- ' https://main.php.net/manage/user-notes.php?action=edit+' . $id,
165+ ' https://main.php.net/manage/user-notes.php?action=edit+' . $note-> id ,
165166 ' <img src="/images/notes-edit@2x.png" height="12" width="12" alt="edit note">' ,
166167 ' admin' ,
167168 ' scrollbars=yes,width=650,height=400'
168169 ) . " \n " .
169170
170171 make_popup_link (
171- ' https://main.php.net/manage/user-notes.php?action=reject+' . $id,
172+ ' https://main.php.net/manage/user-notes.php?action=reject+' . $note-> id ,
172173 ' <img src="/images/notes-reject@2x.png" height="12" width="12" alt="reject note">' ,
173174 ' admin' ,
174175 ' scrollbars=no,width=300,height=200'
175176 ) . " \n " .
176177
177178 make_popup_link (
178- ' https://main.php.net/manage/user-notes.php?action=delete+' . $id,
179+ ' https://main.php.net/manage/user-notes.php?action=delete+' . $note-> id ,
179180 ' <img src="/images/notes-delete@2x.png" height="12" width="12" alt="delete note">' ,
180181 ' admin' ,
181182 ' scrollbars=no,width=300,height=200'
@@ -187,8 +188,8 @@ VOTEDIV;
187188
188189 echo <<<USER_NOTE_TEXT
189190
190- <div class =" note" id=" $id " >{$votediv}{$name}{$admin}<div class =" date" title=" $fdatestr" ><strong>{$datestr}</strong></div>
191- <div class =" text" id=" Hcom{$id}" >
191+ <div class =" note" id=" {$note->id} " >{$votediv}{$name}{$admin}<div class =" date" title=" $fdatestr" ><strong>{$datestr}</strong></div>
192+ <div class =" text" id=" Hcom{$note-> id}" >
192193{$text}
193194 </div>
194195 </div>
@@ -295,7 +296,7 @@ function manual_setup($setup) {
295296 $USERNOTES = manual_notes_load ($filename);
296297 if ($USERNOTES) {
297298 $note = current ($USERNOTES);
298- $timestamps[] = $note[ " xwhen " ] ;
299+ $timestamps[] = $note-> ts ;
299300 }
300301
301302 $lastmod = max ($timestamps);
0 commit comments