summaryrefslogtreecommitdiff
path: root/gentoobrowse-api/service/maintenanceBugs.cpp
blob: 8c56cecbbdf695fde3d83cb07f0a89b06c764b5d (plain)
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
#include "maintenanceimpl.h"
#include <boost/lexical_cast.hpp>
#include <selectcommandUtil.impl.h>
#include <tablepatch.h>
#include <sqlWriter.h>
#include <buffer.h>
#include <scopeExit.h>
#include <boost/filesystem/operations.hpp>
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#include <curlStream.h>
#include <libxml++/parsers/saxparser.h>
#pragma GCC diagnostic pop
#include <lexer.h>
#include <lexer-regex.h>
#include <utils/dbUtils.h>

namespace Gentoo {
	namespace Service {
		AdHoc::Lexer::PatternPtr bugLink = AdHoc::LexerMatchers::regex(
				R"R(Bug:(\d+) - "" status:(\w*) resolution:(\w*) severity:(\w*).*)R", G_REGEX_OPTIMIZE);

		class BugListParser : public xmlpp::SaxParser, AdHoc::Lexer {
			public:
				BugListParser(DB::ModifyCommandPtr i) :
					ins(i)
				{
					rules.push_back({ { InitialState }, bugLink, [this](auto es) {
							ins->bindParamI(0, boost::lexical_cast<int64_t>(*es->pattern()->match(1)));
							ins->bindParamS(2, *es->pattern()->match(2));
							ins->bindParamS(1, *es->pattern()->match(4));
						}});
				}

			protected:
				void on_start_element(const Glib::ustring & e, const xmlpp::SaxParser::AttributeList &) override
				{
					stk.push(e);
				}

				void on_characters(const Glib::ustring & t) override
				{
					if (stk.top() == "a") {
						attributes += t;
					}
					else if (stk.top() == "em") {
						summary += t;
					}
				}

				void on_end_element(const Glib::ustring & e) override
				{
					stk.pop();
					if (e == "li") {
						extract(attributes.c_str(), attributes.length());
						ins->bindParamS(3, summary);
						ins->execute();
						attributes.clear();
						summary.clear();
					}
				}

			private:
				DB::ModifyCommandPtr ins;
				std::stack<Glib::ustring> stk;

				Glib::ustring attributes;
				Glib::ustring summary;
		};

		void
		Maintenance::refreshBugs(const Ice::Current & c)
		{
			boost::filesystem::path root = properties(c)->getPropertyWithDefault(
					"GentooBrowseAPI.BugRoot", "https://bugs.gentoo.org/data/cached");

			auto dbc = db->get();
			DB::TransactionScope tx(dbc.get());
			DB::TablePatch tp;
			tp.pk = { "bugId" };
			tp.cols = { "bugId", "severity", "status", "summary" };
			tp.dest = "gentoobrowse.bugs";
			tp.src = Utils::Database::emptyClone(dbc.get(), "gentoobrowse.bugs");
			auto ins = Utils::Database::tablePatchInserter(dbc.get(), tp);
			BugListParser blp(ins);
			for(const auto & bl : {
					"buglist-CONFIRMED.html",
					"buglist-UNCONFIRMED.html",
					"buglist-IN_PROGRESS.html" }) {
				AdHoc::Net::CurlStreamSource css((root / bl).string());
				css.setopt(CURLOPT_ENCODING, "deflate, gzip");
				AdHoc::Net::CurlStream cs(css);
				blp.parse_stream(cs);
			}
			dbc->patchTable(&tp);
			Utils::Database::drop(dbc.get(), tp.src);
		}
	}
}