Question
I am developing a plugin. Started on TWiki 4x0x2. Got it to work. See:
SoYouWantToWriteAPlugin.
Upgraded to TWiki 4x1x2 and now it fails.
Sample code:
# SMELL: need a new prefs object for each topic
my $wikiName = 'TWikiGuest';
my $twiki = $TWiki::Plugins::SESSION;
$twiki->{prefs} = new TWiki::Prefs($twiki);
$twiki->{prefs}->pushGlobalPreferences();
$twiki->{prefs}->pushPreferences($TWiki::cfg{UsersWebName}, $wikiName, 'USER '.$wikiName);
$twiki->{prefs}->pushWebPreferences($prefs{'web'});
$twiki->{prefs}->pushPreferences($prefs{'web'}, $prefs{'topic'}, 'TOPIC');
$twiki->{prefs}->pushPreferenceValues('SESSION', $twiki->{client}->getSessionValues());
#BvO I think it fails here ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
$page = TWiki::Func::expandCommonVariables($page, $topic, $prefs{'web'});
$page = TWiki::Func::renderText($page, $prefs{'web'});
Error reported is:
Can't call method "getSessionValues" on an undefined value...
I don't understand what value is undefined, let alone how to define it or work around.
Since I don't understand what this code does, and I believe it wil be superceeded in release 4.2. should I just give up and go back to TWiki 4x0x2. Or is there a way to fix this.
Environment
--
BramVanOosterhout - 01 Jul 2007
Answer
If you answer a question - or someone answered one of your questions - please remember to edit the page and set the status to answered. The status selector is below the edit box.
I reread my notes in
SoYouWantToWriteAPlugin and saw that I had noted a change in
PublishContrib. I did not understand what it did. But "desperate times ask for desperate measures". So I used
SvenDowideit 's code slightly modified.
######### Replace this lot by the latest implementation in PublishContrib
# # SMELL: need a new prefs object for each topic
# my $wikiName = 'TWikiGuest';
# my $twiki = $TWiki::Plugins::SESSION;
# $twiki->{prefs} = new TWiki::Prefs($twiki);
# $twiki->{prefs}->pushGlobalPreferences();
# $twiki->{prefs}->pushPreferences($TWiki::cfg{UsersWebName}, $wikiName, 'USER '.$wikiName);
# $twiki->{prefs}->pushWebPreferences($prefs{'web'});
# $twiki->{prefs}->pushPreferences($prefs{'web'}, $prefs{'topic'}, 'TOPIC');
# $twiki->{prefs}->pushPreferenceValues('SESSION', $twiki->{client}->getSessionValues());
#---------------------------------------------------
# clone the current session
my $oldTWiki = $TWiki::Plugins::SESSION;
# Create a new TWiki so that the contexts are correct. This is really,
# really inefficient, but is essential at the moment to maintain correct
# prefs
my $query = $oldTWiki->{cgiQuery};
$query->param('topic', "$prefs{'web'}.$topic");
my $twiki = new TWiki('TWikiGuest', $query);
$TWiki::Plugins::SESSION = $twiki;
########## end replace
########## note the restore twiki object four lines down
$page = TWiki::Func::expandCommonVariables($page, $topic, $prefs{'web'});
$page = TWiki::Func::renderText($page, $prefs{'web'});
# do it twice, in case the rendering defines more variables
$page = TWiki::Func::expandCommonVariables($page, $topic, $prefs{'web'});
$page = TWiki::Func::renderText($page, $prefs{'web'});
$TWiki::Plugins::SESSION = $oldTWiki; # restore twiki object
And now the script works again. As Sven notes, the solution is not fast: 4 minutes to render 800 pages (0.3 second per page). It was under .1s/page in 4.0.2. But it works. And, I have achieved my objective to eliminate all dependencies on undocumented API calls.
Thanks to
SvenDowideit , for putting the solution together.
If there are no simple suggestions to improve the performance, you may close this question. I will also update my comments in
SoYouWantToWriteAPlugin.
--
BramVanOosterhout - 03 Jul 2007
Seems to be answered. On performance see other
Tag:performance
topics. The
VarCachePlugin might help.
--
PeterThoeny - 12 Aug 2007