123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459 |
- use strict;
- use v5.10;
- AddModuleDescription('localnames.pl', 'Local Names Extension');
- our ($q, $Now, %Page, %Action, $OpenPageName, $ScriptName, $DataDir, $RssDir, @MyRules, @MyMaintenance, @MyInitVariables, $FullUrlPattern, $FreeLinkPattern, $CommentsPrefix, $UseCache, @UserGotoBarPages, %AdminPages, @MyAdminCode, @MyFooters, $UsePathInfo);
- our ($LocalNamesPage, %LocalNames, $LocalNamesCollect,
- $LocalNamesCollectMaxWords, $LnDir, $LnCacheHours,
- %WantedPages);
- $LocalNamesPage = 'LocalNames';
- $LocalNamesCollect = 0;
- $LocalNamesCollectMaxWords = 2;
- $LnDir = "$DataDir/ln";
- $LnCacheHours = 12;
- sub GetLnFile {
- return $LnDir . '/' . UrlEncode(shift);
- }
- push (@MyMaintenance, \&LnMaintenance);
- sub LnMaintenance {
- if (opendir(DIR, encode_utf8($RssDir))) {
- foreach my $file (readdir(DIR)) {
- Unlink("$RssDir/$file") if $Now - Modified($file) > $LnCacheHours * 3600;
- }
- closedir DIR;
- }
- }
- push(@MyRules, \&LocalNamesRule);
- sub LocalNamesRule {
- if (m/\G\[\[ln:$FullUrlPattern\s*([^\]]*)\]\]/cg) {
-
- return $q->a({-class=>'url outside ln', -href=>$1}, $2||$1);
- }
- return;
- }
- push(@MyInitVariables, \&LocalNamesInit);
- sub LocalNamesInit {
- %WantedPages = ();
- %LocalNames = ();
- $LocalNamesPage = FreeToNormal($LocalNamesPage);
- $AdminPages{$LocalNamesPage} = 1;
- my $data = GetPageContent($LocalNamesPage);
- while ($data =~ m/\[$FullUrlPattern\s+([^\]]+?)\]/g) {
- my ($page, $url) = ($2, $1);
- my $id = FreeToNormal($page);
- $LocalNames{$id} = $url;
- }
-
-
-
- my @ln = $data =~ m/\[\[ln:$FullUrlPattern[^\]]*?\]\]/g;
- my %todo = map {$_, GetLnFile($_)} @ln;
- my %data = ();
- if (GetParam('cache', $UseCache) > 0) {
- foreach my $uri (keys %todo) {
- if ($Now - Modified($todo{$uri}) < $LnCacheHours * 3600) {
- $data{$uri} = ReadFile($todo{$uri});
- delete($todo{$uri});
- }
- }
- }
- my @need_cache = keys %todo;
- if (keys %todo > 1) {
- eval {
- require LWP::Parallel::UserAgent;
- my $pua = LWP::Parallel::UserAgent->new();
- foreach my $uri (keys %todo) {
- if (my $res = $pua->register(HTTP::Request->new('GET', $uri))) {
- warn $res->error_as_HTML;
- }
- }
- %todo = ();
- my $entries = $pua->wait();
- foreach (keys %$entries) {
- my $uri = $entries->{$_}->request->uri;
- $data{$uri} = $entries->{$_}->response->content;
- }
- }
- }
- foreach my $uri (keys %todo) {
- $data{$uri} = GetRaw($uri);
- }
- if (GetParam('cache', $UseCache) > 0) {
- CreateDir($LnDir);
- foreach my $uri (@need_cache) {
- WriteStringToFile(GetLnFile($uri), $data{$uri});
- }
- }
-
- foreach my $ln (@ln) {
- my ($previous_type, $previous_url);
- foreach my $line (split(/[\r\n]+/, $data{$ln})) {
- if ($line =~ /^LN\s+"$FreeLinkPattern"\s+(?:"$FullUrlPattern"|\.)$/
- or $previous_type eq 'LN'
- and $line =~ /^\.\s+"$FreeLinkPattern"\s+(?:"$FullUrlPattern"|\.)$/) {
- my ($name, $url) = ($1, $2);
- $url = $previous_url if not $url and $previous_url;
- $previous_url = $url;
- $previous_type = 'LN';
- my $id = FreeToNormal($name);
-
- if (not $LocalNames{$id}) {
- $LocalNames{$id} = $url;
- }
- } else {
- $previous_type = undef;
- }
-
-
- }
- }
- }
- *OldLocalNamesResolveId = \&ResolveId;
- *ResolveId = \&NewLocalNamesResolveId;
- sub NewLocalNamesResolveId {
- my $id = shift;
- my ($class, $resolved, @rest) = OldLocalNamesResolveId($id, @_);
- if ((not $resolved or $class eq 'near') and $LocalNames{$id}) {
- return ('near', $LocalNames{$id}, $LocalNamesPage);
- } else {
- $WantedPages{$id} = 1 if not $resolved;
- return ($class, $resolved, @rest);
- }
- }
- *LocalNamesOldSave = \&Save;
- *Save = \&LocalNamesNewSave;
- sub LocalNamesNewSave {
- LocalNamesOldSave(@_);
- my ($currentid, $text) = @_;
-
- return if $currentid eq $LocalNamesPage or not $LocalNamesCollect;
- my $currentname = $currentid;
- $currentname =~ s/_/ /g;
- local ($OpenPageName, %Page);
- OpenPage($LocalNamesPage);
- my $localnames = $Page{text};
- my %map = ();
- while ($text =~ /\[$FullUrlPattern\s+(([^ \]]+?\s*){1,$LocalNamesCollectMaxWords})\]/g) {
- my ($page, $url) = ($2, $1);
- my $id = FreeToNormal($page);
- $map{$id} = () unless defined $map{$id};
- $map{$id}{$url} = 1;
- }
- my %collection = ();
- foreach my $id (keys %map) {
-
- my $page = $id;
- $page =~ s/_/ /g;
-
-
- next if $map{$id}{$LocalNames{$id}};
- $collection{$page} = 1;
-
- my @urls = keys %{$map{$id}};
- my $url = $urls[0];
-
-
- next if $LocalNames{$id}
- and $localnames =~ s/\[$LocalNames{$id}\s+$page\]/[$url $page]/g;
-
- $localnames .= "\n\n* [$url $page]"
- . Ts(" -- defined on %s", "[[$currentname]]");
- $LocalNames{$id} = $url;
- }
-
- my @collection = sort keys %collection;
- Save($LocalNamesPage, $localnames,
- Tss("Local names defined on %1: %2", $currentname,
- length(@collection > 1)
- ? join(', and ',
- join(', ', @collection[0 .. $#collection-1]),
- $collection[-1])
- : @collection), 1)
- unless $localnames eq $Page{text};
- }
- $Action{ln} = \&DoLocalNames;
- sub DoLocalNames {
- print GetHttpHeader('text/plain');
- print "X VERSION 1.2\n";
- print "# Local Pages\n";
- foreach my $id (AllPagesList()) {
- my $title = $id;
- $title =~ s/_/ /g;
- my $url = $ScriptName . ($UsePathInfo ? '/' : '?') . $id;
- print qq{LN "$title" "$url"\n};
- }
- if (GetParam('expand', 0)) {
- print "# Local names defined on $LocalNamesPage:\n";
- my $data = GetPageContent($LocalNamesPage);
- while ($data =~ m/\[$FullUrlPattern\s+([^\]]+?)\]/g) {
- my ($title, $url) = ($2, $1);
- my $id = FreeToNormal($title);
- print qq{LN "$title" "$url"\n};
- }
- print "# Namespace delegations defined on $LocalNamesPage:\n";
- while ($data =~ m/\[\[ln:$FullUrlPattern([^\]]*)?\]\]/g) {
- my ($title, $url) = ($2, $1);
- my $id = FreeToNormal($title);
- print qq{NS "$title" "$url"\n};
- }
- } else {
- print "# Local names defined on $LocalNamesPage:\n";
- foreach my $id (keys %LocalNames) {
- my $title = $id;
- $title =~ s/_/ /g;
- print qq{LN "$title" "$LocalNames{$id}"\n};
- }
- }
- }
- =head2 Define Action
- The Define Action allows you to interactively add local names using a
- form. Example URL: C<http://localhost/cgi-bin/wiki?action=define>.
- You can also provide the C<name> and C<link> parameters yourself if
- you want to use this action from a script.
- As wanted pages (links to missing pages) come up, you will get links
- to appropriate define actions in your footer.
- =cut
- $Action{define} = \&DoDefine;
- sub DoDefine {
- if (GetParam('link', '') and GetParam('name', '')) {
- SetParam('title', $LocalNamesPage);
- SetParam('text', GetPageContent($LocalNamesPage) . "\n* ["
- . GetParam('link', '') . ' ' . GetParam('name', '')
- . "]\n");
- SetParam('summary', 'Defined ' . GetParam('name'));
- return DoPost($LocalNamesPage);
- } else {
- print GetHeader('', T('Define')),
- $q->start_div({-class=>'content define'}),
- GetFormStart(undef, 'get', 'def');
- my $go = T('Go!');
- print $q->p($q->label({-for=>"defined"}, T('Name:') . ' '),
- $q->textfield(-name=>"name", -id=>"defined",
- -tabindex=>"1", -size=>20));
- print $q->p($q->label({-for=>"definition"}, T('URL:') . ' '),
- $q->textfield(-name=>"link", -id=>"definition",
- -tabindex=>"2", -size=>20));
- print $q->p($q->submit(-label=>$go, -tabindex=>"3"),
- GetHiddenValue('action', 'define'),
- GetHiddenValue('recent_edit', 'on'));
- print $q->end_form, $q->end_div();
- PrintFooter();
- }
- }
- push(@MyAdminCode, sub {
- my ($id, $menuref, $restref) = @_;
- push(@$menuref, ScriptLink('action=define', T('Define Local Names'),
- 'define'));
- });
- # link to define action for non-existing pages
- push(@MyFooters, \&GetWantedPages);
- sub GetWantedPages {
- # skip admin pages
- foreach my $id (@UserGotoBarPages, keys %AdminPages) {
- delete $WantedPages{$id};
- }
- # skip comment pages
- if ($CommentsPrefix) {
- foreach my $id (keys %WantedPages) {
- delete $WantedPages{$id} if $id =~ /^$CommentsPrefix/; # TODO use $CommentsPattern ?
- }
- }
- # now something more complicated: if near-links.pl was loaded, then
- # %WantedPages may contain pages that will in fact resolve. That's
- # why we try to resolve all the wanted ids again. And since
- # resolving ids will do stuff to %WantedPages, we need to make a
- # copy of the ids we're looking at.
- my @wanted;
- foreach my $id (keys %WantedPages) {
- my ($class, $resolved) = ResolveId($id);
- push(@wanted, $id) unless $resolved;
- }
- # if any wanted pages remain, print them
- if (@wanted) {
- return $q->div({-class=>'definition'},
- $q->p(T('Define external redirect:'), ' ',
- map { my $page = NormalToFree($_);
- ScriptLink('action=define;name='
- . UrlEncode($page),
- $page,
- 'define');
- } @wanted));
- }
- return '';
- }
|