Skip to content

Instantly share code, notes, and snippets.

@Glorfindel83
Created June 2, 2022 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Glorfindel83/9ec08e9e7690ea7bd89dde409bd1085c to your computer and use it in GitHub Desktop.
Save Glorfindel83/9ec08e9e7690ea7bd89dde409bd1085c to your computer and use it in GitHub Desktop.
// Part of the MSDN blog link repairer code
private static final Pattern MSDN_BLOG_ARTICLE_URL = getBrokenURLPattern(
"blogs\\.msdn\\.com/(b/)?(.*)/archive/\\d+/\\d+/\\d+/(.*)\\.aspx"),
MSDN_BLOG_TAG_URL = getBrokenURLPattern("blogs\\.msdn\\.com/(b/)?([^\\s/]*)/archive/tags/(\\w+)"),
MSDN_BLOG_HOME_URL = getBrokenURLPattern("blogs\\.msdn\\.com/(b/)?([^\\s/]*)/(?!\\w)"),
/**
* Checks the current url for known broken URL patterns; if a substitution can be made, it will be checked for
* availability and replaced.
*
* @return whether the URL is broken or not
*/
private boolean checkForKnownBrokenURL() throws Exception {
// ...
// MSDN blogs
for (Pattern pattern : new Pattern[] { MSDN_BLOG_ARTICLE_URL, MSDN_BLOG_TAG_URL, MSDN_BLOG_HOME_URL }) {
Matcher matcher = pattern.matcher(url);
if (!matcher.find())
continue;
if (Utility.isAvailable(url))
continue;
// Blog name
String blogName = matcher.group(4);
if ("vcblog".equals(blogName)) {
blogName = "cppblog";
}
// Possible base URLs
String[] replacements = new String[] { "devblogs.microsoft.com/" + blogName + "/",
"docs.microsoft.com/en-us/archive/blogs/" + blogName + "/" };
if ("chuckw".equals(blogName)) {
replacements = new String[] { "walbourn.github.io/" };
}
// Determine possible new URLs to check
List<String> newURLs = new ArrayList<>();
if (pattern != MSDN_BLOG_HOME_URL) {
String[] components = matcher.group(5).split("-");
// The algorithm below is rather brute force and not effective if there are hundreds of possibilities.
if (components.length < 8) {
// The article at
// http://blogs.msdn.com/b/rwlodarc/archive/2007/07/18/using-wpf-s-inplacebitmapmetadatawriter.aspx
// is now hosted at
// https://docs.microsoft.com/en-us/archive/blogs/rwlodarc/using-wpfs-inplacebitmapmetadatawriter
// because the word "WPF's" is now treated differently in the URL.
for (int i = (1 << components.length - 1) - 1; i >= 0; i--) {
for (String replacement : replacements) {
StringBuilder part = new StringBuilder(components[0]);
for (int j = 0; j < components.length - 1; j++) {
if ((i & (1 << j)) != 0) {
part.append("-");
}
part.append(components[j + 1]);
}
newURLs.add(matcher.replaceFirst("$1//" + replacement
+ (pattern == MSDN_BLOG_TAG_URL ? "tag/" : "") + part.toString()));
}
}
}
}
if (newURLs.isEmpty()) {
// Default replacement strategy
for (String replacement : replacements) {
newURLs.add(
matcher.replaceFirst("$1//" + replacement + (pattern != MSDN_BLOG_HOME_URL ? "$5" : "")));
}
}
// Check possible new URLs
String newURL = Utility.checkURLs(newURLs);
if (newURL != null) {
url = newURL;
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment