Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Item13897: Moved expandStandardEscapes to Foswiki.pm
expandStandardEscapes is a utility function not related to macro
processing.

Fixed mistypes related to getScriptUrl method been called on $Foswiki::app
directly, not on $Foswiki::app->cfg.

Added few lines of documentation to support
https://foswiki.org/Development/Foswiki3CodeChanges topic.
  • Loading branch information
vrurg committed May 5, 2016
1 parent 1b1eab5 commit 3ebf44b
Show file tree
Hide file tree
Showing 33 changed files with 173 additions and 114 deletions.
45 changes: 44 additions & 1 deletion core/lib/Foswiki.pm
Expand Up @@ -101,7 +101,7 @@ use Try::Tiny;
use Assert;
use Exporter qw(import);
our @EXPORT_OK =
qw(%regex urlEncode urlDecode make_params load_package load_class);
qw(%regex urlEncode urlDecode make_params load_package load_class expandStandardEscapes);

sub SINGLE_SINGLETONS { 0 }
sub SINGLE_SINGLETONS_TRACE { 0 }
Expand Down Expand Up @@ -1333,6 +1333,49 @@ sub entityDecode {

=begin TML
---++ StaticMethod expandStandardEscapes($str) -> $unescapedStr
Expands standard escapes used in parameter values to block evaluation. See
System.FormatTokens for a full list of supported tokens.
=cut

sub expandStandardEscapes {
my $text = shift;

# expand '$n()' and $n! to new line
$text =~ s/\$n\(\)/\n/gs;
$text =~ s/\$n(?=[^[:alpha:]]|$)/\n/gs;

# filler, useful for nested search
$text =~ s/\$nop(\(\))?//gs;

# $quot -> "
$text =~ s/\$quot(\(\))?/\"/gs;

# $comma -> ,
$text =~ s/\$comma(\(\))?/,/gs;

# $percent -> %
$text =~ s/\$perce?nt(\(\))?/\%/gs;

# $lt -> <
$text =~ s/\$lt(\(\))?/\</gs;

# $gt -> >
$text =~ s/\$gt(\(\))?/\>/gs;

# $amp -> &
$text =~ s/\$amp(\(\))?/\&/gs;

# $dollar -> $, done last to avoid creating the above tokens
$text =~ s/\$dollar(\(\))?/\$/gs;

return $text;
}

=begin TML
---++ StaticMethod urlEncode( $perlstring ) -> $bytestring
Encode by converting characters that are reserved in URLs to
Expand Down
9 changes: 9 additions & 0 deletions core/lib/Foswiki/App.pm
Expand Up @@ -110,6 +110,15 @@ has engine => (
isa =>
Foswiki::Object::isaCLASS( 'engine', 'Foswiki::Engine', noUndef => 1, ),
);

# Heap is to be used for data persistent over session lifetime.
# Usage: $sessiom->heap->{key} = <your data>;
has heap => (
is => 'rw',
clearer => 1,
lazy => 1,
default => sub { {} },
);
has i18n => (
is => 'ro',
lazy => 1,
Expand Down
3 changes: 2 additions & 1 deletion core/lib/Foswiki/Attach.pm
Expand Up @@ -13,6 +13,7 @@ use v5.14;

use Assert;
use Unicode::Normalize;
use Foswiki qw(expandStandardEscapes);

use Moo;
use namespace::clean;
Expand Down Expand Up @@ -417,7 +418,7 @@ sub getAttachmentLink {

require Foswiki::Time;
$fileLink = Foswiki::Time::formatTime( $fileTime, $fileLink );
$fileLink = $this->app->macros->expandStandardEscapes($fileLink);
$fileLink = expandStandardEscapes($fileLink);

return $fileLink;
}
Expand Down
57 changes: 54 additions & 3 deletions core/lib/Foswiki/Exception.pm
Expand Up @@ -64,16 +64,67 @@ BEGIN {
}
}

has line => (
=begin TML
---++ ObjectAttribute file
Name of the file where the exception has been raised as returned by the =caller=
funtion.
=cut

has file => (
is => 'rwp',
predicate => 1,
);
has file => (

=begin TML
---++ ObjectAttribute line
Number of the line in the source file where the exception has been raised as
returned by the =caller= funtion.
=cut

has line => (
is => 'rwp',
predicate => 1,
);
has text => ( is => 'rwp', );

=begin TML
---++ ObjectAttribute text
Simple text explaining what's went wrong. Must always be set to something
meaningful. If child class doesn't expect this attribute to be set by a user
then it must generate it using other attributes.
=cut

has text => ( is => 'rwp', );

=begin TML
---++ ObjectAttribute object
Might be set by the object which generated the exception to inidicate the source
of problem.
=cut

has object => ( is => 'ro', );

=begin TML
---++ ObjectAttribute stacktrace
Contains full stack trace if =DEBUG= is =TRUE=. The trace includes calls to
=Foswiki::Exception= methods too to provide as much information for tracing down
errors as possible.
=cut

has stacktrace => (
is => 'rwp',
predicate => 1,
Expand Down
11 changes: 6 additions & 5 deletions core/lib/Foswiki/Form.pm
Expand Up @@ -33,6 +33,7 @@ use v5.14;

use Assert;

use Foswiki qw(expandStandardEscapes);
use Foswiki::Sandbox ();
use Foswiki::Form::FieldDefinition ();
use Foswiki::Form::ListFieldDefinition ();
Expand Down Expand Up @@ -497,8 +498,9 @@ sub _link {
{
target => $topic,
title => $tooltip,
href => $this->app->getScriptUrl( 0, 'view', $web, $topic ),
rel => 'nofollow'
href =>
$this->app->cfg->getScriptUrl( 0, 'view', $web, $topic ),
rel => 'nofollow'
},
$string
);
Expand Down Expand Up @@ -586,9 +588,8 @@ sub renderForEdit {

my $dv = $fieldDef->getDefaultValue($value);
if ( defined($dv) ) {
$dv = $topicObject->expandMacros($dv);
$value =
$app->macros->expandStandardEscapes($dv); # Item2837
$dv = $topicObject->expandMacros($dv);
$value = expandStandardEscapes($dv); # Item2837
}
}

Expand Down
3 changes: 2 additions & 1 deletion core/lib/Foswiki/Form/FieldDefinition.pm
Expand Up @@ -18,6 +18,7 @@ use v5.14;

use Assert;
use CGI ();
use Foswiki qw(expandStandardEscapes);

use Moo;
use namespace::clean;
Expand Down Expand Up @@ -298,7 +299,7 @@ sub populateMetaFromQueryData {
$value = $query->param( $this->name );
$value = '' unless defined $value;
if ( $this->app->inContext('edit') ) {
$value = $this->app->macros->expandStandardEscapes($value);
$value = expandStandardEscapes($value);
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions core/lib/Foswiki/Func.pm
Expand Up @@ -57,7 +57,7 @@ use Scalar::Util ();
use Try::Tiny;
use Assert;

use Foswiki ();
use Foswiki qw(expandStandardEscapes);
use Foswiki::App ();
use Foswiki::Plugins ();
use Foswiki::Meta ();
Expand Down Expand Up @@ -180,7 +180,7 @@ sub getScriptUrl {
my $script = shift;
ASSERT($Foswiki::app) if DEBUG;

return $Foswiki::app->getScriptUrl( 1, $script, $web, $topic, @_ );
return $Foswiki::app->cfg->getScriptUrl( 1, $script, $web, $topic, @_ );
}

=begin TML
Expand Down Expand Up @@ -210,7 +210,7 @@ sub getScriptUrlPath {
my $script = shift;
ASSERT($Foswiki::app) if DEBUG;

return $Foswiki::app->getScriptUrl( 0, $script, $web, $topic, @_ );
return $Foswiki::app->cfg->getScriptUrl( 0, $script, $web, $topic, @_ );
}

=begin TML
Expand Down Expand Up @@ -1574,7 +1574,7 @@ sub webExists {
return 0 unless defined $web;

ASSERT($Foswiki::app) if DEBUG;
return $Foswiki::app->webExists($web);
return $Foswiki::app->store->webExists($web);
}

=begin TML
Expand Down Expand Up @@ -2537,7 +2537,7 @@ sub eachChangeSince {
my ( $web, $time ) = @_;
ASSERT($Foswiki::app) if DEBUG;
($web) = _validateWTA($web);
ASSERT( $Foswiki::app->webExists($web) ) if DEBUG;
ASSERT( $Foswiki::app->store->webExists($web) ) if DEBUG;

my $webObject = $Foswiki::app->create( 'Foswiki::Meta', web => $web );
return $webObject->eachChange($time);
Expand Down Expand Up @@ -3141,7 +3141,7 @@ The set of tokens that is expanded is described in System.FormatTokens.
=cut

sub decodeFormatTokens {
return $Foswiki::app->macros->expandStandardEscapes(@_);
return expandStandardEscapes(@_);
}

=begin TML
Expand Down
6 changes: 3 additions & 3 deletions core/lib/Foswiki/LoginManager.pm
Expand Up @@ -678,7 +678,7 @@ sub redirectToLoggedOutUrl {
# ForceDefaultUrlHost enabled - probably a reverse proxy.
$path_info ||= '';
my $action = $app->request->action || 'view';
$redirectUrl = $app->getScriptUrl( '1', $action ) . $path_info;
$redirectUrl = $app->cfg->getScriptUrl( '1', $action ) . $path_info;
}
elsif ($path_info) {
$redirectUrl = $app->request->url() . $path_info;
Expand Down Expand Up @@ -955,7 +955,7 @@ sub _myScriptURLRE {

my $s = $this->_MYSCRIPTURL;
unless ($s) {
$s = quotemeta( $this->app->getScriptUrl( 1, $M1, $M2, $M3 ) );
$s = quotemeta( $this->app->cfg->getScriptUrl( 1, $M1, $M2, $M3 ) );
$s =~ s@\\$M1@[^/]*?@go;
$s =~ s@\\$M2@[^/]*?@go;
$s =~ s@\\$M3@[^#\?/]*@go;
Expand Down Expand Up @@ -1406,7 +1406,7 @@ sub _LOGOUTURL {
my ( $app, $params, $topic, $web ) = @_;
my $this = $app->users->getLoginManager();

return $app->getScriptUrl(
return $app->cfg->getScriptUrl(
0, 'view',
$app->prefs->getPreference('BASEWEB'),
$app->prefs->getPreference('BASETOPIC'),
Expand Down
8 changes: 4 additions & 4 deletions core/lib/Foswiki/LoginManager/ApacheLogin.pm
Expand Up @@ -77,7 +77,7 @@ sub forceAuthentication {

# Assemble the new URL using the host, the changed script name,
# and the path info.
my $url = $app->getScriptUrl( 1, $newAction );
my $url = $app->cfg->getScriptUrl( 1, $newAction );
if ( $query->path_info() ) {
$url .= '/'
unless $url =~ m#/$# || $query->path_info() =~ m#^/#;
Expand Down Expand Up @@ -105,7 +105,7 @@ sub loginUrl {
my $app = $this->app;
my $topic = $app->topicName;
my $web = $app->webName;
return $app->getScriptUrl( 0, 'logon', $web, $topic, @_ );
return $app->cfg->getScriptUrl( 0, 'logon', $web, $topic, @_ );
}

=begin TML
Expand All @@ -122,8 +122,8 @@ sub login {
my ( $this, $query, $app ) = @_;

my $url =
$app->getScriptUrl( 0, 'viewauth', $app->webName,
$app->topicName, t => time() );
$app->cfg->getScriptUrl( 0, 'viewauth', $app->request->web,
$app->request->topic, t => time() );

$url .= ( ';' . $query->query_string() ) if $query->query_string();

Expand Down
50 changes: 3 additions & 47 deletions core/lib/Foswiki/Macros.pm
Expand Up @@ -3,7 +3,7 @@
package Foswiki::Macros;
use v5.14;

use Foswiki qw(%regex);
use Foswiki qw(%regex expandStandardEscapes);
use Foswiki::Attrs ();

use Moo;
Expand Down Expand Up @@ -304,50 +304,6 @@ sub expandMacrosOnTopicCreation {
}
}

=begin TML
---++ StaticMethod expandStandardEscapes($str) -> $unescapedStr
Expands standard escapes used in parameter values to block evaluation. See
System.FormatTokens for a full list of supported tokens.
=cut

sub expandStandardEscapes {
my $this = shift;
my $text = shift;

# expand '$n()' and $n! to new line
$text =~ s/\$n\(\)/\n/gs;
$text =~ s/\$n(?=[^[:alpha:]]|$)/\n/gs;

# filler, useful for nested search
$text =~ s/\$nop(\(\))?//gs;

# $quot -> "
$text =~ s/\$quot(\(\))?/\"/gs;

# $comma -> ,
$text =~ s/\$comma(\(\))?/,/gs;

# $percent -> %
$text =~ s/\$perce?nt(\(\))?/\%/gs;

# $lt -> <
$text =~ s/\$lt(\(\))?/\</gs;

# $gt -> >
$text =~ s/\$gt(\(\))?/\>/gs;

# $amp -> &
$text =~ s/\$amp(\(\))?/\&/gs;

# $dollar -> $, done last to avoid creating the above tokens
$text =~ s/\$dollar(\(\))?/\$/gs;

return $text;
}

=begin TML
---++ ObjectMethod exists($macro) -> boolean
Expand Down Expand Up @@ -735,7 +691,7 @@ sub _expandMacroOnTopicRendering {
}
my $val = $attrs->{$tag};
$val = $tattrs->{default} unless defined $val;
return $this->expandStandardEscapes($val) if defined $val;
return expandStandardEscapes($val) if defined $val;
return undef;
},
$topicObject,
Expand Down Expand Up @@ -789,7 +745,7 @@ sub _expandMacroOnTopicRendering {
# in the absence of any definition.
my $attrs = new Foswiki::Attrs($args);
if ( defined $attrs->{default} ) {
$e = $this->expandStandardEscapes( $attrs->{default} );
$e = expandStandardEscapes( $attrs->{default} );
}
}
return $e;
Expand Down

0 comments on commit 3ebf44b

Please sign in to comment.