Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix online dump downloads for the various modes and Closes #869 #872

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wikidata.wdtk.dumpfiles.DumpContentType;
import org.wikidata.wdtk.util.DirectoryManager;
import org.wikidata.wdtk.util.WebResourceFetcher;
import org.wikidata.wdtk.util.WebResourceFetcherImpl;

public class JsonOnlineDumpFile extends WmfDumpFile {

Expand Down Expand Up @@ -115,9 +119,22 @@ public void prepareDumpFile() throws IOException {

@Override
protected boolean fetchIsDone() {
// WMF provides no easy way to check this for these files;
// so just assume it is done
return true;
boolean result = false;
String fileName = WmfDumpFile.getDumpFileName(DumpContentType.JSON,
this.projectName, this.dateStamp);
String urlString = getBaseUrl() + fileName;
// make a head request to check if resource exists
try {
HttpURLConnection connection = (HttpURLConnection) WebResourceFetcherImpl.getUrlConnection(new URL(urlString));
connection.setRequestMethod("HEAD");
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
result = true;
}
} catch (IOException e) { // file not found or not readable
result = false;
}
return result;
}

/**
Expand All @@ -127,7 +144,7 @@ protected boolean fetchIsDone() {
*/
String getBaseUrl() {
return WmfDumpFile.getDumpFileWebDirectory(DumpContentType.JSON,
this.projectName);
this.projectName) + this.dateStamp + "/";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public abstract class WmfDumpFile implements MwDumpFile {
WmfDumpFile.WEB_DIRECTORY.put(DumpContentType.CURRENT, "");
WmfDumpFile.WEB_DIRECTORY.put(DumpContentType.FULL, "");
WmfDumpFile.WEB_DIRECTORY.put(DumpContentType.SITES, "");
WmfDumpFile.WEB_DIRECTORY.put(DumpContentType.JSON, "other/");
WmfDumpFile.WEB_DIRECTORY.put(DumpContentType.JSON, "");
}

/**
Expand All @@ -68,7 +68,7 @@ public abstract class WmfDumpFile implements MwDumpFile {
WmfDumpFile.POSTFIXES.put(DumpContentType.FULL,
"-pages-meta-history.xml.bz2");
WmfDumpFile.POSTFIXES.put(DumpContentType.SITES, "-sites.sql.gz");
WmfDumpFile.POSTFIXES.put(DumpContentType.JSON, ".json.gz");
WmfDumpFile.POSTFIXES.put(DumpContentType.JSON, "-all.json.gz");
}

/**
Expand Down Expand Up @@ -172,7 +172,7 @@ public static String getDumpFileWebDirectory(
if ("wikidatawiki".equals(projectName)) {
return WmfDumpFile.DUMP_SITE_BASE_URL
+ WmfDumpFile.WEB_DIRECTORY.get(dumpContentType)
+ "wikidata" + "/";
+ projectName + "/entities/";
} else {
throw new RuntimeException(
"Wikimedia Foundation uses non-systematic directory names for this type of dump file."
Expand Down Expand Up @@ -253,7 +253,7 @@ public static String getDateStampFromDumpFileDirectoryName(
public static String getDumpFileName(DumpContentType dumpContentType,
String projectName, String dateStamp) {
if (dumpContentType == DumpContentType.JSON) {
return dateStamp + WmfDumpFile.getDumpFilePostfix(dumpContentType);
return "wikidata-" + dateStamp + WmfDumpFile.getDumpFilePostfix(dumpContentType);
} else {
return projectName + "-" + dateStamp
+ WmfDumpFile.getDumpFilePostfix(dumpContentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ protected boolean fetchIsDone() {
new InputStreamReader(in, StandardCharsets.UTF_8));
String inputLine = bufferedReader.readLine();
bufferedReader.close();
result = "done".equals(inputLine);
result = inputLine.startsWith("done");
} catch (IOException e) { // file not found or not readable
result = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.wikidata.wdtk.testing.MockDirectoryManager;
import org.wikidata.wdtk.testing.MockStringContentFactory;
import org.wikidata.wdtk.testing.MockWebResourceFetcher;
import org.wikidata.wdtk.util.WebResourceFetcherImpl;

public class WmfDumpFileManagerTest {

Expand All @@ -61,7 +62,8 @@ public void processDumpFileContents(InputStream inputStream,
try {
result = result
+ MockStringContentFactory
.getStringFromInputStream(inputStream) + "\n";
.getStringFromInputStream(inputStream)
+ "\n";
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -146,33 +148,32 @@ public void getAllDailyDumps() throws IOException {

@Test
public void getAllJsonDumps() throws IOException {
wrf.setWebResourceContentsFromResource(
"https://dumps.wikimedia.org/other/wikidata/",
"/other-wikidata-index.html", this.getClass());
WebResourceFetcherImpl onlineWrf = new WebResourceFetcherImpl();

setLocalDump("20141110", DumpContentType.JSON, true);
setLocalDump("20150105", DumpContentType.CURRENT, true);
setLocalDump("20141201", DumpContentType.JSON, true);
setLocalDump("nodate", DumpContentType.JSON, true);

WmfDumpFileManager dumpFileManager = new WmfDumpFileManager(
"wikidatawiki", dm, wrf);
"wikidatawiki", dm, onlineWrf);

List<? extends MwDumpFile> dumpFiles = dumpFileManager
.findAllDumps(DumpContentType.JSON);

String[] dumpDates = { "20150112", "20150105", "20141229", "20141222",
"20141215", "20141210", "20141201", "20141124", "20141117",
"20141110" };
boolean[] dumpIsLocal = { false, false, false, false, false, false,
true, false, false, true };
String[] localDumpDates = { "20141201", "20141110" };

assertEquals(dumpFiles.size(), dumpDates.length);
assertTrue(localDumpDates.length < dumpFiles.size());
for (int i = 0; i < dumpFiles.size(); i++) {
assertEquals(dumpFiles.get(i).getDumpContentType(),
DumpContentType.JSON);
assertEquals(dumpFiles.get(i).getDateStamp(), dumpDates[i]);
if (dumpIsLocal[i]) {
boolean shouldBeLocal = false;
for (String localDumpDate : localDumpDates) {
if (localDumpDate.equals(dumpFiles.get(i).getDateStamp())) {
shouldBeLocal = true;
}
}
if (shouldBeLocal) {
assertTrue(
"Dumpfile " + dumpFiles.get(i) + " should be local.",
dumpFiles.get(i) instanceof WmfLocalDumpFile);
Expand Down