@@ -23,7 +23,185 @@ $PGI = []; $SIDEBAR_DATA = '';
2323// =============================================================================
2424
2525use phpweb \I18n \Languages ;
26- use phpweb \UserNotes \UserNoteService ;
26+ use phpweb \UserNotes \Sorter ;
27+ use phpweb \UserNotes \UserNote ;
28+
29+ /**
30+ * Print out all user notes for this manual page
31+ *
32+ * @param array<string, UserNote> $notes
33+ */
34+ function manual_notes ($ notes ):void {
35+ global $ LANG ;
36+
37+ // Get needed values
38+ list ($ filename ) = $ GLOBALS ['PGI ' ]['this ' ];
39+
40+ // Drop file extension from the name
41+ if (substr ($ filename , -4 ) == '.php ' ) {
42+ $ filename = substr ($ filename , 0 , -4 );
43+ }
44+
45+ $ sorter = new Sorter ();
46+ $ sorter ->sort ($ notes );
47+
48+ $ repo = strtolower ($ LANG );
49+ $ addNote = autogen ('add_a_note ' , $ LANG );
50+ // Link target to add a note to the current manual page,
51+ // and it's extended form with a [+] image
52+ $ addnotelink = '/manual/add-note.php?sect= ' . $ filename .
53+ '&repo= ' . $ repo .
54+ '&redirect= ' . $ _SERVER ['BASE_HREF ' ];
55+ $ addnotesnippet = make_link (
56+ $ addnotelink ,
57+ "+<small> $ addNote</small> " ,
58+ );
59+
60+ $ num_notes = count ($ notes );
61+ $ noteCountHtml = '' ;
62+ if ($ num_notes ) {
63+ $ noteCountHtml = "<span class= \"count \"> $ num_notes note " . ($ num_notes == 1 ? '' : 's ' ) . "</span> " ;
64+ }
65+
66+ $ userContributedNotes = autogen ('user_contributed_notes ' , $ LANG );
67+ echo <<<END_USERNOTE_HEADER
68+ <section id="usernotes">
69+ <div class="head">
70+ <span class="action"> {$ addnotesnippet }</span>
71+ <h3 class="title"> $ userContributedNotes {$ noteCountHtml }</h3>
72+ </div>
73+ END_USERNOTE_HEADER ;
74+
75+ // If we have no notes, then inform the user
76+ if ($ num_notes === 0 ) {
77+ $ noUserContributedNotes = autogen ('no_user_notes ' , $ LANG );
78+ echo "\n <div class= \"note \"> $ noUserContributedNotes</div> " ;
79+ } else {
80+ // If we have notes, print them out
81+ echo '<div id="allnotes"> ' ;
82+ foreach ($ notes as $ note ) {
83+ manual_note_display ($ note );
84+ }
85+ echo "</div> \n" ;
86+ echo "<div class= \"foot \"> $ addnotesnippet</div> \n" ;
87+ }
88+ echo "</section> " ;
89+ }
90+
91+ /**
92+ * Get user notes from the appropriate text dump
93+ *
94+ * @return array<string, UserNote>
95+ */
96+ function manual_notes_load (string $ id ): array
97+ {
98+ $ hash = substr (md5 ($ id ), 0 , 16 );
99+ $ notes_file = $ _SERVER ['DOCUMENT_ROOT ' ] . "/backend/notes/ " .
100+ substr ($ hash , 0 , 2 ) . "/ $ hash " ;
101+
102+ // Open the note file for reading and get the data (12KB)
103+ // ..if it exists
104+ if (!file_exists ($ notes_file )) {
105+ return [];
106+ }
107+ $ notes = [];
108+ if ($ fp = @fopen ($ notes_file , "r " )) {
109+ while (!feof ($ fp )) {
110+ $ line = chop (fgets ($ fp , 12288 ));
111+ if ($ line == "" ) { continue ; }
112+ @list ($ id , $ sect , $ rate , $ ts , $ user , $ note , $ up , $ down ) = explode ("| " , $ line );
113+ $ notes [$ id ] = new UserNote ($ id , $ sect , $ rate , $ ts , $ user , base64_decode ($ note , true ), (int ) $ up , (int ) $ down );
114+ }
115+ fclose ($ fp );
116+ }
117+ return $ notes ;
118+ }
119+
120+ // Print out one user note entry
121+ function manual_note_display (UserNote $ note , $ voteOption = true ): void
122+ {
123+ if ($ note ->user ) {
124+ $ name = "\n <strong class= \"user \"><em> " . htmlspecialchars ($ note ->user ) . "</em></strong> " ;
125+ } else {
126+ $ name = "<strong class= \"user \"><em>Anonymous</em></strong> " ;
127+ }
128+ $ name = ($ note ->id ? "\n <a href= \"# {$ note ->id }\" class= \"name \"> $ name</a><a class= \"genanchor \" href= \"# {$ note ->id }\"> ¶</a> " : "\n $ name " );
129+
130+ // New date style will be relative time
131+ $ date = new DateTime ("@ {$ note ->ts }" );
132+ $ datestr = relTime ($ date );
133+ $ fdatestr = $ date ->format ("Y-m-d h:i " );
134+ $ text = clean_note ($ note ->text );
135+
136+ // Calculate note rating by up/down votes
137+ $ vote = $ note ->upvotes - $ note ->downvotes ;
138+ $ p = floor (($ note ->upvotes / (($ note ->upvotes + $ note ->downvotes ) ?: 1 )) * 100 );
139+ $ rate = !$ p && !($ note ->upvotes + $ note ->downvotes ) ? "no votes... " : "$ p% like this... " ;
140+
141+ // Vote User Notes Div
142+ if ($ voteOption ) {
143+ list ($ redir_filename ) = $ GLOBALS ['PGI ' ]['this ' ];
144+ if (substr ($ redir_filename , -4 ) == '.php ' ) {
145+ $ redir_filename = substr ($ redir_filename , 0 , -4 );
146+ }
147+ $ rredir_filename = urlencode ($ redir_filename );
148+ $ votediv = <<<VOTEDIV
149+ <div class="votes">
150+ <div id="Vu {$ note ->id }">
151+ <a href="/manual/vote-note.php?id= {$ note ->id }&page= {$ rredir_filename }&vote=up" title="Vote up!" class="usernotes-voteu">up</a>
152+ </div>
153+ <div id="Vd {$ note ->id }">
154+ <a href="/manual/vote-note.php?id= {$ note ->id }&page= {$ rredir_filename }&vote=down" title="Vote down!" class="usernotes-voted">down</a>
155+ </div>
156+ <div class="tally" id="V {$ note ->id }" title=" {$ rate }">
157+ {$ vote }
158+ </div>
159+ </div>
160+ VOTEDIV ;
161+ } else {
162+ $ votediv = null ;
163+ }
164+
165+ // If the viewer is logged in, show admin options
166+ if (isset ($ _COOKIE ['IS_DEV ' ]) && $ note ->id ) {
167+
168+ $ admin = "\n <span class= \"admin \"> \n " .
169+
170+ make_popup_link (
171+ 'https://main.php.net/manage/user-notes.php?action=edit+ ' . $ note ->id ,
172+ '<img src="/images/notes-edit@2x.png" height="12" width="12" alt="edit note"> ' ,
173+ 'admin ' ,
174+ 'scrollbars=yes,width=650,height=400 ' ,
175+ ) . "\n " .
176+
177+ make_popup_link (
178+ 'https://main.php.net/manage/user-notes.php?action=reject+ ' . $ note ->id ,
179+ '<img src="/images/notes-reject@2x.png" height="12" width="12" alt="reject note"> ' ,
180+ 'admin ' ,
181+ 'scrollbars=no,width=300,height=200 ' ,
182+ ) . "\n " .
183+
184+ make_popup_link (
185+ 'https://main.php.net/manage/user-notes.php?action=delete+ ' . $ note ->id ,
186+ '<img src="/images/notes-delete@2x.png" height="12" width="12" alt="delete note"> ' ,
187+ 'admin ' ,
188+ 'scrollbars=no,width=300,height=200 ' ,
189+ ) . "\n </span> " ;
190+
191+ } else {
192+ $ admin = '' ;
193+ }
194+
195+ echo <<<USER_NOTE_TEXT
196+
197+ <div class="note" id=" {$ note ->id }"> {$ votediv }{$ name }{$ admin }<div class="date" title=" $ fdatestr"><strong> {$ datestr }</strong></div>
198+ <div class="text" id="Hcom {$ note ->id }">
199+ {$ text }
200+ </div>
201+ </div>
202+ USER_NOTE_TEXT ;
203+
204+ }
27205
28206function manual_navigation_breadcrumbs (array $ setup ) {
29207 $ menu = [];
@@ -120,9 +298,7 @@ function manual_setup($setup): void {
120298 if (substr ($ filename , -4 ) == '.php ' ) {
121299 $ filename = substr ($ filename , 0 , -4 );
122300 }
123-
124- $ userNoteService = new UserNoteService ();
125- $ USERNOTES = $ userNoteService ->load ($ filename );
301+ $ USERNOTES = manual_notes_load ($ filename );
126302 if ($ USERNOTES ) {
127303 $ note = current ($ USERNOTES );
128304 $ timestamps [] = $ note ->ts ;
@@ -246,14 +422,35 @@ function manual_footer($setup): void {
246422 </div>
247423CONTRIBUTE ;
248424
249- $ userNoteService = new UserNoteService ();
250- $ userNoteService ->display ($ USERNOTES );
425+ manual_notes ($ USERNOTES );
251426 site_footer ([
252427 'related_menu ' => $ __RELATED ['toc ' ],
253428 'related_menu_deprecated ' => $ __RELATED ['toc_deprecated ' ],
254429 ]);
255430}
256431
432+ // This function takes a DateTime object and returns a formated string of the time difference relative to now
433+ function relTime (DateTime $ date ) {
434+ $ current = new DateTime ();
435+ $ diff = $ current ->diff ($ date );
436+ $ units = ["year " => $ diff ->format ("%y " ),
437+ "month " => $ diff ->format ("%m " ),
438+ "day " => $ diff ->format ("%d " ),
439+ "hour " => $ diff ->format ("%h " ),
440+ "minute " => $ diff ->format ("%i " ),
441+ "second " => $ diff ->format ("%s " ),
442+ ];
443+ $ out = "just now... " ;
444+ foreach ($ units as $ unit => $ amount ) {
445+ if (empty ($ amount )) {
446+ continue ;
447+ }
448+ $ out = $ amount . " " . ($ amount == 1 ? $ unit : $ unit . "s " ) . " ago " ;
449+ break ;
450+ }
451+ return $ out ;
452+ }
453+
257454function contributors ($ setup ) {
258455 if (!isset ($ _GET ["contributors " ])
259456 || !isset ($ setup ["history " ]["contributors " ])
@@ -280,6 +477,7 @@ function autogen(string $text, string $lang) {
280477 static $ translations = [];
281478
282479 $ lang = ($ lang === "" ) ? "en " : $ lang ;
480+ $ lang = strtolower ($ lang );
283481 if (isset ($ translations [$ lang ])) {
284482 if (isset ($ translations [$ lang ][$ text ]) && $ translations [$ lang ][$ text ] !== "" ) {
285483 return $ translations [$ lang ][$ text ];
0 commit comments