Menu

[r12930]: / AWB / WikiFunctions / Updater.cs  Maximize  Restore  History

Download this file

189 lines (158 with data), 6.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
Copyright (C) 2009-2018 Sam Reed
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 2 of the License, or
(at your option) any later version.
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 St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using WikiFunctions.Background;
using System.Linq;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Collections.Generic;
namespace WikiFunctions
{
public static class Updater
{
private static readonly string AWBDirectory;
/// <summary>
/// Runs Update() at creation time
/// </summary>
static Updater()
{
AWBDirectory = Path.GetDirectoryName(Application.ExecutablePath) + "\\";
Result = AWBEnabledStatus.None;
NewerVersions = new List<string>();
}
/// <summary>
/// Available Enabled statuses for AWB
/// </summary>
[Flags]
public enum AWBEnabledStatus
{
None = 0,
Error = 1,
Disabled = 2,
Enabled = 4,
UpdaterUpdate = 8,
OptionalUpdate = 16,
}
/// <summary>
/// Last AWBEnabledStatus Result from CheckPage Check
/// </summary>
public static AWBEnabledStatus Result { get; private set; }
/// <summary>
/// Text (JSON) of the Current AWB Global CheckPage (en.wp)
/// </summary>
public static string GlobalVersionPage { get; private set; }
/// <summary>
/// Gets the list of versions of AWB newer than the current version
/// </summary>
/// <value>The newer versions.</value>
public static List<string> NewerVersions { get; private set; }
private const string CHECKPAGE_URL =
"https://en.wikipedia.org/w/index.php?title=Wikipedia:AutoWikiBrowser/CheckPage/VersionJSON&action=raw";
private const string CHECKPAGE_URL_TEST =
"https://en.wikipedia.org/w/index.php?title=Wikipedia:AutoWikiBrowser/CheckPage/VersionJSONTest&action=raw";
/// <summary>
/// Do the actual checking for enabledness etc
/// </summary>
private static void UpdateFunc()
{
try
{
string text = Tools.GetHTML(CHECKPAGE_URL);
GlobalVersionPage = text;
var json = JObject.Parse(text);
Result = AWBEnabledStatus.Disabled; // Disabled till proven enabled
var definition = new {version = "", releasedate = "", dotnetversion = "", dev = false, released = false};
var enabledVersions = (from v in json["enabledversions"]
select JsonConvert.DeserializeAnonymousType(v.ToString(), definition)).ToList();
string updaterVersion = json["updaterversion"].ToString();
FileVersionInfo awbVersionInfo =
FileVersionInfo.GetVersionInfo(AWBDirectory + "AutoWikiBrowser.exe");
if (enabledVersions.Any(v => v.version == awbVersionInfo.FileVersion))
{
Result = AWBEnabledStatus.Enabled;
}
string updaterFileVersion = FileVersionInfo.GetVersionInfo(AWBDirectory + "AWBUpdater.exe").FileVersion;
if (Version.Parse(updaterFileVersion) < Version.Parse(updaterVersion))
{
Result |= AWBEnabledStatus.UpdaterUpdate;
}
if ((Result & AWBEnabledStatus.Disabled) == AWBEnabledStatus.Disabled)
{
// If it's disabled, updates aren't optional!
return;
}
var awbVersionParsed = Version.Parse(awbVersionInfo.FileVersion);
var newerVersions = enabledVersions.Where(v => !v.dev && (Version.Parse(v.version) > awbVersionParsed))
.Select(v => v.version);
// Dev versions aren't optional updates
if (newerVersions.Any())
{
NewerVersions.AddRange(newerVersions.ToArray());
Result |= AWBEnabledStatus.OptionalUpdate;
}
}
catch
{
Result = AWBEnabledStatus.Error;
}
}
private static BackgroundRequest _request;
/// <summary>
/// Checks to see if AWBUpdater.exe.new exists, if it does, replace it.
/// </summary>
public static void UpdateUpdaterFile()
{
if (File.Exists(AWBDirectory + "AWBUpdater.exe.new"))
{
File.Copy(AWBDirectory + "AWBUpdater.exe.new", AWBDirectory + "AWBUpdater.exe", true);
File.Delete(AWBDirectory + "AWBUpdater.exe.new");
}
}
/// <summary>
/// Background request to check enabled state of AWB
/// </summary>
public static void CheckForUpdates()
{
if (_request != null)
{
return;
}
_request = new BackgroundRequest();
_request.Execute(UpdateFunc);
}
/// <summary>
/// Waits for background enabled check to complete
/// </summary>
public static void WaitForCompletion()
{
if (_request == null)
{
return;
}
_request.Wait();
_request = null;
}
/// <summary>
/// Runs the Updater program
/// </summary>
public static void RunUpdater()
{
Process.Start(AWBDirectory + "AWBUpdater.exe");
}
}
}