@@ -24,178 +24,7 @@ $PGI = []; $SIDEBAR_DATA = '';
2424
2525require_once __DIR__ . ' /../autoload.php' ;
2626
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 {
36- // Get needed values
37- list ($filename) = $GLOBALS[' PGI' ][' this' ];
38-
39- // Drop file extension from the name
40- if (substr ($filename, -4 ) == ' .php' ) {
41- $filename = substr ($filename, 0 , -4 );
42- }
43-
44- $sorter = new Sorter;
45- $sorter->sort ($notes);
46-
47- // Link target to add a note to the current manual page,
48- // and it's extended form with a [+] image
49- $addnotelink = ' /manual/add-note.php?sect=' . $filename .
50- ' &redirect=' . $_SERVER[' BASE_HREF' ];
51- $addnotesnippet = make_link (
52- $addnotelink,
53- " +<small>add a note</small>"
54- );
55-
56- $num_notes = count ($notes);
57- $noteCountHtml = ' ' ;
58- if ($num_notes) {
59- $noteCountHtml = " <span class=\" count\" >$num_notes note" . ($num_notes == 1 ? ' ' : ' s' ) . " </span>" ;
60- }
61-
62- echo <<<END_USERNOTE_HEADER
63- <section id=" usernotes" >
64- <div class =" head" >
65- <span class =" action" >{$addnotesnippet}</span>
66- <h3 class =" title" >User Contributed Notes {$noteCountHtml}</h3>
67- </div>
68- END_USERNOTE_HEADER;
69-
70- // If we have no notes, then inform the user
71- if ($num_notes === 0 ) {
72- echo " \n <div class=\" note\" >There are no user contributed notes for this page.</div>" ;
73- } else {
74- // If we have notes, print them out
75- echo ' <div id="allnotes">' ;
76- foreach ($notes as $note) {
77- manual_note_display ($note);
78- }
79- echo " </div>\n " ;
80- echo " <div class=\" foot\" >$addnotesnippet</div>\n " ;
81- }
82- echo " </section>" ;
83- }
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
91- {
92- $hash = substr (md5 ($id), 0 , 16 );
93- $notes_file = $_SERVER[' DOCUMENT_ROOT' ] . " /backend/notes/" .
94- substr ($hash, 0 , 2 ) . " /$hash" ;
95-
96- // Open the note file for reading and get the data (12KB)
97- // ..if it exists
98- if (!file_exists ($notes_file)) {
99- return [];
100- }
101- $notes = [];
102- if ($fp = @fopen ($notes_file, " r" )) {
103- while (!feof ($fp)) {
104- $line = chop (fgets ($fp, 12288 ));
105- if ($line == " " ) { continue ; }
106- @list ($id, $sect, $rate, $ts, $user, $note, $up, $down) = explode (" |" , $line);
107- $notes[$id] = new UserNote ($id, $sect, $rate, $ts, $user, base64_decode ($note, true ), (int ) $up, (int ) $down);
108- }
109- fclose ($fp);
110- }
111- return $notes;
112- }
113-
114- // Print out one user note entry
115- function manual_note_display (UserNote $note, $voteOption = true )
116- {
117- if ($note->user ) {
118- $name = " \n <strong class=\" user\" ><em>" . htmlspecialchars ($note->user ) . " </em></strong>" ;
119- } else {
120- $name = " <strong class=\" user\" ><em>Anonymous</em></strong>" ;
121- }
122- $name = ($note->id ? " \n <a href=\" #{$note->id}\" class=\" name\" >$name</a><a class=\" genanchor\" href=\" #{$note->id}\" > ¶</a>" : " \n $name" );
123-
124- // New date style will be relative time
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 );
129-
130- // Calculate note rating by up/down votes
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..." ;
134-
135- // Vote User Notes Div
136- if ($voteOption) {
137- list ($redir_filename) = $GLOBALS[' PGI' ][' this' ];
138- if (substr ($redir_filename, -4 ) == ' .php' ) {
139- $redir_filename = substr ($redir_filename, 0 , -4 );
140- }
141- $rredir_filename = urlencode ($redir_filename);
142- $votediv = <<<VOTEDIV
143- <div class =" votes" >
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>
146- </div>
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>
149- </div>
150- <div class =" tally" id=" V{$note->id}" title=" {$rate}" >
151- {$vote}
152- </div>
153- </div>
154- VOTEDIV;
155- } else {
156- $votediv = null;
157- }
158-
159- // If the viewer is logged in, show admin options
160- if (isset ($_COOKIE[' IS_DEV' ]) && $note->id ) {
161-
162- $admin = " \n <span class=\" admin\" >\n " .
163-
164- make_popup_link (
165- ' https://main.php.net/manage/user-notes.php?action=edit+' . $note->id ,
166- ' <img src="/images/notes-edit@2x.png" height="12" width="12" alt="edit note">' ,
167- ' admin' ,
168- ' scrollbars=yes,width=650,height=400'
169- ) . " \n " .
170-
171- make_popup_link (
172- ' https://main.php.net/manage/user-notes.php?action=reject+' . $note->id ,
173- ' <img src="/images/notes-reject@2x.png" height="12" width="12" alt="reject note">' ,
174- ' admin' ,
175- ' scrollbars=no,width=300,height=200'
176- ) . " \n " .
177-
178- make_popup_link (
179- ' https://main.php.net/manage/user-notes.php?action=delete+' . $note->id ,
180- ' <img src="/images/notes-delete@2x.png" height="12" width="12" alt="delete note">' ,
181- ' admin' ,
182- ' scrollbars=no,width=300,height=200'
183- ) . " \n </span>" ;
184-
185- } else {
186- $admin = ' ' ;
187- }
188-
189- echo <<<USER_NOTE_TEXT
190-
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}" >
193- {$text}
194- </div>
195- </div>
196- USER_NOTE_TEXT;
197-
198- }
27+ use phpweb\UserNotes\UserNoteService;
19928
20029function manual_navigation_breadcrumbs (array $setup) {
20130 $menu = [];
@@ -293,7 +122,9 @@ function manual_setup($setup) {
293122 if (substr ($filename, -4 ) == ' .php' ) {
294123 $filename = substr ($filename, 0 , -4 );
295124 }
296- $USERNOTES = manual_notes_load ($filename);
125+
126+ $userNoteService = new UserNoteService ();
127+ $USERNOTES = $userNoteService->load ($filename);
297128 if ($USERNOTES) {
298129 $note = current ($USERNOTES);
299130 $timestamps[] = $note->ts ;
@@ -384,34 +215,13 @@ CHANGE_LANG;
384215function manual_footer () {
385216 global $USERNOTES, $__RELATED;
386217
387- manual_notes ($USERNOTES);
218+ $userNoteService = new UserNoteService ();
219+ $userNoteService->display ($USERNOTES);
388220 $config = [
389221 ' related_menu' => $__RELATED[' toc' ],
390222 ' related_menu_deprecated' => $__RELATED[' toc_deprecated' ]
391223 ];
392224 site_footer ($config);
393225}
394226
395- // This function takes a DateTime object and returns a formated string of the time difference relative to now
396- function relTime (DateTime $date) {
397- $current = new DateTime;
398- $diff = $current->diff ($date);
399- $units = [" year" => $diff->format (" %y" ),
400- " month" => $diff->format (" %m" ),
401- " day" => $diff->format (" %d" ),
402- " hour" => $diff->format (" %h" ),
403- " minute" => $diff->format (" %i" ),
404- " second" => $diff->format (" %s" ),
405- ];
406- $out = " just now..." ;
407- foreach ($units as $unit => $amount) {
408- if (empty ($amount)) {
409- continue ;
410- }
411- $out = $amount . " " . ($amount == 1 ? $unit : $unit . " s" ) . " ago" ;
412- break ;
413- }
414- return $out;
415- }
416-
417227/* vim: set et ts=4 sw=4: */
0 commit comments