/
November.pm
360 lines (296 loc) · 10.9 KB
/
November.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
use v6;
use Session;
use Cache;
use Digest;
class November does Session does Cache {
use CGI;
use Tags;
use HTML::Template;
use Dispatcher;
use Utils;
use Config;
use November::Storage::File;
use Text::Markup::Wiki::MediaWiki;
has November::Storage $.storage;
has CGI $.cgi;
has Config $.config;
submethod BUILD( :$config = Config.new ) {
$!config = $config;
$!storage = November::Storage::File.new(
storage_root => $!config.server_root ~ 'data/'
);
}
method handle_request(CGI $cgi) {
$!cgi = $cgi;
my $d = Dispatcher.new( default => { self.error_page } );
$d.add: [
[''], { self.view_page },
['view', /^ <-[?/]>+ $/], { self.view_page(~$^page) },
['edit', /^ <-[?/]>+ $/], { self.edit_page(~$^page) },
['in'], { self.log_in },
['out'], { self.log_out },
['recent'], { self.list_recent_changes },
['all'], { self.list_all_pages },
];
my @chunks = $cgi.uri.chunks.list;
$d.dispatch(@chunks);
}
method view_page($page is rw='Main_Page') {
$page .= subst('%20', '_', :g);
unless $.storage.wiki_page_exists($page) {
self.not_found($page);
return;
}
# TODO: we need plugin system (see topics in mail-list)
my $t = Tags.new;
my $title = $page.trans( ['_'] => [' '] );
my $content;
my $cached_page = self.get-cache-entry( $page );
if ( $cached_page ) {
$content = $cached_page;
}
else {
my $markup = $.config.markup;
$content = $markup.format(
# MediaWiki markup can't handle trailing spaces, gh-16
$.storage.read_page( $page ).subst(/[\s|\n]+$/, ''),
link_maker => { self.make_link($^p, $^t) },
extlink_maker => { self.make_extlink($^p, $^t)}
);
self.set-cache-entry( $page, $content );
}
self.response( 'view.tmpl',
{
TITLE => $title,
PAGE => $page,
CONTENT => $content,
PAGETAGS => $t.page_tags($page),
RECENTLY => self.get_changes( page => $page, :limit(8) ),
TAGS => $t.all_tags,
}
);
}
method edit_page($page is rw) {
$page .= subst('%20', '_', :g);
my $sessions = self.read_sessions();
return self.not_authorized() unless self.logged_in();
my $already_exists
= $.storage.wiki_page_exists($page);
my $action = $already_exists ?? 'Editing' !! 'Creating';
my $old_content = $already_exists ?? $.storage.read_page($page) !! '';
my $title = $action ~ ' ' ~ $page.trans( ['_'] => [' '] );
# The 'edit' action handles both showing the form and accepting the
# POST data. The difference is the presence of the 'articletext'
# parameter -- if there is one, the action is considered a save.
if $.cgi.params<articletext> || $.cgi.params<tags> {
my $summary = $.cgi.params<summary>;
my $new_text = $.cgi.params<articletext>;
my $tags = $.cgi.params<tags>;
my $session_id = $.cgi.cookie<session_id>;
my $author = $sessions{$session_id}<user_name>;
if $.cgi.params<preview> {
# It's only a preview, should just send it back formatted
return self.show_preview( $page, $summary, $new_text, $tags );
}
$.storage.save_page($page, $new_text, $author, $summary);
self.remove-cache-entry( $page );
# TODO: we need plugin system (see topics in mail-list)
my $t = Tags.new();
$t.update_tags($page, $tags);
$.cgi.redirect('/view/' ~ $page );
return;
}
# TODO: we need plugin system (see topics in mail-list)
my $t = Tags.new;
self.response( 'edit.tmpl',
{
PAGE => $page,
TITLE => $title,
CONTENT => $old_content,
PAGETAGS => $t.read_page_tags($page),
}
);
}
method show_preview( $page is rw, $summary, $new_text, $tags ) {
$page .= subst('%20', '_', :g);
my $title = $page.trans( ['_'] => [' '] );
my $markup = $.config.markup;
my $content = $markup.format(
# MediaWiki markup can't handle trailing spaces, gh-16
$new_text.subst(/[\s|\n]+$/, ''),
link_maker => { self.make_link($^p, $^t) },
extlink_maker => { self.make_extlink($^p, $^t)}
);
# Should really use the $tags parameter here, this will do for now...
#my $t = Tags.new();
#my $tags = $t.tags_parse( $tags );
self.response( 'edit.tmpl',
{
ACTION => 'Editing',
PAGE => $page,
TITLE => $title,
SUMMARY => $summary,
CONTENT => $new_text,
PREVIEW => $content,
PAGETAGS => $tags,
}
);
}
method logged_in() {
my $sessions = self.read_sessions();
my $session_id = $.cgi.cookie<session_id>;
# RAKUDO: 'defined' should maybe be 'exists', although here it doesn't
# matter.
defined $session_id && defined $sessions{$session_id}
}
method not_authorized {
self.response( 'action_not_authorized.tmpl',
{ DISALLOWED_ACTION => 'edit pages' }
);
}
method read_users {
return {} unless $.config.userfile_path ~~ :e;
return eval( slurp( $.config.userfile_path ) );
}
method not_found($page?) {
#TODO: that should by 404 when no $page
self.response('not_found.tmpl',
{
'PAGE' => $page || 'Action Not found'
}
);
}
method log_in {
if my $user_name = $.cgi.params<user_name> {
my $password = $.cgi.params<password>;
my %users = self.read_users();
# Yes, this is cheating. Stand by for a real MD5 hasher.
if defined %users{$user_name}
and digest(digest($user_name, 'sha256') ~ $password,
'sha256'
) eq %users{$user_name}<password> {
my $session_id = self.new_session($user_name);
my $session_cookie = "session_id=$session_id";
self.response('login_succeeded.tmpl',
{},
{ cookie => $session_cookie }
);
return;
}
self.response('login_failed.tmpl');
return;
}
self.response('log_in.tmpl');
}
method log_out {
if defined $.cgi.cookie<session_id> {
my $session_id = $.cgi.cookie<session_id>;
self.remove_session( $session_id );
my $session_cookie = "session_id=";
self.response('logout_succeeded.tmpl',
{},
{ :cookie($session_cookie) }
);
return;
}
self.response('logout_succeeded.tmpl');
}
method error_page($message = "You have commited a deadly URL") {
self.response( 'error.tmpl', { MESSAGE => $message } );
}
method list_recent_changes {
self.response('recent_changes.tmpl',
{
'CHANGES' => self.get_changes(limit => 50),
}
);
}
method get_changes (:$page, :$limit) {
# RAKUDO: Seemingly impossible to get the right number of list
# containers using an array variable @recent_changes here.
my $recent_changes;
if $page {
$recent_changes = $.storage.read_page_history($page);
}
else {
$recent_changes = $.storage.read_recent_changes;
}
return map {
my $modification = $.storage.read_modification($_);
{
'PAGE' => self.make_link($modification[0],$modification[0]),
'TIME' => time_to_period_str($modification[4])
|| $_,
'AUTHOR' => $modification[2] || 'somebody'
}
}, $recent_changes[ $limit.defined
?? ^($limit min $recent_changes.elems)
!! * ];
}
method list_all_pages {
my $t = Tags.new();
my %params;
%params<TAGS> = $t.all_tags if $t;
my $index;
my $tag = $.cgi.params<tag>;
if $tag and $t {
# TODO: we need plugin system (see topics in mail-list)
my $tags_index = $t.read_tags_index;
$index = $tags_index{$tag};
%params<TAG> = $tag;
}
else {
$index = $.storage.read_index;
}
if $index {
# RAKUDO: @($arrayref) not implemented yet, so:
# my @list = map { { page => $_ } }, @($index);
# does not work. Workaround:
my @list = map { { PAGE => $_,
TITLE => $_.trans( ['_'] => [' '] )
} }, $index.values;
%params<LIST> = @list;
}
self.response('list_all_pages.tmpl', %params);
}
# RAKUDO: die at hash merge if %params undef, so I use default value
method response ($tmpl, %params?={}, %opts?) {
my $template = HTML::Template.from_file($.config.template_path ~ $tmpl);
$template.with_params(
{
WEBROOT => $!config.web_root,
LOGGED_IN => self.logged_in,
SKIN => $!config.skin,
TMPL_PATH => $!config.template_path,
%params
}
);
$.cgi.send_response($template.output, %opts);
}
method make_link($page is copy, $title) {
$page .= subst(' ', '_', :g);
my $root = $!config.web_root;
if $title {
if $page ~~ m/':'/ {
return qq|<a href="{ $root ~ $page }">$title</a>|;
} else {
return qq|<a href="$root/view/$page">$title</a>|;
}
} else {
return sprintf('<a href="%s/%s/%s" %s >%s</a>',
$root,
$.storage.wiki_page_exists($page)
?? ('view', $page, '')
!! ('edit', $page, ' class="nonexistent"'),
$page);
}
}
method make_extlink($url, $title) {
if $title {
return qq|<a href="$url">$title</a>|;
} else {
return qq|<a href="$url">$url</a>|;
}
}
}
# vim:ft=perl6