Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
--- lib/MT/Builder.pm (4.23)
+++ lib/MT/Builder.pm (4.32)
@@ -69,12 +69,13 @@
     # Other namespaces (like 'Foo') would require the colon.
     # Tag and attributes are case-insensitive. So you can write:
     #   <mtfoo>...</MTFOO>
-    while ($text =~ m!(<\$?(MT:?)((?:<[^>]+?>|"(?:<[^>]+?>|.)*?"|'(?:<[^>]+?>|.)*?'|.)+?)[\$/]?>)!gis) {
-        my($whole_tag, $prefix, $tag) = ($1, $2, $3);
+    while ($text =~ m!(<\$?(MT:?)((?:<[^>]+?>|"(?:<[^>]+?>|.)*?"|'(?:<[^>]+?>|.)*?'|.)+?)([-]?)[\$/]?>)!gis) {
+        my($whole_tag, $prefix, $tag, $space_eater) = ($1, $2, $3, $4);
         ($tag, my($args)) = split /\s+/, $tag, 2;
         my $sec_start = pos $text;
         my $tag_start = $sec_start - length $whole_tag;
         _text_block($state, $pos, $tag_start) if $pos < $tag_start;
+        $state->{space_eater} = $space_eater;
         $args ||= '';
         # Structure of a node:
         #   tag name, attribute hashref, contained tokens, template text,
@@ -256,6 +257,8 @@
 sub _text_block {
     my $text = substr ${ $_[0]->{text} }, $_[1], $_[2] - $_[1];
     if ((defined $text) && ($text ne '')) {
+        return if $_[0]->{space_eater} && ($text =~ m/^\s+$/s);
+        $text =~ s/^\s+//s if $_[0]->{space_eater};
         my $rec = bless [ 'TEXT', $text, undef, undef, undef, $_[0]->{tokens}, $_[0]->{tmpl} ], NODE;
         # Avoids circular reference between NODE and TOKENS, MT::Template.
         weaken($rec->[5]);
@@ -338,6 +341,8 @@
                 $uncompiled = $t->[3];
             }
             my($h, $type) = $ctx->handler_for($t->[0]);
+            my $conditional = defined $type && $type == 2;
+
             if ($h) {
                 $timer->pause_partial if $timer;
                 local($ctx->{__stash}{tag}) = $t->[0];
@@ -352,7 +357,7 @@
                     if (ref $args{$v} eq 'ARRAY') {
                         foreach (@{$args{$v}}) {
                             if (m/^\$([A-Za-z_](\w|\.)*)$/) {
-                                $_ = $ctx->var($1);
+                                local $_ = $ctx->var($1);
                             }
                         }
                     } else {
@@ -381,6 +386,13 @@
                 # Stores a reference to the ordered list of arguments,
                 # just in case the handler wants them
                 local $args{'@'} = \@args;
+
+                my $vars = $ctx->{__stash}{vars};
+                local $vars->{__cond_value__} = $vars->{__cond_value__}
+                    if $conditional;
+                local $vars->{__cond_name__}  = $vars->{__cond_name__}
+                    if $conditional;
+
                 my $out = $h->($ctx, \%args, $cond);
 
                 unless (defined $out) {
@@ -395,11 +407,8 @@
                     }
                 }
 
-                if ((defined $type) && ($type == 2)) {
+                if ($conditional) {
                     # conditional; process result
-                    my $vars = $ctx->{__stash}{vars};
-                    local $vars->{__value__} = delete $vars->{__cond_value__};
-                    local $vars->{__name__}  = delete $vars->{__cond_name__};
                     $out = $out ? $ctx->slurp(\%args, $cond) : $ctx->else(\%args, $cond);
                     delete $vars->{__cond_tag__};
                     return $build->error(MT->translate("Error in <mt[_1]> tag: [_2]", $t->[0], $ctx->errstr))
--- lib/MT/Template/ContextHandlers.pm (4.23)
+++ lib/MT/Template/ContextHandlers.pm (4.32)
@@ -3442,7 +3459,14 @@
                 local $ctx->{__stash}{tokens} = $value;
                 local $args->{name} = undef;
                 local $args->{var} = undef;
-                $ctx->var($_, $args->{$_}) for keys %{$args || {}};
+                # Pass through SetVarTemplate arguments as variables
+                # so that they do not affect the global stash
+                my $vars = $ctx->{__stash}{vars} ||= {};
+                my @names = keys %$args;
+                my @var_names;
+                push @var_names, lc $_ for @names;
+                local @{$vars}{@var_names};
+                $vars->{lc($_)} = $args->{$_} for @names;
                 $value = _hdlr_pass_tokens(@_) or return;
             } elsif (ref($value) eq 'ARRAY') {
                 if ( defined $index ) {
@@ -4580,6 +4661,11 @@
 Used to include a template from another blog in the system. Use in
 conjunction with the module, widget or identifier attributes.
 
+=item * local (optional)
+
+Forces an Include of a template that exists in the blog that is being
+published.
+
 =item * global (optional; default "0")
 
 Forces an Include of a globally defined template even if the
@@ -4689,7 +4775,14 @@
         $type = 'widget';
         $tmpl_name =~ s/^Widget: ?//;
     }
-    my $blog_id = $arg->{blog_id} || $ctx->{__stash}{blog_id} || 0;
+    my $blog_id = defined($arg->{blog_id})
+      ? $arg->{blog_id}
+      : ( $arg->{global} )
+        ? 0 
+        : defined($ctx->{__stash}{blog_id})
+          ? $ctx->{__stash}{blog_id}
+          : 0;
+    $blog_id = $ctx->stash('local_blog_id') if $arg->{local};
     my $stash_id = 'template_' . $type . '::' . $blog_id . '::' . $tmpl_name;
     return $ctx->error(MT->translate("Recursion attempt on [_1]: [_2]", MT->translate($name), $tmpl_name))
         if $include_stack{$stash_id};
@@ -4787,7 +4880,7 @@
     my $cache_driver;
     if ($cache_enabled) {
         my $tmpl_mod = $tmpl->modified_on;
-        my $tmpl_ts = MT::Util::ts2epoch($blog, $tmpl_mod);
+        my $tmpl_ts = MT::Util::ts2epoch($tmpl->blog_id ? $tmpl->blog : undef, $tmpl_mod);
         if (($ttl == 0) || (time - $tmpl_ts < $ttl)) {
             $ttl = time - $tmpl_ts;
         }
@@ -4881,13 +4974,15 @@
         $tokens = $cref;
     } else {
         my $blog = $ctx->stash('blog');
-        if ($blog->id != $blog_id) {
+        if ($blog && $blog->id != $blog_id) {
             $blog = MT::Blog->load($blog_id)
                 or return $ctx->error(MT->translate(
                     "Can't find blog for id '[_1]", $blog_id));
         }
-        my @paths = ($file, map File::Spec->catfile($_, $file),
-                            $blog->site_path, $blog->archive_path);
+        my @paths = ($file);
+        push @paths, map { File::Spec->catfile($_, $file) }
+            ($blog->site_path, $blog->archive_path)
+            if $blog;
         my $path;
         for my $p (@paths) {
             $path = $p, last if -e $p && -r _;
@@ -5751,6 +5849,7 @@
           ? $existing->[ $index ] 
           : undef;
     }
+    $existing = '' unless defined $existing;
 
     if ($args->{prepend}) {
         $val = $val . $existing;
@@ -19772,8 +20202,10 @@
     require MT::Template;
     my $tmpl = MT::Template->load({ name => $tmpl_name,
                                     blog_id => $blog_id ? [ 0, $blog_id ] : 0,
-                                    type => 'widgetset' })
-        or return $ctx->error(MT->translate("Specified WidgetSet not found."));
+                                    type => 'widgetset' },
+                                  { sort => 'blog_id',
+                                    direction => 'descend' })
+        or return $ctx->error(MT->translate( "Specified WidgetSet '[_1]' not found.", $tmpl_name ));
     my $text = $tmpl->text;
     return $ctx->build($text) if $text;

--- lib/MT/Template/Context.pm (4.23)
+++ lib/MT/Template/Context.pm (4.32)
@@ -252,6 +263,9 @@
                 if (ref $code eq 'HASH') {
                     $code = $code->{code} ||= MT->handler_to_coderef($code->{handler});
                 }
+                elsif (defined $code and ! ref $code) {
+                    $code = MT->handler_to_coderef($code);
+                }
                 $str = $code->($str, $val, $ctx);
             }
         }