Make class names autoloadable.
authorJan Schneider <jan@horde.org>
Mon, 18 Oct 2010 11:45:26 +0000 (13:45 +0200)
committerJan Schneider <jan@horde.org>
Mon, 18 Oct 2010 11:45:26 +0000 (13:45 +0200)
29 files changed:
wicked/display.php
wicked/lib/Api.php
wicked/lib/Driver.php
wicked/lib/Driver/Sql.php [new file with mode: 0644]
wicked/lib/Driver/sql.php [deleted file]
wicked/lib/Page.php
wicked/lib/Page/AddPage.php
wicked/lib/Page/AllPages.php
wicked/lib/Page/AttachedFiles.php
wicked/lib/Page/BackLinks.php
wicked/lib/Page/DeletePage.php
wicked/lib/Page/EditPage.php
wicked/lib/Page/LeastPopular.php
wicked/lib/Page/LikePages.php
wicked/lib/Page/MergeOrRename.php
wicked/lib/Page/MostPopular.php
wicked/lib/Page/NewPage.php
wicked/lib/Page/RecentChanges.php
wicked/lib/Page/RevertPage.php
wicked/lib/Page/Search.php
wicked/lib/Page/StandardHistoryPage.php [new file with mode: 0644]
wicked/lib/Page/StandardPage.php
wicked/lib/Page/StandardPage/StdHistoryPage.php [deleted file]
wicked/lib/Page/SyncDiff.php
wicked/lib/Page/SyncPages.php
wicked/lib/Sync.php
wicked/lib/Sync/Wicked.php [new file with mode: 0644]
wicked/lib/Sync/wicked.php [deleted file]
wicked/lib/tests/Driver.php

index 46d894b..6dbc118 100644 (file)
@@ -137,7 +137,7 @@ require WICKED_TEMPLATES . '/menu.inc';
 $page->render(Wicked::MODE_DISPLAY, $params);
 require $registry->get('templates', 'horde') . '/common-footer.inc';
 
-if (is_a($page, 'StandardPage') &&
+if ($page instanceof Wicked_Page_StandardPage &&
     (!isset($_SESSION['wickedSession']['history'][0]) ||
      $_SESSION['wickedSession']['history'][0] != $page->pageName())) {
     array_unshift($_SESSION['wickedSession']['history'], $page->pageName());
index 02ca8c1..49166a9 100644 (file)
@@ -205,7 +205,7 @@ class Wicked_Api extends Horde_Registry_Api
     public function listTemplates()
     {
         global $wicked;
-        $templates = $wicked->getMatchingPages('Template', WICKED_PAGE_MATCH_ENDS);
+        $templates = $wicked->getMatchingPages('Template', Wicked_Page::MATCH_ENDS);
         $list = array(array('category' => _("Wiki Templates"),
             'templates' => array()));
         foreach ($templates as $page) {
index 5916edf..fad267d 100644 (file)
@@ -4,16 +4,6 @@
  */
 
 /**
- * VFS
- */
-require_once 'VFS.php';
-
-define('WICKED_PAGE_MATCH_LEFT', 1);
-define('WICKED_PAGE_MATCH_RIGHT', 2);
-define('WICKED_PAGE_MATCH_ENDS', 3);
-define('WICKED_PAGE_MATCH_ANY', 4);
-
-/**
  * Wicked_Driver:: defines an API for implementing storage backends for
  * Wicked.
  *
@@ -46,7 +36,7 @@ abstract class Wicked_Driver {
      *
      * @param array $params  A hash containing connection parameters.
      */
-    function Wicked_Driver($params = array())
+    function __construct($params = array())
     {
         $this->_params = $params;
     }
@@ -391,7 +381,7 @@ abstract class Wicked_Driver {
         if ($driver === null) {
             $driver = $GLOBALS['conf']['storage']['driver'];
         }
-        $driver = basename($driver);
+        $driver = Horde_String::ucfirst(basename($driver));
 
         if ($params === null) {
             $params = Horde::getDriverConfig('storage', $driver);
@@ -399,15 +389,12 @@ abstract class Wicked_Driver {
 
         $class = 'Wicked_Driver_' . $driver;
         if (!class_exists($class)) {
-            include_once dirname(__FILE__) . '/Driver/' . $driver . '.php';
-        }
-        if (class_exists($class)) {
-            $wicked = new $class($params);
-            $result = $wicked->connect();
-            return $wicked;
+            throw new Wicked_Exception('Definition of ' . $class . ' not found.');
         }
 
-        throw new Wicked_Exception('Definition of ' . $class . ' not found.');
+        $wicked = new $class($params);
+        $result = $wicked->connect();
+        return $wicked;
     }
 
 }
diff --git a/wicked/lib/Driver/Sql.php b/wicked/lib/Driver/Sql.php
new file mode 100644 (file)
index 0000000..ae17109
--- /dev/null
@@ -0,0 +1,845 @@
+<?php
+/**
+ * @package Wicked
+ */
+
+/**
+ * Wicked storage implementation for PHP's PEAR database abstraction
+ * layer.
+ *
+ * The table structure can be created by the scripts/drivers/wicked_foo.sql
+ * script.
+ *
+ * @author  Tyler Colbert <tyler@colberts.us>
+ * @author  Chuck Hagenbuch <chuck@horde.org>
+ * @package Wicked
+ */
+class Wicked_Driver_Sql extends Wicked_Driver {
+
+    /**
+     * Handle for the current database connection.
+     *
+     * @var Horde_Db_Adapter
+     */
+    var $_db;
+
+    /**
+     * Retrieves the page of a particular name from the database.
+     *
+     * @param string $pagename The name of the page to retrieve.
+     *
+     * @return array
+     * @throws Wicked_Exception
+     */
+    function retrieveByName($pagename)
+    {
+        $where = 'page_name = ' . $this->_db->quoteString($this->_convertToDriver($pagename));
+
+        $pages = $this->_retrieve($this->_params['table'], $where);
+
+        if (!empty($pages[0])) {
+            return $pages[0];
+        }
+
+        throw new Wicked_Exception($pagename . ' not found');
+    }
+
+    /**
+     * Retrieves a historic version of a page.
+     *
+     * @param string $pagename  The name of the page to retrieve.
+     * @param string $version   The version to retrieve.
+     *
+     * @return array  The page hash.
+     * @throws Wicked_Exception
+     */
+    function retrieveHistory($pagename, $version)
+    {
+        if (empty($version) or !preg_match('/^[0-9]+\.[0-9]+$/', $version)) {
+            throw new Wicked_Exception('invalid version number');
+        }
+
+        list($major, $minor) = explode('.', $version);
+        $where = sprintf('page_name = %s AND page_majorversion = %s AND ' .
+                         'page_minorversion = %s',
+                         $this->_db->quoteString($this->_convertToDriver($pagename)),
+                         (int)$major, (int)$minor);
+
+        return $this->_retrieve($this->_params['historytable'], $where);
+    }
+
+    function getPage($pagename)
+    {
+        $where = 'page_name = ' . $this->_db->quoteString($this->_convertToDriver($pagename));
+        return $this->_retrieve($this->_params['table'], $where);
+    }
+
+    function getPageById($id)
+    {
+        $where = 'page_id = ' . (int)$id;
+        return $this->_retrieve($this->_params['table'], $where);
+    }
+
+    function getAllPages()
+    {
+        return $this->_retrieve($this->_params['table'], '', 'page_name');
+    }
+
+    function getHistory($pagename)
+    {
+        $where = 'page_name = ' . $this->_db->quoteString($this->_convertToDriver($pagename)) .
+                 ' ORDER BY page_majorversion DESC, page_minorversion DESC';
+
+        return $this->_retrieve($this->_params['historytable'], $where);
+    }
+
+    /**
+     * Returns the most recently changed pages.
+     *
+     * @param integer $days  The number of days to look back.
+     *
+     * @return array  Pages.
+     * @throws Wicked_Exception
+     */
+    function getRecentChanges($days = 3)
+    {
+        $where = 'version_created > ' . (time() - (86400 * $days));
+        $result = $this->_retrieve($this->_params['table'],
+                                   $where,
+                                   'version_created DESC');
+        $result2 = $this->_retrieve($this->_params['historytable'],
+                                    $where,
+                                    'version_created DESC');
+        return array_merge($result, $result2);
+    }
+
+    /**
+     * Returns the most popular pages.
+     *
+     * @param integer $limit  The number of most popular pages to return.
+     *
+     * @return array  Pages.
+     * @throws Wicked_Exception
+     */
+    function mostPopular($limit = 10)
+    {
+        return $this->_retrieve($this->_params['table'], '', 'page_hits DESC', $limit);
+    }
+
+    /**
+     * Returns the least popular pages.
+     *
+     * @param integer $limit  The number of least popular pages to return.
+     *
+     * @return array  Pages.
+     * @throws Wicked_Exception
+     */
+    function leastPopular($limit = 10)
+    {
+        return $this->_retrieve($this->_params['table'], '', 'page_hits ASC', $limit);
+    }
+
+    function searchTitles($searchtext)
+    {
+        require_once 'Horde/SQL.php';
+        $searchtext = $this->_convertToDriver($searchtext);
+        $where = Horde_SQL::buildClause($this->_db, 'page_name', 'LIKE', $searchtext);
+        return $this->_retrieve($this->_params['table'], $where);
+    }
+
+    /**
+     * Finds pages with matches in text or title.
+     *
+     * @param string  $searchtext  The search expression (Google-like).
+     * @param boolean $title default true  If true, both page title and text
+     *                                     are searched.  If false, only page
+     *                                     text is searched.
+     *
+     * @return array  A list of pages.
+     * @throws Wicked_Exception
+     */
+    function searchText($searchtext, $title = true)
+    {
+        require_once 'Horde/SQL/Keywords.php';
+        $searchtext = $this->_convertToDriver($searchtext);
+
+        $textClause = Horde_SQL_Keywords::parse('page_text', $searchtext);
+        if (is_a($textClause, 'PEAR_Error')) {
+            throw new Wicked_Exception($textClause);
+        }
+
+        if ($title) {
+            $nameClause = Horde_SQL_Keywords::parse('page_name', $searchtext);
+            if (is_a($nameClause, 'PEAR_Error')) {
+                throw new Wicked_Exception($nameClause);
+            }
+
+            $where = '(' . $nameClause . ') OR (' . $textClause . ')';
+        } else {
+            $where = $textClause;
+        }
+
+        return $this->_retrieve($this->_params['table'], $where);
+    }
+
+    function getBackLinks($pagename)
+    {
+        $where = 'page_text LIKE ' . $this->_db->quoteString('%' . $this->_convertToDriver($pagename) . '%');
+        $pages = $this->_retrieve($this->_params['table'], $where);
+
+        /* We've cast a wide net, so now we filter out pages which don't
+         * actually refer to $pagename. */
+        $patterns = array('/\(\(' . preg_quote($pagename, '/') . '(?:\|[^)]+)?\)\)/');
+        if (preg_match('/^' . Wicked::REGEXP_WIKIWORD . '$/', $pagename)) {
+            $patterns[] = '/\b' . preg_quote($pagename, '/') . '\b/';
+        }
+
+        foreach ($pages as $key => $page) {
+            $match = false;
+            foreach ($patterns as $pattern) {
+                if (preg_match($pattern, $page['page_text'])) {
+                    $match = true;
+                }
+            }
+            if (!$match) {
+                unset($pages[$key]);
+            }
+        }
+
+        return $pages;
+    }
+
+    function getMatchingPages($searchtext, $matchType = Wicked_Page::MATCH_ANY)
+    {
+        $searchtext = Horde_String::lower($searchtext);
+
+        /* Short circuit the simple case. */
+        if ($matchType == Wicked_Page::MATCH_ANY) {
+            return $this->_retrieve($this->_params['table'],
+                                    'LOWER(page_name) LIKE ' . $this->_db->quoteString('%' . $searchtext . '%'));
+        }
+
+        $clauses = array();
+        if ($matchType & Wicked_Page::MATCH_LEFT) {
+            $clauses[] = 'LOWER(page_name) LIKE ' . $this->_db->quoteString($searchtext . '%');
+        }
+        if ($matchType & Wicked_Page::MATCH_RIGHT) {
+            $clauses[] = 'LOWER(page_name) LIKE ' . $this->_db->quoteString('%' . $searchtext);
+        }
+
+        if (!$clauses) {
+            return array();
+        }
+
+        return $this->_retrieve($this->_params['table'], implode(' OR ', $clauses));
+    }
+
+    function getLikePages($pagename)
+    {
+        if (Horde_String::isUpper($pagename, 'UTF-8')) {
+            $firstword = $pagename;
+            $lastword = null;
+        } else {
+            /* Get the first and last word of the page name. */
+            $count = preg_match_all('/[A-Z][a-z]*/', $pagename, $matches);
+            if (!$count) {
+                return array();
+            }
+            $matches = $matches[0];
+
+            $firstword = $matches[0];
+            $lastword = $matches[$count - 1];
+
+            if (strlen($firstword) == 1 && strlen($matches[1]) == 1) {
+                for ($i = 1; $i < $count; $i++) {
+                    $firstword .= $matches[$i];
+                    if (isset($matches[$i + 1]) && strlen($matches[$i + 1]) > 1) {
+                        break;
+                    }
+                }
+            }
+
+            if (strlen($lastword) == 1 && strlen($matches[$count - 2]) == 1) {
+                for ($i = $count - 2; $i > 0; $i--) {
+                    $lastword = $matches[$i] . $lastword;
+                    if (isset($matches[$i - 1]) && strlen($matches[$i - 1]) > 1) {
+                        break;
+                    }
+                }
+            }
+        }
+
+        require_once 'Horde/SQL.php';
+
+        $where = Horde_SQL::buildClause($this->_db, 'page_name', 'LIKE', $firstword);
+        if (!empty($lastword) && $lastword != $firstword) {
+            $where .= ' OR ' . Horde_SQL::buildClause($this->_db, 'page_name', 'LIKE', $lastword);
+        }
+
+        return $this->_retrieve($this->_params['table'], $where);
+    }
+
+    /**
+     * Retrieves data on files attached to a page.
+     *
+     * @param string $pageId        This is the Id of the page for which we'd
+     *                              like to find attached files.
+     * @param boolean $allversions  Whether to include all versions. If false
+     *                              or omitted, only the most recent version
+     *                              of each attachment is returned.
+     * @return array  An array of key/value arrays describing the attached
+     *                files.
+     * @throws Wicked_Exception
+     */
+    function getAttachedFiles($pageId, $allversions = false)
+    {
+        $where = 'page_id = ' . (int)$pageId;
+        $data = $this->_retrieve($this->_params['attachmenttable'], $where);
+
+        if ($allversions) {
+            $more_data = $this->_retrieve($this->_params['attachmenthistorytable'], $where);
+            $data = array_merge($data, $more_data);
+        }
+
+        foreach (array_keys($data) as $key) {
+            $data[$key]['attachment_name'] = $this->_convertFromDriver($data[$key]['attachment_name']);
+        }
+
+        usort($data, array($this, '_getAttachedFiles_usort'));
+        return $data;
+    }
+
+    function _getAttachedFiles_usort($a, $b)
+    {
+        $res = strcmp($a['attachment_name'], $b['attachment_name']);
+        if ($res != 0) {
+            return $res;
+        }
+        $res = ($a['attachment_majorversion'] - $b['attachment_minorversion']);
+        if ($res != 0) {
+            return $res;
+        }
+
+        return ($a['attachment_minorversion'] - $b['attachment_minorversion']);
+    }
+
+    /**
+     * Remove a single version or all versions of an attachment from
+     * $pageId. Calls parent::removeAttachment() to delete files from
+     * VFS.
+     *
+     * @param integer $pageId  The Id of the page the file is attached to.
+     * @param string $attachment  The name of the file.
+     * @param string $version  If specified, the version to delete. If null,
+     *                         then all versions of $attachment will be removed.
+     *
+     * @throws Wicked_Exception
+     */
+    function removeAttachment($pageId, $attachment, $version = null)
+    {
+        /* Try to delete from the VFS first. */
+        parent::removeAttachment($pageId, $attachment, $version);
+
+        /* First try against the current attachments table. */
+        $sql = 'DELETE FROM ' . $this->_params['attachmenttable'] .
+            ' WHERE page_id = ? AND attachment_name = ?';
+        $params = array($pageId, $attachment);
+        if (!is_null($version)) {
+            list($major, $minor) = explode('.', $version);
+            $sql .= ' AND attachment_majorversion = ? AND attachment_minorversion = ?';
+            $params[] = (int)$major;
+            $params[] = (int)$minor;
+        }
+
+        Horde::logMessage('Wicked_Driver_sql::removeAttachment: ' . $sql, 'DEBUG');
+
+        $result = $this->_db->delete($sql, $params);
+
+        /* Now try against the attachment history table. $params is
+         * unchanged. */
+        $sql = 'DELETE FROM ' . $this->_params['attachmenthistorytable'] .
+            ' WHERE page_id = ? AND attachment_name = ?';
+        if (!is_null($version)) {
+            $sql .= ' AND attachment_majorversion = ? AND attachment_minorversion = ?';
+        }
+
+        Horde::logMessage('Wicked_Driver_sql::removeAttachment: ' . $sql, 'DEBUG');
+
+        $this->_db->delete($sql, $params);
+    }
+
+    /**
+     * Removes all attachments from $pageId. Calls
+     * parent::removeAllAttachments() to delete files from VFS.
+     *
+     * @param integer $pageId  The Id of the page to remove attachments from.
+     *
+     * @throws Wicked_Exception
+     */
+    function removeAllAttachments($pageId)
+    {
+        /* Try to delete from the VFS first. */
+        $result = parent::removeAllAttachments($pageId);
+
+        /* First try against the current attachments table. */
+        $sql = 'DELETE FROM ' . $this->_params['attachmenttable'] .
+            ' WHERE page_id = ?';
+        $params = array($pageId);
+
+        Horde::logMessage('Wicked_Driver_sql::removeAllAttachments: ' . $sql, 'DEBUG');
+
+        $result = $this->_db->delete($sql, $params);
+
+        /* Now try against the attachment history table. $params is
+         * unchanged. */
+        $sql = 'DELETE FROM ' . $this->_params['attachmenthistorytable'] .
+            ' WHERE page_id = ?';
+
+        Horde::logMessage('Wicked_Driver_sql::removeAllAttachments: ' . $sql, 'DEBUG');
+
+        $this->_db->delete($sql, $params);
+    }
+
+    /**
+     * Handles the driver-specific portion of attaching a file.
+     *
+     * Wicked_Driver::attachFile() calls down to this method for the driver-
+     * specific portion, and then uses VFS to store the attachment.
+     *
+     * @access protected
+     *
+     * @param array $file  See Wicked_Driver::attachFile().
+     *
+     * @return string  The new version of the file attached.
+     * @throws Wicked_Exception
+     */
+    function _attachFile($file)
+    {
+        $where = 'page_id = ' . intval($file['page_id']) .
+                 ' AND attachment_name = ' . $this->_db->quoteString($file['attachment_name']);
+        $attachments = $this->_retrieve($this->_params['attachmenttable'], $where);
+
+        if ($file['change_author'] === false) {
+            $file['change_author'] = null;
+        }
+
+        if ($attachments) {
+            list($old) = $attachments;
+            $majorversion = $old['attachment_majorversion'];
+            $minorversion = $old['attachment_minorversion'];
+            if ($file['minor']) {
+                $minorversion++;
+            } else {
+                $majorversion++;
+                $minorversion = 0;
+            }
+
+            $sql = sprintf('INSERT INTO %s (page_id, attachment_name, attachment_majorversion, attachment_minorversion, attachment_created, change_author, change_log) SELECT page_id, attachment_name, attachment_majorversion, attachment_minorversion, attachment_created, change_author, change_log FROM %s WHERE page_id = %s AND attachment_name = %s',
+                           $this->_params['attachmenthistorytable'],
+                           $this->_params['attachmenttable'],
+                           intval($file['page_id']),
+                           $this->_db->quoteString($file['attachment_name']));
+            $this->_db->insert($sql);
+
+            $sql = sprintf('UPDATE %s SET attachment_majorversion = %s, attachment_minorversion = %s, change_log = %s, change_author = %s, attachment_created = %s WHERE page_id = %d AND attachment_name = %s',
+                           $this->_params['attachmenttable'],
+                           intval($majorversion),
+                           intval($minorversion),
+                           $this->_db->quoteString($this->_convertToDriver($file['change_log'])),
+                           $this->_db->quoteString($this->_convertToDriver($file['change_author'])),
+                           intval(time()),
+                           intval($file['page_id']),
+                           $this->_db->quoteString($this->_convertToDriver($file['attachment_name'])));
+            $this->_db->update($sql);
+        } else {
+            $majorversion = 1;
+            $minorversion = 0;
+            $sql = sprintf('INSERT INTO %s (page_id, attachment_majorversion, attachment_minorversion, change_log, change_author, attachment_created, attachment_name) VALUES (%d, 1, 0, %s, %s, %s, %s)',
+                           $this->_params['attachmenttable'],
+                           intval($file['page_id']),
+                           $this->_db->quoteString($this->_convertToDriver($file['change_log'])),
+                           $this->_db->quoteString($this->_convertToDriver($file['change_author'])),
+                           intval(time()),
+                           $this->_db->quoteString($this->_convertToDriver($file['attachment_name'])));
+            $this->_db->insert($sql);
+        }
+
+        return (int)$majorversion . '.' . (int)$minorversion;
+    }
+
+    /**
+     * Log a hit to $pagename.
+     *
+     * @param string $pagename  The page that was viewed.
+     *
+     * @throws Wicked_Exception
+     */
+    function logPageView($pagename)
+    {
+        $query = 'UPDATE ' . $this->_params['table'] .
+                 ' SET page_hits = page_hits + 1 WHERE page_name = ?';
+        $values = array($this->_convertToDriver($pagename));
+
+        Horde::logMessage('Wicked_Driver_sql::logPageView(' . $pagename . '): ' . $query, 'DEBUG');
+
+        return $this->_db->update($query, $values);
+    }
+
+    /**
+     * Creates a new page.
+     *
+     * @param string $pagename  The new page's name.
+     * @param string $text      The new page's text.
+     *
+     * @throws Wicked_Exception
+     */
+    function newPage($pagename, $text)
+    {
+        if (!strlen($pagename)) {
+            throw new Wicked_Exception(_("Page name must not be empty"));
+        }
+
+        if ($GLOBALS['browser']->isRobot()) {
+            throw new Wicked_Exception(_("Robots are not allowed to create pages"));
+        }
+
+        $author = $GLOBALS['registry']->getAuth();
+        if ($author === false) {
+            $author = null;
+        }
+
+        $query = 'INSERT INTO ' . $this->_params['table'] . ' ' .
+                 '(page_name, page_text, ' .
+                 'version_created, page_majorversion, ' .
+                 'page_minorversion, page_hits, change_author) ' .
+                 'VALUES (?, ?, ?, 1, 0, 0, ?)';
+        $values = array(
+            $this->_convertToDriver($pagename),
+            $this->_convertToDriver($text),
+            time(),
+            $author,
+        );
+
+        Horde::logMessage('Wicked_Driver_sql::newPage(): ' . $query, 'DEBUG');
+
+        /* Attempt the insertion/update query. */
+        $page_id = $this->_db->insert($query, $values);
+
+        /* Send notification. */
+        $url = Wicked::url($pagename, true, -1);
+        Wicked::mail("Created page: $url\n\n$text\n", array(
+            'Subject' => '[' . $GLOBALS['registry']->get('name') .
+                '] created: ' . $pagename));
+
+        /* Call getPages with no caching so that the new list of pages is
+         * read in. */
+        $this->getPages(true, true);
+        return $page_id;
+    }
+
+    /**
+     * Rename a page (and keep the page's history).
+     *
+     * @param string $pagename  The name of the page to rename.
+     * @param string $newname   The page's new name.
+     *
+     * @throws Wicked_Exception
+     */
+    function renamePage($pagename, $newname)
+    {
+        $query = 'UPDATE ' . $this->_params['table'] .
+                 ' SET page_name = ? WHERE page_name = ?';
+        $values = array($this->_convertToDriver($newname), $this->_convertToDriver($pagename));
+
+        Horde::logMessage('Wicked_Driver_sql::renamePage(): ' . $query, 'DEBUG');
+
+        $this->_db->update($query, $values);
+
+        $query = 'UPDATE ' . $this->_params['historytable'] .
+                 ' SET page_name = ? WHERE page_name = ?';
+        $values = array($this->_convertToDriver($newname), $this->_convertToDriver($pagename));
+
+        Horde::logMessage('Wicked_Driver_sql::renamePage(): ' . $query, 'DEBUG');
+
+        $this->_db->update($query, $values);
+
+        $changelog = sprintf(_("Renamed page from %s"), $pagename);
+        $newPage = $this->retrieveByName($newname);
+
+        /* Call getPages with no caching so that the new list of pages is
+         * read in. */
+        $this->getPages(true, true);
+        return $this->updateText($newname, $newPage['page_text'], $changelog, true);
+    }
+
+    function updateText($pagename, $text, $changelog, $minorchange)
+    {
+        if (!$this->pageExists($pagename)) {
+            return $this->newPage($pagename, $text);
+        }
+
+        /* Copy the old version into the page history. */
+        $query = sprintf(
+            'INSERT INTO %s (page_id, page_name, page_text, page_majorversion, page_minorversion, version_created, change_author, change_log)' .
+            ' SELECT page_id, page_name, page_text, page_majorversion, page_minorversion, version_created, change_author, change_log FROM %s WHERE page_name = ?',
+            $this->_params['historytable'],
+            $this->_params['table']);
+        $values = array($this->_convertToDriver($pagename));
+
+        Horde::logMessage('Page ' . $pagename . ' saved with user agent ' . $GLOBALS['browser']->getAgentString(), 'DEBUG');
+        Horde::logMessage('Wicked_Driver_sql::updateText(): ' . $query, 'DEBUG');
+
+        try {
+            $this->_db->insert($query, $values);
+        } catch (Horde_Db_Exception $e) {
+            Horde::logMessage($e->getMessage(), 'ERR');
+            throw new Wicked_Exception($e);
+        }
+
+        /* Now move on to updating the record. */
+        if ($minorchange) {
+            $versionchange = 'page_minorversion = page_minorversion + 1';
+        } else {
+            $versionchange = 'page_majorversion = page_majorversion + 1, page_minorversion = 0';
+        }
+
+        $author = $GLOBALS['registry']->getAuth();
+        if ($author === false) {
+            $author = null;
+        }
+
+        $query = 'UPDATE ' . $this->_params['table'] .
+                 ' SET change_author = ?, page_text = ?, change_log = ?, version_created = ?, ' . $versionchange .
+                 ' WHERE page_name = ?';
+        $values = array($author,
+                        $this->_convertToDriver($text),
+                        $this->_convertToDriver($changelog),
+                        time(),
+                        $this->_convertToDriver($pagename));
+
+        Horde::logMessage('Wicked_Driver_sql::updateText(): ' . $query, 'DEBUG');
+
+        $this->_db->update($query, $values);
+    }
+
+    function getPages($special = true, $no_cache = false)
+    {
+        static $pageNames;
+        if (!isset($pageNames) || $no_cache) {
+            $query = 'SELECT page_name FROM ' . $this->_params['table'];
+            Horde::logMessage('Wicked_Driver_sql::getPages(): ' . $query, 'DEBUG');
+            try {
+                $result = $this->_db->selectValues($query);
+            } catch (Horde_Db_Exception $e) {
+                throw new Wicked_Exception($e);
+            }
+            $pageNames = $this->_convertFromDriver($result);
+        }
+        if ($special) {
+            return $pageNames + $this->getSpecialPages();
+        }
+
+        return $pageNames;
+    }
+
+    /**
+     */
+    function removeVersion($pagename, $version)
+    {
+        list($major, $minor) = explode('.', $version);
+
+        /* We need to know if we're deleting the current version. */
+        $query = 'SELECT 1 FROM ' . $this->_params['table'] .
+                 ' WHERE page_name = ? AND page_majorversion = ? AND page_minorversion = ?';
+        $values = array($this->_convertToDriver($pagename), $major, $minor);
+
+        Horde::logMessage('Wicked_Driver_sql::removeVersion(): ' . $query, 'DEBUG');
+
+        try {
+            $result = $this->_db->selectValue($query, $values);
+        } catch (Horde_Db_Exception $e) {
+            $result = false;
+        }
+
+        if (!$result) {
+            /* Removing a historical revision - we can just slice it out of the
+             * history table. $values is unchanged. */
+            $query = 'DELETE FROM ' . $this->_params['historytable'] .
+                ' WHERE page_name = ? and page_majorversion = ? and page_minorversion = ?';
+            Horde::logMessage('Wicked_Driver_sql::removeVersion(): ' . $query, 'DEBUG');
+            $this->_db->delete($query, $values);
+            return;
+        }
+
+        /* We're deleting the current version. Have to promote the
+         * next-most revision from the history table. */
+        $query = 'SELECT * FROM ' . $this->_params['historytable'] .
+                 ' WHERE page_name = ? ORDER BY page_majorversion DESC, page_minorversion DESC';
+        $query = $this->_db->addLimitOffset($query, array('limit' => 1));
+
+        Horde::logMessage('Wicked_Driver_sql::removeVersion(): ' . $query, 'DEBUG');
+
+        $revision = $this->_db->selectOne($query, array($this->_convertToDriver($pagename)), DB_FETCHMODE_ASSOC);
+
+        /* Replace the current version of the page with the
+         * version being promoted. */
+        $query = 'UPDATE ' . $this->_params['table'] . ' SET' .
+            ' page_text = ?, page_majorversion = ?, page_minorversion = ?,' .
+            ' version_created = ?, change_author = ?, change_log = ?' .
+            ' WHERE page_name = ?';
+        $values = array($revision['page_text'],
+                        $revision['page_majorversion'],
+                        $revision['page_minorversion'],
+                        $revision['version_created'],
+                        $revision['change_author'],
+                        $revision['change_log'],
+                        $this->_convertToDriver($pagename));
+
+        Horde::logMessage('Wicked_Driver_sql::removeVersion(): ' . $query, 'DEBUG');
+        $this->_db->update($query, $values);
+
+        /* Finally, remove the version that we promoted from the
+         * history table. */
+        $query = 'DELETE FROM ' . $this->_params['historytable'] .
+            ' WHERE page_name = ? and page_majorversion = ? and page_minorversion = ?';
+        $values = array($this->_convertToDriver($pagename), $revision['page_majorversion'], $revision['page_minorversion']);
+
+        Horde::logMessage('Wicked_Driver_sql::removeVersion(): ' . $query, 'DEBUG');
+
+        $this->_db->delete($query, $values);
+    }
+
+    /**
+     */
+    function removeAllVersions($pagename)
+    {
+        $this->_pageNames = null;
+
+        $query = 'DELETE FROM ' . $this->_params['table'] .
+                 ' WHERE page_name = ?';
+        $values = array($this->_convertToDriver($pagename));
+
+        Horde::logMessage('Wicked_Driver_sql::removeAllVersions(): ' . $query, 'DEBUG');
+
+        $this->_db->delete($query, $values);
+
+        $query = 'DELETE FROM ' . $this->_params['historytable'] .
+                 ' WHERE page_name = ?';
+        $values = array($this->_convertToDriver($pagename));
+
+        Horde::logMessage('Wicked_Driver_sql::removeAllVersions(): ' . $query, 'DEBUG');
+
+        $this->_db->delete($query, $values);
+
+        /* Remove attachments and do other cleanup. */
+        return parent::removeAllVersions($pagename);
+    }
+
+    /**
+     * Retrieves a page or set of pages given an SQL WHERE clause.
+     *
+     * @access private
+     *
+     * @param string $table     Which table are we retrieving pages from?
+     * @param string $sqlWhere  Where clause for sql statement (without the
+     *                          'WHERE').
+     * @param string $orderBy   What column should we order results by?
+     * @param integer $limit    Maximum number of pages to fetch.
+     *
+     * @return array | object  Either an array of pages or PEAR::Error.
+     */
+    function _retrieve($table, $sqlWhere, $orderBy = null, $limit = null)
+    {
+        $query = sprintf('SELECT * FROM %s%s%s',
+                         $table,
+                         !empty($sqlWhere) ? ' WHERE ' . $sqlWhere : '',
+                         !empty($orderBy) ? ' ORDER BY ' . $orderBy : '');
+        if (!empty($limit)) {
+            $query = $this->_db->addLimitOffset($query, array('limit' => $limit));
+        }
+
+        Horde::logMessage('Wicked_Driver_sql::_retrieve(): ' . $query, 'DEBUG');
+        try {
+            $result = $this->_db->selectAll($query);
+        } catch (Horde_Db_Exception $e) {
+            Horde::logMessage($e);
+            throw new Wicked_Exception($e);
+        }
+
+        $pages = array();
+        $index = 0;
+        foreach ($result as $row) {
+            if (isset($row['page_name'])) {
+                $row['page_name'] = $this->_convertFromDriver($row['page_name']);
+            }
+            if (isset($row['page_text'])) {
+                $row['page_text'] = $this->_convertFromDriver($row['page_text']);
+            }
+            if (isset($row['change_log'])) {
+                $row['change_log'] = $this->_convertFromDriver($row['change_log']);
+            }
+
+            $pages[] = $row;
+        }
+
+        return $pages;
+    }
+
+    /**
+     * Returns the charset used by the backend.
+     *
+     * @return string  The backend's charset
+     */
+    function getCharset()
+    {
+        return $this->_params['charset'];
+    }
+
+    /**
+     * Converts a value from the driver's charset to the default charset.
+     *
+     * @param mixed $value  A value to convert.
+     *
+     * @return mixed        The converted value.
+     */
+    function _convertFromDriver($value)
+    {
+        return Horde_String::convertCharset($value, $this->getCharset(), 'UTF-8');
+    }
+
+    /**
+     * Converts a value from the default charset to the driver's charset.
+     *
+     * @param mixed $value  A value to convert.
+     *
+     * @return mixed        The converted value.
+     */
+    function _convertToDriver($value)
+    {
+        return Horde_String::convertCharset($value, 'UTF-8', $this->getCharset());
+    }
+
+    /**
+     * Attempts to open a persistent connection to the SQL server.
+     *
+     * @throws Wicked_Exception
+     */
+    function connect()
+    {
+        try {
+            $this->_db = $GLOBALS['injector']->getInstance('Horde_Db_Adapter');
+        } catch (Horde_Exception $e) {
+            throw new Wicked_Exception($e);
+        }
+
+        $this->_params = array_merge(array(
+            'table' => 'wicked_pages',
+            'historytable' => 'wicked_history',
+            'attachmenttable' => 'wicked_attachments',
+            'attachmenthistorytable' => 'wicked_attachment_history'
+        ), $this->_params);
+
+        return true;
+    }
+
+}
diff --git a/wicked/lib/Driver/sql.php b/wicked/lib/Driver/sql.php
deleted file mode 100644 (file)
index 8878b00..0000000
+++ /dev/null
@@ -1,855 +0,0 @@
-<?php
-/**
- * @package Wicked
- */
-
-/**
- * Wicked storage implementation for PHP's PEAR database abstraction
- * layer.
- *
- * The table structure can be created by the scripts/drivers/wicked_foo.sql
- * script.
- *
- * @author  Tyler Colbert <tyler@colberts.us>
- * @author  Chuck Hagenbuch <chuck@horde.org>
- * @package Wicked
- */
-class Wicked_Driver_sql extends Wicked_Driver {
-
-    /**
-     * Handle for the current database connection.
-     *
-     * @var Horde_Db_Adapter
-     */
-    var $_db;
-
-    /**
-     * Constructs a new Wicked SQL driver object.
-     *
-     * @param array $params  A hash containing connection parameters.
-     */
-    function Wicked_Driver_sql($params = array())
-    {
-        parent::Wicked_Driver($params);
-    }
-
-    /**
-     * Retrieves the page of a particular name from the database.
-     *
-     * @param string $pagename The name of the page to retrieve.
-     *
-     * @return array
-     * @throws Wicked_Exception
-     */
-    function retrieveByName($pagename)
-    {
-        $where = 'page_name = ' . $this->_db->quoteString($this->_convertToDriver($pagename));
-
-        $pages = $this->_retrieve($this->_params['table'], $where);
-
-        if (!empty($pages[0])) {
-            return $pages[0];
-        }
-
-        throw new Wicked_Exception($pagename . ' not found');
-    }
-
-    /**
-     * Retrieves a historic version of a page.
-     *
-     * @param string $pagename  The name of the page to retrieve.
-     * @param string $version   The version to retrieve.
-     *
-     * @return array  The page hash.
-     * @throws Wicked_Exception
-     */
-    function retrieveHistory($pagename, $version)
-    {
-        if (empty($version) or !preg_match('/^[0-9]+\.[0-9]+$/', $version)) {
-            throw new Wicked_Exception('invalid version number');
-        }
-
-        list($major, $minor) = explode('.', $version);
-        $where = sprintf('page_name = %s AND page_majorversion = %s AND ' .
-                         'page_minorversion = %s',
-                         $this->_db->quoteString($this->_convertToDriver($pagename)),
-                         (int)$major, (int)$minor);
-
-        return $this->_retrieve($this->_params['historytable'], $where);
-    }
-
-    function getPage($pagename)
-    {
-        $where = 'page_name = ' . $this->_db->quoteString($this->_convertToDriver($pagename));
-        return $this->_retrieve($this->_params['table'], $where);
-    }
-
-    function getPageById($id)
-    {
-        $where = 'page_id = ' . (int)$id;
-        return $this->_retrieve($this->_params['table'], $where);
-    }
-
-    function getAllPages()
-    {
-        return $this->_retrieve($this->_params['table'], '', 'page_name');
-    }
-
-    function getHistory($pagename)
-    {
-        $where = 'page_name = ' . $this->_db->quoteString($this->_convertToDriver($pagename)) .
-                 ' ORDER BY page_majorversion DESC, page_minorversion DESC';
-
-        return $this->_retrieve($this->_params['historytable'], $where);
-    }
-
-    /**
-     * Returns the most recently changed pages.
-     *
-     * @param integer $days  The number of days to look back.
-     *
-     * @return array  Pages.
-     * @throws Wicked_Exception
-     */
-    function getRecentChanges($days = 3)
-    {
-        $where = 'version_created > ' . (time() - (86400 * $days));
-        $result = $this->_retrieve($this->_params['table'],
-                                   $where,
-                                   'version_created DESC');
-        $result2 = $this->_retrieve($this->_params['historytable'],
-                                    $where,
-                                    'version_created DESC');
-        return array_merge($result, $result2);
-    }
-
-    /**
-     * Returns the most popular pages.
-     *
-     * @param integer $limit  The number of most popular pages to return.
-     *
-     * @return array  Pages.
-     * @throws Wicked_Exception
-     */
-    function mostPopular($limit = 10)
-    {
-        return $this->_retrieve($this->_params['table'], '', 'page_hits DESC', $limit);
-    }
-
-    /**
-     * Returns the least popular pages.
-     *
-     * @param integer $limit  The number of least popular pages to return.
-     *
-     * @return array  Pages.
-     * @throws Wicked_Exception
-     */
-    function leastPopular($limit = 10)
-    {
-        return $this->_retrieve($this->_params['table'], '', 'page_hits ASC', $limit);
-    }
-
-    function searchTitles($searchtext)
-    {
-        require_once 'Horde/SQL.php';
-        $searchtext = $this->_convertToDriver($searchtext);
-        $where = Horde_SQL::buildClause($this->_db, 'page_name', 'LIKE', $searchtext);
-        return $this->_retrieve($this->_params['table'], $where);
-    }
-
-    /**
-     * Finds pages with matches in text or title.
-     *
-     * @param string  $searchtext  The search expression (Google-like).
-     * @param boolean $title default true  If true, both page title and text
-     *                                     are searched.  If false, only page
-     *                                     text is searched.
-     *
-     * @return array  A list of pages.
-     * @throws Wicked_Exception
-     */
-    function searchText($searchtext, $title = true)
-    {
-        require_once 'Horde/SQL/Keywords.php';
-        $searchtext = $this->_convertToDriver($searchtext);
-
-        $textClause = Horde_SQL_Keywords::parse('page_text', $searchtext);
-        if (is_a($textClause, 'PEAR_Error')) {
-            throw new Wicked_Exception($textClause);
-        }
-
-        if ($title) {
-            $nameClause = Horde_SQL_Keywords::parse('page_name', $searchtext);
-            if (is_a($nameClause, 'PEAR_Error')) {
-                throw new Wicked_Exception($nameClause);
-            }
-
-            $where = '(' . $nameClause . ') OR (' . $textClause . ')';
-        } else {
-            $where = $textClause;
-        }
-
-        return $this->_retrieve($this->_params['table'], $where);
-    }
-
-    function getBackLinks($pagename)
-    {
-        $where = 'page_text LIKE ' . $this->_db->quoteString('%' . $this->_convertToDriver($pagename) . '%');
-        $pages = $this->_retrieve($this->_params['table'], $where);
-
-        /* We've cast a wide net, so now we filter out pages which don't
-         * actually refer to $pagename. */
-        $patterns = array('/\(\(' . preg_quote($pagename, '/') . '(?:\|[^)]+)?\)\)/');
-        if (preg_match('/^' . Wicked::REGEXP_WIKIWORD . '$/', $pagename)) {
-            $patterns[] = '/\b' . preg_quote($pagename, '/') . '\b/';
-        }
-
-        foreach ($pages as $key => $page) {
-            $match = false;
-            foreach ($patterns as $pattern) {
-                if (preg_match($pattern, $page['page_text'])) {
-                    $match = true;
-                }
-            }
-            if (!$match) {
-                unset($pages[$key]);
-            }
-        }
-
-        return $pages;
-    }
-
-    function getMatchingPages($searchtext, $matchType = WICKED_PAGE_MATCH_ANY)
-    {
-        $searchtext = Horde_String::lower($searchtext);
-
-        /* Short circuit the simple case. */
-        if ($matchType == WICKED_PAGE_MATCH_ANY) {
-            return $this->_retrieve($this->_params['table'],
-                                    'LOWER(page_name) LIKE ' . $this->_db->quoteString('%' . $searchtext . '%'));
-        }
-
-        $clauses = array();
-        if ($matchType & WICKED_PAGE_MATCH_LEFT) {
-            $clauses[] = 'LOWER(page_name) LIKE ' . $this->_db->quoteString($searchtext . '%');
-        }
-        if ($matchType & WICKED_PAGE_MATCH_RIGHT) {
-            $clauses[] = 'LOWER(page_name) LIKE ' . $this->_db->quoteString('%' . $searchtext);
-        }
-
-        if (!$clauses) {
-            return array();
-        }
-
-        return $this->_retrieve($this->_params['table'], implode(' OR ', $clauses));
-    }
-
-    function getLikePages($pagename)
-    {
-        if (Horde_String::isUpper($pagename, 'UTF-8')) {
-            $firstword = $pagename;
-            $lastword = null;
-        } else {
-            /* Get the first and last word of the page name. */
-            $count = preg_match_all('/[A-Z][a-z]*/', $pagename, $matches);
-            if (!$count) {
-                return array();
-            }
-            $matches = $matches[0];
-
-            $firstword = $matches[0];
-            $lastword = $matches[$count - 1];
-
-            if (strlen($firstword) == 1 && strlen($matches[1]) == 1) {
-                for ($i = 1; $i < $count; $i++) {
-                    $firstword .= $matches[$i];
-                    if (isset($matches[$i + 1]) && strlen($matches[$i + 1]) > 1) {
-                        break;
-                    }
-                }
-            }
-
-            if (strlen($lastword) == 1 && strlen($matches[$count - 2]) == 1) {
-                for ($i = $count - 2; $i > 0; $i--) {
-                    $lastword = $matches[$i] . $lastword;
-                    if (isset($matches[$i - 1]) && strlen($matches[$i - 1]) > 1) {
-                        break;
-                    }
-                }
-            }
-        }
-
-        require_once 'Horde/SQL.php';
-
-        $where = Horde_SQL::buildClause($this->_db, 'page_name', 'LIKE', $firstword);
-        if (!empty($lastword) && $lastword != $firstword) {
-            $where .= ' OR ' . Horde_SQL::buildClause($this->_db, 'page_name', 'LIKE', $lastword);
-        }
-
-        return $this->_retrieve($this->_params['table'], $where);
-    }
-
-    /**
-     * Retrieves data on files attached to a page.
-     *
-     * @param string $pageId        This is the Id of the page for which we'd
-     *                              like to find attached files.
-     * @param boolean $allversions  Whether to include all versions. If false
-     *                              or omitted, only the most recent version
-     *                              of each attachment is returned.
-     * @return array  An array of key/value arrays describing the attached
-     *                files.
-     * @throws Wicked_Exception
-     */
-    function getAttachedFiles($pageId, $allversions = false)
-    {
-        $where = 'page_id = ' . (int)$pageId;
-        $data = $this->_retrieve($this->_params['attachmenttable'], $where);
-
-        if ($allversions) {
-            $more_data = $this->_retrieve($this->_params['attachmenthistorytable'], $where);
-            $data = array_merge($data, $more_data);
-        }
-
-        foreach (array_keys($data) as $key) {
-            $data[$key]['attachment_name'] = $this->_convertFromDriver($data[$key]['attachment_name']);
-        }
-
-        usort($data, array($this, '_getAttachedFiles_usort'));
-        return $data;
-    }
-
-    function _getAttachedFiles_usort($a, $b)
-    {
-        $res = strcmp($a['attachment_name'], $b['attachment_name']);
-        if ($res != 0) {
-            return $res;
-        }
-        $res = ($a['attachment_majorversion'] - $b['attachment_minorversion']);
-        if ($res != 0) {
-            return $res;
-        }
-
-        return ($a['attachment_minorversion'] - $b['attachment_minorversion']);
-    }
-
-    /**
-     * Remove a single version or all versions of an attachment from
-     * $pageId. Calls parent::removeAttachment() to delete files from
-     * VFS.
-     *
-     * @param integer $pageId  The Id of the page the file is attached to.
-     * @param string $attachment  The name of the file.
-     * @param string $version  If specified, the version to delete. If null,
-     *                         then all versions of $attachment will be removed.
-     *
-     * @throws Wicked_Exception
-     */
-    function removeAttachment($pageId, $attachment, $version = null)
-    {
-        /* Try to delete from the VFS first. */
-        parent::removeAttachment($pageId, $attachment, $version);
-
-        /* First try against the current attachments table. */
-        $sql = 'DELETE FROM ' . $this->_params['attachmenttable'] .
-            ' WHERE page_id = ? AND attachment_name = ?';
-        $params = array($pageId, $attachment);
-        if (!is_null($version)) {
-            list($major, $minor) = explode('.', $version);
-            $sql .= ' AND attachment_majorversion = ? AND attachment_minorversion = ?';
-            $params[] = (int)$major;
-            $params[] = (int)$minor;
-        }
-
-        Horde::logMessage('Wicked_Driver_sql::removeAttachment: ' . $sql, 'DEBUG');
-
-        $result = $this->_db->delete($sql, $params);
-
-        /* Now try against the attachment history table. $params is
-         * unchanged. */
-        $sql = 'DELETE FROM ' . $this->_params['attachmenthistorytable'] .
-            ' WHERE page_id = ? AND attachment_name = ?';
-        if (!is_null($version)) {
-            $sql .= ' AND attachment_majorversion = ? AND attachment_minorversion = ?';
-        }
-
-        Horde::logMessage('Wicked_Driver_sql::removeAttachment: ' . $sql, 'DEBUG');
-
-        $this->_db->delete($sql, $params);
-    }
-
-    /**
-     * Removes all attachments from $pageId. Calls
-     * parent::removeAllAttachments() to delete files from VFS.
-     *
-     * @param integer $pageId  The Id of the page to remove attachments from.
-     *
-     * @throws Wicked_Exception
-     */
-    function removeAllAttachments($pageId)
-    {
-        /* Try to delete from the VFS first. */
-        $result = parent::removeAllAttachments($pageId);
-
-        /* First try against the current attachments table. */
-        $sql = 'DELETE FROM ' . $this->_params['attachmenttable'] .
-            ' WHERE page_id = ?';
-        $params = array($pageId);
-
-        Horde::logMessage('Wicked_Driver_sql::removeAllAttachments: ' . $sql, 'DEBUG');
-
-        $result = $this->_db->delete($sql, $params);
-
-        /* Now try against the attachment history table. $params is
-         * unchanged. */
-        $sql = 'DELETE FROM ' . $this->_params['attachmenthistorytable'] .
-            ' WHERE page_id = ?';
-
-        Horde::logMessage('Wicked_Driver_sql::removeAllAttachments: ' . $sql, 'DEBUG');
-
-        $this->_db->delete($sql, $params);
-    }
-
-    /**
-     * Handles the driver-specific portion of attaching a file.
-     *
-     * Wicked_Driver::attachFile() calls down to this method for the driver-
-     * specific portion, and then uses VFS to store the attachment.
-     *
-     * @access protected
-     *
-     * @param array $file  See Wicked_Driver::attachFile().
-     *
-     * @return string  The new version of the file attached.
-     * @throws Wicked_Exception
-     */
-    function _attachFile($file)
-    {
-        $where = 'page_id = ' . intval($file['page_id']) .
-                 ' AND attachment_name = ' . $this->_db->quoteString($file['attachment_name']);
-        $attachments = $this->_retrieve($this->_params['attachmenttable'], $where);
-
-        if ($file['change_author'] === false) {
-            $file['change_author'] = null;
-        }
-
-        if ($attachments) {
-            list($old) = $attachments;
-            $majorversion = $old['attachment_majorversion'];
-            $minorversion = $old['attachment_minorversion'];
-            if ($file['minor']) {
-                $minorversion++;
-            } else {
-                $majorversion++;
-                $minorversion = 0;
-            }
-
-            $sql = sprintf('INSERT INTO %s (page_id, attachment_name, attachment_majorversion, attachment_minorversion, attachment_created, change_author, change_log) SELECT page_id, attachment_name, attachment_majorversion, attachment_minorversion, attachment_created, change_author, change_log FROM %s WHERE page_id = %s AND attachment_name = %s',
-                           $this->_params['attachmenthistorytable'],
-                           $this->_params['attachmenttable'],
-                           intval($file['page_id']),
-                           $this->_db->quoteString($file['attachment_name']));
-            $this->_db->insert($sql);
-
-            $sql = sprintf('UPDATE %s SET attachment_majorversion = %s, attachment_minorversion = %s, change_log = %s, change_author = %s, attachment_created = %s WHERE page_id = %d AND attachment_name = %s',
-                           $this->_params['attachmenttable'],
-                           intval($majorversion),
-                           intval($minorversion),
-                           $this->_db->quoteString($this->_convertToDriver($file['change_log'])),
-                           $this->_db->quoteString($this->_convertToDriver($file['change_author'])),
-                           intval(time()),
-                           intval($file['page_id']),
-                           $this->_db->quoteString($this->_convertToDriver($file['attachment_name'])));
-            $this->_db->update($sql);
-        } else {
-            $majorversion = 1;
-            $minorversion = 0;
-            $sql = sprintf('INSERT INTO %s (page_id, attachment_majorversion, attachment_minorversion, change_log, change_author, attachment_created, attachment_name) VALUES (%d, 1, 0, %s, %s, %s, %s)',
-                           $this->_params['attachmenttable'],
-                           intval($file['page_id']),
-                           $this->_db->quoteString($this->_convertToDriver($file['change_log'])),
-                           $this->_db->quoteString($this->_convertToDriver($file['change_author'])),
-                           intval(time()),
-                           $this->_db->quoteString($this->_convertToDriver($file['attachment_name'])));
-            $this->_db->insert($sql);
-        }
-
-        return (int)$majorversion . '.' . (int)$minorversion;
-    }
-
-    /**
-     * Log a hit to $pagename.
-     *
-     * @param string $pagename  The page that was viewed.
-     *
-     * @throws Wicked_Exception
-     */
-    function logPageView($pagename)
-    {
-        $query = 'UPDATE ' . $this->_params['table'] .
-                 ' SET page_hits = page_hits + 1 WHERE page_name = ?';
-        $values = array($this->_convertToDriver($pagename));
-
-        Horde::logMessage('Wicked_Driver_sql::logPageView(' . $pagename . '): ' . $query, 'DEBUG');
-
-        return $this->_db->update($query, $values);
-    }
-
-    /**
-     * Creates a new page.
-     *
-     * @param string $pagename  The new page's name.
-     * @param string $text      The new page's text.
-     *
-     * @throws Wicked_Exception
-     */
-    function newPage($pagename, $text)
-    {
-        if (!strlen($pagename)) {
-            throw new Wicked_Exception(_("Page name must not be empty"));
-        }
-
-        if ($GLOBALS['browser']->isRobot()) {
-            throw new Wicked_Exception(_("Robots are not allowed to create pages"));
-        }
-
-        $author = $GLOBALS['registry']->getAuth();
-        if ($author === false) {
-            $author = null;
-        }
-
-        $query = 'INSERT INTO ' . $this->_params['table'] . ' ' .
-                 '(page_name, page_text, ' .
-                 'version_created, page_majorversion, ' .
-                 'page_minorversion, page_hits, change_author) ' .
-                 'VALUES (?, ?, ?, 1, 0, 0, ?)';
-        $values = array(
-            $this->_convertToDriver($pagename),
-            $this->_convertToDriver($text),
-            time(),
-            $author,
-        );
-
-        Horde::logMessage('Wicked_Driver_sql::newPage(): ' . $query, 'DEBUG');
-
-        /* Attempt the insertion/update query. */
-        $page_id = $this->_db->insert($query, $values);
-
-        /* Send notification. */
-        $url = Wicked::url($pagename, true, -1);
-        Wicked::mail("Created page: $url\n\n$text\n", array(
-            'Subject' => '[' . $GLOBALS['registry']->get('name') .
-                '] created: ' . $pagename));
-
-        /* Call getPages with no caching so that the new list of pages is
-         * read in. */
-        $this->getPages(true, true);
-        return $page_id;
-    }
-
-    /**
-     * Rename a page (and keep the page's history).
-     *
-     * @param string $pagename  The name of the page to rename.
-     * @param string $newname   The page's new name.
-     *
-     * @throws Wicked_Exception
-     */
-    function renamePage($pagename, $newname)
-    {
-        $query = 'UPDATE ' . $this->_params['table'] .
-                 ' SET page_name = ? WHERE page_name = ?';
-        $values = array($this->_convertToDriver($newname), $this->_convertToDriver($pagename));
-
-        Horde::logMessage('Wicked_Driver_sql::renamePage(): ' . $query, 'DEBUG');
-
-        $this->_db->update($query, $values);
-
-        $query = 'UPDATE ' . $this->_params['historytable'] .
-                 ' SET page_name = ? WHERE page_name = ?';
-        $values = array($this->_convertToDriver($newname), $this->_convertToDriver($pagename));
-
-        Horde::logMessage('Wicked_Driver_sql::renamePage(): ' . $query, 'DEBUG');
-
-        $this->_db->update($query, $values);
-
-        $changelog = sprintf(_("Renamed page from %s"), $pagename);
-        $newPage = $this->retrieveByName($newname);
-
-        /* Call getPages with no caching so that the new list of pages is
-         * read in. */
-        $this->getPages(true, true);
-        return $this->updateText($newname, $newPage['page_text'], $changelog, true);
-    }
-
-    function updateText($pagename, $text, $changelog, $minorchange)
-    {
-        if (!$this->pageExists($pagename)) {
-            return $this->newPage($pagename, $text);
-        }
-
-        /* Copy the old version into the page history. */
-        $query = sprintf(
-            'INSERT INTO %s (page_id, page_name, page_text, page_majorversion, page_minorversion, version_created, change_author, change_log)' .
-            ' SELECT page_id, page_name, page_text, page_majorversion, page_minorversion, version_created, change_author, change_log FROM %s WHERE page_name = ?',
-            $this->_params['historytable'],
-            $this->_params['table']);
-        $values = array($this->_convertToDriver($pagename));
-
-        Horde::logMessage('Page ' . $pagename . ' saved with user agent ' . $GLOBALS['browser']->getAgentString(), 'DEBUG');
-        Horde::logMessage('Wicked_Driver_sql::updateText(): ' . $query, 'DEBUG');
-
-        try {
-            $this->_db->insert($query, $values);
-        } catch (Horde_Db_Exception $e) {
-            Horde::logMessage($e->getMessage(), 'ERR');
-            throw new Wicked_Exception($e);
-        }
-
-        /* Now move on to updating the record. */
-        if ($minorchange) {
-            $versionchange = 'page_minorversion = page_minorversion + 1';
-        } else {
-            $versionchange = 'page_majorversion = page_majorversion + 1, page_minorversion = 0';
-        }
-
-        $author = $GLOBALS['registry']->getAuth();
-        if ($author === false) {
-            $author = null;
-        }
-
-        $query = 'UPDATE ' . $this->_params['table'] .
-                 ' SET change_author = ?, page_text = ?, change_log = ?, version_created = ?, ' . $versionchange .
-                 ' WHERE page_name = ?';
-        $values = array($author,
-                        $this->_convertToDriver($text),
-                        $this->_convertToDriver($changelog),
-                        time(),
-                        $this->_convertToDriver($pagename));
-
-        Horde::logMessage('Wicked_Driver_sql::updateText(): ' . $query, 'DEBUG');
-
-        $this->_db->update($query, $values);
-    }
-
-    function getPages($special = true, $no_cache = false)
-    {
-        static $pageNames;
-        if (!isset($pageNames) || $no_cache) {
-            $query = 'SELECT page_name FROM ' . $this->_params['table'];
-            Horde::logMessage('Wicked_Driver_sql::getPages(): ' . $query, 'DEBUG');
-            try {
-                $result = $this->_db->selectValues($query);
-            } catch (Horde_Db_Exception $e) {
-                throw new Wicked_Exception($e);
-            }
-            $pageNames = $this->_convertFromDriver($result);
-        }
-        if ($special) {
-            return $pageNames + $this->getSpecialPages();
-        }
-
-        return $pageNames;
-    }
-
-    /**
-     */
-    function removeVersion($pagename, $version)
-    {
-        list($major, $minor) = explode('.', $version);
-
-        /* We need to know if we're deleting the current version. */
-        $query = 'SELECT 1 FROM ' . $this->_params['table'] .
-                 ' WHERE page_name = ? AND page_majorversion = ? AND page_minorversion = ?';
-        $values = array($this->_convertToDriver($pagename), $major, $minor);
-
-        Horde::logMessage('Wicked_Driver_sql::removeVersion(): ' . $query, 'DEBUG');
-
-        try {
-            $result = $this->_db->selectValue($query, $values);
-        } catch (Horde_Db_Exception $e) {
-            $result = false;
-        }
-
-        if (!$result) {
-            /* Removing a historical revision - we can just slice it out of the
-             * history table. $values is unchanged. */
-            $query = 'DELETE FROM ' . $this->_params['historytable'] .
-                ' WHERE page_name = ? and page_majorversion = ? and page_minorversion = ?';
-            Horde::logMessage('Wicked_Driver_sql::removeVersion(): ' . $query, 'DEBUG');
-            $this->_db->delete($query, $values);
-            return;
-        }
-
-        /* We're deleting the current version. Have to promote the
-         * next-most revision from the history table. */
-        $query = 'SELECT * FROM ' . $this->_params['historytable'] .
-                 ' WHERE page_name = ? ORDER BY page_majorversion DESC, page_minorversion DESC';
-        $query = $this->_db->addLimitOffset($query, array('limit' => 1));
-
-        Horde::logMessage('Wicked_Driver_sql::removeVersion(): ' . $query, 'DEBUG');
-
-        $revision = $this->_db->selectOne($query, array($this->_convertToDriver($pagename)), DB_FETCHMODE_ASSOC);
-
-        /* Replace the current version of the page with the
-         * version being promoted. */
-        $query = 'UPDATE ' . $this->_params['table'] . ' SET' .
-            ' page_text = ?, page_majorversion = ?, page_minorversion = ?,' .
-            ' version_created = ?, change_author = ?, change_log = ?' .
-            ' WHERE page_name = ?';
-        $values = array($revision['page_text'],
-                        $revision['page_majorversion'],
-                        $revision['page_minorversion'],
-                        $revision['version_created'],
-                        $revision['change_author'],
-                        $revision['change_log'],
-                        $this->_convertToDriver($pagename));
-
-        Horde::logMessage('Wicked_Driver_sql::removeVersion(): ' . $query, 'DEBUG');
-        $this->_db->update($query, $values);
-
-        /* Finally, remove the version that we promoted from the
-         * history table. */
-        $query = 'DELETE FROM ' . $this->_params['historytable'] .
-            ' WHERE page_name = ? and page_majorversion = ? and page_minorversion = ?';
-        $values = array($this->_convertToDriver($pagename), $revision['page_majorversion'], $revision['page_minorversion']);
-
-        Horde::logMessage('Wicked_Driver_sql::removeVersion(): ' . $query, 'DEBUG');
-
-        $this->_db->delete($query, $values);
-    }
-
-    /**
-     */
-    function removeAllVersions($pagename)
-    {
-        $this->_pageNames = null;
-
-        $query = 'DELETE FROM ' . $this->_params['table'] .
-                 ' WHERE page_name = ?';
-        $values = array($this->_convertToDriver($pagename));
-
-        Horde::logMessage('Wicked_Driver_sql::removeAllVersions(): ' . $query, 'DEBUG');
-
-        $this->_db->delete($query, $values);
-
-        $query = 'DELETE FROM ' . $this->_params['historytable'] .
-                 ' WHERE page_name = ?';
-        $values = array($this->_convertToDriver($pagename));
-
-        Horde::logMessage('Wicked_Driver_sql::removeAllVersions(): ' . $query, 'DEBUG');
-
-        $this->_db->delete($query, $values);
-
-        /* Remove attachments and do other cleanup. */
-        return parent::removeAllVersions($pagename);
-    }
-
-    /**
-     * Retrieves a page or set of pages given an SQL WHERE clause.
-     *
-     * @access private
-     *
-     * @param string $table     Which table are we retrieving pages from?
-     * @param string $sqlWhere  Where clause for sql statement (without the
-     *                          'WHERE').
-     * @param string $orderBy   What column should we order results by?
-     * @param integer $limit    Maximum number of pages to fetch.
-     *
-     * @return array | object  Either an array of pages or PEAR::Error.
-     */
-    function _retrieve($table, $sqlWhere, $orderBy = null, $limit = null)
-    {
-        $query = sprintf('SELECT * FROM %s%s%s',
-                         $table,
-                         !empty($sqlWhere) ? ' WHERE ' . $sqlWhere : '',
-                         !empty($orderBy) ? ' ORDER BY ' . $orderBy : '');
-        if (!empty($limit)) {
-            $query = $this->_db->addLimitOffset($query, array('limit' => $limit));
-        }
-
-        Horde::logMessage('Wicked_Driver_sql::_retrieve(): ' . $query, 'DEBUG');
-        try {
-            $result = $this->_db->selectAll($query);
-        } catch (Horde_Db_Exception $e) {
-            Horde::logMessage($e);
-            throw new Wicked_Exception($e);
-        }
-
-        $pages = array();
-        $index = 0;
-        foreach ($result as $row) {
-            if (isset($row['page_name'])) {
-                $row['page_name'] = $this->_convertFromDriver($row['page_name']);
-            }
-            if (isset($row['page_text'])) {
-                $row['page_text'] = $this->_convertFromDriver($row['page_text']);
-            }
-            if (isset($row['change_log'])) {
-                $row['change_log'] = $this->_convertFromDriver($row['change_log']);
-            }
-
-            $pages[] = $row;
-        }
-
-        return $pages;
-    }
-
-    /**
-     * Returns the charset used by the backend.
-     *
-     * @return string  The backend's charset
-     */
-    function getCharset()
-    {
-        return $this->_params['charset'];
-    }
-
-    /**
-     * Converts a value from the driver's charset to the default charset.
-     *
-     * @param mixed $value  A value to convert.
-     *
-     * @return mixed        The converted value.
-     */
-    function _convertFromDriver($value)
-    {
-        return Horde_String::convertCharset($value, $this->getCharset(), 'UTF-8');
-    }
-
-    /**
-     * Converts a value from the default charset to the driver's charset.
-     *
-     * @param mixed $value  A value to convert.
-     *
-     * @return mixed        The converted value.
-     */
-    function _convertToDriver($value)
-    {
-        return Horde_String::convertCharset($value, 'UTF-8', $this->getCharset());
-    }
-
-    /**
-     * Attempts to open a persistent connection to the SQL server.
-     *
-     * @throws Wicked_Exception
-     */
-    function connect()
-    {
-        try {
-            $this->_db = $GLOBALS['injector']->getInstance('Horde_Db_Adapter');
-        } catch (Horde_Exception $e) {
-            throw new Wicked_Exception($e);
-        }
-
-        $this->_params = array_merge(array(
-            'table' => 'wicked_pages',
-            'historytable' => 'wicked_history',
-            'attachmenttable' => 'wicked_attachments',
-            'attachmenthistorytable' => 'wicked_attachment_history'
-        ), $this->_params);
-
-        return true;
-    }
-
-}
index 6c5bc38..b6936a1 100644 (file)
  * @author  Tyler Colbert <tyler@colberts.us>
  * @package Wicked
  */
-class Wicked_Page {
+class Wicked_Page
+{
+    const MATCH_LEFT = 1;
+    const MATCH_RIGHT = 2;
+    const MATCH_ENDS = 3;
+    const MATCH_ANY = 4;
 
     /**
      * Display modes supported by this page. Possible modes:
@@ -188,8 +193,8 @@ class Wicked_Page {
     function getCurrentPage()
     {
         return Wicked_Page::getPage(rtrim(Horde_Util::getFormData('page'), '/'),
-                             Horde_Util::getFormData('version'),
-                             Horde_Util::getFormData('referrer'));
+                                    Horde_Util::getFormData('version'),
+                                    Horde_Util::getFormData('referrer'));
     }
 
     /**
@@ -206,33 +211,27 @@ class Wicked_Page {
             $pagename = 'WikiHome';
         }
 
-        $file = WICKED_BASE . '/lib/Page/' . basename($pagename) . '.php';
-        if ($pagename == basename($pagename) &&
-            file_exists($file)) {
-            require_once $file;
-            return new $pagename($referrer);
+        $classname = 'Wicked_Page_' . $pagename;
+        if ($pagename == basename($pagename) && class_exists($classname)) {
+            return new $classname($referrer);
         }
 
-        require_once WICKED_BASE . '/lib/Page/StandardPage.php';
-
         /* If we have a version, but it is actually the most recent version,
          * ignore it. */
         if (!empty($pagever)) {
-            $page = new StandardPage($pagename, false, null);
+            $page = new Wicked_Page_StandardPage($pagename, false, null);
             if ($page->isValid() && $page->version() == $pagever) {
                 return $page;
             }
-            require_once WICKED_BASE . '/lib/Page/StandardPage/StdHistoryPage.php';
-            return new StdHistoryPage($pagename, $pagever);
+            return new Wicked_Page_StandardHistoryPage($pagename, $pagever);
         }
 
-        $page = new StandardPage($pagename);
+        $page = new Wicked_Page_StandardPage($pagename);
         if ($page->isValid() || !$page->allows(Wicked::MODE_EDIT)) {
             return $page;
         }
 
-        require_once WICKED_BASE . '/lib/Page/AddPage.php';
-        return new AddPage($pagename);
+        return new Wicked_Page_AddPage($pagename);
     }
 
     function versionCreated()
index 27cba55..0f398c9 100644 (file)
@@ -1,7 +1,4 @@
 <?php
-
-require_once WICKED_BASE . '/lib/Page/StandardPage.php';
-
 /**
  * Wicked AddPage class.
  *
@@ -13,7 +10,7 @@ require_once WICKED_BASE . '/lib/Page/StandardPage.php';
  * @author  Tyler Colbert <tyler@colberts.us>
  * @package Wicked
  */
-class AddPage extends Wicked_Page {
+class Wicked_Page_AddPage extends Wicked_Page {
 
     /**
      * Display modes supported by this page.
@@ -36,7 +33,7 @@ class AddPage extends Wicked_Page {
      */
     var $_results;
 
-    function AddPage($newpage)
+    function __construct($newpage)
     {
         $this->_newpage = $newpage;
         $this->_results = $GLOBALS['wicked']->searchTitles($newpage);
@@ -61,7 +58,7 @@ class AddPage extends Wicked_Page {
     function display()
     {
         try {
-            $templates = $GLOBALS['wicked']->getMatchingPages('Template', WICKED_PAGE_MATCH_ENDS);
+            $templates = $GLOBALS['wicked']->getMatchingPages('Template', Wicked_Page::MATCH_ENDS);
         } catch (Wicked_Exception $e) {
             $GLOBALS['notification']->push(sprintf(_("Error retrieving templates: %s"),
                                                    $e->getMessage()), 'horde.error');
@@ -74,9 +71,9 @@ class AddPage extends Wicked_Page {
             $pages = array();
             foreach ($this->_results as $page) {
                 if (!empty($page['page_history'])) {
-                    $page = new StdHistoryPage($page);
+                    $page = new Wicked_Page_StandardHistoryPage($page);
                 } else {
-                    $page = new StandardPage($page);
+                    $page = new Wicked_Page_StandardPage($page);
                 }
 
                 $pages[] = array('author' => $page->author(),
index cd8d830..d920817 100644 (file)
@@ -1,7 +1,4 @@
 <?php
-
-require_once WICKED_BASE . '/lib/Page/StandardPage.php';
-
 /**
  * Wicked AllPages class.
  *
@@ -13,7 +10,7 @@ require_once WICKED_BASE . '/lib/Page/StandardPage.php';
  * @author  Tyler Colbert <tyler@colberts.us>
  * @package Wicked
  */
-class AllPages extends Wicked_Page {
+class Wicked_Page_AllPages extends Wicked_Page {
 
     /**
      * Display modes supported by this page.
@@ -43,7 +40,7 @@ class AllPages extends Wicked_Page {
         $template = $GLOBALS['injector']->createInstance('Horde_Template');
         $pages = array();
         foreach ($this->content() as $page) {
-            $page = new StandardPage($page);
+            $page = new Wicked_Page_StandardPage($page);
             $pages[] = array('author' => $page->author(),
                              'created' => $page->formatVersionCreated(),
                              'name' => $page->pageName(),
index c61eda3..0846dfb 100644 (file)
@@ -1,8 +1,4 @@
 <?php
-
-/** StandardPage:: */
-require_once WICKED_BASE . '/lib/Page/StandardPage.php';
-
 /**
  * Wicked AttachedFiles class.
  *
@@ -14,7 +10,7 @@ require_once WICKED_BASE . '/lib/Page/StandardPage.php';
  * @author  Jason M. Felice <jason.m.felice@gmail.com>
  * @package Wicked
  */
-class AttachedFiles extends Wicked_Page {
+class Wicked_Page_AttachedFiles extends Wicked_Page {
 
     /**
      * Display modes supported by this page.
@@ -37,7 +33,7 @@ class AttachedFiles extends Wicked_Page {
     /**
      * Constructor.
      */
-    function AttachedFiles($referrer)
+    function __construct($referrer)
     {
         $this->_referrer = $referrer;
     }
index 07dbda8..5722baf 100644 (file)
@@ -1,7 +1,4 @@
 <?php
-
-require_once WICKED_BASE . '/lib/Page/StandardPage.php';
-
 /**
  * Wicked BackLinks class.
  *
@@ -13,7 +10,7 @@ require_once WICKED_BASE . '/lib/Page/StandardPage.php';
  * @author  Tyler Colbert <tyler@colberts.us>
  * @package Wicked
  */
-class BackLinks extends Wicked_Page {
+class Wicked_Page_BackLinks extends Wicked_Page {
 
     /**
      * Display modes supported by this page.
@@ -30,7 +27,7 @@ class BackLinks extends Wicked_Page {
      */
     var $_referrer = null;
 
-    function BackLinks($referrer)
+    function __construct($referrer)
     {
         $this->_referrer = $referrer;
     }
@@ -49,9 +46,9 @@ class BackLinks extends Wicked_Page {
         require WICKED_TEMPLATES . '/pagelist/header.inc';
         foreach ($summaries as $page) {
             if (!empty($page['page_history'])) {
-                $page = new StdHistoryPage($page);
+                $page = new Wicked_Page_StandardHistoryPage($page);
             } else {
-                $page = new StandardPage($page);
+                $page = new Wicked_Page_StandardPage($page);
             }
             require WICKED_TEMPLATES . '/pagelist/summary.inc';
         }
index 0490787..efbfe81 100644 (file)
@@ -9,17 +9,12 @@
  */
 
 /**
- * Page
- */
-require_once WICKED_BASE . '/lib/Page.php';
-
-/**
  * Wicked DeletePage class (for confirming deletion).
  *
  * @author  Chuck Hagenbuch <chuck@horde.org>
  * @package Wicked
  */
-class DeletePage extends Wicked_Page {
+class Wicked_Page_DeletePage extends Wicked_Page {
 
     /**
      * Display modes supported by this page.
@@ -35,7 +30,7 @@ class DeletePage extends Wicked_Page {
      */
     var $_referrer = null;
 
-    function DeletePage($referrer)
+    function __construct($referrer)
     {
         $this->_referrer = $referrer;
     }
index 55dc5ee..b5531d9 100644 (file)
@@ -6,12 +6,6 @@
  * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
  *
  * @package Wicked
- */
-
-/**
- * Page
- */
-require_once WICKED_BASE . '/lib/Page.php';
 
 /**
  * Wicked EditPage class.
@@ -19,7 +13,7 @@ require_once WICKED_BASE . '/lib/Page.php';
  * @author  Jason M. Felice <jason.m.felice@gmail.com>
  * @package Wicked
  */
-class EditPage extends Wicked_Page {
+class Wicked_Page_EditPage extends Wicked_Page {
 
     /**
      * Display modes supported by this page.
@@ -37,7 +31,7 @@ class EditPage extends Wicked_Page {
      */
     var $_referrer = null;
 
-    function EditPage($referrer)
+    function __construct($referrer)
     {
         $this->_referrer = $referrer;
         if ($GLOBALS['conf']['lock']['driver'] != 'none') {
index 45afea1..c2596fe 100644 (file)
@@ -1,7 +1,4 @@
 <?php
-
-require_once WICKED_BASE . '/lib/Page/StandardPage.php';
-
 /**
  * Wicked LeastPopular class.
  *
@@ -13,7 +10,7 @@ require_once WICKED_BASE . '/lib/Page/StandardPage.php';
  * @author  Tyler Colbert <tyler@colberts.us>
  * @package Wicked
  */
-class LeastPopular extends Wicked_Page {
+class Wicked_Page_LeastPopular extends Wicked_Page {
 
     /**
      * Display modes supported by this page.
@@ -47,7 +44,7 @@ class LeastPopular extends Wicked_Page {
         $template = $GLOBALS['injector']->createInstance('Horde_Template');
         $pages = array();
         foreach ($this->content(10) as $page) {
-            $page = new StandardPage($page);
+            $page = new Wicked_Page_StandardPage($page);
             $pages[] = array('author' => $page->author(),
                              'created' => $page->formatVersionCreated(),
                              'name' => $page->pageName(),
index 9a66c55..4ccb80c 100644 (file)
@@ -1,7 +1,4 @@
 <?php
-
-require_once WICKED_BASE . '/lib/Page/StandardPage.php';
-
 /**
  * Wicked LikePages class.
  *
@@ -13,7 +10,7 @@ require_once WICKED_BASE . '/lib/Page/StandardPage.php';
  * @author  Tyler Colbert <tyler@colberts.us>
  * @package Wicked
  */
-class LikePages extends Wicked_Page {
+class Wicked_Page_LikePages extends Wicked_Page {
 
     /**
      * Display modes supported by this page.
@@ -30,7 +27,7 @@ class LikePages extends Wicked_Page {
      */
     var $_referrer = null;
 
-    function LikePages($referrer)
+    function __construct($referrer)
     {
         $this->_referrer = $referrer;
     }
@@ -50,9 +47,9 @@ class LikePages extends Wicked_Page {
         require WICKED_TEMPLATES . '/pagelist/header.inc';
         foreach ($summaries as $page) {
             if (!empty($page['page_history'])) {
-                $page = new StdHistoryPage($page);
+                $page = new Wicked_Page_StandardHistoryPage($page);
             } else {
-                $page = new StandardPage($page);
+                $page = new Wicked_Page_StandardPage($page);
             }
             require WICKED_TEMPLATES . '/pagelist/summary.inc';
         }
index e6d84f7..f2f2009 100644 (file)
@@ -1,7 +1,4 @@
 <?php
-
-require_once WICKED_BASE . '/lib/Page/StandardPage.php';
-
 /**
  * Special page for merging or renaming pages.
  *
@@ -13,7 +10,7 @@ require_once WICKED_BASE . '/lib/Page/StandardPage.php';
  * @author  Jason M. Felice <eraserhd@speakeasy.net>
  * @package Wicked
  */
-class MergeOrRename extends Wicked_Page {
+class Wicked_Page_MergeOrRename extends Wicked_Page {
 
     /**
      * Display modes supported by this page.
@@ -38,7 +35,7 @@ class MergeOrRename extends Wicked_Page {
      */
     var $_errors = array();
 
-    function MergeOrRename($referrer)
+    function __construct($referrer)
     {
         $this->_referrer = $referrer;
     }
index d36beb8..ed9a3cf 100644 (file)
@@ -1,7 +1,4 @@
 <?php
-
-require_once WICKED_BASE . '/lib/Page/StandardPage.php';
-
 /**
  * Wicked MostPopular class.
  *
@@ -13,7 +10,7 @@ require_once WICKED_BASE . '/lib/Page/StandardPage.php';
  * @author  Tyler Colbert <tyler@colberts.us>
  * @package Wicked
  */
-class MostPopular extends Wicked_Page {
+class Wicked_Page_MostPopular extends Wicked_Page {
 
     /**
      * Display modes supported by this page.
@@ -47,7 +44,7 @@ class MostPopular extends Wicked_Page {
         $template = $GLOBALS['injector']->createInstance('Horde_Template');
         $pages = array();
         foreach ($this->content(10) as $page) {
-            $page = new StandardPage($page);
+            $page = new Wicked_Page_StandardPage($page);
             $pages[] = array('author' => $page->author(),
                              'created' => $page->formatVersionCreated(),
                              'name' => $page->pageName(),
index 101e5b9..427f9d3 100644 (file)
@@ -9,17 +9,12 @@
  */
 
 /**
- * StandardPage
- */
-require_once WICKED_BASE . '/lib/Page/StandardPage.php';
-
-/**
  * Wicked NewPage class.
  *
  * @author  Chuck Hagenbuch <chuck@horde.org>
  * @package Wicked
  */
-class NewPage extends Wicked_Page {
+class Wicked_Page_NewPage extends Wicked_Page {
 
     /**
      * Display modes supported by this page.
@@ -44,7 +39,7 @@ class NewPage extends Wicked_Page {
      */
     var $_template = null;
 
-    function NewPage($referrer)
+    function __construct($referrer)
     {
         $this->_referrer = $referrer;
         $this->_template = Horde_Util::getFormData('template');
index 28c4f5d..4d380ec 100644 (file)
@@ -1,7 +1,4 @@
 <?php
-
-require_once WICKED_BASE . '/lib/Page/StandardPage.php';
-
 /**
  * Wicked RecentChanges class.
  *
@@ -13,7 +10,7 @@ require_once WICKED_BASE . '/lib/Page/StandardPage.php';
  * @author  Tyler Colbert <tyler@colberts.us>
  * @package Wicked
  */
-class RecentChanges extends Wicked_Page {
+class Wicked_Page_RecentChanges extends Wicked_Page {
 
     /**
      * Display modes supported by this page.
@@ -40,7 +37,7 @@ class RecentChanges extends Wicked_Page {
         $bydate = array();
         $changes = array();
         foreach ($summaries as $page) {
-            $page = new StandardPage($page);
+            $page = new Wicked_Page_StandardPage($page);
 
             $createDate = $page->versionCreated();
             $tm = localtime($createDate, true);
index b3a3fa9..2b8bb20 100644 (file)
@@ -1,7 +1,4 @@
 <?php
-
-require_once WICKED_BASE . '/lib/Page/StandardPage.php';
-
 /**
  * Wicked RevertPage class (for confirming reversions).
  *
@@ -13,7 +10,7 @@ require_once WICKED_BASE . '/lib/Page/StandardPage.php';
  * @author  Chuck Hagenbuch <chuck@horde.org>
  * @package Wicked
  */
-class RevertPage extends Wicked_Page {
+class Wicked_Page_RevertPage extends Wicked_Page {
 
     /**
      * Display modes supported by this page.
@@ -29,7 +26,7 @@ class RevertPage extends Wicked_Page {
      */
     var $_referrer = null;
 
-    function RevertPage($referrer)
+    function __construct($referrer)
     {
         $this->_referrer = $referrer;
     }
index ad2eaf8..7d6bd9a 100644 (file)
@@ -1,8 +1,4 @@
 <?php
-
-require_once WICKED_BASE . '/lib/Page/StandardPage.php';
-require_once WICKED_BASE . '/lib/Page/StandardPage/StdHistoryPage.php';
-
 /**
  * Wicked SearchAll class.
  *
@@ -14,7 +10,7 @@ require_once WICKED_BASE . '/lib/Page/StandardPage/StdHistoryPage.php';
  * @author Ben Chavet <ben@horde.org>
  * @package Wicked
  */
-class Search extends Wicked_Page {
+class Wicked_Page_Search extends Wicked_Page {
 
     /**
      * Display modes supported by this page.
@@ -86,7 +82,7 @@ class Search extends Wicked_Page {
 
         /* Prepare exact match section */
         $exact = array();
-        $page = new StandardPage($searchtext);
+        $page = new Wicked_Page_StandardPage($searchtext);
         if ($GLOBALS['wicked']->pageExists($searchtext)) {
             $exact[] = array('author' => htmlspecialchars($page->author()),
                              'created' => $page->formatVersionCreated(),
@@ -109,9 +105,9 @@ class Search extends Wicked_Page {
         $titles = array();
         foreach ($this->_results['titles'] as $page) {
             if (!empty($page['page_history'])) {
-                $page = new StdHistoryPage($page);
+                $page = new Wicked_Page_StandardHistoryPage($page);
             } else {
-                $page = new StandardPage($page);
+                $page = new Wicked_Page_StandardPage($page);
             }
 
             $titles[] = array('author' => $page->author(),
@@ -127,9 +123,9 @@ class Search extends Wicked_Page {
         $pages = array();
         foreach ($this->_results['pages'] as $page) {
             if (!empty($page['page_history'])) {
-                $page = new StdHistoryPage($page);
+                $page = new Wicked_Page_StandardHistoryPage($page);
             } else {
-                $page = new StandardPage($page);
+                $page = new Wicked_Page_StandardPage($page);
             }
 
             $pages[] = array('author' => $page->author(),
diff --git a/wicked/lib/Page/StandardHistoryPage.php b/wicked/lib/Page/StandardHistoryPage.php
new file mode 100644 (file)
index 0000000..24388a4
--- /dev/null
@@ -0,0 +1,61 @@
+<?php
+/**
+ * Wicked Page class for old versions of pages.
+ *
+ * @author  Tyler Colbert <tyler@colberts.us>
+ * @package Wicked
+ */
+class Wicked_Page_StandardHistoryPage extends Wicked_Page_StandardPage {
+
+    /**
+     * Display modes supported by this page.
+     *
+     * @var array
+     */
+    var $supportedModes = array(
+        Wicked::MODE_DISPLAY => true,
+        Wicked::MODE_EDIT => false,
+        Wicked::MODE_REMOVE => true,
+        Wicked::MODE_HISTORY => true,
+        Wicked::MODE_DIFF => true,
+        Wicked::MODE_LOCKING => false,
+        Wicked::MODE_UNLOCKING => false);
+
+    /**
+     * Construct a standard history page class to represent an old
+     * version of a wiki page.
+     *
+     * @param string  $pagename    The name of the page to load.
+     * @param integer $version     The version of the page to load.
+     *
+     * @throws Wicked_Exception
+     */
+    function __construct($pagename, $version = null)
+    {
+        if (empty($version)) {
+            parent::__construct($pagename);
+            return;
+        }
+
+        // Retrieve the version.
+        $pages = $GLOBALS['wicked']->retrieveHistory($pagename, $version);
+
+        // If it didnt find one, return an error.
+        if (empty($pages[0])) {
+            throw new Wicked_Exception(_("History page not found"));
+        }
+
+        $this->_page = $pages[0];
+    }
+
+    function isOld()
+    {
+        return true;
+    }
+
+    function pageUrl($linkpage = null, $actionId = null)
+    {
+        return Horde_Util::addParameter(parent::pageUrl($linkpage, $actionId), 'version', $this->version());
+    }
+
+}
index 4bf143d..acf459e 100644 (file)
@@ -10,7 +10,7 @@
  * @author  Tyler Colbert <tyler@colberts.us>
  * @package Wicked
  */
-class StandardPage extends Wicked_Page {
+class Wicked_Page_StandardPage extends Wicked_Page {
 
     /**
      * Display modes supported by this page.
@@ -192,8 +192,6 @@ class StandardPage extends Wicked_Page {
      */
     function history()
     {
-        require_once WICKED_BASE . '/lib/Page/StandardPage/StdHistoryPage.php';
-
         try {
             $summaries = $GLOBALS['wicked']->getHistory($this->pageName());
         } catch (Wicked_Exception $e) {
@@ -219,7 +217,7 @@ class StandardPage extends Wicked_Page {
         $show_edit = false;
         foreach ($summaries as $page) {
             $i++;
-            $page = new StdHistoryPage($page);
+            $page = new Wicked_Page_StandardHistoryPage($page);
             require WICKED_TEMPLATES . '/history/summary.inc';
         }
 
diff --git a/wicked/lib/Page/StandardPage/StdHistoryPage.php b/wicked/lib/Page/StandardPage/StdHistoryPage.php
deleted file mode 100644 (file)
index f976432..0000000
+++ /dev/null
@@ -1,61 +0,0 @@
-<?php
-/**
- * Wicked Page class for old versions of pages.
- *
- * @author  Tyler Colbert <tyler@colberts.us>
- * @package Wicked
- */
-class StdHistoryPage extends StandardPage {
-
-    /**
-     * Display modes supported by this page.
-     *
-     * @var array
-     */
-    var $supportedModes = array(
-        Wicked::MODE_DISPLAY => true,
-        Wicked::MODE_EDIT => false,
-        Wicked::MODE_REMOVE => true,
-        Wicked::MODE_HISTORY => true,
-        Wicked::MODE_DIFF => true,
-        Wicked::MODE_LOCKING => false,
-        Wicked::MODE_UNLOCKING => false);
-
-    /**
-     * Construct a standard history page class to represent an old
-     * version of a wiki page.
-     *
-     * @param string  $pagename    The name of the page to load.
-     * @param integer $version     The version of the page to load.
-     *
-     * @throws Wicked_Exception
-     */
-    function __construct($pagename, $version = null)
-    {
-        if (empty($version)) {
-            parent::__construct($pagename);
-            return;
-        }
-
-        // Retrieve the version.
-        $pages = $GLOBALS['wicked']->retrieveHistory($pagename, $version);
-
-        // If it didnt find one, return an error.
-        if (empty($pages[0])) {
-            throw new Wicked_Exception(_("History page not found"));
-        }
-
-        $this->_page = $pages[0];
-    }
-
-    function isOld()
-    {
-        return true;
-    }
-
-    function pageUrl($linkpage = null, $actionId = null)
-    {
-        return Horde_Util::addParameter(parent::pageUrl($linkpage, $actionId), 'version', $this->version());
-    }
-
-}
index 0fe86db..cf7ddc5 100644 (file)
@@ -1,7 +1,4 @@
 <?php
-
-require_once WICKED_BASE . '/lib/Page/SyncPages.php';
-
 /**
  * Wicked SyncDiff class.
  *
@@ -13,7 +10,7 @@ require_once WICKED_BASE . '/lib/Page/SyncPages.php';
  * @author  Duck <duck@obala.net>
  * @package Wicked
  */
-class SyncDiff extends SyncPages {
+class Wicked_Page_SyncDiff extends Wicked_Page_SyncPages {
 
     /**
      * Display modes supported by this page.
@@ -32,10 +29,9 @@ class SyncDiff extends SyncPages {
      */
     var $_pageName;
 
-    function SyncDiff()
+    function __construct()
     {
-        parent::SyncPages();
-
+        parent::__construct();
         $this->_pageName = Horde_Util::getGet('sync_page');
     }
 
index 0f56a9b..7761bb8 100644 (file)
@@ -1,8 +1,4 @@
 <?php
-
-require_once WICKED_BASE . '/lib/Page/StandardPage.php';
-require_once WICKED_BASE . '/lib/Sync.php';
-
 /**
  * Wicked SyncPages class.
  *
@@ -14,7 +10,7 @@ require_once WICKED_BASE . '/lib/Sync.php';
  * @author  Duck <duck@obala.net>
  * @package Wicked
  */
-class SyncPages extends Wicked_Page {
+class Wicked_Page_SyncPages extends Wicked_Page {
 
     /**
      * Display modes supported by this page.
@@ -32,7 +28,7 @@ class SyncPages extends Wicked_Page {
      *
      * @throws Wicked_Exception
      */
-    function SyncPages()
+    function __construct()
     {
         $this->_loadSyncDriver();
 
index 9008999..e25909c 100644 (file)
@@ -34,26 +34,22 @@ abstract class Wicked_Sync {
      * @return Wicked_Sync    The newly created concrete Wicked_Sync
      *                        instance, or false on an error.
      */
-    function factory($driver = 'wicked', $params = array())
+    function factory($driver = 'Wicked', $params = array())
     {
+        $driver = Horde_String::ucfirst(basename($driver));
         $class = 'Wicked_Sync_' . $driver;
+
         if (!class_exists($class)) {
-            include dirname(__FILE__) . '/Sync/' . $driver . '.php';
+            return false;
         }
 
         if (empty($params['user'])) {
             $params['user'] = $GLOBALS['registry']->getAuth();
         }
-
         if (empty($params['password'])) {
             $params['password'] = $GLOBALS['registry']->getAuthCredential('password');
         }
-
-        if (class_exists($class)) {
-            return new $class($params);
-        } else {
-            return false;
-        }
+        return new $class($params);
     }
 
     /**
@@ -61,7 +57,7 @@ abstract class Wicked_Sync {
      *
      * @param array $params  A hash containing connection parameters.
      */
-    function Wicked_Sync($params = array())
+    function __construct($params = array())
     {
         $this->_params = $params;
     }
diff --git a/wicked/lib/Sync/Wicked.php b/wicked/lib/Sync/Wicked.php
new file mode 100644 (file)
index 0000000..02bd9af
--- /dev/null
@@ -0,0 +1,127 @@
+<?php
+/**
+ * Copyright 2008-2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ *
+ * @package Wicked
+ */
+
+/**
+ * Wicked_Driver:: defines an API for implementing storage backends for
+ * Wicked.
+ *
+ * @author  Duck <duck@obala.net>
+ * @package Wicked
+ */
+class Wicked_Sync_Wicked extends Wicked_Sync {
+
+    /**
+     * Returns a list of available pages.
+     *
+     * @return array  An array of all available pages.
+     */
+    var $_client;
+
+    /**
+     * Returns a list of available pages.
+     *
+     * @return array  An array of all available pages.
+     */
+    function listPages()
+    {
+        return $this->_getData('list');
+    }
+
+    /**
+     * Get the wiki source of a page specified by its name.
+     *
+     * @param string $name  The name of the page to fetch
+     *
+     * @return string  Page data.
+     * @throws Wicked_Exception
+     */
+    function getPageSource($pageName)
+    {
+        return $this->_getData('getPageSource', array($pageName));
+    }
+
+    /**
+     * Return basic page information.
+     *
+     * @param string $pageName Page name
+     *
+     * @return array  Page data.
+     * @throws Wicked_Exception
+     */
+    function getPageInfo($pageName)
+    {
+        return $this->_getData('getPageInfo', array($pageName));
+    }
+
+    /**
+     * Return basic pages information.
+     *
+     * @param array $pages Page names to get info for
+     *
+     * @return array  Pages data.
+     * @throws Wicked_Exception
+     */
+    function getMultiplePageInfo($pages = array())
+    {
+        return $this->_getData('getMultiplePageInfo', array($pages));
+    }
+
+    /**
+     * Return page history.
+     *
+     * @param string $pagename Page name
+     *
+     * @return array  An array of page parameters.
+     */
+    function getPageHistory($pagename)
+    {
+        return $this->_getData('getPageHistory', array($pagename));
+    }
+
+    /**
+     * Updates content of a wiki page. If the page does not exist it is
+     * created.
+     *
+     * @param string $pagename Page to edit
+     * @param string $text Page content
+     * @param string $changelog Description of the change
+     * @param boolean $minorchange True if this is a minor change
+     *
+     * @throws Wicked_Exception
+     */
+    function editPage($pagename, $text, $changelog = '', $minorchange = false)
+    {
+        $this->_getData('edit', array($pagename, $text, $changelog, $minorchange));
+    }
+
+    /**
+     * Process remote call
+     *
+     * @param string $method Method name to call
+     * @param array $params Array of parameters
+     *
+     * @return mixed
+     * @throws Wicked_Exception
+     */
+    function _getData($method, $params = array())
+    {
+        try {
+            return Horde_Rpc::request(
+                'xmlrpc',
+                $this->_params['url'],
+                $this->_params['prefix'] . '.' . $method,
+                $params,
+                array('user' => $this->_params['user'],
+                      'pass' => $this->_params['password']));
+        } catch (Horde_Exception $e) {
+            throw new Wicked_Exception($e);
+        }
+    }
+}
diff --git a/wicked/lib/Sync/wicked.php b/wicked/lib/Sync/wicked.php
deleted file mode 100644 (file)
index 60eb392..0000000
+++ /dev/null
@@ -1,130 +0,0 @@
-<?php
-/**
- * Copyright 2008-2010 The Horde Project (http://www.horde.org/)
- *
- * See the enclosed file COPYING for license information (GPL). If you
- * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
- *
- * @package Wicked
- */
-
-/** Horde_RPC */
-require_once 'Horde/RPC.php';
-
-/**
- * Wicked_Driver:: defines an API for implementing storage backends for
- * Wicked.
- *
- * @author  Duck <duck@obala.net>
- * @package Wicked
- */
-class Wicked_Sync_wicked extends Wicked_Sync {
-
-    /**
-     * Returns a list of available pages.
-     *
-     * @return array  An array of all available pages.
-     */
-    var $_client;
-
-    /**
-     * Returns a list of available pages.
-     *
-     * @return array  An array of all available pages.
-     */
-    function listPages()
-    {
-        return $this->_getData('list');
-    }
-
-    /**
-     * Get the wiki source of a page specified by its name.
-     *
-     * @param string $name  The name of the page to fetch
-     *
-     * @return string  Page data.
-     * @throws Wicked_Exception
-     */
-    function getPageSource($pageName)
-    {
-        return $this->_getData('getPageSource', array($pageName));
-    }
-
-    /**
-     * Return basic page information.
-     *
-     * @param string $pageName Page name
-     *
-     * @return array  Page data.
-     * @throws Wicked_Exception
-     */
-    function getPageInfo($pageName)
-    {
-        return $this->_getData('getPageInfo', array($pageName));
-    }
-
-    /**
-     * Return basic pages information.
-     *
-     * @param array $pages Page names to get info for
-     *
-     * @return array  Pages data.
-     * @throws Wicked_Exception
-     */
-    function getMultiplePageInfo($pages = array())
-    {
-        return $this->_getData('getMultiplePageInfo', array($pages));
-    }
-
-    /**
-     * Return page history.
-     *
-     * @param string $pagename Page name
-     *
-     * @return array  An array of page parameters.
-     */
-    function getPageHistory($pagename)
-    {
-        return $this->_getData('getPageHistory', array($pagename));
-    }
-
-    /**
-     * Updates content of a wiki page. If the page does not exist it is
-     * created.
-     *
-     * @param string $pagename Page to edit
-     * @param string $text Page content
-     * @param string $changelog Description of the change
-     * @param boolean $minorchange True if this is a minor change
-     *
-     * @throws Wicked_Exception
-     */
-    function editPage($pagename, $text, $changelog = '', $minorchange = false)
-    {
-        $this->_getData('edit', array($pagename, $text, $changelog, $minorchange));
-    }
-
-    /**
-     * Process remote call
-     *
-     * @param string $method Method name to call
-     * @param array $params Array of parameters
-     *
-     * @return mixed
-     * @throws Wicked_Exception
-     */
-    function _getData($method, $params = array())
-    {
-        try {
-            return Horde_RPC::request(
-                'xmlrpc',
-                $this->_params['url'],
-                $this->_params['prefix'] . '.' . $method,
-                $params,
-                array('user' => $this->_params['user'],
-                      'pass' => $this->_params['password']));
-        } catch (Horde_Exception $e) {
-            throw new Wicked_Exception($e);
-        }
-    }
-}
index c921d2e..7e2fe40 100644 (file)
@@ -14,9 +14,6 @@ class Wicked_Driver_TC extends HordeUnitTestCase {
         @define('TEST_PAGE_1', 'driver-pages.phpt Test Page One');
         @define('TEST_PAGE_2', 'Renamed driver-pages.phpt Test Page (Called "Two")');
 
-        require_once WICKED_BASE . '/lib/Driver.php';
-        require_once WICKED_BASE . '/lib/Wicked.php';
-
         $this->wicked = Wicked_Driver::factory('sql', $this->getTestDatabaseSQLDriverConfig());
     }