Skip to content

Commit 07afeac

Browse files
committed
*Wiki: introduce new constants to denote RevisionDeleted content.
*WMFWikiFarm: new method to remove all sessions, required for testing purposes. *Increase test coverage. *AllWikiLinksearch: add Wikifunctions as a major wiki.
1 parent bd7a9ed commit 07afeac

File tree

6 files changed

+111
-22
lines changed

6 files changed

+111
-22
lines changed

src/org/wikipedia/WMFWikiFarm.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,14 @@ public Collection<WMFWiki> getAllSharedSessions()
117117
return wikis;
118118
}
119119

120+
/**
121+
* Removes all shared sessions from this object.
122+
*/
123+
public void clear()
124+
{
125+
sessions.clear();
126+
}
127+
120128
/**
121129
* Sets a function that is called every time a WMFWiki session is created
122130
* with this manager. The sole parameter is the new session. Use for a

src/org/wikipedia/Wiki.java

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3952,16 +3952,25 @@ protected Revision parseRevision(String xml, String title)
39523952

39533953
// summary
39543954
String summary = null, parsedsummary = null;
3955+
boolean commentdeleted = xml.contains("commenthidden=\"");
39553956
if (xml.contains("comment=\""))
39563957
{
39573958
summary = parseAttribute(xml, "comment", 0);
39583959
parsedsummary = parseAttribute(xml, "parsedcomment", 0);
39593960
}
3961+
else if (commentdeleted)
3962+
{
3963+
summary = Event.COMMENT_DELETED;
3964+
parsedsummary = Event.COMMENT_DELETED;
3965+
}
39603966

39613967
// user
39623968
String user2 = null;
3969+
boolean userdeleted = xml.contains("userhidden=\"");
39633970
if (xml.contains("user=\""))
39643971
user2 = parseAttribute(xml, "user", 0);
3972+
else if (userdeleted)
3973+
user2 = Event.USER_DELETED;
39653974

39663975
// flags: minor, bot, new
39673976
boolean minor = xml.contains("minor=\"\"");
@@ -3977,10 +3986,18 @@ else if (xml.contains("size=\""))
39773986
else if (xml.contains("len=\"")) // deletedrevs
39783987
size = Integer.parseInt(parseAttribute(xml, "len", 0));
39793988

3980-
// sha1
3989+
// sha1/content
3990+
// Silly workaround: prop=revisions, prop=deletedrevisions,
3991+
// list=recentchanges and list=alldeletedrevisions all don't tell you
3992+
// whether content has been revision deleted until you fetch the content.
3993+
// Instead, fetch the SHA-1 of the content to minimize data transfer.
3994+
// list=usercontribs does tell you
39813995
String sha1 = null;
3996+
boolean contentdeleted = xml.contains("sha1hidden=\"") || xml.contains("texthidden=\"");
39823997
if (xml.contains("sha1=\""))
39833998
sha1 = parseAttribute(xml, "sha1", 0);
3999+
else if (contentdeleted)
4000+
sha1 = Wiki.Event.CONTENT_DELETED;
39844001

39854002
Revision revision = new Revision(oldid, timestamp, user2, summary, parsedsummary, title, sha1, minor, bot, rvnew, size);
39864003
// set rcid
@@ -4009,16 +4026,9 @@ else if (xml.contains("sizediff=\""))
40094026
revision.setTags(tags);
40104027

40114028
// revisiondelete
4012-
revision.setCommentDeleted(xml.contains("commenthidden=\""));
4013-
revision.setUserDeleted(xml.contains("userhidden=\""));
4014-
// Silly workaround: prop=revisions, prop=deletedrevisions,
4015-
// list=recentchanges and list=alldeletedrevisions all don't tell you
4016-
// whether content has been revision deleted until you fetch the content.
4017-
// Instead, fetch the SHA-1 of the content to minimize data transfer.
4018-
revision.setContentDeleted(xml.contains("sha1hidden=\""));
4019-
// list=usercontribs does tell you
4020-
if (xml.contains("texthidden=\""))
4021-
revision.setContentDeleted(true);
4029+
revision.setCommentDeleted(commentdeleted);
4030+
revision.setUserDeleted(userdeleted);
4031+
revision.setContentDeleted(contentdeleted);
40224032
return revision;
40234033
}
40244034

@@ -6070,6 +6080,11 @@ else if (xml.contains("reason=\""))
60706080
reason = parseAttribute(xml, "reason", 0);
60716081
parsedreason = null; // not available in list=blocks / getBlockList!
60726082
}
6083+
else if (reasonhidden)
6084+
{
6085+
reason = Wiki.Event.COMMENT_DELETED;
6086+
parsedreason = Wiki.Event.COMMENT_DELETED;
6087+
}
60736088
else
60746089
{
60756090
reason = parseAttribute(xml, "comment", 0);
@@ -6080,11 +6095,15 @@ else if (xml.contains("reason=\""))
60806095
boolean userhidden = xml.contains("userhidden=\"\"");
60816096
if (user == null && xml.contains("user=\""))
60826097
user = parseAttribute(xml, "user", 0);
6098+
else if (userhidden)
6099+
user = Wiki.Event.USER_DELETED;
60836100

60846101
// generic target name
60856102
// space is important -- commons.getImageHistory("File:Chief1.gif");
60866103
if (target == null && xml.contains(" title=\""))
60876104
target = parseAttribute(xml, "title", 0);
6105+
else if (actionhidden)
6106+
target = Wiki.Event.CONTENT_DELETED;
60886107

60896108
OffsetDateTime timestamp = OffsetDateTime.parse(parseAttribute(xml, "timestamp", 0));
60906109

@@ -6912,6 +6931,33 @@ public abstract class Event implements Comparable<Event>
69126931
private List<String> tags;
69136932
private boolean commentDeleted = false, userDeleted = false,
69146933
contentDeleted = false;
6934+
6935+
/**
6936+
* Placeholder string for the event reason when it is RevisionDeleted
6937+
* without access. Currently null, but proposed to be changed to an
6938+
* illegal value in a future version to distinguish between different
6939+
* types of "comment not available".
6940+
* @since 0.38
6941+
*/
6942+
protected static final String COMMENT_DELETED = null;
6943+
6944+
/**
6945+
* Placeholder string for the event user when it is RevisionDeleted
6946+
* without access. Currently null, but proposed to be changed to an
6947+
* illegal value in a future version to distinguish between different
6948+
* types of "user not available".
6949+
* @since 0.38
6950+
*/
6951+
protected static final String USER_DELETED = null;
6952+
6953+
/**
6954+
* Placeholder string for event content when it is RevisionDeleted
6955+
* without access. Currently null, but proposed to be changed to an
6956+
* illegal value in a future version to distinguish between different
6957+
* types of "content not available".
6958+
* @since 0.38
6959+
*/
6960+
protected static final String CONTENT_DELETED = null;
69156961

69166962
/**
69176963
* Creates a new Event record.

src/org/wikipedia/tools/AllWikiLinksearch.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public class AllWikiLinksearch
6565
/**
6666
* Major Wikimedia projects prone to spam, namely { "en", "de", "fr", "it" }.
6767
* { "wikipedia", "wiktionary", "wikibooks", "wikiquote", "wikivoyage" } .org,
68-
* plus Wikimedia Commons, Meta, mediawiki.org and WikiData.
68+
* plus Wikimedia Commons, Meta, mediawiki.org, WikiData, and Wikifunctions.
6969
*/
7070
public static final List<WMFWiki> MAJOR_WIKIS;
7171

@@ -103,6 +103,7 @@ public class AllWikiLinksearch
103103
wikilist.add(sessions.sharedSession("commons.wikimedia.org"));
104104
wikilist.add(sessions.sharedSession("www.mediawiki.org"));
105105
wikilist.add(sessions.sharedSession("www.wikidata.org"));
106+
wikilist.add(sessions.sharedSession("www.wikifunctions.org"));
106107
MAJOR_WIKIS = Collections.unmodifiableList(wikilist);
107108
}
108109

test/org/wikipedia/WMFWikiFarmTest.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import java.time.OffsetDateTime;
2424
import java.util.*;
25-
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.api.*;
2626
import static org.junit.jupiter.api.Assertions.*;
2727
import org.junit.jupiter.params.ParameterizedTest;
2828
import org.junit.jupiter.params.provider.CsvSource;
@@ -108,6 +108,30 @@ public void instance()
108108
assertEquals(one, sessions);
109109
}
110110

111+
@Test
112+
public void getAllSharedSessions()
113+
{
114+
WMFWiki wiki1 = sessions.sharedSession("en.wikipedia.org");
115+
WMFWiki wiki2 = sessions.sharedSession("de.wikipedia.org");
116+
WMFWiki wiki3 = sessions.sharedSession("fr.wikipedia.org");
117+
Collection<WMFWiki> stuff = sessions.getAllSharedSessions();
118+
assertEquals(3, stuff.size());
119+
assertTrue(stuff.contains(wiki1));
120+
assertTrue(stuff.contains(wiki2));
121+
assertTrue(stuff.contains(wiki3));
122+
}
123+
124+
@Test
125+
public void clear()
126+
{
127+
WMFWiki wiki = sessions.sharedSession("en.wikipedia.org");
128+
Collection<WMFWiki> stuff = sessions.getAllSharedSessions();
129+
assertEquals(1, stuff.size());
130+
sessions.clear();
131+
stuff = sessions.getAllSharedSessions();
132+
assertTrue(stuff.isEmpty());
133+
}
134+
111135
@Test
112136
public void setInitializer()
113137
{
@@ -141,4 +165,10 @@ public void getWikidataItems() throws Exception
141165
assertEquals("Q937", actual.get(5));
142166
assertNull(actual.get(6)); // local page exists, but no corresponding WD item
143167
}
168+
169+
@AfterEach
170+
public void cleanup()
171+
{
172+
sessions.clear();
173+
}
144174
}

test/org/wikipedia/WikiTest.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -681,21 +681,24 @@ public void getLogEntries() throws Exception
681681
// https://test.wikipedia.org/w/api.php?action=query&list=logevents&letitle=User%3AMER-C%2FTest
682682
rh = testWiki.new RequestHelper().byTitle("User:MER-C/Test");
683683
le = testWiki.getLogEntries(Wiki.ALL_LOGS, null, rh);
684-
assertNull(le.get(0).getComment(), "reason hidden");
685-
assertNull(le.get(0).getParsedComment(), "reason hidden");
684+
assertEquals(Wiki.Event.COMMENT_DELETED, le.get(0).getComment(), "reason hidden");
685+
assertEquals(Wiki.Event.COMMENT_DELETED, le.get(0).getParsedComment(), "reason hidden");
686686
assertTrue(le.get(0).isCommentDeleted(), "reason hidden");
687-
assertNull(le.get(0).getUser(), "user hidden");
687+
assertEquals(Wiki.Event.USER_DELETED, le.get(0).getUser(), "user hidden");
688688
assertTrue(le.get(0).isUserDeleted(), "user hidden");
689+
689690
// tags (not related to the LogEntry being RevisionDeleted)
690691
assertEquals(List.of("HotCat", "MyStupidTestTag"), le.get(0).getTags());
692+
693+
// back to RevisionDeleted log entries, no access
691694
// https://test.wikipedia.org/w/api.php?action=query&list=logevents&leuser=MER-C
692695
// &lestart=20161002050030&leend=20161002050000&letype=delete
693696
rh = testWiki.new RequestHelper()
694697
.byUser("MER-C")
695698
.withinDateRange(OffsetDateTime.parse("2016-10-02T05:00:00Z"), OffsetDateTime.parse("2016-10-02T05:30:00Z"));
696699
le = testWiki.getLogEntries(Wiki.DELETION_LOG, null, rh);
697-
assertNull(le.get(1).getTitle(), "action hidden");
698-
assertTrue(le.get(1).isContentDeleted(), "action hidden");
700+
assertEquals(Wiki.Event.CONTENT_DELETED, le.get(1).getTitle(), "target hidden");
701+
assertTrue(le.get(1).isContentDeleted(), "target hidden");
699702
}
700703

701704
@Test
@@ -1106,10 +1109,10 @@ public void getRevision() throws Exception
11061109
assertTrue(rev.isUserDeleted(), "user revdeled flag");
11071110
assertTrue(rev.isCommentDeleted(), "summary revdeled flag");
11081111
assertTrue(rev.isContentDeleted(), "content revdeled flag");
1109-
assertNull(rev.getComment(), "summary revdeled");
1110-
assertNull(rev.getParsedComment(), "summary revdeled");
1111-
assertNull(rev.getUser(), "user revdeled");
1112-
assertNull(rev.getSha1(), "sha1/content revdeled");
1112+
assertEquals(Wiki.Event.COMMENT_DELETED, rev.getComment(), "summary revdeled");
1113+
assertEquals(Wiki.Event.COMMENT_DELETED, rev.getParsedComment(), "summary revdeled");
1114+
assertEquals(Wiki.Event.USER_DELETED, rev.getUser(), "user revdeled");
1115+
assertEquals(Wiki.Event.CONTENT_DELETED, rev.getSha1(), "sha1/content revdeled");
11131116

11141117
// Revision has been deleted (not RevisionDeleted)
11151118
// https://test.wikipedia.org/wiki/User:MER-C/UnitTests/Delete

test/org/wikipedia/tools/ExternalLinkPopularityTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public void getWiki() throws Exception
5454
@Test
5555
public void setMaxLinks() throws Exception
5656
{
57+
assertThrows(IllegalArgumentException.class, () -> elp.setMaxLinks(0));
5758
int limit = 250;
5859
elp.setMaxLinks(limit);
5960
assertEquals(limit, elp.getMaxLinks());

0 commit comments

Comments
 (0)