Filename | /var/www/foswiki11/lib/Foswiki/Plugins/DelayMacroPlugin.pm |
Statements | Executed 16 statements in 454µs |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 34µs | 114µs | initPlugin | Foswiki::Plugins::DelayMacroPlugin::
1 | 1 | 1 | 13µs | 27µs | BEGIN@18 | Foswiki::Plugins::DelayMacroPlugin::
1 | 1 | 1 | 4µs | 4µs | BEGIN@20 | Foswiki::Plugins::DelayMacroPlugin::
1 | 1 | 1 | 3µs | 3µs | BEGIN@21 | Foswiki::Plugins::DelayMacroPlugin::
0 | 0 | 0 | 0s | 0s | _delayMacro | Foswiki::Plugins::DelayMacroPlugin::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | # See bottom of file for default license and copyright information | ||||
2 | |||||
3 | =begin TML | ||||
4 | |||||
5 | ---+ package DelayMacroPlugin | ||||
6 | |||||
7 | This is a simple plugin that can delay the expansion of macros. | ||||
8 | You use this when you have a macro inside another macro and that supports | ||||
9 | tokens $percnt and $quot such as SEARCH and you want this macro to expand | ||||
10 | after the SEARCH is complete. | ||||
11 | |||||
12 | =cut | ||||
13 | |||||
14 | # change the package name!!! | ||||
15 | package Foswiki::Plugins::DelayMacroPlugin; | ||||
16 | |||||
17 | # Always use strict to enforce variable scoping | ||||
18 | 2 | 29µs | 2 | 41µs | # spent 27µs (13+14) within Foswiki::Plugins::DelayMacroPlugin::BEGIN@18 which was called:
# once (13µs+14µs) by Foswiki::Plugin::BEGIN@2.10 at line 18 # spent 27µs making 1 call to Foswiki::Plugins::DelayMacroPlugin::BEGIN@18
# spent 14µs making 1 call to strict::import |
19 | |||||
20 | 2 | 19µs | 1 | 4µs | # spent 4µs within Foswiki::Plugins::DelayMacroPlugin::BEGIN@20 which was called:
# once (4µs+0s) by Foswiki::Plugin::BEGIN@2.10 at line 20 # spent 4µs making 1 call to Foswiki::Plugins::DelayMacroPlugin::BEGIN@20 |
21 | 2 | 354µs | 1 | 3µs | # spent 3µs within Foswiki::Plugins::DelayMacroPlugin::BEGIN@21 which was called:
# once (3µs+0s) by Foswiki::Plugin::BEGIN@2.10 at line 21 # spent 3µs making 1 call to Foswiki::Plugins::DelayMacroPlugin::BEGIN@21 |
22 | |||||
23 | # $VERSION is referred to by Foswiki, and is the only global variable that | ||||
24 | # *must* exist in this package. This should always be in the format | ||||
25 | # $Rev: 6882 (2010-03-24) $ so that Foswiki can determine the checked-in status of the | ||||
26 | # extension. | ||||
27 | 1 | 600ns | our $VERSION = '$Rev: 6882 (2010-03-24) $'; | ||
28 | |||||
29 | # $RELEASE is used in the "Find More Extensions" automation in configure. | ||||
30 | 1 | 200ns | our $RELEASE = '1.1'; | ||
31 | |||||
32 | # Short description of this plugin | ||||
33 | # One line description, is shown in the %SYSTEMWEB%.TextFormattingRules topic: | ||||
34 | 1 | 300ns | our $SHORTDESCRIPTION = 'Plugin delays expansion of Foswiki macros'; | ||
35 | |||||
36 | # You must set $NO_PREFS_IN_TOPIC to 0 if you want your plugin to use | ||||
37 | # preferences set in the plugin topic. This is required for compatibility | ||||
38 | # with older plugins, but imposes a significant performance penalty, and | ||||
39 | # is not recommended. Instead, leave $NO_PREFS_IN_TOPIC at 1 and use | ||||
40 | # =$Foswiki::cfg= entries, or if you want the users | ||||
41 | # to be able to change settings, then use standard Foswiki preferences that | ||||
42 | # can be defined in your %USERSWEB%.SitePreferences and overridden at the web | ||||
43 | # and topic level. | ||||
44 | # | ||||
45 | # %SYSTEMWEB%.DevelopingPlugins has details of how to define =$Foswiki::cfg= | ||||
46 | # entries so they can be used with =configure=. | ||||
47 | 1 | 200ns | our $NO_PREFS_IN_TOPIC = 1; | ||
48 | |||||
49 | =begin TML | ||||
50 | |||||
51 | ---++ initPlugin($topic, $web, $user) -> $boolean | ||||
52 | * =$topic= - the name of the topic in the current CGI query | ||||
53 | * =$web= - the name of the web in the current CGI query | ||||
54 | * =$user= - the login name of the user | ||||
55 | * =$installWeb= - the name of the web the plugin topic is in | ||||
56 | (usually the same as =$Foswiki::cfg{SystemWebName}=) | ||||
57 | |||||
58 | *REQUIRED* | ||||
59 | |||||
60 | Called to initialise the plugin. If everything is OK, should return | ||||
61 | a non-zero value. On non-fatal failure, should write a message | ||||
62 | using =Foswiki::Func::writeWarning= and return 0. In this case | ||||
63 | %<nop>FAILEDPLUGINS% will indicate which plugins failed. | ||||
64 | |||||
65 | In the case of a catastrophic failure that will prevent the whole | ||||
66 | installation from working safely, this handler may use 'die', which | ||||
67 | will be trapped and reported in the browser. | ||||
68 | |||||
69 | __Note:__ Please align macro names with the Plugin name, e.g. if | ||||
70 | your Plugin is called !FooBarPlugin, name macros FOOBAR and/or | ||||
71 | FOOBARSOMETHING. This avoids namespace issues. | ||||
72 | |||||
73 | =cut | ||||
74 | |||||
75 | # spent 114µs (34+79) within Foswiki::Plugins::DelayMacroPlugin::initPlugin which was called:
# once (34µs+79µs) by Foswiki::Plugin::__ANON__[/var/www/foswiki11/lib/Foswiki/Plugin.pm:241] at line 234 of /var/www/foswiki11/lib/Foswiki/Plugin.pm | ||||
76 | 1 | 2µs | my ( $topic, $web, $user, $installWeb ) = @_; | ||
77 | |||||
78 | # check for Plugins.pm versions | ||||
79 | 1 | 30µs | 1 | 20µs | if ( $Foswiki::Plugins::VERSION < 2.0 ) { # spent 20µs making 1 call to version::vxs::VCMP |
80 | Foswiki::Func::writeWarning( 'Version mismatch between ', | ||||
81 | __PACKAGE__, ' and Plugins.pm' ); | ||||
82 | return 0; | ||||
83 | } | ||||
84 | |||||
85 | 1 | 4µs | 1 | 34µs | Foswiki::Func::registerTagHandler( 'DELAY', \&_delayMacro ); # spent 34µs making 1 call to Foswiki::Func::registerTagHandler |
86 | |||||
87 | 1 | 3µs | 1 | 25µs | Foswiki::Func::registerTagHandler( 'DELAYSEARCH', \&_delayMacro ); # spent 25µs making 1 call to Foswiki::Func::registerTagHandler |
88 | |||||
89 | # Plugin correctly initialized | ||||
90 | 1 | 8µs | return 1; | ||
91 | } | ||||
92 | |||||
93 | # delayMacro expands %DELAY.. and returns | ||||
94 | # DELAY hidden by $percnt and $quot if delay > 0 | ||||
95 | # The macro specified by macro parameter if delay = 0 | ||||
96 | sub _delayMacro { | ||||
97 | my($session, $params, $theTopic, $theWeb) = @_; | ||||
98 | # $session - a reference to the Foswiki session object (if you don't know | ||||
99 | # what this is, just ignore it) | ||||
100 | # $params= - a reference to a Foswiki::Attrs object containing | ||||
101 | # parameters. | ||||
102 | # This can be used as a simple hash that maps parameter names | ||||
103 | # to values, with _DEFAULT being the name for the default | ||||
104 | # (unnamed) parameter. | ||||
105 | # $theTopic - name of the topic in the query | ||||
106 | # $theWeb - name of the web in the query | ||||
107 | # Return: the result of processing the macro. This will replace the | ||||
108 | # macro call in the final text. | ||||
109 | |||||
110 | # For example, %EXAMPLETAG{'hamburger' sideorder="onions"}% | ||||
111 | # $params->{_DEFAULT} will be 'hamburger' | ||||
112 | # INPUT will be 'onions' | ||||
113 | |||||
114 | my $delay = defined $params->{delay} ? $params->{delay} : 1; | ||||
115 | $delay = 1 unless $delay =~ /\d+/; | ||||
116 | my $macro = $params->{macro} || 'SEARCH'; | ||||
117 | my $default = defined $params->{_DEFAULT} ? $params->{_DEFAULT} : ''; | ||||
118 | my $result = ''; | ||||
119 | |||||
120 | if ( $delay-- > 0 ) { | ||||
121 | $result = "\$percntDELAY{"; | ||||
122 | $result .= "\$quot$default\$quot "; | ||||
123 | $result .= "delay=\$quot$delay\$quot "; | ||||
124 | |||||
125 | foreach my $key ( keys %$params ) { | ||||
126 | next if ($key eq '_RAW' || $key eq '_DEFAULT' || $key eq 'delay'); | ||||
127 | |||||
128 | $result .= "$key=\$quot" . $params->{$key} . "\$quot "; | ||||
129 | } | ||||
130 | $result .= "}\$percnt"; | ||||
131 | } | ||||
132 | else { | ||||
133 | $result = "%" . $macro . "{"; | ||||
134 | $result .= "\"$default\" "; | ||||
135 | |||||
136 | foreach my $key ( keys %$params ) { | ||||
137 | next if ($key eq '_RAW' || $key eq '_DEFAULT' || | ||||
138 | $key eq 'macro' || $key eq 'delay' ); | ||||
139 | |||||
140 | $result .= $key . "=\"" . $params->{$key} . "\" "; | ||||
141 | } | ||||
142 | |||||
143 | $result .= "}%"; | ||||
144 | } | ||||
145 | |||||
146 | return $result; | ||||
147 | |||||
148 | } | ||||
149 | |||||
150 | |||||
151 | 1 | 3µs | 1; | ||
152 | __END__ |