[pmwiki-users] Suggestion for a Page object

Martin Fick fick at fgm.com
Fri Apr 8 15:16:42 CDT 2005


Pm,

What do you think about adding a class along the lines of this:

class Page {
  var $pagestore;
  var $pagename;
  var $page; // Format returned from ReadPage
  
  function Page($pagestore, $pagename, $pagefile="") {
    $this->pagestore = & $pagestore;
    $this->pagename = $pagename;
  }
  function get($prop) {
    if($prop == 'pagename')  return $this->pagename;
    if($prop == 'pagestore') return $this->pagestore;
    if($prop == 'dirfmt')    return $this->getPageStoreFmt();
    if($prop == 'pagefile')  return $this->getPageFile();
    if($prop == 'size')      return strlen($this->get('text'));
    if (!$this->page)        $this->page = & $this->readPage(); 
    if ($prop == 'page')     return $this->page;
    return $this->page[$prop];
  }
  function getPageFile() {
    $fmt = $this->get('dirfmt');
    $pagename = $this->get('pagename');
    return FmtPageName($fmt, $pagename);
  }
  function getPageStoreFmt() {
    $pagestore = $this->get('pagestore');
    return $pagestore->dirfmt;
  }
  function & readPage() {
    $pagefile = $this->get('pagefile');
    $pagestore = $this->get('pagestore');
    return $pagestore->readR($pagefile);  # read using file instead of pagename
  }
}

Complemented or merged with a class like this:

class CachingPage extends Page {
  var $props;
  function CachingPage($pagestore, $pagename, $props=NULL) {
    Page::Page($pagestore, $pagename);
    $this->props = & $props;
  }
  function get($prop) {
    $val = $this->props[$prop];
    if($val) return $val;
    $val = Page::get($prop);
    $this->props[$prop]=$val;
    return $val;
  }
}


  The idea being to help make attributes load on demand. 
This class would help ease the feature enhancement of
pagelists.  It would allow the various formatting functions
to display extra information about pages not already
gathered in FmtPageList.  It is obviously a little more
complex than an array, but I think it could make
programming alternate formats easier (I am customizing
quite a few varieties already myself).  

  Also, if ListPages returns an array of these (Caching)Page
objects, the pagefile can already be stored in it and the
FmtNamePage lookup can be skipped. This is similar to the
earlier ugly hack that I sent doing this, except not so
ugly and with broader utility.

  I imagine that this object could be enhanced to perform
certain default lookups automatically and to discard info
when no longer desired so as to free up memory.  By adding
a clear method, this object could end up saving memory over
the current approach.
 
  -Martin




More information about the pmwiki-users mailing list