create new tag
, view all tags

Feature Proposal: Reading TWIKIPREF from Core code


We have a javascript interface for reading TWIKIPREF cookie settings: pub/TWiki/TWikiJavascripts/twikiPref.js. We now also need to be able to read the prefs server side, see Bugs:Item3159, where we could do away with inserting javascript into the rendered HTML if the plugin would know the user cookie.

Description and Documentation

Is there an existing lib file where this could be added to, or do we need something like TWikiPref.pm?


Impact and Available Solutions


-- Contributors: ArthurClemens


Is there a reason why you can't just use CGI::Cookie to do this?

use CGI::Cookie;
my %cookies = CGI::Cookie->fetch();
my $pref = $cookies{TWIKIPREF}->value();

-- CrawfordCurrie - 04 Dec 2006

It needs one more step because the all TWIKIPREF values are stored in this cookie using key-values. Each pref is separated with a pipe |, and key-values are separated with a =.

-- ArthurClemens - 04 Dec 2006

Would that be new TWikiFuncDotPm functions, such as TWiki::Func::getTWikiPrefs(foo) and TWiki::Func::setTWikiPrefs(foo, value)?

-- PeterThoeny - 05 Dec 2006

I think this has to be approached with great caution. Cookies are extremely limited in size, and care should be taken to ensure they are not used as a dumping ground. The session already provides almost limitless storage for values that have to persist, and has an existing API in TWiki::Func. The session should IMHO be used in preference to cookies wherever possible.

The one possible exception to this is the case Arthur is alluding to; where a cookie value is set in the browser, and must be communicated back to TWiki (for example, which twisties are open, that sort of thing). These values are a bit of a PITA to store in the session.

Arthur, can you clarify why you want these values back on the server, and why the session storage is not adequate for them? It may make more sense to deserialise the TWIKIPREF cookie into the Session object.

-- CrawfordCurrie - 05 Dec 2006

Why not a session cookie: these are settings we want to keep after next visit. If a user wants to hide attachments, he probably wants to have it hidden next time as well. Same goes for the size of the edit box.

Yet these are things you want to control in the interface, so a setting in a user topic would be a too great hassle. Besides, it may depend on the computer you are working on.

So using cookies: the settings of a twisty is stored using javascript - client side, as TwistyContrib does not use any perl. For users that do not have javascript enabled we provide a fallback mechanism that shows all to-be-collapsed content normally, without any toggle buttons. Content is made hidden and buttons visible by javascript.

Normally this code will be executed at page onload. But if the page takes long to download you will see collapsing content everywhere. So TwistyPlugin introduces a smartness: it inserts javascript right after the collapsing element that calls an init on that element. Downside is that this pollutes the generated html. And it needs an extra check plus cleanup to prevent people to use the TwistyPlugin javascript code as a circumvention to write javascript where the TWikimaster has made this impossible in configure.

It would be better if the plugin uses the cookie settings to display the correct initial setting of the twisty contents. No extra javascript needed, and it creates instant display of the element.

As far as I can see now it would only be necessary to read TWIKIPREF - writing can be done using the existing Javascript code.

-- ArthurClemens - 05 Dec 2006

I understand, I think. Given that:

  1. this is a one-off,
  2. we don't really want to encourage use of cookies for storing state, and
  3. CGI::Cookie already provides 99% of what you want to do,
I recommend you use CGI::Cookie in the TwistyPlugin code. It is trivial to split the cookie value on the pipe symbol, thus:
use CGI::Cookie;
my %cookies = CGI::Cookie->fetch(); 
my %values = map { split(/=/, $_) } split(/\|/, $cookies{TWIKIPREF}->value());

-- CrawfordCurrie - 06 Dec 2006

Edit | Attach | Watch | Print version | History: r8 < r7 < r6 < r5 < r4 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r8 - 2006-12-06 - CrawfordCurrie
  • Learn about TWiki  
  • Download TWiki
This site is powered by the TWiki collaboration platform Powered by Perl Hosted by OICcam.com Ideas, requests, problems regarding TWiki? Send feedback. Ask community in the support forum.
Copyright © 1999-2018 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.