-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathnote.pl
149 lines (133 loc) · 3.58 KB
/
note.pl
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
#!/usr/bin/perl
# Adapter script for Note.pm module
# exports Note.pm functionality for command line use.
use strict;
use FindBin;
use lib "$FindBin::Bin/lib";
use Getopt::Long;
use Note;
use XML::Twig;
use utf8;
binmode STDOUT;
my $text;
my $correct_options = GetOptions(
"text=s" => \$text,
);
if (($ARGV[0] eq "create") && (scalar(@ARGV) == 3) && $correct_options)
{
my $raw = Note::create($ARGV[1], $ARGV[2], $text);
print $raw;
exit;
}
if (($ARGV[0] eq "get") && (scalar(@ARGV) == 2))
{
my $raw = Note::get($ARGV[1]);
print $raw;
exit;
}
if (($ARGV[0] eq "comment") && (scalar(@ARGV) == 2) && $correct_options)
{
my $r = Note::comment($ARGV[1], $text);
print "note commented: $r\n" if defined($r);
exit;
}
if (($ARGV[0] eq "close") && (scalar(@ARGV) == 2))
{
my $r = Note::close($ARGV[1]);
print "note closed: $r\n" if defined($r);
exit;
}
if (($ARGV[0] eq "reopen") && (scalar(@ARGV) == 2))
{
my $r = Note::reopen($ARGV[1]);
print "note reopened: $r\n" if defined($r);
exit;
}
if (($ARGV[0] eq "hide") && (scalar(@ARGV) == 2))
{
my $r = Note::hide($ARGV[1]);
print "note hidden: $r\n" if defined($r);
exit;
}
if (($ARGV[0] eq "reset") && (scalar(@ARGV) == 2))
{
my $t = XML::Twig->new(keep_encoding => 1);
my $raw = Note::get($ARGV[1]);
exit unless defined($raw);
$t->parse($raw);
my ($id, $user, $text, $date, $lon, $lat);
my $note = $t->root->first_child('note');
if ($note->field('status') != 'open')
{
print "note is not open\n";
}
my $lon=$note->{'att'}->{'lon'};
my $lat=$note->{'att'}->{'lat'};
my $id=$note->field('id');
my @comments = $note->first_child('comments')->children('comment');
my $text = "(This note has been re-created from old note #$id)\n";
my $an = 0;
my $removed = 0;
foreach my $comment(@comments)
{
my $msg = $comment->field('text');
my $user = $comment->field('user');
my $date = $comment->field('date');
my $action = $comment->field('action');
if (($action eq 'commented') && (!defined($user) || ($user eq "")))
{
$text .= "--------------------------\n(anonymous comment removed)\n" unless $an;
$an = 1;
$removed++;
}
else
{
$user = "anonymous user" if (!defined($user) || ($user eq ""));
$text .= "--------------------------\n$user on $date:\n$msg\n";
$an = 0;
}
}
if (!defined($lat) || !defined($lon) || !defined($id))
{
print "cannot load note\n";
exit;
};
if ($removed == 0)
{
print "no anonymous comments\n";
exit;
}
my $r = Note::create($lat, $lon, $text);
if (defined($r))
{
$t->parse($raw);
my $id = $t->root->first_child('note')->field('id');;
print "new note created: $id\n";
$r = Note::hide($ARGV[1]);
if (defined($r))
{
print "old note hidden\n";
}
else
{
print "cannot hide old note\n";
}
}
else
{
print "cannot create new note: $r\n";
}
exit;
}
print <<EOF;
Usage:
$0 create <lat> <lon> <options> create note
$0 get <id> load and print note XML
$0 comment <id> <options> add comment to the note
$0 close <id> <options> close note
$0 reopen <id> reopen note
$0 hide <id> hide note
$0 reset <id> hide note, and create a new one with the first comment
options:
--text <comment>
EOF