-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCInitiative.pm
142 lines (130 loc) · 4.49 KB
/
CInitiative.pm
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
package CInitiative;
use Date::Parse;
use CArea;
use config;
use encoding 'utf8';
use Encode;
use strict;
sub new {my ($obj, $xml, $use_db)=@_;
my $ref = {};
bless($ref, $obj);
$ref->{'id'} = int($xml->id->getString());
$ref->{'issue_id'} = int($xml->issue_id->getString());
$ref->{'name'} = $xml->name->getString();
$ref->{'issueState'} = $xml->issue_state->getString();
$ref->{'draftText'} = $xml->current_draft_content->getString();
$ref->{'discussionUrl'} = $xml->discussion_url->getString();
$ref->{'xml'} = $xml;
if ($use_db) {
$ref->{'area'} = CArea::load_from_db(int($xml->area_id->getString()),
$xml->area_name->getString());
} else {
$ref->{'area'} = new CArea(int($xml->area_id->getString()),
$xml->area_name->getString());
}
return $ref;
}
sub getId {my $obj = shift;$obj->{'id'};}
sub getIssueId {my $obj = shift;$obj->{'issue_id'};}
sub getName {my $obj = shift;$obj->{'name'};}
sub getIssueState {my $obj = shift;$obj->{'issueState'};}
sub getDraftText {my $obj=shift;$obj->{'draftText'};}
sub getDiscussionUrl {my $obj=shift;$obj->{'discussionUrl'};}
sub getXML {my $obj=shift;$obj->{'xml'};}
sub getArea {my $obj=shift;$obj->{'area'};}
sub hasDiscussion {my $obj = shift;
length($obj->{'discussionUrl'})>0;
}
sub getTimestamp {my ($obj, $what, $max)=@_;
my $ret = int(str2time($obj->{'xml'}->$what->getString()));
$$max = $ret if ($max and $ret > $$max);
return $ret;
}
sub isRevoked {my $obj = shift;
$obj->getTimestamp('revoked') > 0;
}
sub check_updates_from_db {my ($obj, $updates)=@_;
my $row;
Db::sthGetIssue->execute($obj->getIssueId());
if ($row = Db::sthGetIssue->fetchrow_hashref) {
if ($obj->getIssueState() ne $row->{'state'}) {
$updates->newIssueState($obj) unless $obj->isRevoked();
}
Db::sthUpdateIssue->execute(
$obj->getIssueState(),
$obj->getXML->issue_created->getString(),
$obj->getXML->issue_accepted->getString(),
$obj->getXML->issue_half_frozen->getString(),
$obj->getXML->issue_fully_frozen->getString(),
$obj->getXML->issue_closed->getString(),
$obj->getIssueId()
);
} else {
$updates->newIssue($obj);
Db::sthInsertIssue->execute(
$obj->getIssueId(), $obj->getArea->getId(), $obj->getIssueState(),
$obj->getXML->issue_created->getString(),
$obj->getXML->issue_accepted->getString(),
$obj->getXML->issue_half_frozen->getString(),
$obj->getXML->issue_fully_frozen->getString(),
$obj->getXML->issue_closed->getString()
);
}
Db::sthGetInitiative->execute($obj->getId());
if ($row = Db::sthGetInitiative->fetchrow_hashref) {
if ($obj->getDraftText() ne $row->{'draft_content'} || $obj->getName() ne $row->{'name'}) {
$updates->initiativeTextUpdated($obj);
}
Db::sthUpdateInitiative->execute(
$obj->getName(),
$obj->getDiscussionUrl(),
$obj->getXML()->created_at->getString(),
$obj->getXML()->draft_updated_at->getString(),
$obj->getDraftText(), $obj->getId());
} else {
$updates->newInitiative($obj);
Db::sthInsertInitiative->execute(
$obj->getId(), $obj->getArea()->getId(), $obj->getName(),
$obj->getDiscussionUrl(),
$obj->getXML()->created->getString(),
$obj->getXML()->current_draft_created->getString(),
$obj->getDraftText());
}
}
sub check_lr_updates {my ($obj, $lastRunTimestamp, $updates, $max)=@_;
if ($lastRunTimestamp < $obj->getTimestamp('issue_created', $max)) {
$updates->newIssue($obj);
} elsif (
$lastRunTimestamp < $obj->getTimestamp('issue_accepted', $max) ||
$lastRunTimestamp < $obj->getTimestamp('issue_half_frozen', $max) ||
$lastRunTimestamp < $obj->getTimestamp('issue_fully_frozen', $max) ||
$lastRunTimestamp < $obj->getTimestamp('issue_closed', $max)) {
$updates->newIssueState($obj) unless $obj->isRevoked();
}
if ($lastRunTimestamp < $obj->getTimestamp('created', $max)) {
$updates->newInitiative($obj);
} elsif ($lastRunTimestamp < $obj->getTimestamp('current_draft_created', $max)) {
$updates->initiativeTextUpdated($obj);
}
if ($lastRunTimestamp < $obj->getTimestamp('revoked', $max)) {
$updates->initiativeRevoked($obj);
}
}
sub save {my $obj = shift;
system('mkdir', '-p', $config::DRAFT_TEXT_DIR);
open(OUTFILE, ">:utf8", $config::DRAFT_TEXT_DIR."/".$obj->getId());
print OUTFILE $obj->getDraftText();
close(OUTFILE);
}
sub getDiff {my $obj = shift;
my $tmpfile = qx(mktemp);
open(TMPFILE, ">:utf8", $tmpfile);
print TMPFILE $obj->getDraftText();
close(TMPFILE);
open(my $DIFFOUT, "-|:utf8", 'diff', $tmpfile, $config::DRAFT_TEXT_DIR.'/'.$obj->getId());
my $diff = join('',<$DIFFOUT>);
close($DIFFOUT);
unlink($tmpfile);
$diff;
}
1;