[pmwiki-users] Trying to add new page variable problem

Peter & Melodye Bowers pbowers at pobox.com
Mon Feb 23 14:25:58 CST 2009


> Hi, I think I get that, but from a more general point of view I am
> trying to understand how to use markup like (:sitemap_priority 0.5:), ie
> like the (:title:) markup

You'll have to set up your own function in the $EditFunctions[] array which
will scan the text for this markup and set $page['sitemap_priority']
appropriately before the page is actually saved.  You'll probably also want
to set up your own Markup(...) to hide or else display more presentably the
actual markup.

That's the way I would approach the problem -- there are probably better
ways out there...

> Also if you look at the current sitemap code, it directly accesses the
> $page variable so that it can optionally bypass the page auth.  If you
> look at PageVar you can see it respects the read auth limits.  So to try
> and keep the key features of the current recipe I am trying to
> understand the PageVar function and also how the other markup function
> is weaving it's magic (I can't use PageVar directly if I want to bypass
> the read auth)

In the text file syntax of a wiki page, each line starts with some sort of
label followed by a colon followed by the relevant data.  (Note you should
look at a file and compare it to what you know about that page so that you
can see what it looks like.) It could look something like this:

version=pmwiki-2.2.0-beta37 ordered=1 urlencoded=1
agent=Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR
1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0; .NET CLR 2.0.50727)
author=David Smith
csum=Corrected spelling
ctime=1172845515
host=80.90.81.71
name=Expats.Expats
rev=2
targets=
text=(:title Foreigners (expats) in Albania:)%0aTirana is the capital of
Albania.  With a population of nearly a million people, Tirana holds nearly
one third of the population of the country.%0a%0aThe exact number of
expatriates is not known, but there is a significant number living here from
all over the world, whether missionary, businesspersons, embassy, or
otherwise.  This section of the CCL Wiki is intended to be a place where
expatriates can share information about living here in Tirana.  Favorite
restaurants, things to do, education opportunities for children - whatever
you would like to see in this section just go ahead and get it started and
somebody else will hopefully expand it.%0a%0a!Pages in this
section:%0a(:pagelist group=Expats:)
time=1174551769
title=Foreigners (expats) in Albania
author:1174551769=David Smith
csum:1174551769=Corrected spelling

(Names changed to protect the innocent.  Note that my email client has
wrapped at least the agent= and the text= line -- if you were reading these
in a text editor with wordwrap turned off you would see that each element
was a single line.)

When the page is read in (via ReadPage() to ignore authorizations or, if you
want to respect auth, RetrieveAuthPage()) then it creates an array out of
that file (in most contexts called $page).  The index into the array (the
key) is the label (the text before the equals) and the value of that array
element is the text to the right of the equals sign.  (This is an
over-simplification, but it will work in most cases -- I think dealing with
page history and revisions is the only time when this doesn't work.)  (All
this happens in the read() function within the PageStore class.)  Thus you
can access the $page['text'] to get the text of the page or $page['date'] to
get a date of the page, etc.  If you have created a key/label named
sitemap_priority then you could access it as $page['sitemap_priority'] if it
existed in the file before it was read into memory.

When the page is being *written* it writes out all keys followed by an
equals sign followed by all values, each on its own line.  It also does a
bit of stuff to make multi-line text go on a single line (using %0a as a
newline separator) and etc.  But what this means is that if you want to
create a new page attribute all you  have to do is set
$page['my-new-attribute'] before the actual page-post occurs.  If you get
your function into the $EditFunctions in the right order then that should
solve the problem for you.  

> >From what I can see, the the text variables are accessed via
> $page['text']['some_var'], but the markup like title/date is accessed
> via $page['date'], etc.  What I can't quite understand is how the $page
> object is built.  I think it's actually down to how the page is being
> saved, but I can't see how to inject a new variable into the page?

I'm not sure exactly what you mean with the $page['text']['some_var']...
$page['text'] is a string and can only be indexed as a string, not as an
array...

Hopefully my comments above have explained how the $page[] array is used
writing the page and how it is built as the page is read from disk.

> As an aside, I can't really see how the pagevar attributes get into the
> $page['text'] array either??

$page['text'] contains just the source of the text of your page -- just what
you would see if you edited the page or if you accessed the page with
?action=source.

> Can anyone talk me through this bit of code please?

Hope that helps.  You're getting into some potentially dangerous territory
as you mess around with the $page array before it is written.  You're going
to want to understand this stuff a *lot* better than the brief intro I've
just tried to provide.  For that I'm afraid there's no substitute for a
bottle of aspirin and some good, old-fashioned code-reading combined with a
lot of echo statements interspersed to confirm what you *think* it's
doing...

Good luck!

-Peter




More information about the pmwiki-users mailing list