1 rizwank 1.1 # TWiki Collaboration Platform, http://TWiki.org/
2 #
3 # Copyright (C) 1999-2004 Peter Thoeny, peter@thoeny.com
4 #
5 # For licensing info read license.txt file in the TWiki root.
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details, published at
15 # http://www.gnu.org/copyleft/gpl.html
16 package TWiki::UI::Preview;
17
18 use strict;
19 use TWiki;
20 use TWiki::UI;
21
22 rizwank 1.1 sub preview {
23 my ( $webName, $topic, $userName, $query ) = @_;
24
25 my $skin = $query->param( "skin" );
26 my $changeform = $query->param( 'submitChangeForm' ) || "";
27 my $dontNotify = $query->param( "dontnotify" ) || "";
28 my $saveCmd = $query->param( "cmd" ) || "";
29 my $theParent = $query->param( 'topicparent' ) || "";
30 my $formTemplate = $query->param( "formtemplate" );
31 my $textparam = $query->param( "text" );
32
33 return unless TWiki::UI::webExists( $webName, $topic );
34
35 my $tmpl = "";
36 my $text = "";
37 my $ptext = "";
38 my $meta = "";
39 my $formFields = "";
40 my $wikiUserName = &TWiki::userToWikiName( $userName );
41
42 return if TWiki::UI::isMirror( $webName, $topic );
43 rizwank 1.1
44 # reset lock time, this is to prevent contention in case of a long edit session
45 &TWiki::Store::lockTopic( $topic );
46
47 $skin = &TWiki::Prefs::getPreferencesValue( "SKIN" ) unless( $skin );
48
49 # Is user looking to change the form used? Sits oddly in preview, but
50 # to avoid Javascript and pick up text on edit page it has to be in preview.
51 if( $changeform ) {
52 &TWiki::Form::changeForm( $webName, $topic, $query );
53 return;
54 }
55
56 # get view template, standard view or a view with a different skin
57 $tmpl = &TWiki::Store::readTemplate( "preview", $skin );
58 $tmpl =~ s/%DONTNOTIFY%/$dontNotify/go;
59 if( $saveCmd ) {
60 return unless TWiki::UI::userIsAdmin( $webName, $topic, $wikiUserName );
61 $tmpl =~ s/\(preview\)/\(preview cmd=$saveCmd\)/go;
62 }
63 $tmpl =~ s/%CMD%/$saveCmd/go;
64 rizwank 1.1
65 if( $saveCmd ne "repRev" ) {
66 my $dummy = "";
67 ( $meta, $dummy ) = &TWiki::Store::readTopic( $webName, $topic );
68
69 # parent setting
70 if( $theParent eq "none" ) {
71 $meta->remove( "TOPICPARENT" );
72 } elsif( $theParent ) {
73 $meta->put( "TOPICPARENT", ( "name" => $theParent ) );
74 }
75 $tmpl =~ s/%TOPICPARENT%/$theParent/go;
76
77 if( $formTemplate ) {
78 $meta->remove( "FORM" );
79 $meta->put( "FORM", ( name => $formTemplate ) ) if( $formTemplate ne "none" );
80 $tmpl =~ s/%FORMTEMPLATE%/$formTemplate/go;
81 } else {
82 $tmpl =~ s/%FORMTEMPLATE%//go;
83 }
84
85 rizwank 1.1 # get the edited text and combine text, form and attachments for preview
86 &TWiki::Form::fieldVars2Meta( $webName, $query, $meta );
87 $text = $textparam;
88 if( ! $text ) {
89 # empty topic not allowed
90 TWiki::UI::oops( $webName, $topic, "empty" );
91 return;
92 }
93 #AS added hook for plugins that want to do heavy stuff
94 TWiki::Plugins::afterEditHandler( $text, $topic, $webName );
95 $ptext = $text;
96
97 if( $meta->count( "FORM" ) ) {
98 $formFields = &TWiki::Form::getFieldParams( $meta );
99 }
100 } else {
101 # undocumented "repRev" mode
102 $text = $textparam; # text to save
103 ( $meta, $ptext ) = &TWiki::Store::_extractMetaData( $webName, $topic, $text );
104 $text =~ s/%_(.)_%/%__$1__%/go;
105 }
106 rizwank 1.1
107 my @verbatim = ();
108 $ptext = &TWiki::takeOutVerbatim( $ptext, \@verbatim );
109 $ptext =~ s/ {3}/\t/go;
110 $ptext = &TWiki::Prefs::updateSetFromForm( $meta, $ptext );
111 $ptext = &TWiki::handleCommonTags( $ptext, $topic );
112 $ptext = &TWiki::Render::getRenderedVersion( $ptext );
113
114 # do not allow click on link before save: (mods by TedPavlic)
115 my $oopsUrl = '%SCRIPTURLPATH%/oops%SCRIPTSUFFIX%/%WEB%/%TOPIC%';
116 $oopsUrl = &TWiki::handleCommonTags( $oopsUrl, $topic );
117 $ptext =~ s@(?<=<a\s)([^>]*)(href=(?:".*?"|[^"].*?(?=[\s>])))@$1href="$oopsUrl?template=oopspreview"@goi;
118 $ptext =~ s@<form(?:|\s.*?)>@<form action="$oopsUrl">\n<input type="hidden" name="template" value="oopspreview">@goi;
119 $ptext =~ s@(?<=<)([^\s]+?[^>]*)(onclick=(?:"location.href='.*?'"|location.href='[^']*?'(?=[\s>])))@$1onclick="location.href='$oopsUrl\?template=oopspreview'"@goi;
120
121 $ptext = &TWiki::putBackVerbatim( $ptext, "pre", @verbatim );
122
123 $tmpl = &TWiki::handleCommonTags( $tmpl, $topic );
124 $tmpl = &TWiki::handleMetaTags( $webName, $topic, $tmpl, $meta );
125 $tmpl = &TWiki::Render::getRenderedVersion( $tmpl );
126 $tmpl =~ s/%TEXT%/$ptext/go;
127 rizwank 1.1
128 $text = &TWiki::Render::encodeSpecialChars( $text );
129
130 $tmpl =~ s/%HIDDENTEXT%/$text/go;
131 $tmpl =~ s/%FORMFIELDS%/$formFields/go;
132 $tmpl =~ s/( ?) *<\/?(nop|noautolink)\/?>\n?/$1/gois; # remove <nop> and <noautolink> tags
133
134 &TWiki::writeHeader( $query );
135 print $tmpl;
136 }
137
138 1;
|