From 3c0c79a526ca50d65dc797d7abaf5a8087941ff2 Mon Sep 17 00:00:00 2001 From: Alex Schroeder Date: Thu, 31 Jan 2013 09:21:54 +0100 Subject: [PATCH 01/10] logbannedcontent.pl: Logging BannedHosts as well. Renamed wrappers to make sure all have the Log prefix. Moved log writing to a separate sub. Provide a little wrapper text for banned hosts. Using TimeToW3 to use a standard date and time format. Use the id if no page has been opened yet (since DoEdit calls UserIsBanned and DoPost calls UserCanEdit before either calls OpenPage). --- modules/logbannedcontent.pl | 42 +++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/modules/logbannedcontent.pl b/modules/logbannedcontent.pl index 2a973ed2..ab5cf00f 100755 --- a/modules/logbannedcontent.pl +++ b/modules/logbannedcontent.pl @@ -20,25 +20,31 @@ use vars qw($BannedFile); $BannedFile = "$DataDir/spammer.log" unless defined $BannedFile; -*OldBannedContent = *BannedContent; -*BannedContent = *LogBannedContent; -$BannedContent = $OldBannedContent; # copy variable +*LogOldBannedContent = *BannedContent; +*BannedContent = *LogNewBannedContent; +$BannedContent = $LogOldBannedContent; # copy variable -sub LogBannedContent { +sub LogNewBannedContent { my $str = shift; - my $rule = OldBannedContent($str); - if ($rule) { - my $visitor = $ENV{'REMOTE_ADDR'}; - ($sec, $min, $hr, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time); - $year=$year+1900; - $mon += 1; - # Fix for 0's - $mon = sprintf("%02d", $mon); - $mday = sprintf("%02d", $mday); - $sec = sprintf("%02d", $sec); - $min = sprintf("%02d", $min); - $hr = sprintf("%02d", $hr); - AppendStringToFile($BannedFile, "$year/$mon/$mday\t$hr:$min:$sec\t$visitor: $OpenPageName - $rule\n"); - } + my $rule = LogOldBannedContent($str); + LogWrite($rule) if $rule; return $rule; } + +*LogOldUserIsBanned = *UserIsBanned; +*UserIsBanned = *LogNewUserIsBanned; + +sub LogNewUserIsBanned { + my $str = shift; + my $rule = LogOldUserIsBanned($str); + LogWrite(Ts('Host or IP matched %s', $rule)) if $rule; + return $rule; +} + +sub LogWrite { + my $rule = shift; + my $id = $OpenPageName || GetId(); + AppendStringToFile($BannedFile, + join("\t", TimeToW3($Now), $ENV{'REMOTE_ADDR'}, $id, $rule) + . "\n"); +} From 925f0788fb88a4d2bce25e136f1fd9505c6bb777 Mon Sep 17 00:00:00 2001 From: Alex Schroeder Date: Thu, 31 Jan 2013 09:29:25 +0100 Subject: [PATCH 02/10] Add $1 to the explanation if such a group is part of the regular expression. --- modules/banned-regexps.pl | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/modules/banned-regexps.pl b/modules/banned-regexps.pl index 725497f3..bf027599 100644 --- a/modules/banned-regexps.pl +++ b/modules/banned-regexps.pl @@ -67,9 +67,12 @@ sub RegexpNewBannedContent { my ($regexp, $comment, $re) = ($1, $4, undef); eval { $re = qr/$regexp/i; }; if (defined($re) && $str =~ $re) { - $rule = Tss('Rule "%1" matched on this page.', QuoteHtml($regexp)) . ' ' + my $explanation = ($1 + ? Tss('Regular expression "%1" matched "%2" on this page.', QuoteHtml($regexp), $1) + : Ts('Regular expression "%s" matched on this page.', QuoteHtml($regexp))); + $rule = $explanation . ' ' . ($comment ? Ts('Reason: %s.', $comment) : T('Reason unknown.')) . ' ' - . Ts('See %s for more information.', GetPageLink($BannedRegexps)); + . Ts('See %s for more information.', GetPageLink($BannedRegexps)); last; } } From 1e6f732fa9b43832b3ca678991feec8fc9e02aa9 Mon Sep 17 00:00:00 2001 From: Alex Schroeder Date: Thu, 31 Jan 2013 09:43:05 +0100 Subject: [PATCH 03/10] Use LogWrite. --- modules/banned-regexps.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/banned-regexps.pl b/modules/banned-regexps.pl index 725497f3..4b9bfa94 100644 --- a/modules/banned-regexps.pl +++ b/modules/banned-regexps.pl @@ -74,7 +74,7 @@ sub RegexpNewBannedContent { } } } - LogBannedContent($rule) if $rule and $BannedFile; + LogWrite($rule) if $rule and $BannedFile; return $rule if $rule; return 0; } From 6e82239616d861e4828673cb68c092cf062dac19 Mon Sep 17 00:00:00 2001 From: Alex Schroeder Date: Fri, 1 Feb 2013 14:09:59 +0100 Subject: [PATCH 04/10] An extension to generate a table of content. --- modules/toc-js.pl | 81 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 modules/toc-js.pl diff --git a/modules/toc-js.pl b/modules/toc-js.pl new file mode 100644 index 00000000..453aa15a --- /dev/null +++ b/modules/toc-js.pl @@ -0,0 +1,81 @@ +# Copyright (C) 2013 Alex Schroeder +# +# 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 3 of the License, or (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 along with +# this program. If not, see . + +package OddMuse; + +$ModulesDescription .= '

toc-js.pl, see Javascript Table of Contents Extension

'; + +use vars qw($TocOutlineLibrary); + +$TocOutlineLibrary = 'http://h5o.googlecode.com/files/outliner.0.5.0.62.js'; + +# Add the dojo script to edit pages. +push (@MyInitVariables, \&TocScript); + +sub TocScript { + # cookie is not initialized yet so we cannot use GetParam + # Cross browser compatibility: http://www.tek-tips.com/faqs.cfm?fid=4862 + # HTML5 Outlines: http://blog.tremily.us/posts/HTML5_outlines/ + # Required library: http://code.google.com/p/h5o/ + if (GetParam('action', 'browse') eq 'browse') { + $HtmlHeaders .= qq{ + + +}; + } +} From 14a6cc4e2fba393a1f26bd9203ad2c88b921ac0c Mon Sep 17 00:00:00 2001 From: Alex Schroeder Date: Fri, 1 Feb 2013 15:05:04 +0100 Subject: [PATCH 05/10] Find existing TOC from toc.pl using the class attribute. --- modules/toc-js.pl | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/modules/toc-js.pl b/modules/toc-js.pl index 453aa15a..cf13c857 100644 --- a/modules/toc-js.pl +++ b/modules/toc-js.pl @@ -52,9 +52,19 @@ sub TocScript { } } - var initToc=function() - { + var initToc=function() { var toc = document.getElementById('toc'); + + if (!toc) { + var divs = document.getElementsByTagName('div'); + for (var i = 0; i < divs.length; i++) { + if (divs[i].getAttribute('class') == 'toc') { + toc = divs[i]; + break; + } + } + } + if (!toc) { var h2 = document.getElementsByTagName('h2')[0]; if (h2) { @@ -70,6 +80,7 @@ sub TocScript { outline.sections = outline.sections[0].sections; } var html = outline.asHTML(true); + toc.innerHTML = html; } } From 70895ed6317eb73c47a34a6dab95451a813d0fa3 Mon Sep 17 00:00:00 2001 From: Alex Schroeder Date: Fri, 1 Feb 2013 15:08:54 +0100 Subject: [PATCH 06/10] Only print outline when there is more than one element. --- modules/toc-js.pl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/modules/toc-js.pl b/modules/toc-js.pl index cf13c857..7ad92393 100644 --- a/modules/toc-js.pl +++ b/modules/toc-js.pl @@ -79,9 +79,11 @@ sub TocScript { if (outline.sections.length == 1) { outline.sections = outline.sections[0].sections; } - var html = outline.asHTML(true); - toc.innerHTML = html; + if (outline.sections.length > 1) { + var html = outline.asHTML(true); + toc.innerHTML = html; + } } } From cdee73b8596f82d7f1e758f280f9d6fbe7ca9751 Mon Sep 17 00:00:00 2001 From: Alex Schroeder Date: Fri, 1 Feb 2013 15:12:54 +0100 Subject: [PATCH 07/10] At least two siblings or parent and child nodes required. --- modules/toc-js.pl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/toc-js.pl b/modules/toc-js.pl index 7ad92393..3b014f8d 100644 --- a/modules/toc-js.pl +++ b/modules/toc-js.pl @@ -80,7 +80,8 @@ sub TocScript { outline.sections = outline.sections[0].sections; } - if (outline.sections.length > 1) { + if (outline.sections.length > 1 + || outline.sections[0].sections.length > 0) { var html = outline.asHTML(true); toc.innerHTML = html; } From f8df77d1a6e049f805563ed0b2a5c7f16b04e14b Mon Sep 17 00:00:00 2001 From: Alex Schroeder Date: Fri, 1 Feb 2013 15:17:52 +0100 Subject: [PATCH 08/10] Remove the TOC if we don't have enough sections. --- modules/toc-js.pl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/toc-js.pl b/modules/toc-js.pl index 3b014f8d..ea19b134 100644 --- a/modules/toc-js.pl +++ b/modules/toc-js.pl @@ -84,6 +84,8 @@ sub TocScript { || outline.sections[0].sections.length > 0) { var html = outline.asHTML(true); toc.innerHTML = html; + } else { + toc.parentNode.removeChild(toc); } } } From e772254293c15169864eb990929b433eb0b1d867 Mon Sep 17 00:00:00 2001 From: Alex Schroeder Date: Fri, 1 Feb 2013 15:26:02 +0100 Subject: [PATCH 09/10] Make sure we don't create an empty TOC element if there aren't enough sections. --- modules/toc-js.pl | 50 +++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/modules/toc-js.pl b/modules/toc-js.pl index ea19b134..02dbddb7 100644 --- a/modules/toc-js.pl +++ b/modules/toc-js.pl @@ -53,39 +53,39 @@ sub TocScript { } var initToc=function() { - var toc = document.getElementById('toc'); - if (!toc) { - var divs = document.getElementsByTagName('div'); - for (var i = 0; i < divs.length; i++) { - if (divs[i].getAttribute('class') == 'toc') { - toc = divs[i]; - break; - } - } + var outline = HTML5Outline(document.body); + if (outline.sections.length == 1) { + outline.sections = outline.sections[0].sections; } - if (!toc) { - var h2 = document.getElementsByTagName('h2')[0]; - if (h2) { - toc = document.createElement('div'); - toc.setAttribute('class', 'toc'); - h2.parentNode.insertBefore(toc, h2); - } - } + if (outline.sections.length > 1 + || outline.sections[0].sections.length > 0) { - if (toc) { - var outline = HTML5Outline(document.body); - if (outline.sections.length == 1) { - outline.sections = outline.sections[0].sections; + var toc = document.getElementById('toc'); + + if (!toc) { + var divs = document.getElementsByTagName('div'); + for (var i = 0; i < divs.length; i++) { + if (divs[i].getAttribute('class') == 'toc') { + toc = divs[i]; + break; + } + } } - if (outline.sections.length > 1 - || outline.sections[0].sections.length > 0) { + if (!toc) { + var h2 = document.getElementsByTagName('h2')[0]; + if (h2) { + toc = document.createElement('div'); + toc.setAttribute('class', 'toc'); + h2.parentNode.insertBefore(toc, h2); + } + } + + if (toc) { var html = outline.asHTML(true); toc.innerHTML = html; - } else { - toc.parentNode.removeChild(toc); } } } From 31fcd5dc997cbd960b3be8cf8ee1c71e8608e21f Mon Sep 17 00:00:00 2001 From: Alex Schroeder Date: Fri, 1 Feb 2013 15:30:31 +0100 Subject: [PATCH 10/10] Avoid a javascript error. --- modules/toc-js.pl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/toc-js.pl b/modules/toc-js.pl index 02dbddb7..a5022734 100644 --- a/modules/toc-js.pl +++ b/modules/toc-js.pl @@ -60,7 +60,8 @@ sub TocScript { } if (outline.sections.length > 1 - || outline.sections[0].sections.length > 0) { + || outline.sections.length == 1 + && outline.sections[0].sections.length > 0) { var toc = document.getElementById('toc');