Skip to content

Commit 6d42d24

Browse files
committed
*Add log entry formatter.
*Wiki: in requests, don't overwrite supplied parameters with the default ones. Fixes #198.
1 parent c87d21c commit 6d42d24

File tree

2 files changed

+104
-3
lines changed

2 files changed

+104
-3
lines changed

src/org/wikipedia/LogEntries.java

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* @(#)LogEntries.java 0.01 02/06/2024
3+
* Copyright (C) 2024-20XX MER-C and contributors
4+
*
5+
* This program is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU General Public License
7+
* as published by the Free Software Foundation; either version 3
8+
* of the License, or (at your option) any later version. Additionally
9+
* this file is subject to the "Classpath" exception.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
21+
package org.wikipedia;
22+
23+
import java.time.format.DateTimeFormatter;
24+
import java.util.*;
25+
26+
/**
27+
* Utility class for {@link Wiki.LogEntry}. For utility methods and data specific
28+
* to {@link Wiki.Revision}s, see {@link Revisions}.
29+
* @author MER-C
30+
* @version 0.01
31+
*/
32+
public class LogEntries
33+
{
34+
/**
35+
* Turns a list of revisions into human-readable wikitext. Be careful, as
36+
* slowness may result when copying large amounts of wikitext produced by
37+
* this method, or by the wiki trying to parse it. Takes the form of:
38+
*
39+
* <p>*2009-01-01 00:00 User (talk | contribs) [action] [target] (comment)
40+
* @param logs a bunch of log entries
41+
* @return those log entries formatted as wikitext
42+
*/
43+
public static String toWikitext(Iterable<Wiki.LogEntry> logs)
44+
{
45+
StringBuilder buffer = new StringBuilder(100000);
46+
buffer.append("<div style=\"font-family: monospace; font-size: 120%\">\n");
47+
for (Wiki.LogEntry log : logs)
48+
{
49+
// timestamp
50+
buffer.append("*");
51+
buffer.append(DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(log.getTimestamp()));
52+
buffer.append(" ");
53+
54+
// user
55+
String user2 = log.getUser();
56+
if (user2 == null || user2.equals(Wiki.Event.USER_DELETED))
57+
buffer.append(Events.DELETED_EVENT_HTML);
58+
else
59+
{
60+
buffer.append(Users.generateWikitextSummaryLinksShort(user2));
61+
buffer.append(" ");
62+
}
63+
64+
// action
65+
buffer.append(log.getAction());
66+
buffer.append(" ");
67+
68+
// target
69+
String target = log.getTitle();
70+
if (target != null)
71+
{
72+
buffer.append("[[:");
73+
buffer.append(target);
74+
buffer.append("]] ");
75+
}
76+
77+
// comment
78+
String summary = log.getComment();
79+
if (summary == null || summary.equals(Wiki.Event.COMMENT_DELETED))
80+
buffer.append(Events.DELETED_EVENT_HTML);
81+
else
82+
{
83+
// kill wikimarkup
84+
buffer.append("(<nowiki>");
85+
buffer.append(summary);
86+
buffer.append("</nowiki>)");
87+
}
88+
89+
// details
90+
Map details = log.getDetails();
91+
if (details != null && !details.isEmpty())
92+
{
93+
buffer.append(" ");
94+
buffer.append(details.toString());
95+
}
96+
buffer.append("\n");
97+
}
98+
buffer.append("</div>");
99+
return buffer.toString();
100+
}
101+
}

src/org/wikipedia/Wiki.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8413,9 +8413,9 @@ public String makeApiCall(Map<String, String> getparams, Map<String, Object> pos
84138413
{
84148414
// build the URL
84158415
StringBuilder urlbuilder = new StringBuilder(apiUrl + "?");
8416-
getparams = new HashMap<>(getparams); // ensure this map is mutable
8417-
getparams.putAll(defaultApiParams);
8418-
for (Map.Entry<String, String> entry : getparams.entrySet())
8416+
HashMap<String, String> getparams2 = new HashMap<>(defaultApiParams);
8417+
getparams2.putAll(getparams);
8418+
for (Map.Entry<String, String> entry : getparams2.entrySet())
84198419
{
84208420
urlbuilder.append(entry.getKey());
84218421
urlbuilder.append('=');

0 commit comments

Comments
 (0)