home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / groupoffice-com-2.01 / modules / wiki / lib / page.php < prev    next >
PHP Script  |  2004-03-08  |  3KB  |  98 lines

  1. <?php
  2. // $Id: page.php,v 1.1 2004/01/12 22:14:05 comsubvie Exp $
  3.  
  4. // Abstractor to read and write wiki pages.
  5. class WikiPage
  6. {
  7.   var $name = '';                       // Name of page.
  8.   var $dbname = '';                     // Name used in DB queries.
  9.   var $text = '';                       // Page's text in wiki markup form.
  10.   var $time = '';                       // Page's modification time.
  11.   var $hostname = '';                   // Hostname of last editor.
  12.   var $username = '';                   // Username of last editor.
  13.   var $comment  = '';                   // Description of last edit.
  14.   var $version = -1;                    // Version number of page.
  15.   var $mutable = 1;                     // Whether page may be edited.
  16.   var $exists = 0;                      // Whether page already exists.
  17.   var $db;                              // Database object.
  18.  
  19.   function WikiPage($db_, $name_ = '')
  20.   {
  21.     $this->db = $db_;
  22.     $this->name = $name_;
  23.     $this->dbname = str_replace('\\', '\\\\', $name_);
  24.     $this->dbname = str_replace('\'', '\\\'', $this->dbname);
  25.   }
  26.  
  27.   // Check whether a page exists.
  28.   // Returns: nonzero if page exists in database.
  29.  
  30.   function exists()
  31.   {
  32.     global $PgTbl;
  33.  
  34.     $qid = $this->db->query("SELECT MAX(version) FROM $PgTbl " .
  35.                             "WHERE title='$this->dbname'");
  36.     return !!(($result = $this->db->result($qid)) && $result[0]);
  37.   }
  38.  
  39.   // Read in a page's contents.
  40.   // Returns: contents of the page.
  41.  
  42.   function read()
  43.   {
  44.     global $PgTbl;
  45.  
  46.     $query = "SELECT title, time, author, body, mutable, version, " .
  47.              "username, comment " .
  48.              "FROM $PgTbl WHERE title = '$this->dbname' ";
  49.     if($this->version != -1)
  50.       { $query = $query . "AND version = '$this->version'"; }
  51.     else
  52.       { $query = $query . "ORDER BY version DESC"; }
  53.  
  54.     $qid = $this->db->query($query);
  55.  
  56.     if(!($result = $this->db->result($qid)))
  57.       { return ""; }
  58.  
  59.     $this->time     = $result[1];
  60.     $this->hostname = $result[2];
  61.     $this->exists   = 1;
  62.     $this->version  = $result[5];
  63.     $this->mutable  = ($result[4] == 'on');
  64.     $this->username = $result[6];
  65.     $this->text     = $result[3];
  66.     $this->comment  = $result[7];
  67.  
  68.     return $this->text;
  69.   }
  70.  
  71.   // Write out a page's contents.
  72.   // Note: caller is responsible for performing locking.
  73.   // Note: it is assumed that the 'time' member actually contains the
  74.   //       modification-time for the *previous* version.  It is expected that
  75.   //       the previous version will have been read into the same object.
  76.   //       Yes, this is a tiny kludge. :-)
  77.  
  78.   function write()
  79.   {
  80.     global $PgTbl;
  81.  
  82.     $this->db->query("INSERT INTO $PgTbl (title, version, time, supercede, " .
  83.                      "mutable, username, author, comment, body) " .
  84.                      "VALUES('$this->dbname', $this->version, NULL, NULL, '" .
  85.                      ($this->mutable ? 'on' : 'off') . "', " .
  86.                      "'$this->username', '$this->hostname', " .
  87.                      "'$this->comment', '$this->text')");
  88.  
  89.     if($this->version > 1)
  90.     {
  91.       $this->db->query("UPDATE $PgTbl SET time=$this->time, " .
  92.                        "supercede=NULL WHERE title='$this->dbname' " .
  93.                        "AND version=" . ($this->version - 1));
  94.     }
  95.   }
  96. }
  97. ?>
  98.