Skip to content

Commit

Permalink
Update version checking to use JSON page
Browse files Browse the repository at this point in the history
Much less string manipulation

Completely untested atm... Fun and games

Bug: T139126


git-svn-id: https://svn.code.sf.net/p/autowikibrowser/code/AWB@12262 5227b50d-9349-4574-b055-4b0a7525e11c
  • Loading branch information
reedy committed Sep 16, 2018
1 parent cd3ab19 commit bb4868b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 74 deletions.
23 changes: 13 additions & 10 deletions WikiFunctions/Session.cs
Expand Up @@ -23,6 +23,7 @@
using System.Security.Authentication;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Newtonsoft.Json.Linq;
using WikiFunctions.API;

namespace WikiFunctions
Expand Down Expand Up @@ -237,8 +238,6 @@ public WikiStatusResult Update()
public static string AWBVersion
{ get { return Assembly.GetExecutingAssembly().GetName().Version.ToString(); } }

private static readonly Regex BadName = new Regex(@"badname:\s*(.*)\s*(:?|#.*)$", RegexOptions.IgnoreCase | RegexOptions.Compiled);

private static readonly Regex DirectionMarks = new Regex(@"^(\u200E|\u200F)+", RegexOptions.Multiline);

/// <summary>
Expand Down Expand Up @@ -295,12 +294,13 @@ private WikiStatusResult UpdateWikiStatus()
// TODO: assess the impact on servers later
Editor.Maxlag = /*User.IsBot ? 5 : 20*/ -1;

var versionJson = JObject.Parse(Updater.GlobalVersionPage);
// check if username is globally blacklisted
foreach (Match badName in BadName.Matches(Updater.GlobalVersionPage))
foreach (string badName in versionJson["badnames"])
{
if (!string.IsNullOrEmpty(badName.Groups[1].Value.Trim()) &&
if (!string.IsNullOrEmpty(badName.Trim()) &&
!string.IsNullOrEmpty(User.Name) &&
Regex.IsMatch(User.Name, badName.Groups[1].Value.Trim(),
Regex.IsMatch(User.Name, badName.Trim(),
RegexOptions.IgnoreCase | RegexOptions.Multiline))
return WikiStatusResult.NotRegistered;
}
Expand Down Expand Up @@ -363,11 +363,14 @@ private WikiStatusResult UpdateWikiStatus()

if (Variables.Project != ProjectEnum.custom)
{
string globalUsers = Tools.StringBetween(VersionCheckPage, "<!--globalusers-->",
"<!--globalusersend-->");

if (UserNameInText(User.Name, globalUsers))
return WikiStatusResult.Registered;
var globalUsers = versionJson["globalusers"];
foreach (string s in globalUsers)
{
if (User.Name == s)
{
return WikiStatusResult.Registered;
}
}
}
return WikiStatusResult.NotRegistered;
}
Expand Down
102 changes: 38 additions & 64 deletions WikiFunctions/Updater.cs
Expand Up @@ -21,6 +21,9 @@
using System.Text.RegularExpressions;
using System.Windows.Forms;
using WikiFunctions.Background;
using System.Linq;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace WikiFunctions
{
Expand Down Expand Up @@ -57,12 +60,10 @@ public enum AWBEnabledStatus
public static AWBEnabledStatus Result { get; private set; }

/// <summary>
/// Text of the Current AWB Global Checkpage (en.wp)
/// Text (JSON) of the Current AWB Global Checkpage (en.wp)
/// </summary>
public static string GlobalVersionPage { get; private set; }

private static readonly Regex EnabledVersions = new Regex(@"\*(.*?) enabled", RegexOptions.IgnoreCase | RegexOptions.Compiled);

/// <summary>
/// Do the actual checking for enabledness etc
/// </summary>
Expand All @@ -72,56 +73,45 @@ private static void UpdateFunc()
{
string text =
Tools.GetHTML(
"https://en.wikipedia.org/w/index.php?title=Wikipedia:AutoWikiBrowser/CheckPage/Version&action=raw");
"https://en.wikipedia.org/w/index.php?title=Wikipedia:AutoWikiBrowser/CheckPage/VersionJSON&action=raw");
GlobalVersionPage = text;

int awbCurrentVersion =
StringToVersion(Regex.Match(text, @"<!-- Current version: (.*?) -->").Groups[1].Value);
var json = JObject.Parse(text);

Result = AWBEnabledStatus.Disabled; // Disabled till proven enabled

var definition = new { version = "", dotnetversion = "", svn = false };
var enabledVersions = from v in json["enabledversions"] select JsonConvert.DeserializeAnonymousType(v.ToString(), definition);

string updaterVersion = json["updaterversion"].ToString();

FileVersionInfo awbVersionInfo =
FileVersionInfo.GetVersionInfo(AWBDirectory + "AutoWikiBrowser.exe");

if (enabledVersions.Any(v => v.version == awbVersionInfo.ToString()))
{
Result = AWBEnabledStatus.Enabled;
}

string updaterFileVersion = FileVersionInfo.GetVersionInfo(AWBDirectory + "AWBUpdater.exe").FileVersion;

int awbNewestVersion =
StringToVersion(Regex.Match(text, @"<!-- Newest version: (.*?) -->").Groups[1].Value);
if (Version.Parse(updaterFileVersion) < Version.Parse(updaterVersion))
{
Result |= AWBEnabledStatus.UpdaterUpdate;
}

if ((awbCurrentVersion > 4000) || (awbNewestVersion > 4000))
if ((Result & AWBEnabledStatus.Disabled) == AWBEnabledStatus.Disabled)
{
int updaterVersion =
StringToVersion(Regex.Match(text, @"<!-- Updater version: (.*?) -->").Groups[1].Value);

FileVersionInfo awbVersionInfo =
FileVersionInfo.GetVersionInfo(AWBDirectory + "AutoWikiBrowser.exe");
int awbFileVersion = StringToVersion(awbVersionInfo.FileVersion);

Result = AWBEnabledStatus.Disabled; // Disabled till proven enabled

//if (awbFileVersion < awbCurrentVersion)
//{
// return;
//}

foreach (Match m in EnabledVersions.Matches(text))
{
if (StringToVersion(m.Groups[1].Value) == awbFileVersion)
{
Result = AWBEnabledStatus.Enabled;
break;
}
}

if (Result == AWBEnabledStatus.Disabled)
{
return;
}

if ((updaterVersion > 1400) &&
(updaterVersion >
StringToVersion(FileVersionInfo.GetVersionInfo(AWBDirectory + "AWBUpdater.exe").FileVersion)))
{
Result |= AWBEnabledStatus.UpdaterUpdate;
}

if ((awbFileVersion >= awbCurrentVersion) && (awbFileVersion < awbNewestVersion))
{
Result |= AWBEnabledStatus.OptionalUpdate;
}
// If it's disabled, updates aren't optional!
return;
}

var awbVersionParsed = Version.Parse(awbVersionInfo.FileVersion);

// SVN versions aren't optional updates
if (enabledVersions.Any(v => (Version.Parse(v.version) > awbVersionParsed && !v.svn)))
{
Result |= AWBEnabledStatus.OptionalUpdate;
}
}
catch
Expand All @@ -130,22 +120,6 @@ private static void UpdateFunc()
}
}

/// <summary>
/// Change a string version (x.x.x.x) to a version number (xxxx)
/// </summary>
/// <param name="version">Version String</param>
/// <returns>Version Number</returns>
private static int StringToVersion(string version)
{
int res;
if (!int.TryParse(version.Replace(".", ""), out res))
{
res = 0;
}

return res;
}

private static BackgroundRequest _request;

/// <summary>
Expand Down

0 comments on commit bb4868b

Please sign in to comment.