summaryrefslogtreecommitdiff
path: root/gentoobrowse-api/service/maintenanceBugs.cpp
blob: 0917f97f7159151d36bc9e3913a74386c7a83e50 (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
#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 <utils/lexer.h>
#include <utils/dbUtils.h>

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

		class BugListParser : public xmlpp::SaxParser, Utils::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(1));
							ins->bindParamS(1, *es->pattern->match(3));
						}});
				}

			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") {
						fprintf(stderr, "attributes = '%s', summary = '%s'\n", attributes.c_str(), summary.c_str());
						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);
		}
	}
}