lib/TWiki/UI/Save.pm
changeset 0 414e01d06fd5
child 1 e2915a7cbdfa
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lib/TWiki/UI/Save.pm	Sat Jan 26 15:50:53 2008 +0100
     1.3 @@ -0,0 +1,569 @@
     1.4 +# Module of TWiki Enterprise Collaboration Platform, http://TWiki.org/
     1.5 +#
     1.6 +# Copyright (C) 1999-2007 Peter Thoeny, peter@thoeny.org
     1.7 +# and TWiki Contributors. All Rights Reserved. TWiki Contributors
     1.8 +# are listed in the AUTHORS file in the root of this distribution.
     1.9 +# NOTE: Please extend that file, not this notice.
    1.10 +#
    1.11 +# Additional copyrights apply to some or all of the code in this
    1.12 +# file as follows:
    1.13 +# Based on parts of Ward Cunninghams original Wiki and JosWiki.
    1.14 +# Copyright (C) 1998 Markus Peter - SPiN GmbH (warpi@spin.de)
    1.15 +# Some changes by Dave Harris (drh@bhresearch.co.uk) incorporated
    1.16 +#
    1.17 +# This program is free software; you can redistribute it and/or
    1.18 +# modify it under the terms of the GNU General Public License
    1.19 +# as published by the Free Software Foundation; either version 2
    1.20 +# of the License, or (at your option) any later version. For
    1.21 +# more details read LICENSE in the root of this distribution.
    1.22 +#
    1.23 +# This program is distributed in the hope that it will be useful,
    1.24 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.25 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    1.26 +#
    1.27 +# As per the GPL, removal of this notice is prohibited.
    1.28 +
    1.29 +=pod
    1.30 +
    1.31 +---+ package TWiki::UI::Save
    1.32 +
    1.33 +UI delegate for save function
    1.34 +
    1.35 +=cut
    1.36 +
    1.37 +package TWiki::UI::Save;
    1.38 +
    1.39 +use strict;
    1.40 +use Error qw( :try );
    1.41 +use Assert;
    1.42 +
    1.43 +require TWiki;
    1.44 +require TWiki::UI;
    1.45 +require TWiki::Meta;
    1.46 +require TWiki::OopsException;
    1.47 +
    1.48 +# Used by save and preview
    1.49 +sub buildNewTopic {
    1.50 +    my( $session, $script ) = @_;
    1.51 +
    1.52 +    my $query = $session->{cgiQuery};
    1.53 +    my $webName = $session->{webName};
    1.54 +    my $topic = $session->{topicName};
    1.55 +    my $store = $session->{store};
    1.56 +    my $revision = $query->param( 'rev' ) || undef;
    1.57 +
    1.58 +    unless( scalar($query->param()) ) {
    1.59 +        # insufficient parameters to save
    1.60 +        throw TWiki::OopsException(
    1.61 +            'attention',
    1.62 +            def => 'bad_script_parameters',
    1.63 +            web => $session->{webName},
    1.64 +            topic => $session->{topicName},
    1.65 +            params => [ $script ]);
    1.66 +    }
    1.67 +
    1.68 +    TWiki::UI::checkMirror( $session, $webName, $topic );
    1.69 +    TWiki::UI::checkWebExists( $session, $webName, $topic, 'save' );
    1.70 +
    1.71 +    my $topicExists  = $store->topicExists( $webName, $topic );
    1.72 +
    1.73 +    # Prevent saving existing topic?
    1.74 +    my $onlyNewTopic = TWiki::isTrue( $query->param( 'onlynewtopic' ));
    1.75 +    if( $onlyNewTopic && $topicExists ) {
    1.76 +        # Topic exists and user requested oops if it exists
    1.77 +        throw TWiki::OopsException( 'attention',
    1.78 +                                    def => 'topic_exists',
    1.79 +                                    web => $webName,
    1.80 +                                    topic => $topic );
    1.81 +    }
    1.82 +
    1.83 +    # prevent non-Wiki names?
    1.84 +    my $onlyWikiName = TWiki::isTrue( $query->param( 'onlywikiname' ));
    1.85 +    if( ( $onlyWikiName )
    1.86 +          && ( ! $topicExists )
    1.87 +            && ( ! TWiki::isValidTopicName( $topic ) ) ) {
    1.88 +        # do not allow non-wikinames
    1.89 +        throw TWiki::OopsException(
    1.90 +            'attention',
    1.91 +            def => 'not_wikiword',
    1.92 +            web => $webName,
    1.93 +            topic => $topic,
    1.94 +            params => [ $topic ] );
    1.95 +    }
    1.96 +
    1.97 +    my $user = $session->{user};
    1.98 +    TWiki::UI::checkAccess( $session, $webName, $topic,
    1.99 +                            'CHANGE', $user );
   1.100 +
   1.101 +    my $saveOpts = {};
   1.102 +    $saveOpts->{minor} = 1 if $query->param( 'dontnotify' );
   1.103 +    my $originalrev = $query->param( 'originalrev' ); # rev edit started on
   1.104 +
   1.105 +    # Populate the new meta data
   1.106 +    my $newMeta = new TWiki::Meta( $session, $webName, $topic );
   1.107 +
   1.108 +    my ( $prevMeta, $prevText );
   1.109 +    my ( $templateText, $templateMeta );
   1.110 +    my $templatetopic = $query->param( 'templatetopic');
   1.111 +    my $templateweb = $webName;
   1.112 +
   1.113 +    if( $topicExists ) {
   1.114 +        ( $prevMeta, $prevText ) =
   1.115 +          $store->readTopic( $user, $webName, $topic, $revision );
   1.116 +        if( $prevMeta ) {
   1.117 +            foreach my $k ( keys %$prevMeta ) {
   1.118 +                unless( $k =~ /^_/ || $k eq 'FORM' || $k eq 'TOPICPARENT' ||
   1.119 +                          $k eq 'FIELD' ) {
   1.120 +                    $newMeta->copyFrom( $prevMeta, $k );
   1.121 +                }
   1.122 +            }
   1.123 +        }
   1.124 +    } elsif ($templatetopic) {
   1.125 +        ( $templateweb, $templatetopic ) =
   1.126 +          $session->normalizeWebTopicName( $templateweb, $templatetopic );
   1.127 +
   1.128 +        ( $templateMeta, $templateText ) =
   1.129 +          $store->readTopic( $user, $templateweb,
   1.130 +                             $templatetopic, $revision );
   1.131 +        $templateText = '' if $query->param( 'newtopic' ); # created by edit
   1.132 +        $templateText =
   1.133 +          $session->expandVariablesOnTopicCreation(
   1.134 +              $templateText, $user, $webName, $topic );
   1.135 +        foreach my $k ( keys %$templateMeta ) {
   1.136 +            unless( $k =~ /^_/ || $k eq 'FORM' || $k eq 'TOPICPARENT' ||
   1.137 +                      $k eq 'FIELD' || $k eq 'TOPICMOVED' ) {
   1.138 +                $newMeta->copyFrom( $templateMeta, $k );
   1.139 +            }
   1.140 +        }
   1.141 +        # topic creation, there is no original rev
   1.142 +        $originalrev = 0;
   1.143 +    }
   1.144 +
   1.145 +    # Determine the new text
   1.146 +    my $newText = $query->param( 'text' );
   1.147 +
   1.148 +    my $forceNewRev = $query->param( 'forcenewrevision' );
   1.149 +    $saveOpts->{forcenewrevision} = $forceNewRev;
   1.150 +    my $newParent = $query->param( 'topicparent' );
   1.151 +
   1.152 +    if( defined( $newText) ) {
   1.153 +        # text is defined in the query, save that text
   1.154 +        $newText =~ s/\r//g;
   1.155 +        $newText .= "\n" unless $newText =~ /\n$/s;
   1.156 +
   1.157 +    } elsif( defined $templateText ) {
   1.158 +        # no text in the query, but we have a templatetopic
   1.159 +        $newText = $templateText;
   1.160 +        $originalrev = 0; # disable merge
   1.161 +
   1.162 +    } else {
   1.163 +        $newText = '';
   1.164 +        if( defined $prevText ) {
   1.165 +            $newText = $prevText;
   1.166 +            $originalrev = 0; # disable merge
   1.167 +        }
   1.168 +    }
   1.169 +
   1.170 +    my $mum;
   1.171 +    if( $newParent ) {
   1.172 +        if( $newParent ne 'none' ) {
   1.173 +            $mum = { 'name' => $newParent };
   1.174 +        }
   1.175 +    } elsif( $templateMeta ) {
   1.176 +        $mum = $templateMeta->get( 'TOPICPARENT' );
   1.177 +    } elsif( $prevMeta ) {
   1.178 +        $mum = $prevMeta->get( 'TOPICPARENT' );
   1.179 +    }
   1.180 +    $newMeta->put( 'TOPICPARENT', $mum ) if $mum;
   1.181 +
   1.182 +    my $formName = $query->param( 'formtemplate' );
   1.183 +    my $formDef;
   1.184 +    my $copyMeta;
   1.185 +
   1.186 +    if( $formName ) {
   1.187 +        # new form, default field values will be null
   1.188 +        $formName = '' if( $formName eq 'none' );
   1.189 +    } elsif( $templateMeta ) {
   1.190 +        # populate the meta-data with field values from the template
   1.191 +        $formName = $templateMeta->get( 'FORM' );
   1.192 +        $formName = $formName->{name} if $formName;;
   1.193 +        $copyMeta = $templateMeta;
   1.194 +    } elsif( $prevMeta ) {
   1.195 +        # populate the meta-data with field values from the existing topic
   1.196 +        $formName = $prevMeta->get( 'FORM' );
   1.197 +        $formName = $formName->{name} if $formName;;
   1.198 +        $copyMeta = $prevMeta;
   1.199 +    }
   1.200 +
   1.201 +    if( $formName ) {
   1.202 +        require TWiki::Form;
   1.203 +        $formDef = new TWiki::Form( $session, $webName, $formName );
   1.204 +        unless( $formDef ) {
   1.205 +            throw TWiki::OopsException(
   1.206 +                'attention',
   1.207 +                def => 'no_form_def',
   1.208 +                web => $session->{webName},
   1.209 +                topic => $session->{topicName},
   1.210 +                params => [ $webName, $formName ] );
   1.211 +        }
   1.212 +        $newMeta->put( 'FORM', { name => $formName });
   1.213 +    }
   1.214 +    if( $copyMeta && $formDef ) {
   1.215 +        # Copy existing fields into new form, filtering on the
   1.216 +        # known field names so we don't copy dead data. Though we
   1.217 +        # really should, of course. That comes later.
   1.218 +        my $filter = join(
   1.219 +            '|',
   1.220 +            map { $_->{name} }
   1.221 +              grep { $_->{name} } @{$formDef->getFields()} );
   1.222 +        $newMeta->copyFrom( $copyMeta, 'FIELD', qr/^($filter)$/ );
   1.223 +    }
   1.224 +    if( $formDef ) {
   1.225 +        # override with values from the query
   1.226 +        my( $seen, $missing ) =
   1.227 +          $formDef->getFieldValuesFromQuery( $query, $newMeta );
   1.228 +        if( $seen && @$missing ) {
   1.229 +            # chuck up if there is at least one field value defined in the
   1.230 +            # query and a mandatory field was not defined in the
   1.231 +            # query or by an existing value.
   1.232 +            throw TWiki::OopsException(
   1.233 +                'attention',
   1.234 +                def=>'mandatory_field',
   1.235 +                web => $session->{webName},
   1.236 +                topic => $session->{topicName},
   1.237 +                params => [ join( ' ', @$missing ) ] );
   1.238 +        }
   1.239 +    }
   1.240 +
   1.241 +    my $merged;
   1.242 +    # assumes rev numbers start at 1
   1.243 +    if( $originalrev ) {
   1.244 +        my( $orev, $odate );
   1.245 +        if( $originalrev =~ /^(\d+)_(\d+)$/ ) {
   1.246 +            ( $orev, $odate ) = ( $1, $2 );
   1.247 +        } elsif( $originalrev =~ /^\d+$/ ) {
   1.248 +            $orev = $originalrev;
   1.249 +        } else {
   1.250 +            $orev = 0;
   1.251 +        }
   1.252 +        my( $date, $author, $rev, $comment ) = $newMeta->getRevisionInfo();
   1.253 +        # If the last save was by me, don't merge
   1.254 +        if(( $orev ne $rev ||
   1.255 +               $odate && $date && $odate ne $date ) &&
   1.256 +                 $author ne $user ) {
   1.257 +
   1.258 +            require TWiki::Merge;
   1.259 +
   1.260 +            my $pti = $prevMeta->get( 'TOPICINFO' );
   1.261 +            if( $pti->{reprev} && $pti->{version} &&
   1.262 +                  $pti->{reprev} == $pti->{version} ) {
   1.263 +                # If the ancestor revision was generated by a reprev,
   1.264 +                # then the original is lost and we can't 3-way merge
   1.265 +
   1.266 +                $session->{plugins}->_dispatch(
   1.267 +                    'beforeMergeHandler',
   1.268 +                    $newText,
   1.269 +                    $pti->{version}, $prevText,
   1.270 +                    undef, undef,
   1.271 +                    $webName, $topic);
   1.272 +
   1.273 +                $newText = TWiki::Merge::merge2(
   1.274 +                    $pti->{version}, $prevText,
   1.275 +                    $rev, $newText,
   1.276 +                    '.*?\n',
   1.277 +                    $session );
   1.278 +            } else {
   1.279 +                # common ancestor; we can 3-way merge
   1.280 +                my( $ancestorMeta, $ancestorText ) =
   1.281 +                  $store->readTopic( undef, $webName, $topic, $orev );
   1.282 +
   1.283 +                $session->{plugins}->_dispatch(
   1.284 +                    'beforeMergeHandler',
   1.285 +                    $newText,
   1.286 +                    $rev, $prevText,
   1.287 +                    $orev, $ancestorText,
   1.288 +                    $webName, $topic);
   1.289 +
   1.290 +                $newText = TWiki::Merge::merge3(
   1.291 +                    $orev, $ancestorText,
   1.292 +                    $rev, $prevText,
   1.293 +                    'new', $newText,
   1.294 +                    '.*?\n', $session );
   1.295 +            }
   1.296 +            if( $formDef && $prevMeta ) {
   1.297 +                $newMeta->merge( $prevMeta, $formDef );
   1.298 +            }
   1.299 +            $merged = [ $orev, $author, $rev||1 ];
   1.300 +        }
   1.301 +    }
   1.302 +
   1.303 +    return( $newMeta, $newText, $saveOpts, $merged );
   1.304 +}
   1.305 +
   1.306 +=pod
   1.307 +
   1.308 +---++ StaticMethod save($session)
   1.309 +
   1.310 +Command handler for =save= command.
   1.311 +This method is designed to be
   1.312 +invoked via the =UI::run= method.
   1.313 +
   1.314 +See TWiki.TWikiScripts for details of parameters.
   1.315 +
   1.316 +Note: =cmd= has been deprecated in favour of =action=. It will be deleted at
   1.317 +some point.
   1.318 +
   1.319 +=cut
   1.320 +
   1.321 +sub save {
   1.322 +    my $session = shift;
   1.323 +
   1.324 +    my $query = $session->{cgiQuery};
   1.325 +    my $web = $session->{webName};
   1.326 +    my $topic = $session->{topicName};
   1.327 +    my $store = $session->{store};
   1.328 +    my $user = $session->{user};
   1.329 +
   1.330 +    # Do not remove, keep as undocumented feature for compatibility with
   1.331 +    # TWiki 4.0.x: Allow for dynamic topic creation by replacing strings
   1.332 +    # of at least 10 x's XXXXXX with a next-in-sequence number.
   1.333 +    # See Codev.AllowDynamicTopicNameCreation
   1.334 +    if ( $topic =~ /X{10}/ ) {
   1.335 +		my $n = 0;
   1.336 +		my $baseTopic = $topic;
   1.337 +		$store->clearLease( $web, $baseTopic );
   1.338 +		do {
   1.339 +			$topic = $baseTopic;
   1.340 +			$topic =~ s/X{10}X*/$n/e;
   1.341 +			$n++;
   1.342 +		} while( $store->topicExists( $web, $topic ));
   1.343 +        $session->{topicName} = $topic;
   1.344 +    }
   1.345 +
   1.346 +    # Allow for more flexible topic creation with sortable names and
   1.347 +    # better performance. See Codev.AutoIncTopicNameOnSave
   1.348 +    if( $topic =~ /AUTOINC([0-9]+)/ ) {
   1.349 +        my $start = $1;
   1.350 +        my $baseTopic = $topic;
   1.351 +        $store->clearLease( $web, $baseTopic );
   1.352 +        my $nameFilter = $topic;
   1.353 +        $nameFilter =~ s/AUTOINC([0-9]+)/([0-9]+)/;
   1.354 +        my @list =
   1.355 +          sort{ $a <=> $b }
   1.356 +            map{ s/^$nameFilter$/$1/; s/^0*([0-9])/$1/; $_ }
   1.357 +              grep{ /^$nameFilter$/ }
   1.358 +                $store->getTopicNames( $web );
   1.359 +        if( scalar @list ) {
   1.360 +            # find last one, and increment by one
   1.361 +            my $next = $list[$#list] + 1;
   1.362 +            my $len = length( $start );
   1.363 +            $start =~ s/^0*([0-9])/$1/; # cut leading zeros
   1.364 +            $next = $start if( $start > $next );
   1.365 +            my $pad =  $len - length($next);
   1.366 +            if( $pad > 0 ) {
   1.367 +                $next = '0' x $pad . $next; # zero-pad
   1.368 +            }
   1.369 +            $topic =~ s/AUTOINC[0-9]+/$next/;
   1.370 +        } else {
   1.371 +            # first auto-inc topic
   1.372 +            $topic =~ s/AUTOINC[0-9]+/$start/;
   1.373 +        }
   1.374 +        $session->{topicName} = $topic;
   1.375 +    }
   1.376 +
   1.377 +    my $saveaction = '';
   1.378 +    foreach my $action qw( save checkpoint quietsave cancel preview
   1.379 +                           addform replaceform delRev repRev ) {
   1.380 +        if ($query->param('action_' . $action)) {
   1.381 +            $saveaction = $action;
   1.382 +            last;
   1.383 +        }
   1.384 +    }
   1.385 +
   1.386 +    # the 'action' parameter has been deprecated, though is still available
   1.387 +    # for compatibility with old templates.
   1.388 +    if( !$saveaction && $query->param( 'action' )) {
   1.389 +        $saveaction = lc($query->param( 'action' ));
   1.390 +        $session->writeWarning(<<WARN);
   1.391 +Use of deprecated "action" parameter to "save". Correct your templates!
   1.392 +WARN
   1.393 +
   1.394 +        # handle old values for form-related actions:
   1.395 +        $saveaction = 'addform' if ( $saveaction eq 'add form');
   1.396 +        $saveaction = 'replaceform' if ( $saveaction eq 'replace form...');
   1.397 +    }
   1.398 +
   1.399 +    if( $saveaction eq 'cancel' ) {
   1.400 +        my $lease = $store->getLease( $web, $topic );
   1.401 +        if( $lease && $lease->{user} eq $user ) {
   1.402 +            $store->clearLease( $web, $topic );
   1.403 +        }
   1.404 +
   1.405 +        # redirect to a sensible place (a topic that exists)
   1.406 +        my( $w, $t ) = ( '', '' );
   1.407 +        foreach my $test ( $topic,
   1.408 +                           $query->param( 'topicparent' ),
   1.409 +                           $TWiki::cfg{HomeTopicName} ) {
   1.410 +            ( $w, $t ) =
   1.411 +              $session->normalizeWebTopicName( $web, $test );
   1.412 +            last if( $store->topicExists( $w, $t ));
   1.413 +        }
   1.414 +        my $viewURL = $session->getScriptUrl( 1, 'view', $w, $t );
   1.415 +        $session->redirect( $viewURL, undef, 1 );
   1.416 +
   1.417 +        return;
   1.418 +    }
   1.419 +
   1.420 +    if( $saveaction eq 'preview' ) {
   1.421 +        require TWiki::UI::Preview;
   1.422 +        TWiki::UI::Preview::preview( $session );
   1.423 +        return;
   1.424 +    }
   1.425 +
   1.426 +    my $editaction = lc($query->param( 'editaction' )) || '';
   1.427 +    my $edit = $query->param( 'edit' ) || 'edit';
   1.428 +    my $editparams = $query->param( 'editparams' ) || '';
   1.429 +
   1.430 +    ## SMELL: The form affecting actions do not preserve edit and editparams
   1.431 +    if( $saveaction eq 'addform' ||
   1.432 +          $saveaction eq 'replaceform' ||
   1.433 +            $saveaction eq 'preview' && $query->param( 'submitChangeForm' )) {
   1.434 +        require TWiki::UI::ChangeForm;
   1.435 +        $session->writeCompletePage
   1.436 +          ( TWiki::UI::ChangeForm::generate( $session, $web,
   1.437 +                                             $topic, $editaction ) );
   1.438 +        return;
   1.439 +    }
   1.440 +
   1.441 +    my $redirecturl;
   1.442 +
   1.443 +    if( $saveaction eq 'checkpoint' ) {
   1.444 +        $query->param( -name=>'dontnotify', -value=>'checked' );
   1.445 +        my $editURL = $session->getScriptUrl( 1, $edit, $web, $topic );
   1.446 +        $redirecturl = $editURL.'?t='.time();
   1.447 +        $redirecturl .= '&redirectto='.$query->param( 'redirectto' )
   1.448 +          if $query->param( 'redirectto' );
   1.449 +        # select the appropriate edit template
   1.450 +        $redirecturl .= '&action='.$editaction if $editaction;
   1.451 +        $redirecturl .= '&skin='.$query->param('skin')
   1.452 +          if $query->param('skin');
   1.453 +        $redirecturl .= '&cover='.$query->param('cover')
   1.454 +          if $query->param('cover');
   1.455 +        $redirecturl .= '&nowysiwyg='.$query->param('nowysiwyg')
   1.456 +          if $query->param('nowysiwyg');
   1.457 +        $redirecturl .= $editparams
   1.458 +          if $editparams;  # May contain anchor
   1.459 +        my $lease = $store->getLease( $web, $topic );
   1.460 +        if( $lease && $lease->{user} eq $user ) {
   1.461 +            $store->setLease( $web, $topic, $user, $TWiki::cfg{LeaseLength} );
   1.462 +        }
   1.463 +        # drop through
   1.464 +    }
   1.465 +
   1.466 +    if( $saveaction eq 'quietsave' ) {
   1.467 +        $query->param( -name=>'dontnotify', -value=>'checked' );
   1.468 +        $saveaction = 'save';
   1.469 +        # drop through
   1.470 +    }
   1.471 +
   1.472 +    if( $saveaction =~ /^(del|rep)Rev$/ ) {
   1.473 +        # hidden, largely undocumented functions, used by administrators for
   1.474 +        # reverting spammed topics. These functions support rewriting
   1.475 +        # history, in a Joe Stalin kind of way. They should be replaced with
   1.476 +        # mechanisms for hiding revisions.
   1.477 +        $query->param( -name => 'cmd', -value => $saveaction );
   1.478 +        # drop through
   1.479 +    }
   1.480 +
   1.481 +    my $saveCmd = $query->param( 'cmd' ) || 0;
   1.482 +    if ( $saveCmd && ! $session->{users}->isAdmin( $session->{user} )) {
   1.483 +        throw TWiki::OopsException(
   1.484 +            'accessdenied',
   1.485 +            def => 'only_group',
   1.486 +            web => $web, topic => $topic,
   1.487 +            params => [ $TWiki::cfg{SuperAdminGroup} ] );
   1.488 +    }
   1.489 +
   1.490 +    #success - redirect to topic view (unless its a checkpoint save)
   1.491 +    $redirecturl ||= $session->getScriptUrl( 1, 'view', $web, $topic );
   1.492 +
   1.493 +    if( $saveCmd eq 'delRev' ) {
   1.494 +        # delete top revision
   1.495 +        try {
   1.496 +            $store->delRev( $user, $web, $topic );
   1.497 +        } catch Error::Simple with {
   1.498 +            throw TWiki::OopsException(
   1.499 +                'attention',
   1.500 +                def => 'save_error',
   1.501 +                web => $web,
   1.502 +                topic => $topic,
   1.503 +                params => [ shift->{-text} ]);
   1.504 +        };
   1.505 +
   1.506 +        $session->redirect( $redirecturl, undef, 1 );
   1.507 +        return;
   1.508 +    }
   1.509 +
   1.510 +    if( $saveCmd eq 'repRev' ) {
   1.511 +        # replace top revision with the text from the query, trying to
   1.512 +        # make it look as much like the original as possible. The query
   1.513 +        # text is expected to contain %META as well as text.
   1.514 +        my $meta = new TWiki::Meta( $session, $web, $topic,
   1.515 +                                    $query->param( 'text' ));
   1.516 +        my $saveOpts = {
   1.517 +            timetravel => 1,
   1.518 +            operation => 'cmd',
   1.519 +           };
   1.520 +        try {
   1.521 +            $store->repRev( $user, $web, $topic,
   1.522 +                            $meta->text(), $meta, $saveOpts );
   1.523 +        } catch Error::Simple with {
   1.524 +            throw TWiki::OopsException(
   1.525 +                'attention',
   1.526 +                def => 'save_error',
   1.527 +                web => $web,
   1.528 +                topic => $topic,
   1.529 +                params => [ shift->{-text} ] );
   1.530 +        };
   1.531 +
   1.532 +        $session->redirect( $redirecturl, undef, ( $saveaction ne 'checkpoint' ) );
   1.533 +        return;
   1.534 +    }
   1.535 +
   1.536 +    my( $newMeta, $newText, $saveOpts, $merged ) =
   1.537 +      buildNewTopic($session, 'save');
   1.538 +
   1.539 +    if( $saveaction =~ /^(save|checkpoint)$/ ) {
   1.540 +        $session->{plugins}->afterEditHandler( $newText, $topic, $web, $newMeta );
   1.541 +    }
   1.542 +
   1.543 +    try {
   1.544 +        $store->saveTopic( $user, $web, $topic,
   1.545 +                           $newText, $newMeta, $saveOpts );
   1.546 +    } catch Error::Simple with {
   1.547 +        throw TWiki::OopsException(
   1.548 +            'attention',
   1.549 +            def => 'save_error',
   1.550 +            web => $web,
   1.551 +            topic => $topic,
   1.552 +            params => [ shift->{-text} ] );
   1.553 +    };
   1.554 +
   1.555 +    my $lease = $store->getLease( $web, $topic );
   1.556 +    # clear the lease, if (and only if) we own it
   1.557 +    if( $lease && $lease->{user} eq $user ) {
   1.558 +        $store->clearLease( $web, $topic );
   1.559 +    }
   1.560 +
   1.561 +    if( $merged ) {
   1.562 +        throw TWiki::OopsException(
   1.563 +            'attention',
   1.564 +            def => 'merge_notice',
   1.565 +            web => $web, topic => $topic,
   1.566 +            params => $merged );
   1.567 +    }
   1.568 +
   1.569 +    $session->redirect( $redirecturl, undef, ( $saveaction ne 'checkpoint' ) );
   1.570 +}
   1.571 +
   1.572 +1;