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

  1 rizwank 1.1 #
  2             # TWiki Collaboration Platform, http://TWiki.org/
  3             #
  4             # Copyright (C) 1999-2004 Peter Thoeny, peter@thoeny.com
  5             #
  6             # Based on parts of Ward Cunninghams original Wiki and JosWiki.
  7             # Copyright (C) 1998 Markus Peter - SPiN GmbH (warpi@spin.de)
  8             # Some changes by Dave Harris (drh@bhresearch.co.uk) incorporated
  9             #
 10             # For licensing info read license.txt file in the TWiki root.
 11             # This program is free software; you can redistribute it and/or
 12             # modify it under the terms of the GNU General Public License
 13             # as published by the Free Software Foundation; either version 2
 14             # of the License, or (at your option) any later version.
 15             #
 16             # This program is distributed in the hope that it will be useful,
 17             # but WITHOUT ANY WARRANTY; without even the implied warranty of
 18             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 19             # GNU General Public License for more details, published at 
 20             # http://www.gnu.org/copyleft/gpl.html
 21             =begin twiki
 22 rizwank 1.1 
 23             ---+ TWiki::UI::Save
 24             
 25             UI delegate for save function
 26             
 27             =cut
 28             
 29             package TWiki::UI::Save;
 30             
 31             use strict;
 32             use TWiki;
 33             use TWiki::UI;
 34             use TWiki::UI::Preview;
 35             
 36             =pod
 37             
 38             ---++ save( )
 39             Command handler for save command. Some parameters are passed in CGI:
 40             | =cmd= | |
 41             | =text= | target text for the topic |
 42             | =unlock= | if defined, unlock the written topic |
 43 rizwank 1.1 | =dontnotify= | if defined, suppress change notification |
 44             | =submitChangeForm= | |
 45             | =topicparent= | |
 46             | =formtemplate= | if define, use the named template for the form |
 47             Note that this function is rundundant once savemulti takes over all saving
 48             duties.
 49             
 50             =cut
 51             
 52             sub save {
 53               my( $webName, $topic, $userName, $query ) = @_;
 54               if ( _save( @_ )) {
 55                 TWiki::redirect( $query, TWiki::getViewUrl( TWiki::Store::normalizeWebTopicName($webName, $topic)) );
 56               }
 57             }
 58             
 59             # Private - do not call outside this module!
 60             sub _save {
 61               my( $webName, $topic, $userName, $query ) = @_;
 62             
 63               my $saveCmd = $query->param( "cmd" ) || "";
 64 rizwank 1.1   my $text = $query->param( "text" );
 65               my $meta = "";
 66             
 67               # A template was requested; read it, and expand URLPARAMs within the
 68               # template using our CGI record
 69               my $templatetopic = $query->param( "templatetopic");
 70               if ($templatetopic) {
 71                 ($meta, $text) = &TWiki::Store::readTopic( $webName, $templatetopic );
 72                 $text = TWiki::expandVariablesOnTopicCreation( $text );
 73               }
 74             	
 75               my $unlock = $query->param( "unlock" ) || "";
 76               my $dontNotify = $query->param( "dontnotify" ) || "";
 77               my $changeform = $query->param( 'submitChangeForm' ) || "";
 78               my $theParent = $query->param( 'topicparent' ) || "";
 79               my $onlyWikiName = $query->param( 'onlywikiname' ) || "";
 80               my $onlyNewTopic = $query->param( 'onlynewtopic' ) || "";
 81               my $formTemplate = $query->param( "formtemplate" );
 82             
 83               my $topicExists  = TWiki::Store::topicExists( $webName, $topic );
 84             
 85 rizwank 1.1   return 0 unless TWiki::UI::webExists( $webName, $topic );
 86             
 87               return 0 if TWiki::UI::isMirror( $webName, $topic );
 88             
 89               # Prevent saving existing topic?
 90               if( $onlyNewTopic && $topicExists ) {
 91                 # Topic exists and user requested oops if it exists
 92                 TWiki::UI::oops( $webName, $topic, "createnewtopic" );
 93                 return 0;
 94               }
 95             
 96               # prevent non-Wiki names?
 97               if( ( $onlyWikiName )
 98                   && ( ! $topicExists )
 99                   && ( ! ( &TWiki::isWikiName( $topic ) || &TWiki::isAbbrev( $topic ) ) ) ) {
100                 # do not allow non-wikinames, redirect to view topic
101                 TWiki::UI::redirect( TWiki::getViewUrl( $webName, $topic ) );
102                 return 0;
103               }
104             
105               my $wikiUserName = TWiki::userToWikiName( $userName );
106 rizwank 1.1   return 0 unless TWiki::UI::isAccessPermitted( $webName, $topic,
107                                                         "change", $wikiUserName );
108             
109               # check permission for undocumented cmd=... parameter
110               return 0 if ( $saveCmd &&
111                           ! TWiki::UI::userIsAdmin( $webName, $topic, $wikiUserName ));
112             
113               # PTh 06 Nov 2000: check if proper use of save script
114               if( ! ( defined $text ) ) {
115                 TWiki::UI::oops( $webName, $topic, "save" );
116                 return 0;
117               } elsif( ! $text ) {
118                 # empty topic not allowed
119                 TWiki::UI::oops( $webName, $topic, "empty" );
120                 return 0;
121               }
122             
123               if( $changeform ) {
124                 use TWiki::Form;
125                 TWiki::Form::changeForm( $webName, $topic, $query );
126                 return 0;
127 rizwank 1.1   }
128             
129               $text = TWiki::Render::decodeSpecialChars( $text );
130               $text =~ s/ {3}/\t/go;
131             
132               if( $saveCmd eq "repRev" ) {
133                 $text =~ s/%__(.)__%/%_$1_%/go;
134                 ( $meta, $text ) = TWiki::Store::_extractMetaData( $webName, $topic, $text );
135               } else {
136                 # normal case: Get latest attachment from file for preview
137                 my $tmp;
138             	# read meta (if not already read when reading template)
139                 ( $meta, $tmp ) = TWiki::Store::readTopic( $webName, $topic ) unless $meta;
140             
141                 # parent setting
142                 if( $theParent eq "none" ) {
143                   $meta->remove( "TOPICPARENT" );
144                 } elsif( $theParent ) {
145                   $meta->put( "TOPICPARENT", ( "name" => $theParent ) );
146                 }
147             
148 rizwank 1.1     if( $formTemplate ) {
149                   $meta->remove( "FORM" );
150                   $meta->put( "FORM", ( name => $formTemplate ) ) if( $formTemplate ne "none" );
151                 }
152             
153                 use TWiki::Form;
154                 # CODE_SMELL: this fieldVars2Meta thing should be in UI, not Meta
155             	# Expand field variables, unless this new page is templated
156                 TWiki::Form::fieldVars2Meta( $webName, $query, $meta ) unless $templatetopic;
157                 use TWiki::Prefs;
158                 $text = TWiki::Prefs::updateSetFromForm( $meta, $text );
159               }
160             
161               my $error = TWiki::Store::saveTopic( $webName, $topic, $text, $meta, $saveCmd, $unlock, $dontNotify );
162               if( $error ) {
163                 TWiki::UI::oops( $webName, $topic, "saveerr", $error );
164                 return 0;
165               }
166             
167               return 1;
168             }
169 rizwank 1.1 
170             =pod
171             
172             ---++ savemulti( )
173             Command handler for savemulti command. Some parameters are passed in CGI:
174             | =action= | savemulti overrides, everything else is passed on the normal =save= |
175             action values are:
176             | =save= | save, unlock topic, return to view, dontnotify is OFF |
177             | =quietsave= | save, unlock topic,  return to view, dontnotify is ON |
178             | =checkpoint= | save and continue editing, dontnotify is ON |
179             | =cancel= | exit without save, unlock topic, return to view (does _not_ undo Checkpoint saves) |
180             | =preview= | preview edit text; same as before |
181             This function can replace "save" eventually.
182             
183             =cut
184             
185             sub savemulti {
186               my( $webName, $topic, $userName, $query ) = @_;
187             
188               my $redirecturl = TWiki::getViewUrl( TWiki::Store::normalizeWebTopicName($webName, $topic));
189             
190 rizwank 1.1   my $saveaction = lc($query->param( 'action' ));
191               if ( $saveaction eq "checkpoint" ) {
192                 $query->param( -name=>"dontnotify", -value=>"checked" );
193                 $query->param( -name=>"unlock", -value=>'0' );
194                 my $editURL = TWiki::getScriptUrl( $webName, $topic, "edit" );
195                 my $randompart = randomURL();
196                 $redirecturl = "$editURL|$randompart";
197               } elsif ( $saveaction eq "quietsave" ) {
198                 $query->param( -name=>"dontnotify", -value=>"checked" );
199               } elsif ( $saveaction eq "cancel" ) {
200                 my $viewURL = TWiki::getScriptUrl( $webName, $topic, "view" );
201                 TWiki::redirect( $query, "$viewURL?unlock=on" );
202                 return;
203               } elsif( $saveaction eq "preview" ) {
204                 TWiki::UI::Preview::preview( $webName, $topic, $userName, $query );
205                 return;
206               }
207             
208               # save called by preview
209               if ( _save( $webName, $topic, $userName, $query )) {
210                 TWiki::redirect( $query, $redirecturl );
211 rizwank 1.1   }
212             }
213             
214             ## Random URL:
215             # returns 4 random bytes in 0x01-0x1f range in %xx form
216             # =========================
217             sub randomURL
218             {
219               my (@hc) = (qw (01 02 03 04 05 06 07 08 09 0b 0c 0d 0e 0f 10
220             		  11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f));
221               #  srand; # needed only for perl < 5.004
222               return "%$hc[rand(30)]%$hc[rand(30)]%$hc[rand(30)]%$hc[rand(30)]";
223             }
224             
225             1;
226             

Rizwan Kassim
Powered by
ViewCVS 0.9.2