Skip to content

Commit

Permalink
Extract making of user links from userwatchlist.jsp to ParserUtils. U…
Browse files Browse the repository at this point in the history
…nit test it.
  • Loading branch information
MER-C committed Oct 6, 2017
1 parent acd2969 commit e551939
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 9 deletions.
1 change: 1 addition & 0 deletions pom.xml
Expand Up @@ -59,6 +59,7 @@
<configuration>
<includes>
<include>**/WikiUnitTest.java</include>
<include>**/ParserUtilsUnitTest.java</include>
<include>**/UserLinkAdditionFinderUnitTest.java</include>
</includes>
</configuration>
Expand Down
19 changes: 19 additions & 0 deletions src/org/wikipedia/ParserUtils.java
Expand Up @@ -21,6 +21,7 @@

import java.time.format.DateTimeFormatter;
import java.util.*;
import org.wikipedia.servlets.ServletUtils;

/**
* Various parsing methods that e.g. turning Wiki.java objects into wikitext
Expand Down Expand Up @@ -357,6 +358,24 @@ public static String linksearchResultsToHTML(List[] results, Wiki wiki, String d
return buffer.toString();
}

/**
* Creates user links in HTML of the form <tt>User (talk | contribs |
* deletedcontribs | block | block log)</tt>
* @param username the username, NOT sanitized for XSS
* @returned the generated HTML
*/
public static String generateUserLinks(Wiki wiki, String username)
{
String domain = wiki.getDomain();
String userenc = ServletUtils.sanitizeForURL(username.replace(' ', '_'));
return "<a href=\"//" + domain + "/wiki/User:" + userenc + "\">" + username + "</a> ("
+ "<a href=\"//" + domain + "/wiki/User_talk:" + userenc + "\">talk</a> | "
+ "<a href=\"//" + domain + "/wiki/Special:Contributions/" + userenc + "\">contribs</a> | "
+ "<a href=\"//" + domain + "/wiki/Special:DeletedContributions/" + userenc + "\">deleted contribs</a> | "
+ "<a href=\"//" + domain + "/wiki/Special:Block/" + userenc + "\">block</a> | "
+ "<a href=\"//" + domain + "/w/index.php?title=Special:Log&type=block&page=User:" + userenc + "\">block log</a>)";
}

/**
* Reverse of Wiki.decode()
* @param in input string
Expand Down
11 changes: 2 additions & 9 deletions src/org/wikipedia/servlets/userwatchlist.jsp
Expand Up @@ -170,21 +170,14 @@ Someone # Spam
{
String user = entry.getKey();
String reason = ServletUtils.sanitizeForHTML(entry.getValue());
String userenc = ServletUtils.sanitizeForURL(user);
// user links
%>
<h3><%= user %></h3>
<p>
<ul>
<li><a href="//en.wikipedia.org/wiki/User:<%= userenc %>"><%= user %></a> |
<a href="//en.wikipedia.org/wiki/User_talk:<%= userenc %>">talk</a> |
<a href="//en.wikipedia.org/wiki/Special:Contributions/<%= userenc %>">contribs</a> |
<a href="//en.wikipedia.org/wiki/Special:DeletedContributions/<%= userenc %>">deleted contribs</a> |
<a href="//en.wikipedia.org/wiki/Special:Block/<%= userenc %>">block</a> |
<a href="//en.wikipedia.org/w/index.php?title=Special:Log&type=block&page=User:<%= userenc %>">block log</a>

<li>
<%
out.println(ParserUtils.generateUserLinks(enWiki, user));
if (!reason.isEmpty())
out.println("<li><i>" + reason + "</i>");
out.println("</ul>");
Expand Down
69 changes: 69 additions & 0 deletions test/org/wikipedia/ParserUtilsUnitTest.java
@@ -0,0 +1,69 @@
/**
* @(#)ParserUtilsUnitTest.java 0.31 05/10/2017
* Copyright (C) 2017 MER-C
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version. Additionally
* this file is subject to the "Classpath" exception.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package org.wikipedia;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

/**
* Unit tests for org.wikipedia.ParserUtils
* @author MER-C
*/
public class ParserUtilsUnitTest
{
private static Wiki testWiki = Wiki.createInstance("test.wikipedia.org");

/**
* Initialize wiki objects.
* @throws Exception if a network error occurs
*/
@BeforeClass
public static void setUpClass() throws Exception
{
testWiki.setMaxLag(-1);
}

@Test
public void generateUserLinks() throws Exception
{
String expected =
"<a href=\"//test.wikipedia.org/wiki/User:MER-C\">MER-C</a> ("
+ "<a href=\"//test.wikipedia.org/wiki/User_talk:MER-C\">talk</a> | "
+ "<a href=\"//test.wikipedia.org/wiki/Special:Contributions/MER-C\">contribs</a> | "
+ "<a href=\"//test.wikipedia.org/wiki/Special:DeletedContributions/MER-C\">deleted contribs</a> | "
+ "<a href=\"//test.wikipedia.org/wiki/Special:Block/MER-C\">block</a> | "
+ "<a href=\"//test.wikipedia.org/w/index.php?title=Special:Log&type=block&page=User:MER-C\">block log</a>)";
assertEquals("generateUserLinks", expected, ParserUtils.generateUserLinks(testWiki, "MER-C"));

expected = "<a href=\"//test.wikipedia.org/wiki/User:A_B_%E3%81%AE\">A B の</a> ("
+ "<a href=\"//test.wikipedia.org/wiki/User_talk:A_B_%E3%81%AE\">talk</a> | "
+ "<a href=\"//test.wikipedia.org/wiki/Special:Contributions/A_B_%E3%81%AE\">contribs</a> | "
+ "<a href=\"//test.wikipedia.org/wiki/Special:DeletedContributions/A_B_%E3%81%AE\">deleted contribs</a> | "
+ "<a href=\"//test.wikipedia.org/wiki/Special:Block/A_B_%E3%81%AE\">block</a> | "
+ "<a href=\"//test.wikipedia.org/w/index.php?title=Special:Log&type=block&page=User:A_B_%E3%81%AE\">block log</a>)";
assertEquals("generateUserLinks: special characters", expected, ParserUtils.generateUserLinks(testWiki, "A B の"));
}

}

0 comments on commit e551939

Please sign in to comment.