- Timestamp:
- 06/04/09 00:08:33 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Release01x00/CommentPlugin/lib/Foswiki/Plugins/CommentPlugin/Comment.pm
r1984 r4026 37 37 # PUBLIC save the given comment. 38 38 sub save { 39 39 40 #my ( $text, $topic, $web ) = @_; 40 41 41 42 my $wikiName = Foswiki::Func::getWikiName(); 42 if( ! Foswiki::Func::checkAccessPermission( 'change', $wikiName, '', 43 $_[1], $_[2] ) ) { 43 if ( 44 !Foswiki::Func::checkAccessPermission( 45 'change', $wikiName, '', $_[1], $_[2] 46 ) 47 ) 48 { 49 44 50 # user has no permission to change the topic 45 throw Foswiki::OopsException( 'accessdenied', 46 def => 'topic_access', 47 web => $_[2], 48 topic => $_[1] ); 49 } else { 50 _buildNewTopic( @_ ); 51 throw Foswiki::OopsException( 52 'accessdenied', 53 def => 'topic_access', 54 web => $_[2], 55 topic => $_[1] 56 ); 57 } 58 else { 59 _buildNewTopic(@_); 51 60 } 52 61 } … … 54 63 # PUBLIC STATIC convert COMMENT statements to form prompts 55 64 sub prompt { 65 56 66 #my ( $previewing, $text, $web, $topic ) = @_; 57 67 58 68 my $defaultType = 59 69 Foswiki::Func::getPreferencesValue('COMMENTPLUGIN_DEFAULT_TYPE') 60 || 'above';70 || 'above'; 61 71 62 72 my $message = ''; 73 63 74 # Is commenting disabled? 64 75 my $disable = ''; 65 76 if ( $_[0] ) { 77 66 78 # We are in Preview mode 67 $message = "(Edit - Preview)";79 $message = "(Edit - Preview)"; 68 80 $disable = 'disabled'; 69 81 } 70 82 71 83 my $idx = 0; 72 $_[1] =~ s/%COMMENT({.*?})?%/_handleInput($1,$_[2],$_[3],\$idx,$message,$disable,$defaultType)/eg; 84 $_[1] =~ 85 s/%COMMENT({.*?})?%/_handleInput($1,$_[2],$_[3],\$idx,$message,$disable,$defaultType)/eg; 73 86 } 74 87 … … 84 97 my $templatetopic = ''; 85 98 my $templateweb = $web || ''; 86 if ( $attrtemplatetopic ) { 87 my ($templocweb, $temploctopic ) = 88 Foswiki::Func::normalizeWebTopicName($templateweb, $attrtemplatetopic); 99 if ($attrtemplatetopic) { 100 my ( $templocweb, $temploctopic ) = 101 Foswiki::Func::normalizeWebTopicName( $templateweb, 102 $attrtemplatetopic ); 89 103 $templatetopic = "$templocweb.$temploctopic"; 90 104 } … … 94 108 # PRIVATE generate an input form for a %COMMENT tag 95 109 sub _handleInput { 96 my ( $attributes, $web, $topic, $pidx, $message, 97 $disable, $defaultType ) =@_;98 99 $attributes =~ s/^{(.*)}$/$1/ if ( $attributes);110 my ( $attributes, $web, $topic, $pidx, $message, $disable, $defaultType ) = 111 @_; 112 113 $attributes =~ s/^{(.*)}$/$1/ if ($attributes); 100 114 101 115 my $attrs = new Foswiki::Attrs( $attributes, 1 ); 102 my $type = 103 $attrs->remove( 'type' ) || $attrs->remove( 'mode' ) || $defaultType; 104 my $silent = $attrs->remove( 'nonotify' ); 105 my $location = $attrs->remove( 'location' ); 106 my $remove = $attrs->remove( 'remove' ); 107 my $nopost = $attrs->remove( 'nopost' ); 108 my $default = $attrs->remove( 'default' ); 109 my $attrtemplatetopic = $attrs->remove( 'templatetopic' ) || ''; 110 my $templatetopic = _getTemplateLocation( $attrtemplatetopic, $web ); 116 my $type = $attrs->remove('type') || $attrs->remove('mode') || $defaultType; 117 my $silent = $attrs->remove('nonotify'); 118 my $location = $attrs->remove('location'); 119 my $remove = $attrs->remove('remove'); 120 my $nopost = $attrs->remove('nopost'); 121 my $default = $attrs->remove('default'); 122 my $attrtemplatetopic = $attrs->remove('templatetopic') || ''; 123 my $templatetopic = _getTemplateLocation( $attrtemplatetopic, $web ); 111 124 112 125 $message ||= $default || ''; 113 126 $message ||= $default || ''; 114 $disable ||= '';127 $disable ||= ''; 115 128 116 129 # clean off whitespace … … 120 133 # Expand the template in the context of the web where the comment 121 134 # box is (not the target of the comment!) 122 my $input = _getTemplate( "PROMPT:$type", $web, $topic, $templatetopic ) || ''; 135 my $input = _getTemplate( "PROMPT:$type", $web, $topic, $templatetopic ) 136 || ''; 123 137 return $input if $input =~ m/^%RED%/so; 124 138 … … 129 143 # change the url if it is. 130 144 my $anchor = undef; 131 my $target = $attrs->remove( 'target' ); 132 if ( $target ) { 145 my $target = $attrs->remove('target'); 146 if ($target) { 147 133 148 # extract web and anchor 134 149 if ( $target =~ s/^(\w+)\.// ) { … … 156 171 if ( $disable eq '' ) { 157 172 my $hiddenFields = ""; 158 $hiddenFields .= "\n".CGI::hidden( 159 -name=>'comment_action', -value=>'save' ); 160 $hiddenFields .= "\n".CGI::hidden( 161 -name=>'comment_type', -value=>$type ); 162 if( defined( $silent )) { 163 $hiddenFields .= "\n".CGI::hidden( 164 -name=>'comment_nonotify', value=>1 ); 165 } 166 if ( $templatetopic ) { 167 $hiddenFields .= "\n".CGI::hidden( 168 -name=>'comment_templatetopic', -value=>$templatetopic ); 169 } 170 if ( $location ) { 171 $hiddenFields .= "\n".CGI::hidden( 172 -name=>'comment_location', -value=>$location ); 173 } elsif ( $anchor ) { 174 $hiddenFields .= "\n".CGI::hidden( 175 -name=>'comment_anchor', -value=>$anchor ); 176 } else { 177 $hiddenFields .= "\n".CGI::hidden( 178 -name=>'comment_index', -value=>$$pidx ); 179 } 180 if( $nopost ) { 181 $hiddenFields .= "\n".CGI::hidden( 182 -name=>'comment_nopost', -value=>$nopost ); 183 } 184 if( $remove ) { 185 $hiddenFields .= "\n".CGI::hidden( 186 -name=>'comment_remove', -value=>$$pidx ); 173 $hiddenFields .= 174 "\n" . CGI::hidden( -name => 'comment_action', -value => 'save' ); 175 $hiddenFields .= 176 "\n" . CGI::hidden( -name => 'comment_type', -value => $type ); 177 if ( defined($silent) ) { 178 $hiddenFields .= 179 "\n" . CGI::hidden( -name => 'comment_nonotify', value => 1 ); 180 } 181 if ($templatetopic) { 182 $hiddenFields .= "\n" 183 . CGI::hidden( 184 -name => 'comment_templatetopic', 185 -value => $templatetopic 186 ); 187 } 188 if ($location) { 189 $hiddenFields .= "\n" 190 . CGI::hidden( 191 -name => 'comment_location', 192 -value => $location 193 ); 194 } 195 elsif ($anchor) { 196 $hiddenFields .= "\n" 197 . CGI::hidden( -name => 'comment_anchor', -value => $anchor ); 198 } 199 else { 200 $hiddenFields .= "\n" 201 . CGI::hidden( -name => 'comment_index', -value => $$pidx ); 202 } 203 if ($nopost) { 204 $hiddenFields .= "\n" 205 . CGI::hidden( -name => 'comment_nopost', -value => $nopost ); 206 } 207 if ($remove) { 208 $hiddenFields .= "\n" 209 . CGI::hidden( -name => 'comment_remove', -value => $$pidx ); 187 210 } 188 211 $input .= $hiddenFields; 189 212 } 190 if ( $noform ) { 191 my $form = _getTemplate( "FORM:$type", $topic, $web, 192 $templatetopic, 'off' ) || ''; 193 if ( $form ) { 213 if ($noform) { 214 my $form = 215 _getTemplate( "FORM:$type", $topic, $web, $templatetopic, 'off' ) 216 || ''; 217 if ($form) { 194 218 $form =~ s/%COMMENTPROMPT%/$input/; 195 219 $input = $form; 196 220 } 197 221 } 198 unless ($noform eq 'on') { 199 $input = CGI::start_form( -name => $type.$n, 200 -id => $type.$n, 201 -action=>$url, 202 -method=>'post' ).$input.CGI::end_form(); 222 unless ( $noform eq 'on' ) { 223 $input = CGI::start_form( 224 -name => $type . $n, 225 -id => $type . $n, 226 -action => $url, 227 -method => 'post' 228 ) 229 . $input 230 . CGI::end_form(); 203 231 } 204 232 } … … 214 242 215 243 # Get the templates. 216 my $templateFile = $templatetopic 217 || Foswiki::Func::getPreferencesValue('COMMENTPLUGIN_TEMPLATES') 218 || 'comments'; 219 220 my $templates = 221 Foswiki::Func::loadTemplate( $templateFile ); 222 if (! $templates ) { 223 Foswiki::Func::writeWarning("Could not read template file '$templateFile'"); 244 my $templateFile = 245 $templatetopic 246 || Foswiki::Func::getPreferencesValue('COMMENTPLUGIN_TEMPLATES') 247 || 'comments'; 248 249 my $templates = Foswiki::Func::loadTemplate($templateFile); 250 if ( !$templates ) { 251 Foswiki::Func::writeWarning( 252 "Could not read template file '$templateFile'"); 224 253 return; 225 254 } 226 255 227 my $t = Foswiki::Func::expandTemplate( $name);256 my $t = Foswiki::Func::expandTemplate($name); 228 257 return "%RED%No such template def TMPL:DEF{$name}%ENDCOLOR%" 229 258 unless ( defined($t) && $t ne '' ) || $warn eq 'off'; … … 237 266 238 267 my $val = $attrs->{$name}; 239 return $val if defined( $val);268 return $val if defined($val); 240 269 return $default; 241 270 } … … 243 272 # PRIVATE STATIC Performs comment insertion in the topic. 244 273 sub _buildNewTopic { 274 245 275 #my ( $text, $topic, $web ) = @_; 246 276 my ( $topic, $web ) = ( $_[1], $_[2] ); … … 249 279 return unless $query; 250 280 251 my $type = $query->param( 'comment_type' ) || 252 Foswiki::Func::getPreferencesValue('COMMENTPLUGIN_DEFAULT_TYPE') || 253 'above'; 254 my $index = $query->param( 'comment_index' ) || 0; 255 my $anchor = $query->param( 'comment_anchor' ); 256 my $location = $query->param( 'comment_location' ); 257 my $remove = $query->param( 'comment_remove' ); 258 my $nopost = $query->param( 'comment_nopost' ); 259 my $templatetopic = $query->param( 'comment_templatetopic' ) || ''; 281 my $type = 282 $query->param('comment_type') 283 || Foswiki::Func::getPreferencesValue('COMMENTPLUGIN_DEFAULT_TYPE') 284 || 'above'; 285 my $index = $query->param('comment_index') || 0; 286 my $anchor = $query->param('comment_anchor'); 287 my $location = $query->param('comment_location'); 288 my $remove = $query->param('comment_remove'); 289 my $nopost = $query->param('comment_nopost'); 290 my $templatetopic = $query->param('comment_templatetopic') || ''; 260 291 261 292 my $output = _getTemplate( "OUTPUT:$type", $topic, $web, $templatetopic ); … … 266 297 # Expand the template 267 298 my $position = 'AFTER'; 268 if ( $output =~ s/%POS:(.*?)%//g ) {299 if ( $output =~ s/%POS:(.*?)%//g ) { 269 300 $position = $1; 270 301 } … … 285 316 # text if the =text= parameter isn't specified - which for comments, 286 317 # it isn't. 287 my $premeta = '';318 my $premeta = ''; 288 319 my $postmeta = ''; 289 my $inpost = 0; 290 my $text = ''; 291 foreach my $line ( split( /\r?\n/, $_[0] )) { 292 if( $line =~ /^%META:[A-Z]+{[^}]*}%/ ) { 293 if ( $inpost) { 294 $postmeta .= $line."\n"; 295 } else { 296 $premeta .= $line."\n"; 297 } 298 } else { 299 $text .= $line."\n"; 320 my $inpost = 0; 321 my $text = ''; 322 foreach my $line ( split( /\r?\n/, $_[0] ) ) { 323 if ( $line =~ /^%META:[A-Z]+{[^}]*}%/ ) { 324 if ($inpost) { 325 $postmeta .= $line . "\n"; 326 } 327 else { 328 $premeta .= $line . "\n"; 329 } 330 } 331 else { 332 $text .= $line . "\n"; 300 333 $inpost = 1; 301 334 } 302 335 } 336 303 337 #make sure the anchor or location exits 304 if (defined($location) and not($text =~ /(?<!location\=\")($location)/)) { 338 if ( defined($location) and not( $text =~ /(?<!location\=\")($location)/ ) ) 339 { 305 340 undef $location; 306 341 } 307 if ( defined($anchor) and not($text =~ /^($anchor\s)/)) {342 if ( defined($anchor) and not( $text =~ /^($anchor\s)/ ) ) { 308 343 undef $anchor; 309 344 } 310 345 311 unless( $nopost ) { 312 if( $position eq 'TOP' ) { 313 $text = $output.$text; 314 } elsif ( $position eq 'BOTTOM' ) { 346 unless ($nopost) { 347 if ( $position eq 'TOP' ) { 348 $text = $output . $text; 349 } 350 elsif ( $position eq 'BOTTOM' ) { 351 315 352 # Awkward newlines here, to avoid running into meta-data. 316 353 # This should _not_ be a problem. … … 318 355 $text .= "\n" unless $output =~ m/^\n/s; 319 356 $text .= $output; 320 $text .= "\n" unless $text =~ m/\n$/s; 321 } else { 322 if ( $location ) { 357 $text .= "\n" unless $text =~ m/\n$/s; 358 } 359 else { 360 if ($location) { 323 361 if ( $position eq 'BEFORE' ) { 324 $text =~ s/(?<!location\=\")($location)/$output$1/m;325 } else { # AFTER326 $text =~ s/(?<!location\=\")($location)/$1$output/m;362 $text .= $output 363 unless ( 364 $text =~ s/(?<!location\=\")($location)/$output$1/m ); 327 365 } 328 } elsif ( $anchor ) { 366 else { # AFTER 367 $text .= $output 368 unless ( 369 $text =~ s/(?<!location\=\")($location)/$1$output/m ); 370 371 } 372 $text .= "\n" unless $text =~ m/\n$/s; 373 } 374 elsif ($anchor) { 375 329 376 # position relative to anchor 330 377 if ( $position eq 'BEFORE' ) { 331 $text =~ s/^($anchor\s)/$output$1/m; 332 } else { # AFTER 333 $text =~ s/^($anchor\s)/$1$output/m; 378 $text .= $output 379 unless ( $text =~ s/^($anchor\s)/$output$1/m ); 334 380 } 335 } else { 381 else { # AFTER 382 $text .= $output 383 unless ( $text =~ s/^($anchor\s)/$1$output/m ); 384 } 385 $text .= "\n" unless $text =~ m/\n$/s; 386 } 387 else { 388 336 389 # Position relative to index'th comment 337 390 my $idx = 0; 338 unless( $text =~ s((%COMMENT({.*?})?%.*\n)) 339 (&_nth($1,\$idx,$position,$index,$output))eg ) { 391 unless ( 392 $text =~ s((%COMMENT({.*?})?%.*\n)) 393 (&_nth($1,\$idx,$position,$index,$output))eg 394 ) 395 { 396 340 397 # If there was a problem adding relative to the comment, 341 398 # add to the end of the topic 342 399 $text .= $output; 343 }; 344 } 345 } 346 } 347 348 if (defined $remove) { 400 } 401 $text .= "\n" unless $text =~ m/\n$/s; 402 } 403 } 404 } 405 406 if ( defined $remove ) { 407 349 408 # remove the index'th comment box 350 409 my $idx = 0; … … 359 418 my ( $tag, $pidx, $position, $index, $output ) = @_; 360 419 361 if ( $$pidx == $index ) {420 if ( $$pidx == $index ) { 362 421 if ( $position eq 'BEFORE' ) { 363 $tag = $output.$tag; 364 } else { # AFTER 422 $tag = $output . $tag; 423 } 424 else { # AFTER 365 425 $tag .= $output; 366 426 } … … 372 432 # PRIVATE remove the nth comment box 373 433 sub _remove_nth { 374 my ( $tag, $pidx, $index ) = @_;375 $tag = '' if ( $$pidx == $index);434 my ( $tag, $pidx, $index ) = @_; 435 $tag = '' if ( $$pidx == $index ); 376 436 $$pidx++; 377 437 return $tag;
Note: See TracChangeset
for help on using the changeset viewer.
