-
Notifications
You must be signed in to change notification settings - Fork 1
/
unreviewed-pages.cs
200 lines (191 loc) · 9.78 KB
/
unreviewed-pages.cs
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
189
190
191
192
193
194
195
196
197
198
199
200
using System;
using System.Collections.Generic;
using System.Net;
using System.IO;
using System.Linq;
using System.Web;
using System.Xml;
using System.Text;
class pageinfo
{
public string pending_since, stable_revid;
}
class Program
{
static string wiki, cat, template;
static int requireddepth = 0;
static bool talks;
static WebClient cl = new WebClient();
static HashSet<string> candidates = new HashSet<string>();
static Dictionary<string, pageinfo> pages = new Dictionary<string, pageinfo>();
static void sendresponse(string wiki, string cat, string template, int depth, string result)
{
string strExeFilePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string? strWorkPath = Path.GetDirectoryName(strExeFilePath);
string strHtmlPath = Path.Combine(strWorkPath!, "unreviewed-pages.html");
var sr = new StreamReader(strHtmlPath);
string resulttext = sr.ReadToEnd();
string title = "";
if (cat != "" && template != "")
title = " (" + cat + ", " + template + ")";
else if (cat != "")
title = " (" + cat + ")";
else if (template != "")
title = " (" + template + ")";
resulttext = resulttext.Replace("%result%", result).Replace("%wiki%", wiki).Replace("%cat%", cat).Replace("%template%", template).Replace("%depth%", depth.ToString()).Replace("%title%", title);
if (talks)
resulttext = resulttext.Replace("%checked_talks%", "checked");
Console.WriteLine(resulttext);
return;
}
static void searchsubcats(string category, int currentdepth)
{
if (currentdepth <= requireddepth)
{
string cont = "", query = "https://" + wiki + ".org/w/api.php?action=query&list=categorymembers&format=xml&cmtitle=Category:" + Uri.EscapeDataString(category) + "&cmprop=" + (talks ? "title" : "ids") + "&cmlimit=max" + (talks ? "" : "&cmnamespace=100|102|0|6|10|14");
while (cont != null)
{
var apiout = Encoding.UTF8.GetString(cont == "" ? cl.DownloadData(query) : cl.DownloadData(query + "&cmcontinue=" + Uri.EscapeDataString(cont)));
using (var r = new XmlTextReader(new StringReader(apiout)))
{
r.WhitespaceHandling = WhitespaceHandling.None;
r.Read(); r.Read(); r.Read(); cont = r.GetAttribute("cmcontinue");
while (r.Read())
if (r.Name == "cm")
{
string id_or_title = r.GetAttribute(talks ? "title" : "pageid");
if (talks && id_or_title.IndexOf(":") >= 0)
id_or_title = id_or_title.Substring(id_or_title.IndexOf(":") + 1);
if (!candidates.Contains(id_or_title))
candidates.Add(id_or_title);
}
}
}
cont = ""; //собираем категории
query = "https://" + wiki + ".org/w/api.php?action=query&list=categorymembers&format=xml&cmtitle=Category:" + Uri.EscapeDataString(category) + "&cmnamespace=14&cmprop=title&cmlimit=max";
while (cont != null)
{
var apiout = Encoding.UTF8.GetString(cont == "" ? cl.DownloadData(query) : cl.DownloadData(query + "&cmcontinue=" + Uri.EscapeDataString(cont)));
using (var r = new XmlTextReader(new StringReader(apiout)))
{
r.WhitespaceHandling = WhitespaceHandling.None;
r.Read(); r.Read(); r.Read(); cont = r.GetAttribute("cmcontinue");
while (r.Read())
if (r.Name == "cm")
{
string fullcategoryname = r.GetAttribute("title");
string shortcategoryname = fullcategoryname.Substring(fullcategoryname.IndexOf(':') + 1);
searchsubcats(shortcategoryname, currentdepth + 1);
}
}
}
}
}
static void Main()
{
string input = Environment.GetEnvironmentVariable("QUERY_STRING");
//input = "wiki=ru.wikipedia&cat=Статьи+проекта+Волгоград+и+Волгоградская+область+IV+уровня+низкой+важности&depth=0&template=";
if (input == "" || input == null)
{
sendresponse("ru.wikipedia", "", "", 0, "");
return;
}
var parameters = HttpUtility.ParseQueryString(input);
talks = parameters["talks"] == "on";
wiki = parameters["wiki"];
cat = parameters["cat"] ?? "";
template = parameters["template"] ?? "";
requireddepth = Convert.ToInt16(parameters["depth"]);
if (requireddepth < 0)
{
sendresponse(wiki, cat, template, 0, "Use non-negative depth value");
return;
}
if (cat == "" && template == "")
{
sendresponse(wiki, cat, template, requireddepth, "Input category, template name or both");
return;
}
bool broken_title = false;
using (var r = new XmlTextReader(new StringReader(Encoding.UTF8.GetString(cl.DownloadData("https://" + wiki + ".org/w/api.php?action=query&prop=pageprops&format=xml&titles=category:" + Uri.EscapeDataString(cat))))))
while (r.Read())
if (r.Name == "page" && r.GetAttribute("_idx") == "-1")
broken_title = true;
using (var r = new XmlTextReader(new StringReader(Encoding.UTF8.GetString(cl.DownloadData("https://" + wiki + ".org/w/api.php?action=query&prop=pageprops&format=xml&titles=" + Uri.EscapeDataString(template))))))
while (r.Read())
if (r.Name == "page" && r.GetAttribute("_idx") == "-1")
broken_title = true;
if (broken_title)
{
sendresponse(wiki, cat, template, requireddepth, "There is no such category or such template in this wiki, you probably misspelled the page name");
return;
}
if (cat != "")
searchsubcats(cat, 0);
if (template != "")
{
string cont = "", query = "https://" + wiki + ".org/w/api.php?action=query&format=xml&list=embeddedin&eititle=" + Uri.EscapeDataString(template) + "&eilimit=max&einamespace=100|102|0|6|10|14";
while (cont != null)
{
string apiout = Encoding.UTF8.GetString(cont == "" ? cl.DownloadData(query) : cl.DownloadData(query + "&eicontinue=" + Uri.EscapeDataString(cont)));
using (var r = new XmlTextReader(new StringReader(apiout)))
{
r.WhitespaceHandling = WhitespaceHandling.None;
r.Read(); r.Read(); r.Read(); cont = r.GetAttribute("eicontinue");
while (r.Read())
if (r.Name == "ei")
if (!candidates.Contains(r.GetAttribute("pageid")))
candidates.Add(r.GetAttribute("pageid"));
}
}
}
var requeststrings = new HashSet<string>();
int c = 0;
string idset = "";
foreach (var id in candidates)
{
idset += "|" + id;
if (++c % (talks ? 10 : 49) == 0)
{
requeststrings.Add(idset.Substring(1));
idset = "";
}
}
if (idset.Length > 0)
requeststrings.Add(idset.Substring(1));
foreach(var rstring in requeststrings)
using (var r = new XmlTextReader(new StringReader(Encoding.UTF8.GetString(cl.DownloadData("https://" + wiki + ".org/w/api.php?action=query&format=xml&prop=flagged&" + (talks ? "titles=" : "pageids=") + rstring)))))
{
r.WhitespaceHandling = WhitespaceHandling.None;
while (r.Read())
if (r.Name == "page" && r.NodeType == XmlNodeType.Element)
{
string title = r.GetAttribute("title");
r.Read();
if (r.Name != "flagged")
pages.Add(title, new pageinfo() { pending_since = "never", stable_revid = "" });
else if (r.GetAttribute("pending_since") != null)
pages.Add(title, new pageinfo() { pending_since = r.GetAttribute("pending_since").Substring(0, 10), stable_revid = r.GetAttribute("stable_revid") } );
}
}
if (candidates.Count == 0)
sendresponse(wiki, cat, template, requireddepth, "There are no pages in this category or using this template");
else if (pages.Count == 0)
sendresponse(wiki, cat, template, requireddepth, "All pages in this category or using this template are reviewed in last revision");
else
{
string result = "<table border=\"1\" cellspacing=\"0\"><tr><th>Page</th><th>Date of first unreviewed revision</th></tr>\n";
foreach (var p in pages.OrderByDescending(p => p.Value.pending_since))
{
string link;
if (p.Value.pending_since == "never")
link = "https://" + wiki + ".org/wiki/" + Uri.EscapeDataString(p.Key);
else
link = "https://" + wiki + ".org/w/index.php?title=" + Uri.EscapeDataString(p.Key) + "&type=revision&diff=cur&oldid=" + p.Value.stable_revid;
result += "<tr><td><a target=\"_blank\" href=\"" + link + "\">" + p.Key + "</a></td><td>" + p.Value.pending_since + "</td></tr>\n";
}
result += "</table></center>";
sendresponse(wiki, cat, template, requireddepth, result);
}
}
}