2005-03-25 11:44:48 +00:00
|
|
|
# Copyright (C) 2005 Flavio Poletti <flavio@polettix.it>
|
|
|
|
|
#
|
|
|
|
|
# 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
|
2016-08-16 14:59:13 +02:00
|
|
|
# the Free Software Foundation; either version 3 of the License, or
|
2005-03-25 11:44:48 +00:00
|
|
|
# (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# 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. See the
|
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2016-08-16 14:59:13 +02:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2005-03-25 11:44:48 +00:00
|
|
|
|
|
|
|
|
# This module adds an action and a link in the UserGotoBar to build
|
|
|
|
|
# a Local Site Map starting from the current page. The map is a sort
|
|
|
|
|
# of Table Of Contents in which the current page is considered the
|
|
|
|
|
# root of the document.
|
|
|
|
|
#
|
|
|
|
|
# Basic idea got from MoinMoin.
|
|
|
|
|
|
2015-03-27 03:01:01 +02:00
|
|
|
use strict;
|
2015-08-18 10:48:03 +02:00
|
|
|
use v5.10;
|
2015-03-27 03:01:01 +02:00
|
|
|
|
2015-04-10 13:31:28 +03:00
|
|
|
our ($q, %Action, %IndexHash, $FS, $LinkPattern, $FreeLinks, $FreeLinkPattern, $WikiLinks, $BracketWiki, @MyInitVariables, $UserGotoBar);
|
2005-03-25 11:44:48 +00:00
|
|
|
|
|
|
|
|
##########################################################################
|
|
|
|
|
#
|
|
|
|
|
# End-user capabilities
|
|
|
|
|
#
|
|
|
|
|
##########################################################################
|
|
|
|
|
# Actions
|
|
|
|
|
$Action{'localmap'} = \&DoLocalMap;
|
|
|
|
|
|
|
|
|
|
# Variables
|
2015-04-10 13:31:28 +03:00
|
|
|
our ($LocalMapDefaultDepth);
|
2005-03-25 11:44:48 +00:00
|
|
|
$LocalMapDefaultDepth = 3 unless defined $LocalMapDefaultDepth;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##########################################################################
|
|
|
|
|
#
|
|
|
|
|
# Implementation
|
|
|
|
|
#
|
|
|
|
|
##########################################################################
|
2014-08-21 22:23:23 +02:00
|
|
|
AddModuleDescription('olocalmap.pl');
|
2005-03-25 11:44:48 +00:00
|
|
|
|
|
|
|
|
push(@MyInitVariables, \&InitLocalMap);
|
|
|
|
|
|
|
|
|
|
sub InitLocalMap {
|
|
|
|
|
my $id = GetCurrentPageName();
|
|
|
|
|
my $action = lc(GetParam('action', ''));
|
|
|
|
|
AllPagesList(); # Build %IndexHash
|
|
|
|
|
|
|
|
|
|
# Avoid putting stuff in non-pages (like RecentChanges) and in
|
|
|
|
|
# the page result of the action
|
2015-03-27 03:01:01 +02:00
|
|
|
return 0 unless (length($id)
|
|
|
|
|
&& $IndexHash{$id}
|
2005-03-25 11:44:48 +00:00
|
|
|
&& ($action cmp 'localmap'));
|
|
|
|
|
|
|
|
|
|
# Add a link to the list of parents
|
|
|
|
|
$UserGotoBar .= ScriptLink("action=localmap;id=$id", T('LocalMap')) . ' ';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub DoLocalMap {
|
|
|
|
|
my $id = GetParam('id', '');
|
|
|
|
|
MyReportError(T('No page id for action localmap'), '400 BAD REQUEST',
|
|
|
|
|
undef, GetParam('raw', 0))
|
|
|
|
|
unless length($id);
|
|
|
|
|
|
|
|
|
|
AllPagesList(); # Build %IndexHash
|
|
|
|
|
MyReportError(Ts('Requested page %s does not exist', $id),
|
|
|
|
|
'503 SERVICE UNAVAILABLE', undef, GetParam('raw', 0))
|
|
|
|
|
unless ($IndexHash{FreeToNormal($id)});
|
|
|
|
|
|
|
|
|
|
print GetHeader('', QuoteHtml(Ts('Local Map for %s', $id)), '');
|
|
|
|
|
|
|
|
|
|
my $depth = GetParam('depth', $LocalMapDefaultDepth);
|
|
|
|
|
$id = FreeToNormal($id);
|
|
|
|
|
my %got; # Tracks already hit pages
|
|
|
|
|
print($q->ul(LocalMapWorkHorse($id, $depth, \%got)));
|
|
|
|
|
PrintFooter();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub LocalMapWorkHorse {
|
|
|
|
|
my ($id, $depth, $GotPagesRef) = @_;
|
|
|
|
|
|
|
|
|
|
$GotPagesRef->{$id} = $depth;
|
|
|
|
|
return '' unless exists($IndexHash{$id});
|
|
|
|
|
my $name = $id;
|
|
|
|
|
$name =~ s/_/ /g;
|
|
|
|
|
|
|
|
|
|
my $retval_me .= ScriptLink("action=localmap;id=" . UrlEncode($id), $name);
|
|
|
|
|
$retval_me .= ' (' . GetPageLink($id, T('view')) . ')';
|
2015-05-02 03:44:07 +03:00
|
|
|
$retval_me = $q->li($retval_me);
|
2005-03-25 11:44:48 +00:00
|
|
|
|
|
|
|
|
my $retval_children = '';
|
|
|
|
|
if ($depth > 0) {
|
2015-09-04 04:55:48 +03:00
|
|
|
my $data = ParseData(ReadFileOrDie(GetPageFile($id)));
|
|
|
|
|
my @flags = split(/$FS/, $data->{'flags'});
|
|
|
|
|
my @blocks = split(/$FS/, $data->{'blocks'});
|
2005-03-25 11:44:48 +00:00
|
|
|
my @subpages;
|
|
|
|
|
|
|
|
|
|
# Iterate over blocks, operate only on "dirty" ones
|
|
|
|
|
for (my $i = 0; $i < @flags; ++$i) {
|
|
|
|
|
next unless $flags[$i];
|
|
|
|
|
my $sub_id;
|
|
|
|
|
local $_ = $blocks[$i];
|
|
|
|
|
|
|
|
|
|
if ($WikiLinks
|
2015-08-23 21:22:12 +03:00
|
|
|
&& ($BracketWiki && m/\G(\[$LinkPattern\s+([^\]]+?)\])/cg
|
|
|
|
|
or m/\G(\[$LinkPattern\])/cg or m/\G($LinkPattern)/cg)) {
|
2005-03-25 11:44:48 +00:00
|
|
|
$sub_id = $1;
|
|
|
|
|
} elsif ($FreeLinks
|
2015-03-27 03:01:01 +02:00
|
|
|
&& (($BracketWiki
|
2015-08-23 21:22:12 +03:00
|
|
|
&& m/\G(\[\[($FreeLinkPattern)\|([^\]]+)\]\])/cg)
|
|
|
|
|
or m/\G(\[\[\[($FreeLinkPattern)\]\]\])/cg
|
|
|
|
|
or m/\G(\[\[($FreeLinkPattern)\]\])/cg)) {
|
2005-03-25 11:44:48 +00:00
|
|
|
$sub_id = $2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($sub_id) {
|
|
|
|
|
$sub_id = FreeToNormal($sub_id);
|
|
|
|
|
if (exists $IndexHash{$sub_id}
|
|
|
|
|
&& ! exists $GotPagesRef->{$sub_id}) {
|
|
|
|
|
push(@subpages, $sub_id);
|
|
|
|
|
$GotPagesRef->{$sub_id} = $depth - 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Recollect. We cannot do it inside the for loop because otherwise
|
|
|
|
|
# we would spoil the hash pointed by $GotPagesRef
|
|
|
|
|
foreach my $sub_id (@subpages) {
|
2015-03-27 03:01:01 +02:00
|
|
|
$retval_children .=
|
2005-03-25 11:44:48 +00:00
|
|
|
LocalMapWorkHorse($sub_id, $depth - 1, $GotPagesRef);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Enclose all inside an unnumbered list
|
|
|
|
|
$retval_children = $q->ul($retval_children) if length($retval_children);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Return the two sections
|
|
|
|
|
return $retval_me . $retval_children;
|
|
|
|
|
}
|