preview.pl: New module to report changes in HTML

This commit is contained in:
Alex Schroeder
2015-09-21 09:12:32 +02:00
parent 831de74800
commit fa493d7360
2 changed files with 95 additions and 0 deletions

58
modules/preview.pl Normal file
View File

@@ -0,0 +1,58 @@
# Copyright (C) 2015 Alex Schroeder <alex@gnu.org>
#
# 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 <http://www.gnu.org/licenses/>.
use strict;
use v5.10;
=head1 Preview Extension
This module allows you to preview changes in HTML output. Oddmuse keeps a cache
of the HTML produced for each wiki page. If you install new modules, the HTML
produced for a page can change, but visitors will not see the new HTML because
the cache still contains the old HTML. Now you have two options: 1. clear the
HTML cache (Oddmuse will regenerate it as visitors look at the old pages), or 2.
edit each page in order to regenerate the HTML cache. What happens in practice
is that you add new modules and you aren't sure whether this breaks old pages
and so you do don't dare clear the HTML cache. Let sleeping dogs lie.
The Preview Extension produces a list of all the pages where the cached HTML
differs from the HTML produced by the current set of modules. If you agree with
the new HTML, feel free to clear the HTML cache. That's the point of this
extension.
=cut
our ($q, %Action, $UseCache);
AddModuleDescription('preview.pl', 'Preview Extension');
$Action{preview} = \&DoPreview;
sub DoPreview {
print GetHeader('', T('Pages with changed HTML'));
print $q->start_div({-class=>'content preview'}), $q->start_p();
foreach my $id (AllPagesList()) {
OpenPage($id);
my $cache = ToString(\&PrintPageHtml);
local $UseCache = 0;
my $html = ToString(\&PrintPageHtml);
if ($cache ne $html) {
print GetPageLink($id), ' ',
ScriptLink('action=browse;id=$id;cache=0', T('Preview')),
$q->br();
}
}
print $q->end_p(), $q->end_div();
PrintFooter();
}

37
t/preview.t Normal file
View File

@@ -0,0 +1,37 @@
# Copyright (C) 2015 Alex Schroeder <alex@gnu.org>
#
# 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 <http://www.gnu.org/licenses/>.
require 't/test.pl';
package OddMuse;
use Test::More tests => 5;
use utf8;
# This is a page whose HTML will change when we install the Markup Extension.
test_page(update_page('Test', 'This is *bold*.'), '\*bold\*');
# This is a page whose HTML will not change.
update_page('Boring', 'This is just text.');
# New markup module installed but HTML returned is cached.
add_module('markup.pl');
test_page(get_page('Test'), '\*bold\*');
# We can get the new HTML using cache=0.
test_page(get_page('Test?cache=0'), '<b>bold</b>');
# Install Preview Extension and see whether Test is listed and Boring is not.
add_module('preview.pl');
$page = get_page('action=preview');
test_page($page, 'Test');
test_page_negative($page, 'Boring');