This file is indexed.

/usr/share/horde/wicked/lib/Driver/Sql.php is in php-horde-wicked 2.0.1-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
<?php
/**
 * Copyright 2003-2013 Horde LLC (http://www.horde.org/)
 *
 * See the enclosed file COPYING for license information (GPL). If you
 * did not receive this file, see http://www.horde.org/licenses/gpl.
 *
 * @category Horde
 * @license  http://www.horde.org/licenses/gpl GPL
 * @author   Tyler Colbert <tyler@colberts.us>
 * @author   Chuck Hagenbuch <chuck@horde.org>
 * @author   Jan Schneider <jan@horde.org>
 * @package  Wicked
 */

/**
 * Wicked storage implementation for the Horde_Db database abstraction layer.
 *
 * @category Horde
 * @license  http://www.horde.org/licenses/gpl GPL
 * @author   Tyler Colbert <tyler@colberts.us>
 * @author   Chuck Hagenbuch <chuck@horde.org>
 * @author   Jan Schneider <jan@horde.org>
 * @package  Wicked
 */
class Wicked_Driver_Sql extends Wicked_Driver
{
    /**
     * Handle for the current database connection.
     *
     * @var Horde_Db_Adapter
     */
    protected $_db;

    /**
     * A cached list of all available page names.
     *
     * @var array
     */
    protected $_pageNames;

    /**
     * Constructor.
     *
     * @param array $params  A hash containing connection parameters.
     */
    public function __construct($params = array())
    {
        if (!isset($params['db'])) {
            throw new InvalidArgumentException('Missing db parameter.');
        }
        $this->_db = $params['db'];
        unset($params['db']);

        $params = array_merge(array(
            'table' => 'wicked_pages',
            'historytable' => 'wicked_history',
            'attachmenttable' => 'wicked_attachments',
            'attachmenthistorytable' => 'wicked_attachment_history'
        ), $params);
        parent::__construct($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
     */
    public function retrieveByName($pagename)
    {
        $pages = $this->_retrieve(
            $this->_params['table'],
            array('page_name = ?', array($this->_convertToDriver($pagename))));

        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
     */
    public function retrieveHistory($pagename, $version)
    {
        if (!preg_match('/^\d+$/', $version)) {
            throw new Wicked_Exception('invalid version number');
        }

        return $this->_retrieve(
            $this->_params['historytable'],
            array('page_name = ? AND page_version = ?',
                  array($this->_convertToDriver($pagename), (int)$version)));
    }

    public function getPageById($id)
    {
        return $this->_retrieve($this->_params['table'],
                                array('page_id = ?', array((int)$id)));
    }

    public function getAllPages()
    {
        return $this->_retrieve($this->_params['table'], '', 'page_name');
    }

    public function getHistory($pagename)
    {
        return $this->_retrieve(
            $this->_params['historytable'],
            array('page_name = ?', array($this->_convertToDriver($pagename))),
            'page_version DESC');
    }

    /**
     * Returns the most recently changed pages.
     *
     * @param integer $days  The number of days to look back.
     *
     * @return array  Pages.
     * @throws Wicked_Exception
     */
    public function getRecentChanges($days = 3)
    {
        $where = array('version_created > ?', array(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 recently changed pages.
     *
     * @param integer $limit  The number of most recent pages to return.
     *
     * @return array  Pages.
     * @throws Wicked_Exception
     */
    public function mostRecent($limit = 10)
    {
        $result = $this->_retrieve($this->_params['table'],
                                   '',
                                   'version_created DESC',
                                   $limit);
        $result2 = $this->_retrieve($this->_params['historytable'],
                                    '',
                                    'version_created DESC',
                                    $limit);
        $result = array_merge($result, $result2);
        usort(
            $result,
            function($a, $b)
            {
                return $b['version_created'] - $a['version_created'];
            }
        );
        return array_slice($result, 0, $limit);
    }

    /**
     * Returns the most popular pages.
     *
     * @param integer $limit  The number of most popular pages to return.
     *
     * @return array  Pages.
     * @throws Wicked_Exception
     */
    public 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
     */
    public function leastPopular($limit = 10)
    {
        return $this->_retrieve($this->_params['table'], '',
                                'page_hits ASC', $limit);
    }

    public function searchTitles($searchtext)
    {
        $searchtext = $this->_convertToDriver($searchtext);
        try {
            $where = $this->_db->buildClause('page_name', 'LIKE', $searchtext);
        } catch (Horde_Db_Exception $e) {
            throw new Wicked_Exception($e);
        }
        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      Search both page title and text?
     *
     * @return array  A list of pages.
     * @throws Wicked_Exception
     */
    public function searchText($searchtext, $title = true)
    {
        $searchtext = $this->_convertToDriver($searchtext);

        try {
            $textClause = Horde_Db_SearchParser::parse('page_text', $searchtext);
        } catch (Horde_Db_Exception $e) {
            throw new Wicked_Exception($e);
        }

        if ($title) {
            try {
                $nameClause = Horde_Db_SearchParser::parse('page_name', $searchtext);
            } catch (Horde_Db_Exception $e) {
                throw new Wicked_Exception($e);
            }

            $where = '(' . $nameClause . ') OR (' . $textClause . ')';
        } else {
            $where = $textClause;
        }

        return $this->_retrieve($this->_params['table'], $where);
    }

    public function getBackLinks($pagename)
    {
        try {
            $where = $this->_db->buildClause(
                'page_text', 'LIKE', $this->_convertToDriver($pagename));
        } catch (Horde_Db_Exception $e) {
            throw new Wicked_Exception($e);
        }
        $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. */
        /* @todo this should match the current wiki engine's syntax. */
        $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;
                    break;
                }
            }
            if (!$match) {
                unset($pages[$key]);
            }
        }

        return $pages;
    }

    public function getMatchingPages($searchtext,
                                     $matchType = Wicked_Page::MATCH_ANY)
    {
        $searchtext = strtolower($searchtext);

        try {
            /* Short circuit the simple case. */
            if ($matchType == Wicked_Page::MATCH_ANY) {
                return $this->_retrieve(
                    $this->_params['table'],
                    'LOWER(page_name) LIKE ' . $this->_db->quote('%' . $searchtext . '%'));
            }

            $clauses = array();
            if ($matchType & Wicked_Page::MATCH_LEFT) {
                $clauses[] = 'LOWER(page_name) LIKE ' . $this->_db->quote($searchtext . '%');
            }
            if ($matchType & Wicked_Page::MATCH_RIGHT) {
                $clauses[] = 'LOWER(page_name) LIKE ' . $this->_db->quote('%' . $searchtext);
            }
        } catch (Horde_Db_Exception $e) {
            throw new Wicked_Exception($e);
        }

        if (!$clauses) {
            return array();
        }

        return $this->_retrieve($this->_params['table'],
                                implode(' OR ', $clauses));
    }

    public 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-z0-9]*/', $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;
                    }
                }
            }
        }

        try {
            $where = $this->_db->buildClause('page_name', 'LIKE', $firstword);
            if (!empty($lastword) && $lastword != $firstword) {
                $where .= ' OR ' . $this->_db->buildClause('page_name', 'LIKE', $lastword);
            }
        } catch (Horde_Db_Exception $e) {
            throw new Wicked_Exception($e);
        }

        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
     */
    public function getAttachedFiles($pageId, $allversions = false)
    {
        $where = array('page_id = ?', array((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,
            function($a, $b)
            {
                if ($res = strcmp($a['attachment_name'], $b['attachment_name'])) {
                    return $res;
                }
                return ($a['attachment_version'] - $b['attachment_version']);
            }
        );

        return $data;
    }

    /**
     * Removes a single version or all versions of an attachment from
     * $pageId.
     *
     * @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
     */
    public 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((int)$pageId, $attachment);
        if (!is_null($version)) {
            $sql .= ' AND attachment_version = ?';
            $params[] = (int)$version;
        }

        try {
            $this->_db->beginDbTransaction();
            $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_version = ?';
            }
            $this->_db->delete($sql, $params);
            $this->_db->commitDbTransaction();
        } catch (Horde_Db_Exception $e) {
            $this->_db->rollbackDbTransaction();
            throw new Wicked_Exception($e);
        }
    }

    /**
     * Removes all attachments from a page.
     *
     * @param integer $pageId  A page ID.
     *
     * @throws Wicked_Exception
     */
    public function removeAllAttachments($pageId)
    {
        /* Try to delete from the VFS first. */
        $result = parent::removeAllAttachments($pageId);

        $params = array((int)$pageId);
        try {
            $this->_db->beginDbTransaction();
            /* First try against the current attachments table. */
            $result = $this->_db->delete(
                'DELETE FROM ' . $this->_params['attachmenttable']
                . ' WHERE page_id = ?',
                $params);

            /* Now try against the attachment history table. $params is
             * unchanged. */
            $this->_db->delete(
                'DELETE FROM ' . $this->_params['attachmenthistorytable']
                . ' WHERE page_id = ?',
                $params);
            $this->_db->commitDbTransaction();
        } catch (Horde_Db_Exception $e) {
            $this->_db->rollbackDbTransaction();
            throw new Wicked_Exception($e);
        }
    }

    /**
     * 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.
     *
     * @param array $file  See Wicked_Driver::attachFile().
     *
     * @return integer  The new version of the file attached.
     * @throws Wicked_Exception
     */
    protected function _attachFile($file)
    {
        if ($file['change_author'] === false) {
            $file['change_author'] = null;
        }

        $attachments = $this->_retrieve(
            $this->_params['attachmenttable'],
            array('page_id = ? AND attachment_name = ?',
                  array((int)$file['page_id'], $file['attachment_name'])));

        if ($attachments) {
            $version = $attachments[0]['attachment_version'] + 1;

            try {
                $this->_db->beginDbTransaction();
                $this->_db->insert(
                    sprintf('INSERT INTO %s (page_id, attachment_name, attachment_version, attachment_created, change_author, change_log) SELECT page_id, attachment_name, attachment_version, attachment_created, change_author, change_log FROM %s WHERE page_id = ? AND attachment_name = ?',
                            $this->_params['attachmenthistorytable'],
                            $this->_params['attachmenttable']),
                    array((int)$file['page_id'],
                          $file['attachment_name']));

                $this->_db->update(
                    sprintf('UPDATE %s SET attachment_version = ?, change_log = ?, change_author = ?, attachment_created = ? WHERE page_id = ? AND attachment_name = ?',
                            $this->_params['attachmenttable']),
                    array((int)$version,
                          $this->_convertToDriver($file['change_log']),
                          $this->_convertToDriver($file['change_author']),
                          time(),
                          (int)$file['page_id'],
                          $this->_convertToDriver($file['attachment_name'])));
                $this->_db->commitDbTransaction();
            } catch (Horde_Db_Exception $e) {
                $this->_db->rollbackDbTransaction();
                throw new Wicked_Exception($e);
            }
        } else {
            $version = 1;
            try {
                $this->_db->insert(
                    sprintf('INSERT INTO %s (page_id, attachment_version, change_log, change_author, attachment_created, attachment_name) VALUES (?, 1, ?, ?, ?, ?)',
                            $this->_params['attachmenttable']),
                    array((int)$file['page_id'],
                          $this->_convertToDriver($file['change_log']),
                          $this->_convertToDriver($file['change_author']),
                          time(),
                          $this->_convertToDriver($file['attachment_name'])));
            } catch (Horde_Db_Exception $e) {
                throw new Wicked_Exception($e);
            }
        }

        return $version;
    }

    /**
     * Logs a page view.
     *
     * @param string $pagename  The page that was viewed.
     *
     * @throws Wicked_Exception
     */
    public function logPageView($pagename)
    {
        try {
            return $this->_db->update(
                'UPDATE ' . $this->_params['table']
                . ' SET page_hits = page_hits + 1 WHERE page_name = ?',
                array($this->_convertToDriver($pagename)));
        } catch (Horde_Db_Exception $e) {
            throw new Wicked_Exception($e);
        }
    }

    /**
     * Logs an attachment download.
     *
     * @param integer $pageid     The page with the attachment.
     * @param string $attachment  The attachment name.
     *
     * @throws Wicked_Exception
     */
    public function logAttachmentDownload($pageid, $attachment)
    {
        try {
            return $this->_db->update(
                'UPDATE ' . $this->_params['attachmenttable']
                . ' SET attachment_hits = attachment_hits + 1'
                . ' WHERE page_id = ? AND attachment_name = ?',
                array((int)$pageid, $this->_convertToDriver($attachment)));
        } catch (Horde_Db_Exception $e) {
            throw new Wicked_Exception($e);
        }
    }

    /**
     * Creates a new page.
     *
     * @param string $pagename  The new page's name.
     * @param string $text      The new page's text.
     *
     * @throws Wicked_Exception
     */
    public 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;
        }

        /* Attempt the insertion/update query. */
        try {
            $page_id = $this->_db->insert(
                'INSERT INTO ' . $this->_params['table']
                . ' (page_name, page_text, version_created, page_version,'
                . ' page_hits, change_author) VALUES (?, ?, ?, 1, 0, ?)',
                array($this->_convertToDriver($pagename),
                      $this->_convertToDriver($text),
                      time(),
                      $author));
        } catch (Horde_Db_Exception $e) {
            throw new Wicked_Exception($e);
        }

        /* 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;
    }

    /**
     * Renames a page, keeping 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
     */
    public function renamePage($pagename, $newname)
    {
        try {
            $this->_db->beginDbTransaction();
            $this->_db->update(
                'UPDATE ' . $this->_params['table']
                . ' SET page_name = ? WHERE page_name = ?',
                array($this->_convertToDriver($newname),
                      $this->_convertToDriver($pagename)));

            $this->_db->update(
                'UPDATE ' . $this->_params['historytable']
                . ' SET page_name = ? WHERE page_name = ?',
                array($this->_convertToDriver($newname),
                      $this->_convertToDriver($pagename)));
            $this->_db->commitDbTransaction();
        } catch (Horde_Db_Exception $e) {
            $this->_db->rollbackDbTransaction();
            throw new Wicked_Exception($e);
        }

        $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);
    }

    public function updateText($pagename, $text, $changelog)
    {
        if (!$this->pageExists($pagename)) {
            return $this->newPage($pagename, $text);
        }

        /* Copy the old version into the page history. */
        Horde::logMessage('Page ' . $pagename . ' saved with user agent ' . $GLOBALS['browser']->getAgentString(), 'DEBUG');

        $author = $GLOBALS['registry']->getAuth();
        if ($author === false) {
            $author = null;
        }

        try {
            $this->_db->beginDbTransaction();
            $this->_db->insert(
                sprintf('INSERT INTO %s (page_id, page_name, page_text, page_version, version_created, change_author, change_log) SELECT page_id, page_name, page_text, page_version, version_created, change_author, change_log FROM %s WHERE page_name = ?',
                        $this->_params['historytable'],
                        $this->_params['table']),
                array($this->_convertToDriver($pagename)));

            /* Now move on to updating the record. */
            $this->_db->update(
                'UPDATE ' . $this->_params['table']
                . ' SET change_author = ?, page_text = ?, change_log = ?,'
                . ' version_created = ?, page_version = page_version + 1'
                . ' WHERE page_name = ?',
                array($author,
                      $this->_convertToDriver($text),
                      $this->_convertToDriver($changelog),
                      time(),
                      $this->_convertToDriver($pagename)));
            $this->_db->commitDbTransaction();
        } catch (Horde_Db_Exception $e) {
            $this->_db->rollbackDbTransaction();
            throw new Wicked_Exception($e);
        }
    }

    public function getPages($special = true, $no_cache = false)
    {
        if (!isset($this->_pageNames) || $no_cache) {
            try {
                $result = $this->_db->selectAssoc(
                    'SELECT page_id, page_name FROM ' . $this->_params['table']);
            } catch (Horde_Db_Exception $e) {
                throw new Wicked_Exception($e);
            }
            $this->_pageNames = $this->_convertFromDriver($result);
        }

        if ($special) {
            return $this->_pageNames + $this->getSpecialPages();
        }

        return $this->_pageNames;
    }

    /**
     */
    public function removeVersion($pagename, $version)
    {
        $values = array($this->_convertToDriver($pagename), (int)$version);

        /* We need to know if we're deleting the current version. */
        try {
            $result = $this->_db->selectValue(
                'SELECT 1 FROM ' . $this->_params['table']
                . ' WHERE page_name = ? AND page_version = ?',
                $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. */
            try {
                $this->_db->delete(
                    'DELETE FROM ' . $this->_params['historytable']
                    . ' WHERE page_name = ? and page_version = ?',
                    $values);
            } catch (Horde_Db_Exception $e) {
                throw new Wicked_Exception($e);
            }
            return;
        }

        /* We're deleting the current version. Have to promote the next-most
         * revision from the history table. */
        try {
            $query = 'SELECT * FROM ' . $this->_params['historytable'] .
                ' WHERE page_name = ? ORDER BY page_version DESC';
            $query = $this->_db->addLimitOffset($query, array('limit' => 1));
            $revision = $this->_db->selectOne(
                $query, array($this->_convertToDriver($pagename)));

            /* Replace the current version of the page with the version being
             * promoted. */
            $this->_db->beginDbTransaction();
            $this->_db->update(
                'UPDATE ' . $this->_params['table'] . ' SET' .
                ' page_text = ?, page_version = ?,' .
                ' version_created = ?, change_author = ?, change_log = ?' .
                ' WHERE page_name = ?',
                array($revision['page_text'],
                      (int)$revision['page_version'],
                      (int)$revision['version_created'],
                      $revision['change_author'],
                      $revision['change_log'],
                      $this->_convertToDriver($pagename)));

            /* Finally, remove the version that we promoted from the history
             * table. */
            $this->_db->delete(
                'DELETE FROM ' . $this->_params['historytable'] .
                ' WHERE page_name = ? and page_version = ?',
                array($this->_convertToDriver($pagename),
                      (int)$revision['page_version']));
            $this->_db->commitDbTransaction();
        } catch (Horde_Db_Exception $e) {
            $this->_db->rollbackDbTransaction();
            throw new Wicked_Exception($e);
        }
    }

    /**
     */
    public function removeAllVersions($pagename)
    {
        /* Remove attachments and do other cleanup. */
        parent::removeAllVersions($pagename);

        $this->_pageNames = null;

        try {
            $this->_db->beginDbTransaction();
            $this->_db->delete(
                'DELETE FROM ' . $this->_params['table']
                . ' WHERE page_name = ?',
                array($this->_convertToDriver($pagename)));

            $this->_db->delete(
                'DELETE FROM ' . $this->_params['historytable']
                . ' WHERE page_name = ?',
                array($this->_convertToDriver($pagename)));
            $this->_db->commitDbTransaction();
        } catch (Horde_Db_Exception $e) {
            $this->_db->rollbackDbTransaction();
            throw new Wicked_Exception($e);
        }
    }

    /**
     * Retrieves a set of pages matching an SQL WHERE clause.
     *
     * @param string $table        Table to retrieve pages from.
     * @param array|string $where  Where clause for sql statement (without the
     *                             'WHERE'). If an array the 1st element is the
     *                             clause with placeholder, the 2nd element the
     *                             values.
     * @param string $orderBy      Order results by this column.
     * @param integer $limit       Maximum number of pages to fetch.
     *
     * @return array  A list of page hashes.
     * @throws Wicked_Exception
     */
    protected function _retrieve($table, $where, $orderBy = null, $limit = null)
    {
        $query = 'SELECT * FROM ' . $table;
        $values = array();
        if (!empty($where)) { 
            $query .= ' WHERE ';
            if (is_array($where)) {
                $query .= $where[0];
                $values = $where[1];
            } else {
                $query .= $where;
            }
        }
        if (!empty($orderBy)) {
            $query .= ' ORDER BY ' . $orderBy;
        }
        if (!empty($limit)) {
            try {
                $query = $this->_db->addLimitOffset($query, array('limit' => $limit));
            } catch (Horde_Db_Exception $e) {
                throw new Wicked_Exception($e);
            }
        }

        try {
            $result = $this->_db->select($query, $values);
        } catch (Horde_Db_Exception $e) {
            throw new Wicked_Exception($e);
        }

        $pages = array();
        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
     */
    public function getCharset()
    {
        return $this->_db->getOption('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.
     */
    protected 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.
     */
    protected function _convertToDriver($value)
    {
        return Horde_String::convertCharset($value, 'UTF-8', $this->getCharset());
    }
}