-
Notifications
You must be signed in to change notification settings - Fork 2
/
JSTreeConnector.pm
490 lines (388 loc) · 14.7 KB
/
JSTreeConnector.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# Copyright (C) 2013-2014 Michael Daum http://michaeldaumconsulting.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version. For
# more details read LICENSE in the root of this distribution.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
package Foswiki::Plugins::ClassificationPlugin::JSTreeConnector;
use strict;
use warnings;
use Foswiki::Func ();
use Foswiki::Meta ();
use Foswiki::Sandbox ();
use Foswiki::Plugins::ClassificationPlugin ();
use JSON ();
use Error qw( :try );
use constant TRACE => 0; # toggle me
################################################################################
# static
sub writeDebug {
print STDERR $_[0]."\n" if TRACE;
}
################################################################################
# constructor
sub new {
my $class = shift;
return bless({@_}, $class);
}
################################################################################
# dispatch all handler_... methods
sub dispatchAction {
my ($this, $session, $subject, $verb, $response) = @_;
my $request = Foswiki::Func::getRequestObject();
my $theWeb = $request->param('web') || $session->{webName};
$theWeb = Foswiki::Sandbox::untaint($theWeb, \&Foswiki::Sandbox::validateWebName) || '';
my $result;
try {
my $hierarchy = Foswiki::Plugins::ClassificationPlugin::getHierarchy($theWeb);
throw Error::Simple("Hierarchy not found for web '$theWeb'") unless defined $hierarchy;
my $theAction = $request->param('action');
throw Error::Simple("No action specified") unless defined $theAction;
my $method = "handle_".$theAction;
throw Error::Simple("Unknown action '$theAction'") unless $this->can($method);
$result = $this->$method($session, $hierarchy);
} catch Error::Simple with {
my $error = shift;
$result = {
"type" => "error",
"title" => "Error",
"message" => $error->{-text}
};
$response->header(
-status => 500,
-content_type => "text/plain",
);
};
$response->print(JSON::to_json($result)) if defined $result;
return;
}
################################################################################
sub getChildren {
my ($this, $session, $cat, $selected, $depth, $displayCounts, $sort, $seen) = @_;
return if $depth == 0;
#writeDebug("getChildren($cat->{name}, $depth)");
$seen ||= {};
return if $seen->{$cat};
$seen->{$cat} = 1;
my @result = ();
my @children = $cat->getChildren;
if ($sort eq 'on' || $sort eq 'order') {
@children = sort {
$a->{order} <=> $b->{order} ||
lc($a->{title}) cmp lc($b->{title})
} @children;
} elsif ($sort eq 'title') {
@children = sort {
lc($a->{title}) cmp lc($b->{title})
} @children;
} elsif ($sort eq 'name') {
@children = sort {
lc($a->{name}) cmp lc($b->{name})
} @children;
}
foreach my $child (@children) {
next if $child->{name} eq 'BottomCategory';
my $nrChildren = scalar(grep {!/^BottomCategory$/} keys %{$child->{children}});
my $nrTopics = $displayCounts?$child->countTopics():0;
my $state = $nrChildren?"closed":"";
foreach my $selCat (@$selected) {
if ($child ne $selCat && $child->subsumes($selCat)) {
$state = "open";
last;
}
}
my $record = {
data => {
"title" => $child->{title}.($nrTopics?"<span class='jstree-count'>($nrTopics)</span>":""),
"icon" => $child->getIconUrl(),
"attr" => {
"href" => $child->getUrl(),
"title" => $child->{summary},
},
},
"attr" => {
"class" => $child->{name},
},
"metadata" => {
"name" => $child->{name},
"title" => $child->{title},
"nrChildren" => int($nrChildren),
"nrTopics" => int($nrTopics),
"editUrl" => Foswiki::Func::getScriptUrl($child->{origWeb}, $child->{name}, "edit",
t => time(),
#redirectto => Foswiki::Func::getScriptUrl($session->{webName}, $session->{topicName}, "view")
),
},
state => $state,
};
if ($state eq 'open') {
$record->{children} = $this->getChildren($session, $child, $selected, $depth-1, $displayCounts, $sort, $seen);
}
push @result, $record;
}
$seen->{$cat} = 0; # prevent cycles, but allow this branch to be displayed somewhere else
return \@result;
}
################################################################################
# handlers
################################################################################
################################################################################
sub handle_refresh {
my ($this, $session, $hierarchy) = @_;
writeDebug("refresh called for ".$hierarchy->{web});
$hierarchy->init;
$hierarchy->finish;
return {
type => "notice",
title => "Success",
message => "refreshed hierarchy in web $hierarchy->{web}",
};
}
################################################################################
sub handle_get_children {
my ($this, $session, $hierarchy) = @_;
#writeDebug("get_children called");
my $request = Foswiki::Func::getRequestObject();
my $catName = $request->param('cat') || "TopCategory";
my @select = $request->param('select');
my $maxDepth = $request->param('maxDepth');
$maxDepth = -1 unless defined $maxDepth;
my $displayCounts = Foswiki::Func::isTrue($request->param('counts'), 0);
my $sort = $request->param('sort');
$sort = 'on' if !defined($sort) || $sort !~ /^(on|title|order|name)$/;
#writeDebug("select=@select") if @select;
my $cat = $hierarchy->getCategory($catName);
throw Error::Simple("Unknown category '$catName' in hierarchy ".$hierarchy->{web})
unless defined $cat;
my %select = ();
foreach (@select) {
foreach my $item (split(/\s*,\s+/)) {
my $cat = $hierarchy->getCategory($item);
$select{$item} = $cat if defined $cat;
}
}
@select = values %select;
return $this->getChildren($session, $cat, \@select, $maxDepth, $displayCounts, $sort);
}
################################################################################
sub handle_search {
my ($this, $session, $hierarchy) = @_;
my $request = Foswiki::Func::getRequestObject();
my %cats = ();
my $search = join(".*", split(/\s+/, $request->param("title")));
$hierarchy->filterCategories({
casesensitive => "off",
title => $search,
callback => sub {
my $cat = shift;
$cats{".".$cat->{name}} = 1;
foreach my $parent ($cat->getAllParents) {
$cats{".".$parent} = 1;
}
}
});
return [keys %cats];
}
################################################################################
sub handle_move_node {
my ($this, $session, $hierarchy) = @_;
#writeDebug("move_node called");
my $request = Foswiki::Func::getRequestObject();
my $catName = $request->param("cat");
throw Error::Simple("No category") unless defined $catName;
my $cat = $hierarchy->getCategory($catName);
throw Error::Simple("Unknown category '$catName'") unless defined $cat;
my $newParentName = $request->param("parent") || "TopCategory";
my $newParent = $hierarchy->getCategory($newParentName);
throw Error::Simple("Unknown category '$newParentName'") unless defined $newParent;
my $oldParentName = $request->param("oldParent") || "TopCategory";
my $oldParent = $hierarchy->getCategory($oldParentName);
throw Error::Simple("Unknown category '$oldParentName'") unless defined $oldParent;
my $doCopy = $request->param("copy") || 0;
throw Error::Simple("Copy not implemented yet") if $doCopy;
# reparent
my ($meta) = Foswiki::Func::readTopic($cat->{origWeb}, $cat->{name});
throw Error::Simple("Woops, category not found: $cat->{origWeb}.$cat->{name}")
unless Foswiki::Func::topicExists($cat->{origWeb}, $cat->{name});
$meta = $cat->reparent($newParent, $oldParent, $meta);
throw Error::Simple("Woops, can't reparent $catName") unless defined $meta;
# reorder
my $nextCatName = $request->param("next") || '';
my $nextCat;
if ($nextCatName) {
$nextCat = $hierarchy->getCategory($nextCatName);
throw Error::Simple("Unknown category '$nextCatName'") unless defined $nextCat;
}
my $prevCatName = $request->param("prev") || '';
my $prevCat;
if ($prevCatName) {
$prevCat = $hierarchy->getCategory($prevCatName);
throw Error::Simple("Unknown category '$prevCatName'") unless defined $prevCat;
}
#writeDebug("catName=$catName, newParentName=$newParentName, oldParentName=$oldParentName, nextCatName=$nextCatName, prevCatName=$prevCatName, doCopy=$doCopy");
my @sortedCats =
sort {
(defined($prevCat) && $a->{name} eq $catName && $b->{name} eq $prevCatName)?1:
(defined($prevCat) && $a->{name} eq $prevCatName && $b->{name} eq $catName)?-1:
(defined($nextCat) && $a->{name} eq $catName && $b->{name} eq $nextCatName)?-1:
(defined($nextCat) && $a->{name} eq $nextCatName && $b->{name} eq $catName)?1:
$a->{order} <=> $b->{order} || $a->{title} cmp $b->{title};
} grep {$_->{name} !~ /^BottomCategory$/} values %{$newParent->{children}};
#print STDERR "sortedCats=".join(", ", map {$_->{name}} @sortedCats)."\n";
my $index = 10;
foreach my $item (@sortedCats) {
try {
my ($meta) = Foswiki::Func::readTopic($item->{origWeb}, $item->{name});
$item->setOrder($index, $meta);
Foswiki::Func::saveTopic($item->{origWeb}, $item->{name}, $meta);
} catch Foswiki::AccessControlException with {
throw Error::Simple("No write access to $item->{origWeb}.$item->{name}");
};
$index+= 10;
}
try {
Foswiki::Func::saveTopic($cat->{origWeb}, $cat->{name}, $meta);
} catch Foswiki::AccessControlException with {
throw Error::Simple("No write access to $cat->{origWeb}.$cat->{name}");
};
# init'ing hierarchy
if ($cat->{hierarchy}{web} ne $cat->{origWeb}) {
$hierarchy->init;
$hierarchy->finish;
}
return {
type => "notice",
title => "Success",
message => "moved ".$cat->{title}." to ".$newParent->{title},
id => $cat->{name},
};
}
################################################################################
sub handle_rename_node {
my ($this, $session, $hierarchy) = @_;
#writeDebug("rename_node called");
my $request = Foswiki::Func::getRequestObject();
my $catName = $request->param("cat");
throw Error::Simple("No category") unless defined $catName;
my $cat = $hierarchy->getCategory($catName);
throw Error::Simple("Unknown category '$catName'") unless defined $cat;
my $newTitle = $request->param("title");
$newTitle = $cat->{name} if !defined($newTitle) || $newTitle eq "";
$newTitle =~ s/^\s+|\s+$//g;
my ($meta) = Foswiki::Func::readTopic($cat->{origWeb}, $cat->{name});
my $field = $meta->get('FIELD', 'TopicTitle');
throw Error::Simple("No TopicTitle field in $cat->{origWeb}.$cat->{name}") unless $field;
my $oldTitle = $field->{value};
$field->{value} = $newTitle;
$meta->putKeyed('FIELD', $field);
try {
Foswiki::Func::saveTopic($cat->{origWeb}, $cat->{name}, $meta);
} catch Foswiki::AccessControlException with {
throw Error::Simple("No write access to $cat->{origWeb}.$cat->{name}");
};
# init'ing hierarchy
if ($cat->{hierarchy}{web} ne $cat->{origWeb}) {
$hierarchy->init;
$hierarchy->finish;
}
return {
type => "notice",
title => "Success",
message => "changed title to $newTitle",
id => $cat->{name},
};
}
################################################################################
sub handle_create_node {
my ($this, $session, $hierarchy) = @_;
#writeDebug("create_node called");
my $request = Foswiki::Func::getRequestObject();
my $catName = $request->param("cat");
throw Error::Simple("No category") unless defined $catName;
my $title = $request->param("title") || $catName;
my $cat = $hierarchy->getCategory($catName);
throw Error::Simple("Category '$catName' already exists") if defined $cat;
my $parentName = $request->param("parent") || '';
if ($parentName) {
throw Error::Simple("Parent category '$parentName' does not exists")
unless defined $hierarchy->getCategory($parentName);
}
my $position = $request->param("position");
$position = '' unless defined $position;
#writeDebug("catName=$catName, parentName=$parentName, position=$position, title=$title");
my $tmplObj;
my $tmplText;
($tmplObj, $tmplText) = Foswiki::Func::readTopic("Applications.ClassificationApp", "CategoryTemplate")
if Foswiki::Func::topicExists("Applications.ClassificationApp", "CategoryTemplate");
my $obj = Foswiki::Meta->new($session, $hierarchy->{web}, $catName);
$obj->text($tmplText) if defined $tmplText;
# add form
$obj->putKeyed("FORM", { name => "Applications.ClassificationApp.Category" });
$obj->putKeyed("FIELD", {
name => "TopicType",
title => "TopicType",
value => "Category, CategorizedTopic, WikiTopic",
});
$obj->putKeyed("FIELD", {
name => "TopicTitle",
title => "<nop>TopicTitle",
value => "$title",
});
$obj->putKeyed("FIELD", {
name => "Category",
title => "Category",
value => "$parentName",
});
$obj->putKeyed("FIELD", {
name => "Order",
title => "Order",
value => "$position",
});
$obj->save();
#writeDebug("new category object:".$obj->getEmbeddedStoreForm());
$hierarchy->init;
$hierarchy->finish;
return {
type => "notice",
title => "Success",
message => "created category '$title'",
id => $catName
};
}
################################################################################
sub handle_remove_node {
my ($this, $session, $hierarchy) = @_;
#writeDebug("remove_node called");
my $request = Foswiki::Func::getRequestObject();
my $catName = $request->param("cat");
throw Error::Simple("No category") unless defined $catName;
my $cat = $hierarchy->getCategory($catName);
throw Error::Simple("Unknown category '$catName' in hierarchy ".$hierarchy->{web})
unless defined $cat;
# SMELL: duplicates code in with Foswiki::UI::Rename
my $fromWeb = $hierarchy->{web};
my $fromTopic = $catName;
my $toWeb = $Foswiki::cfg{TrashWebName};
my $toTopic = $catName;
my $n = 1;
while (Foswiki::Func::topicExists($toWeb, $toTopic)) {
$toTopic = $toTopic . $n;
$n++;
}
#writeDebug("moving $fromWeb.$fromTopic to $toWeb.$toTopic");
Foswiki::Func::moveTopic($fromWeb, $fromTopic, $toWeb, $toTopic);
return {
type => "notice",
title => "Success",
message => "deleted category '".$cat->{title}."'",
id => $catName
};
}
1;