(file) Return to Edit.pm CVS log (file) (dir) Up to [RizwankCVS] / geekymedia_web / twiki / lib / TWiki / UI

  1 rizwank 1.1 # TWiki Collaboration Platform, http://TWiki.org/
  2             #
  3             # Copyright (C) 1999-2004 Peter Thoeny, peter@thoeny.com
  4             #
  5             # Based on parts of Ward Cunninghams original Wiki and JosWiki.
  6             # Copyright (C) 1998 Markus Peter - SPiN GmbH (warpi@spin.de)
  7             # Some changes by Dave Harris (drh@bhresearch.co.uk) incorporated
  8             #
  9             # For licensing info read license.txt file in the TWiki root.
 10             # This program is free software; you can redistribute it and/or
 11             # modify it under the terms of the GNU General Public License
 12             # as published by the Free Software Foundation; either version 2
 13             # of the License, or (at your option) any later version.
 14             #
 15             # This program is distributed in the hope that it will be useful,
 16             # but WITHOUT ANY WARRANTY; without even the implied warranty of
 17             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18             # GNU General Public License for more details, published at 
 19             # http://www.gnu.org/copyleft/gpl.html
 20             =begin twiki
 21             
 22 rizwank 1.1 ---+ TWiki::UI::Edit
 23             Edit command handler
 24             
 25             =cut
 26             package TWiki::UI::Edit;
 27             
 28             use strict;
 29             use TWiki;
 30             use TWiki::Form;
 31             use TWiki::Plugins;
 32             use TWiki::Prefs;
 33             use TWiki::Store;
 34             use TWiki::UI;
 35             
 36             =pod
 37             
 38             ---++ edit( $webName, $topic, $userName, $query )
 39             Edit handler. Most parameters are in the CGI query:
 40             | =cmd= | |
 41             | =breaklock= | if defined, breaks any pre-existing lock before edit |
 42             | =onlywikiname= | if defined, requires a wiki name for the topic name if this is a new topic |
 43 rizwank 1.1 | =onlynewtopic= | if defined, and the topic exists, then moans |
 44             | =formtemplate= | name of the form for the topic; will replace existing form |
 45             | =templatetopic= | name of the topic to copy if creating a new topic |
 46             | =skin= | skin to use |
 47             | =topicparent= | what to put in the topic prent meta data |
 48             | =text= | text that will replace the old topic text if a formtemplate is defined (what the heck is this for?) |
 49             | =contenttype= | optional parameter that defines the application type to write into the CGI header. Defaults to text/html. |
 50             
 51             =cut
 52             sub edit {
 53               my ( $webName, $topic, $userName, $query ) = @_;
 54             
 55               my $saveCmd = $query->param( 'cmd' ) || "";
 56               my $breakLock = $query->param( 'breaklock' ) || "";
 57               my $onlyWikiName = $query->param( 'onlywikiname' ) || "";
 58               my $onlyNewTopic = $query->param( 'onlynewtopic' ) || "";
 59               my $formTemplate  = $query->param( "formtemplate" ) || "";
 60               my $templateTopic = $query->param( "templatetopic" ) || "";
 61               # apptype is undocumented legacy
 62               my $cgiAppType = $query->param( 'contenttype' ) || $query->param( 'apptype' ) || "text/html";
 63               my $skin = $query->param( "skin" );
 64 rizwank 1.1   my $theParent = $query->param( 'topicparent' ) || "";
 65               my $ptext = $query->param( 'text' );
 66             
 67               my $getValuesFromFormTopic = ( ( $formTemplate ) && ( ! $ptext ) );
 68             
 69               return unless TWiki::UI::webExists( $webName, $topic );
 70             
 71               return if TWiki::UI::isMirror( $webName, $topic );
 72             
 73               my $tmpl = "";
 74               my $text = "";
 75               my $meta = "";
 76               my $extra = "";
 77               my $topicExists  = &TWiki::Store::topicExists( $webName, $topic );
 78             
 79               # Prevent editing existing topic?
 80               if( $onlyNewTopic && $topicExists ) {
 81                 # Topic exists and user requested oops if it exists
 82                 TWiki::UI::oops( $webName, $topic, "createnewtopic" );
 83                 return;
 84               }
 85 rizwank 1.1 
 86               # prevent non-Wiki names?
 87               if( ( $onlyWikiName )
 88                   && ( ! $topicExists )
 89                   && ( ! ( &TWiki::isWikiName( $topic ) || &TWiki::isAbbrev( $topic ) ) ) ) {
 90                 # do not allow non-wikinames, redirect to view topic
 91                 TWiki::UI::redirect( TWiki::getViewUrl( $webName, $topic ) );
 92                 return;
 93               }
 94             
 95               # Read topic 
 96               if( $topicExists ) {
 97                 ( $meta, $text ) = &TWiki::Store::readTopic( $webName, $topic );
 98               }
 99             
100               my $wikiUserName = &TWiki::userToWikiName( $userName );
101               return unless TWiki::UI::isAccessPermitted( $webName, $topic,
102                                                         "change", $wikiUserName );
103             
104               # Special save command
105               return if( $saveCmd && ! TWiki::UI::userIsAdmin( $webName, $topic, $wikiUserName ));
106 rizwank 1.1 
107               # Check for locks
108               my( $lockUser, $lockTime ) = &TWiki::Store::topicIsLockedBy( $webName, $topic );
109               if( ( ! $breakLock ) && ( $lockUser ) ) {
110                 # warn user that other person is editing this topic
111                 $lockUser = &TWiki::userToWikiName( $lockUser );
112                 use integer;
113                 $lockTime = ( $lockTime / 60 ) + 1; # convert to minutes
114                 my $editLock = $TWiki::editLockTime / 60;
115                 TWiki::UI::oops( $webName, $topic, "locked",
116                                  $lockUser, $editLock, $lockTime );
117                 return;
118               }
119               &TWiki::Store::lockTopic( $topic );
120             
121               my $templateWeb = $webName;
122             
123               # Get edit template, standard or a different skin
124               $skin = TWiki::Prefs::getPreferencesValue( "SKIN" ) unless ( $skin );
125               $tmpl = &TWiki::Store::readTemplate( "edit", $skin );
126               unless( $topicExists ) {
127 rizwank 1.1     if( $templateTopic ) {
128                   if( $templateTopic =~ /^(.+)\.(.+)$/ ) {
129                     # is "Webname.SomeTopic"
130                     $templateWeb   = $1;
131                     $templateTopic = $2;
132                   }
133             
134                   ( $meta, $text ) = &TWiki::Store::readTopic( $templateWeb, $templateTopic );
135                 }
136                 unless( $text ) {
137                   ( $meta, $text ) = &TWiki::Store::readTemplateTopic( "WebTopicEditTemplate" );
138                 }
139                 $extra = "(not exist)";
140             
141                 # If present, instantiate form
142                 if( ! $formTemplate ) {
143                   my %args = $meta->findOne( "FORM" );
144                   $formTemplate = $args{"name"};
145                 }
146             
147                 $text = TWiki::expandVariablesOnTopicCreation( $text, $userName );
148 rizwank 1.1   }
149             
150               # parent setting
151               if( $theParent eq "none" ) {
152                 $meta->remove( "TOPICPARENT" );
153               } elsif( $theParent ) {
154                 if( $theParent =~ /^([^.]+)\.([^.]+)$/ ) {
155                   my $parentWeb = $1;
156                   if( $1 eq $webName ) {
157                     $theParent = $2;
158                   }
159                 }
160                 $meta->put( "TOPICPARENT", ( "name" => $theParent ) );
161               }
162               $tmpl =~ s/%TOPICPARENT%/$theParent/;
163             
164               # Processing of formtemplate - comes directly from query parameter formtemplate , 
165               # or indirectly from webtopictemplate parameter.
166               my $oldargsr;
167               if( $formTemplate ) {
168                 my @args = ( name => $formTemplate );
169 rizwank 1.1     $meta->remove( "FORM" );
170                 if( $formTemplate ne "none" ) {
171                   $meta->put( "FORM", @args );
172                 } else {
173                   $meta->remove( "FORM" );
174                 }
175                 $tmpl =~ s/%FORMTEMPLATE%/$formTemplate/go;
176                 if( defined $ptext ) {
177                   $text = $ptext;
178                   $text = &TWiki::Render::decodeSpecialChars( $text );
179                 }
180               }
181             
182               if( $saveCmd eq "repRev" ) {
183                 $text = TWiki::Store::readTopicRaw( $webName, $topic );
184               }
185             
186               $text =~ s/&/&amp\;/go;
187               $text =~ s/</&lt\;/go;
188               $text =~ s/>/&gt\;/go;
189               $text =~ s/\t/   /go;
190 rizwank 1.1 
191               #AS added hook for plugins that want to do heavy stuff
192               TWiki::Plugins::beforeEditHandler( $text, $topic, $webName ) unless( $saveCmd eq "repRev" );
193               #/AS
194             
195               if( $TWiki::doLogTopicEdit ) {
196                 # write log entry
197                 &TWiki::Store::writeLog( "edit", "$webName.$topic", $extra );
198               }
199             
200               if( $saveCmd ) {
201                 $tmpl =~ s/\(edit\)/\(edit cmd=$saveCmd\)/go;
202               }
203               $tmpl =~ s/%CMD%/$saveCmd/go;
204               $tmpl = &TWiki::handleCommonTags( $tmpl, $topic );
205               if( $saveCmd ne "repRev" ) {
206                 $tmpl = &TWiki::handleMetaTags( $webName, $topic, $tmpl, $meta );
207               } else {
208                 $tmpl =~ s/%META{[^}]*}%//go;
209               }
210               $tmpl = &TWiki::Render::getRenderedVersion( $tmpl );
211 rizwank 1.1 
212               # Don't want to render form fields, so this after getRenderedVersion
213               my %formMeta = $meta->findOne( "FORM" );
214               my $form = "";
215               $form = $formMeta{"name"} if( %formMeta );
216               if( $form && $saveCmd ne "repRev" ) {
217                 my @fieldDefs = &TWiki::Form::getFormDef( $templateWeb, $form );
218             
219                 if( ! @fieldDefs ) {
220                   TWiki::UI::oops( $webName, $topic, "noformdef" );
221                   return;
222                 }
223                 my $formText = &TWiki::Form::renderForEdit( $webName, $topic, $form, $meta, $query, $getValuesFromFormTopic, @fieldDefs );
224                 $tmpl =~ s/%FORMFIELDS%/$formText/go;
225               } elsif( $saveCmd ne "repRev" && TWiki::Prefs::getPreferencesValue( "WEBFORMS", $webName )) {
226             	# follows a hybrid html monster to let the 'choose form button' align at
227             	# the right of the page in all browsers
228                 $form = '<div style="text-align:right;"><table width="100%" border="0" cellspacing="0" cellpadding="0" class="twikiChangeFormButtonHolder"><tr><td align="right">'
229                   . &TWiki::Form::chooseFormButton( "Add form" )
230                     . '</td></tr></table></div>';
231                 $tmpl =~ s/%FORMFIELDS%/$form/go;
232 rizwank 1.1   } else {
233                 $tmpl =~ s/%FORMFIELDS%//go;
234               }
235             
236               $tmpl =~ s/%FORMTEMPLATE%//go; # Clear if not being used
237               $tmpl =~ s/%TEXT%/$text/go;
238               $tmpl =~ s/( ?) *<\/?(nop|noautolink)\/?>\n?/$1/gois;   # remove <nop> and <noautolink> tags
239             
240               TWiki::writeHeaderFull ( $query, 'edit', $cgiAppType, length($tmpl) );
241             
242               print $tmpl;
243             }
244             
245             1;

Rizwan Kassim
Powered by
ViewCVS 0.9.2