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

  1. <?php
  2. // $Id: pagestore.php,v 1.1 2004/01/12 22:14:05 comsubvie Exp $
  3.  
  4. require('lib/db.php');
  5. require('lib/page.php');
  6.  
  7. // Abstractor for the page database.  Note that page.php contains the actual
  8. //   code to read/write pages; this serves more general query functions.
  9. class PageStore
  10. {
  11.   var $dbh;
  12.  
  13.   function PageStore()
  14.   {
  15.     global $DBPersist, $DBServer, $DBUser, $DBPasswd, $DBName;
  16.  
  17.     $this->dbh = new WikiDB($DBPersist, $DBServer, $DBUser, $DBPasswd, $DBName);
  18.   }
  19.  
  20.   // Create a page object.
  21.   function page($name = '')
  22.   {
  23.     return new WikiPage($this->dbh, $name);
  24.   }
  25.  
  26.   // Find text in the database.
  27.   function find($text)
  28.   {
  29.     global $PgTbl;
  30.  
  31.     $qid = $this->dbh->query("SELECT t1.title, t1.body, t1.version, " .
  32.                              "MAX(t2.version) " .
  33.                              "FROM $PgTbl AS t1, $PgTbl AS t2 " .
  34.                              "WHERE t1.title = t2.title " .
  35.                              "GROUP BY t2.title, t1.version " .
  36.                              "HAVING t1.version = MAX(t2.version) " .
  37.                              "AND (body LIKE '%$text%' OR title LIKE '%$text%')");
  38.  
  39.  
  40.     $list = array();
  41.     while(($result = $this->dbh->result($qid)))
  42.     {
  43.       $list[] = $result[0];
  44.     }
  45.  
  46.     return $list;
  47.   }
  48.  
  49.   // Retrieve a page's edit history.
  50.   function history($page)
  51.   {
  52.     global $PgTbl;
  53.  
  54.     $page = addslashes($page);
  55.     $qid = $this->dbh->query("SELECT time, author, version, username, " .
  56.                              "comment " .
  57.                              "FROM $PgTbl WHERE title='$page' " .
  58.                              "ORDER BY version DESC");
  59.  
  60.     $list = array();
  61.     while(($result = $this->dbh->result($qid)))
  62.     {
  63.       $list[] = array($result[0], $result[1], $result[2], $result[3],
  64.                       $result[4]);
  65.     }
  66.  
  67.     return $list;
  68.   }
  69.  
  70.   // Look up an interwiki prefix.
  71.   function interwiki($name)
  72.   {
  73.     global $IwTbl;
  74.  
  75.     $name = addslashes($name);
  76.     $qid = $this->dbh->query("SELECT url FROM $IwTbl WHERE prefix='$name'");
  77.     if(($result = $this->dbh->result($qid)))
  78.       { return $result[0]; }
  79.     return '';
  80.   }
  81.  
  82.   // Clear all the links cached for a particular page.
  83.   function clear_link($page)
  84.   {
  85.     global $LkTbl;
  86.  
  87.     $page = addslashes($page);
  88.     $this->dbh->query("DELETE FROM $LkTbl WHERE page='$page'");
  89.   }
  90.  
  91.   // Clear all the interwiki definitions for a particular page.
  92.   function clear_interwiki($page)
  93.   {
  94.     global $IwTbl;
  95.  
  96.     $page = addslashes($page);
  97.     $this->dbh->query("DELETE FROM $IwTbl WHERE where_defined='$page'");
  98.   }
  99.  
  100.   // Clear all the sisterwiki definitions for a particular page.
  101.   function clear_sisterwiki($page)
  102.   {
  103.     global $SwTbl;
  104.  
  105.     $page = addslashes($page);
  106.     $this->dbh->query("DELETE FROM $SwTbl WHERE where_defined='$page'");
  107.   }
  108.  
  109.   // Add a link for a given page to the link table.
  110.   function new_link($page, $link)
  111.   {
  112.     // Assumption: this will only ever be called with one page per
  113.     //   script invocation.  If this assumption should change, $links should
  114.     //   be made a 2-dimensional array.
  115.  
  116.     global $LkTbl;
  117.     static $links = array();
  118.  
  119.     $page = addslashes($page);
  120.     $link = addslashes($link);
  121.  
  122.     if(empty($links[$link]))
  123.     {
  124.       $this->dbh->query("INSERT INTO $LkTbl VALUES('$page', '$link', 1)");
  125.       $links[$link] = 1;
  126.     }
  127.     else
  128.     {
  129.       $links[$link]++;
  130.       $this->dbh->query("UPDATE $LkTbl SET count=" . $links[$link] .
  131.                         " WHERE page='$page' AND link='$link'");
  132.     }
  133.   }
  134.  
  135.   // Add an interwiki definition for a particular page.
  136.   function new_interwiki($where_defined, $prefix, $url)
  137.   {
  138.     global $IwTbl;
  139.  
  140.     $url = str_replace("'", "\\'", $url);
  141.     if (preg_match("/(.*)\?(.*)/", $url, $match)) 
  142.     {
  143.       $match[2] = preg_replace("/&(?!amp;)/", '&', $match[2]);
  144.       $url = $match[1] . '?'. $match[2];
  145.     }
  146.     
  147.     $where_defined = addslashes($where_defined);
  148.  
  149.     $qid = $this->dbh->query("SELECT where_defined FROM $IwTbl " .
  150.                              "WHERE prefix='$prefix'");
  151.     if($this->dbh->result($qid))
  152.     {
  153.       $this->dbh->query("UPDATE $IwTbl SET where_defined='$where_defined', " .
  154.                         "url='$url' WHERE prefix='$prefix'");
  155.     }
  156.     else
  157.     {
  158.       $this->dbh->query("INSERT INTO $IwTbl(prefix, where_defined, url) " .
  159.                         "VALUES('$prefix', '$where_defined', '$url')");
  160.     }
  161.   }
  162.  
  163.   // Add a sisterwiki definition for a particular page.
  164.   function new_sisterwiki($where_defined, $prefix, $url)
  165.   {
  166.     global $SwTbl;
  167.  
  168.     $url = str_replace("'", "\\'", $url);
  169.     if (preg_match("/(.*)\?(.*)/", $url, $match)) 
  170.     {
  171.       $match[2] = preg_replace("/&(?!amp;)/", '&', $match[2]);
  172.       $url = $match[1] . '?'. $match[2];
  173.     }
  174.  
  175.     $where_defined = addslashes($where_defined);
  176.  
  177.     $qid = $this->dbh->query("SELECT where_defined FROM $SwTbl " .
  178.                              "WHERE prefix='$prefix'");
  179.     if($this->dbh->result($qid))
  180.     {
  181.       $this->dbh->query("UPDATE $SwTbl SET where_defined='$where_defined', " .
  182.                         "url='$url' WHERE prefix='$prefix'");
  183.     }
  184.     else
  185.     {
  186.       $this->dbh->query("INSERT INTO $SwTbl(prefix, where_defined, url) " .
  187.                         "VALUES('$prefix', '$where_defined', '$url')");
  188.     }
  189.   }
  190.  
  191.   // Find all twins of a page at sisterwiki sites.
  192.   function twinpages($page)
  193.   {
  194.     global $RemTbl;
  195.  
  196.     $list = array();
  197.     $page = addslashes($page);
  198.     $q2 = $this->dbh->query("SELECT site, page FROM $RemTbl " .
  199.                             "WHERE page LIKE '$page'");
  200.     while(($twin = $this->dbh->result($q2)))
  201.       { $list[] = array($twin[0], $twin[1]); }
  202.  
  203.     return $list;
  204.   }
  205.  
  206.   // Lock the database tables.
  207.   function lock()
  208.   {
  209.     global $PgTbl, $IwTbl, $SwTbl, $LkTbl;
  210.  
  211.     $this->dbh->query("LOCK TABLES $PgTbl WRITE, $IwTbl WRITE, $SwTbl WRITE, " .
  212.                       "$LkTbl WRITE");
  213.   }
  214.  
  215.   // Unlock the database tables.
  216.   function unlock()
  217.   {
  218.     $this->dbh->query("UNLOCK TABLES");
  219.   }
  220.  
  221.   // Retrieve a list of all of the pages in the wiki.
  222.   function allpages()
  223.   {
  224.     global $PgTbl;
  225.  
  226.     $qid = $this->dbh->query("SELECT t1.title, t1.version, t1.author, t1.time, " .
  227.                              "t1.username, LENGTH(t1.body), t1.comment, " .
  228.                              "t1.mutable, MAX(t2.version) " .
  229.                              "FROM $PgTbl AS t1, $PgTbl AS t2 " .
  230.                              "WHERE t1.title = t2.title " .
  231.                              "GROUP BY t2.title, t1.version " .
  232.                              "HAVING t1.version = MAX(t2.version)");
  233.  
  234.     $list = array();
  235.     while(($result = $this->dbh->result($qid)))
  236.     {
  237.       $list[] = array($result[3], $result[0], $result[2], $result[4],
  238.                       $result[5], $result[6], $result[7] == 'on', $result[1]);
  239.     }
  240.  
  241.     return $list;
  242.   }
  243.  
  244.   // Retrieve a list of all new pages in the wiki.
  245.   function newpages()
  246.   {
  247.     global $PgTbl;
  248.  
  249.     $qid = $this->dbh->query("SELECT title, author, time, username, " .
  250.                              "LENGTH(body), comment " .
  251.                              "FROM $PgTbl WHERE version=1");
  252.  
  253.     $list = array();
  254.     while(($result = $this->dbh->result($qid)))
  255.     {
  256.       $list[] = array($result[2], $result[0], $result[1], $result[3],
  257.                       $result[4], $result[5]);
  258.     }
  259.  
  260.     return $list;
  261.   }
  262.  
  263.   // Return a list of all empty (deleted) pages in the wiki.
  264.   function emptypages()
  265.   {
  266.     global $PgTbl;
  267.  
  268.     $qid = $this->dbh->query("SELECT t1.title, t1.version, t1.author, " .
  269.                              "t1.time, t1.username, t1.comment, t1.body, " .
  270.                              "MAX(t2.version) " .
  271.                              "FROM $PgTbl AS t1, $PgTbl AS t2 " .
  272.                              "WHERE t1.title = t2.title " .
  273.                              "GROUP BY t2.title, t1.version " .
  274.                              "HAVING t1.version = MAX(t2.version) " .
  275.                              "AND t1.body=''");
  276.  
  277.     $list = array();
  278.     while(($result = $this->dbh->result($qid)))
  279.     {
  280.       $list[] = array($result[3], $result[0], $result[2],
  281.                       $result[4], 0, $result[5]);
  282.     }
  283.  
  284.     return $list;
  285.   }
  286.  
  287.   // Return a list of information about a particular set of pages.
  288.   function givenpages($names)
  289.   {
  290.     global $PgTbl;
  291.  
  292.     $list = array();
  293.     foreach($names as $page)
  294.     {
  295.       $esc_page = addslashes($page);
  296.       $qid = $this->dbh->query("SELECT time, author, username, LENGTH(body), " .
  297.                                "comment FROM $PgTbl WHERE title='$esc_page' " .
  298.                                "ORDER BY version DESC");
  299.  
  300.       if(!($result = $this->dbh->result($qid)))
  301.         { continue; }
  302.  
  303.       $list[] = array($result[0], $page, $result[1], $result[2],
  304.                       $result[3], $result[4]);
  305.     }
  306.  
  307.     return $list;
  308.   }
  309.  
  310.   // Expire old versions of pages.
  311.   function maintain()
  312.   {
  313.     global $PgTbl, $RtTbl, $ExpireLen, $RatePeriod;
  314.  
  315.     $qid = $this->dbh->query("SELECT title, MAX(version) FROM $PgTbl " .
  316.                              "GROUP BY title");
  317.  
  318.     if ($ExpireLen != 0) 
  319.     {
  320.       while(($result = $this->dbh->result($qid)))
  321.       {
  322.         $result[0] = addslashes($result[0]);
  323.         $this->dbh->query("DELETE FROM $PgTbl WHERE title='$result[0]' AND " .
  324.                           "(version < $result[1] OR body='') AND " .
  325.                           "TO_DAYS(NOW()) - TO_DAYS(supercede) > $ExpireLen");
  326.       }
  327.     }
  328.  
  329.     if($RatePeriod)
  330.     {
  331.       $this->dbh->query("DELETE FROM $RtTbl " .
  332.                         "WHERE ip NOT LIKE '%.*' " .
  333.                         "AND TO_DAYS(NOW()) > TO_DAYS(time)");
  334.     }
  335.   }
  336. }
  337. ?>
  338.