4 * Copyright 2005 $ThePhpWikiProgrammingTeam
6 * This file is part of PhpWiki.
8 * PhpWiki is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * PhpWiki is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * @author: Reini Urban
27 require_once 'lib/WikiDB/backend.php';
29 class WikiDB_backend_PDO
30 extends WikiDB_backend
33 function __construct($dbparams)
35 $this->_dbparams = $dbparams;
36 if (strstr($dbparams['dsn'], "://")) { // pear DB syntax
37 $parsed = parseDSN($dbparams['dsn']);
38 $this->_parsedDSN = $parsed;
39 /* Need to convert the DSN
40 * dbtype(dbsyntax)://username:password@protocol+hostspec/database?option=value&option2=value2
41 * to dbtype:option1=value1;option2=value2
42 * PDO passes the DSN verbatim to the driver. But we need to extract the username + password
43 * and cannot rely that the parseDSN keys have the same name as needed by the driver.
44 * e.g: odbc:DSN=SAMPLE;UID=db2inst1;PWD=ibmdb2, mysql:host=127.0.0.1;dbname=testdb
46 $driver = $parsed['phptype'];
47 unset($parsed['phptype']);
48 unset($parsed['dbsyntax']);
49 $dbparams['dsn'] = $driver . ":";
50 $this->_dbh->database = $parsed['database'];
51 // mysql needs to map database=>dbname, hostspec=>host. TODO for the others.
52 $dsnmap = array('mysql' => array('database' => 'dbname', 'hostspec' => 'host')
54 foreach (array('protocol', 'hostspec', 'port', 'socket', 'database') as $option) {
55 if (!empty($parsed[$option])) {
56 $optionname = (isset($dsnmap[$driver][$option]) and !isset($parsed[$optionname]))
57 ? $dsnmap[$driver][$option]
59 $dbparams['dsn'] .= ($optionname . "=" . $parsed[$option] . ";");
60 unset($parsed[$optionname]);
62 unset($parsed[$option]);
64 unset($parsed['username']);
65 unset($parsed['password']);
66 // pass the rest verbatim to the driver.
67 foreach ($parsed as $option => $value) {
68 $dbparams['dsn'] .= ($option . "=" . $value . ";");
71 list($driver, $dsn) = explode(":", $dbparams['dsn'], 2);
72 foreach (explode(";", trim($dsn)) as $pair) {
74 list($option, $value) = explode("=", $pair, 2);
75 $this->_parsedDSN[$option] = $value;
78 $this->_dbh->database = isset($this->_parsedDSN['database'])
79 ? $this->_parsedDSN['database']
80 : $this->_parsedDSN['dbname'];
82 if (empty($this->_parsedDSN['password'])) $this->_parsedDSN['password'] = '';
85 // try to load it dynamically (unix only)
86 if (!loadPhpExtension("pdo_$driver")) {
87 echo $GLOBALS['php_errormsg'], "<br>\n";
88 trigger_error(sprintf("dl() problem: Required extension '%s' could not be loaded!",
93 // persistent is defined as DSN option, or with a config value.
94 // phptype://username:password@hostspec/database?persistent=false
95 $this->_dbh = new PDO($dbparams['dsn'],
96 $this->_parsedDSN['username'],
97 $this->_parsedDSN['password'],
98 array(PDO::ATTR_AUTOCOMMIT => true,
99 PDO::ATTR_TIMEOUT => DATABASE_TIMEOUT,
100 PDO::ATTR_PERSISTENT => !empty($parsed['persistent'])
101 or DATABASE_PERSISTENT
103 } catch (PDOException $e) {
104 echo "<br>\nCan't connect to database: %s" . $e->getMessage();
105 if (DEBUG & _DEBUG_VERBOSE or DEBUG & _DEBUG_SQL) {
106 echo "<br>\nDSN: '", $dbparams['dsn'], "'";
107 echo "<br>\n_parsedDSN: '", print_r($this->_parsedDSN), "'";
108 echo "<br>\nparsed: '", print_r($parsed), "'";
110 if (isset($dbparams['_tryroot_from_upgrade']))
111 trigger_error(sprintf("Can't connect to database: %s", $e->getMessage()),
116 if (DEBUG & _DEBUG_SQL) { // not yet implemented
117 $this->_dbh->debug = true;
119 $this->_dsn = $dbparams['dsn'];
120 $this->_dbh->databaseType = $driver;
121 $this->_dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
122 // Use the faster FETCH_NUM, with some special ASSOC based exceptions.
124 $this->_hasTransactions = true;
126 $this->_dbh->beginTransaction();
127 } catch (PDOException $e) {
128 $this->_hasTransactions = false;
130 $sth = $this->_dbh->prepare("SELECT version()");
132 $this->_serverinfo['version'] = $sth->fetchColumn();
133 $this->commit(); // required to match the try catch block above!
135 $prefix = isset($dbparams['prefix']) ? $dbparams['prefix'] : '';
137 = array('page_tbl' => $prefix . 'page',
138 'version_tbl' => $prefix . 'version',
139 'link_tbl' => $prefix . 'link',
140 'recent_tbl' => $prefix . 'recent',
141 'nonempty_tbl' => $prefix . 'nonempty');
142 $page_tbl = $this->_table_names['page_tbl'];
143 $version_tbl = $this->_table_names['version_tbl'];
144 $this->page_tbl_fields = "$page_tbl.id AS id, $page_tbl.pagename AS pagename, "
145 . "$page_tbl.hits hits";
146 $this->page_tbl_field_list = array('id', 'pagename', 'hits');
147 $this->version_tbl_fields = "$version_tbl.version AS version, "
148 . "$version_tbl.mtime AS mtime, "
149 . "$version_tbl.minor_edit AS minor_edit, $version_tbl.content AS content, "
150 . "$version_tbl.versiondata AS versiondata";
151 $this->version_tbl_field_list = array('version', 'mtime', 'minor_edit', 'content',
155 = array('maxmajor' => "MAX(CASE WHEN minor_edit=0 THEN version END)",
156 'maxminor' => "MAX(CASE WHEN minor_edit<>0 THEN version END)",
157 'maxversion' => "MAX(version)",
158 'notempty' => "<>''",
159 'iscontent' => "$version_tbl.content<>''");
160 $this->_lock_count = 0;
163 function beginTransaction()
165 if ($this->_hasTransactions)
166 $this->_dbh->beginTransaction();
171 if ($this->_hasTransactions)
172 $this->_dbh->commit();
177 if ($this->_hasTransactions)
178 $this->_dbh->rollback();
184 $sth = $this->_dbh->prepare($sql);
185 return $sth->execute();
188 /* with one result row */
189 function getRow($sql)
191 $sth = $this->_dbh->prepare($sql);
193 return $sth->fetch(PDO::FETCH_BOTH);
199 * Close database connection.
205 if ($this->_lock_count) {
206 trigger_error("WARNING: database still locked " .
207 '(lock_count = $this->_lock_count)' . "\n<br />",
210 $this->unlock(array(), 'force');
216 * Fast test for wikipage.
218 function is_wiki_page($pagename)
221 extract($this->_table_names);
222 $sth = $dbh->prepare("SELECT $page_tbl.id AS id"
223 . " FROM $nonempty_tbl, $page_tbl"
224 . " WHERE $nonempty_tbl.id=$page_tbl.id"
225 . " AND pagename=?");
226 $sth->bindParam(1, $pagename, PDO::PARAM_STR, 100);
228 return $sth->fetchColumn();
233 function get_all_pagenames()
236 extract($this->_table_names);
237 $sth = $dbh->exec("SELECT pagename"
238 . " FROM $nonempty_tbl, $page_tbl"
239 . " WHERE $nonempty_tbl.id=$page_tbl.id"
241 return $sth->fetchAll(PDO::FETCH_NUM);
245 * filter (nonempty pages) currently ignored
247 function numPages($filter = false, $exclude = '')
250 extract($this->_table_names);
251 $sth = $dbh->exec("SELECT count(*)"
252 . " FROM $nonempty_tbl, $page_tbl"
253 . " WHERE $nonempty_tbl.id=$page_tbl.id");
254 return $sth->fetchColumn();
257 function increaseHitCount($pagename)
260 $page_tbl = $this->_table_names['page_tbl'];
261 $sth = $dbh->prepare("UPDATE $page_tbl SET hits=hits+1"
262 . " WHERE pagename=?"
264 $sth->bindParam(1, $pagename, PDO::PARAM_STR, 100);
269 * Read page information from database.
271 function get_pagedata($pagename)
274 $page_tbl = $this->_table_names['page_tbl'];
275 $sth = $dbh->prepare("SELECT id,pagename,hits,pagedata FROM $page_tbl"
276 . " WHERE pagename=? LIMIT 1");
277 $sth->bindParam(1, $pagename, PDO::PARAM_STR, 100);
279 $row = $sth->fetch(PDO::FETCH_NUM);
280 return $row ? $this->_extract_page_data($row[3], $row[2]) : false;
283 public function _extract_page_data($data, $hits)
286 return array('hits' => $hits);
288 return array_merge(array('hits' => $hits), $this->_unserialize($data));
291 function update_pagedata($pagename, $newdata)
294 $page_tbl = $this->_table_names['page_tbl'];
296 // Hits is the only thing we can update in a fast manner.
297 if (count($newdata) == 1 && isset($newdata['hits'])) {
298 // Note that this will fail silently if the page does not
299 // have a record in the page table. Since it's just the
300 // hit count, who cares?
301 $sth = $dbh->prepare("UPDATE $page_tbl SET hits=? WHERE pagename=? LIMIT 1");
302 $sth->bindParam(1, $newdata['hits'], PDO::PARAM_INT);
303 $sth->bindParam(2, $pagename, PDO::PARAM_STR, 100);
307 $this->beginTransaction();
308 $data = $this->get_pagedata($pagename);
311 $this->_get_pageid($pagename, true); // Creates page record
314 $hits = (empty($data['hits'])) ? 0 : (int)$data['hits'];
315 unset($data['hits']);
317 foreach ($newdata as $key => $val) {
320 else if (empty($val))
325 $sth = $dbh->prepare("UPDATE $page_tbl"
326 . " SET hits=?, pagedata=?"
327 . " WHERE pagename=?"
329 $sth->bindParam(1, $hits, PDO::PARAM_INT);
330 $sth->bindParam(2, $this->_serialize($data), PDO::PARAM_LOB);
331 $sth->bindParam(3, $pagename, PDO::PARAM_STR, 100);
332 if ($sth->execute()) {
341 function get_cached_html($pagename)
344 $page_tbl = $this->_table_names['page_tbl'];
345 $sth = $dbh->prepare("SELECT cached_html FROM $page_tbl WHERE pagename=? LIMIT 1");
346 $sth->bindParam(1, $pagename, PDO::PARAM_STR, 100);
348 return $sth->fetchColumn(PDO::FETCH_NUM);
351 function set_cached_html($pagename, $data)
354 $page_tbl = $this->_table_names['page_tbl'];
355 if (empty($data)) $data = '';
356 $sth = $dbh->prepare("UPDATE $page_tbl"
357 . " SET cached_html=?"
358 . " WHERE pagename=?"
360 $sth->bindParam(1, $data, PDO::PARAM_STR);
361 $sth->bindParam(2, $pagename, PDO::PARAM_STR, 100);
365 function _get_pageid($pagename, $create_if_missing = false)
369 $cache =& $request->_dbi->_cache->_id_cache;
370 if (isset($cache[$pagename])) {
371 if ($cache[$pagename] or !$create_if_missing) {
372 return $cache[$pagename];
376 // attributes play this game.
377 if ($pagename === '') return 0;
380 $page_tbl = $this->_table_names['page_tbl'];
381 $sth = $dbh->prepare("SELECT id FROM $page_tbl WHERE pagename=? LIMIT 1");
382 $sth->bindParam(1, $pagename, PDO::PARAM_STR, 100);
383 $id = $sth->fetchColumn();
384 if (!$create_if_missing) {
388 //mysql, mysqli or mysqlt
389 if (substr($dbh->databaseType, 0, 5) == 'mysql') {
390 // have auto-incrementing, atomic version
391 $sth = $dbh->prepare("INSERT INTO $page_tbl"
393 . " VALUES (NULL,?)");
394 $sth->bindParam(1, $pagename, PDO::PARAM_STR, 100);
396 $id = $dbh->lastInsertId();
398 $this->beginTransaction();
399 $sth = $dbh->prepare("SELECT MAX(id) FROM $page_tbl");
401 $id = $sth->fetchColumn();
402 $sth = $dbh->prepare("INSERT INTO $page_tbl"
403 . " (id,pagename,hits)"
404 . " VALUES (?,?,0)");
406 $sth->bindParam(1, $id, PDO::PARAM_INT);
407 $sth->bindParam(2, $pagename, PDO::PARAM_STR, 100);
418 function get_latest_version($pagename)
421 extract($this->_table_names);
422 $sth = $dbh->prepare("SELECT latestversion"
423 . " FROM $page_tbl, $recent_tbl"
424 . " WHERE $page_tbl.id=$recent_tbl.id"
427 $sth->bindParam(1, $pagename, PDO::PARAM_STR, 100);
429 return $sth->fetchColumn();
432 function get_previous_version($pagename, $version)
435 extract($this->_table_names);
436 $sth = $dbh->prepare("SELECT version"
437 . " FROM $version_tbl, $page_tbl"
438 . " WHERE $version_tbl.id=$page_tbl.id"
441 . " ORDER BY version DESC"
443 $sth->bindParam(1, $pagename, PDO::PARAM_STR, 100);
444 $sth->bindParam(2, $version, PDO::PARAM_INT);
446 return $sth->fetchColumn();
452 * @param string $pagename Name of the page
453 * @param int $version Which version to get
454 * @param bool $want_content Do we need content?
456 * @return array hash The version data, or false if specified version does not
459 function get_versiondata($pagename, $version, $want_content = false)
462 extract($this->_table_names);
463 extract($this->_expressions);
465 assert(is_string($pagename) and $pagename != '');
466 assert($version > 0);
468 // FIXME: optimization: sometimes don't get page data?
470 $fields = $this->page_tbl_fields . ", $page_tbl.pagedata AS pagedata"
471 . ', ' . $this->version_tbl_fields;
473 $fields = $this->page_tbl_fields . ", '' AS pagedata"
474 . ", $version_tbl.version AS version, $version_tbl.mtime AS mtime, "
475 . "$version_tbl.minor_edit AS minor_edit, $iscontent AS have_content, "
476 . "$version_tbl.versiondata as versiondata";
478 $sth = $dbh->prepare("SELECT $fields"
479 . " FROM $page_tbl, $version_tbl"
480 . " WHERE $page_tbl.id=$version_tbl.id"
484 $sth->bindParam(1, $pagename, PDO::PARAM_STR, 100);
485 $sth->bindParam(2, $version, PDO::PARAM_INT);
487 $row = $sth->fetch(PDO::FETCH_NUM);
488 return $row ? $this->_extract_version_data_num($row, $want_content) : false;
491 private function _extract_version_data_num($row, $want_content)
497 //$pagename &= $row[1];
498 $data = empty($row[8]) ? array() : $this->_unserialize($row[8]);
499 $data['mtime'] = $row[5];
500 $data['is_minor_edit'] = !empty($row[6]);
502 $data['%content'] = $row[7];
504 $data['%content'] = !empty($row[7]);
506 if (!empty($row[3])) {
507 $data['%pagedata'] = $this->_extract_page_data($row[3], $row[2]);
512 function _extract_version_data_assoc($row)
518 $data = empty($versiondata) ? array() : $this->_unserialize($versiondata);
519 $data['mtime'] = $mtime;
520 $data['is_minor_edit'] = !empty($minor_edit);
522 $data['%content'] = $content;
523 elseif ($have_content)
524 $data['%content'] = true; else
525 $data['%content'] = '';
526 if (!empty($pagedata)) {
527 $data['%pagedata'] = $this->_extract_page_data($pagedata, $hits);
533 * Create a new revision of a page.
535 function set_versiondata($pagename, $version, $data)
538 $version_tbl = $this->_table_names['version_tbl'];
540 $minor_edit = (int)!empty($data['is_minor_edit']);
541 unset($data['is_minor_edit']);
543 $mtime = (int)$data['mtime'];
544 unset($data['mtime']);
545 assert(!empty($mtime));
547 $content = isset($data['%content']) ? (string)$data['%content'] : '';
548 unset($data['%content']);
549 unset($data['%pagedata']);
551 $this->lock(array('page', 'recent', 'version', 'nonempty'));
552 $this->beginTransaction();
553 $id = $this->_get_pageid($pagename, true);
554 $backend_type = $this->backendType();
555 // optimize: mysql can do this with one REPLACE INTO.
556 if (substr($backend_type, 0, 5) == 'mysql') {
557 $sth = $dbh->prepare("REPLACE INTO $version_tbl"
558 . " (id,version,mtime,minor_edit,content,versiondata)"
559 . " VALUES(?,?,?,?,?,?)");
560 $sth->bindParam(1, $id, PDO::PARAM_INT);
561 $sth->bindParam(2, $version, PDO::PARAM_INT);
562 $sth->bindParam(3, $mtime, PDO::PARAM_INT);
563 $sth->bindParam(4, $minor_edit, PDO::PARAM_INT);
564 $sth->bindParam(5, $content, PDO::PARAM_STR, 100);
565 $sth->bindParam(6, $this->_serialize($data), PDO::PARAM_STR, 100);
566 $rs = $sth->execute();
568 $sth = $dbh->prepare("DELETE FROM $version_tbl"
569 . " WHERE id=? AND version=?");
570 $sth->bindParam(1, $id, PDO::PARAM_INT);
571 $sth->bindParam(2, $version, PDO::PARAM_INT);
573 $sth = $dbh->prepare("INSERT INTO $version_tbl"
574 . " (id,version,mtime,minor_edit,content,versiondata)"
575 . " VALUES(?,?,?,?,?,?)");
576 $sth->bindParam(1, $id, PDO::PARAM_INT);
577 $sth->bindParam(2, $version, PDO::PARAM_INT);
578 $sth->bindParam(3, $mtime, PDO::PARAM_INT);
579 $sth->bindParam(4, $minor_edit, PDO::PARAM_INT);
580 $sth->bindParam(5, $content, PDO::PARAM_STR, 100);
581 $sth->bindParam(6, $this->_serialize($data), PDO::PARAM_STR, 100);
582 $rs = $sth->execute();
584 $this->_update_recent_table($id);
585 $this->_update_nonempty_table($id);
586 if ($rs) $this->commit();
587 else $this->rollBack();
588 $this->unlock(array('page', 'recent', 'version', 'nonempty'));
592 * Delete an old revision of a page.
594 function delete_versiondata($pagename, $version)
597 extract($this->_table_names);
599 $this->lock(array('version'));
600 if (($id = $this->_get_pageid($pagename))) {
601 $dbh->query("DELETE FROM $version_tbl"
602 . " WHERE id=$id AND version=$version");
603 $this->_update_recent_table($id);
604 // This shouldn't be needed (as long as the latestversion
605 // never gets deleted.) But, let's be safe.
606 $this->_update_nonempty_table($id);
608 $this->unlock(array('version'));
612 * Delete page from the database with backup possibility.
613 * i.e save_page('') and DELETE nonempty id
615 * deletePage increments latestversion in recent to a non-existent version,
616 * and removes the nonempty row,
617 * so that get_latest_version returns id+1 and get_previous_version returns prev id
618 * and page->exists returns false.
620 function delete_page($pagename)
623 * @var WikiRequest $request
628 extract($this->_table_names);
630 $this->beginTransaction();
631 //$dbh->CommitLock($recent_tbl);
632 if (($id = $this->_get_pageid($pagename, false)) === false) {
637 $user =& $request->_user;
638 $meta = array('author' => $user->getId(),
639 'author_id' => $user->getAuthenticatedId(),
641 $this->lock(array('version', 'recent', 'nonempty', 'page', 'link'));
642 $version = $this->get_latest_version($pagename);
643 if ($dbh->query("UPDATE $recent_tbl SET latestversion=latestversion+1,"
644 . "latestmajor=latestversion+1,latestminor=NULL WHERE id=$id")
646 $insert = $dbh->prepare("INSERT INTO $version_tbl"
647 . " (id,version,mtime,minor_edit,content,versiondata)"
648 . " VALUES(?,?,?,?,?,?)");
649 $version_plus_1 = $version + 1;
652 $insert->bindParam(1, $id, PDO::PARAM_INT);
653 $insert->bindParam(2, $version_plus_1, PDO::PARAM_INT);
654 $insert->bindParam(3, $mtime, PDO::PARAM_INT);
655 $insert->bindParam(4, $zero, PDO::PARAM_INT);
656 $insert->bindParam(5, $empty_string, PDO::PARAM_STR, 100);
657 $insert->bindParam(6, $this->_serialize($meta), PDO::PARAM_STR, 100);
658 if ($insert->execute()
659 and $dbh->query("DELETE FROM $nonempty_tbl WHERE id=$id")
660 and $this->set_links($pagename, array())
662 // need to keep perms and LOCKED, otherwise you can reset the perm
663 // by action=remove and re-create it with default perms
664 // keep hits but delete meta-data
665 //and $dbh->Execute("UPDATE $page_tbl SET pagedata='' WHERE id=$id")
666 $this->unlock(array('version', 'recent', 'nonempty', 'page', 'link'));
671 $this->unlock(array('version', 'recent', 'nonempty', 'page', 'link'));
679 * Delete page completely from the database.
681 function purge_page($pagename)
684 extract($this->_table_names);
686 $this->lock(array('version', 'recent', 'nonempty', 'page', 'link'));
687 if (($id = $this->_get_pageid($pagename, false))) {
688 $dbh->query("DELETE FROM $version_tbl WHERE id=$id");
689 $dbh->query("DELETE FROM $recent_tbl WHERE id=$id");
690 $dbh->query("DELETE FROM $nonempty_tbl WHERE id=$id");
691 $this->set_links($pagename, array());
692 $sth = $dbh->prepare("SELECT COUNT(*) FROM $link_tbl WHERE linkto=$id");
694 if ($sth->fetchColumn()) {
695 // We're still in the link table (dangling link) so we can't delete this
697 $dbh->query("UPDATE $page_tbl SET hits=0, pagedata='' WHERE id=$id");
700 $dbh->query("DELETE FROM $page_tbl WHERE id=$id");
704 $result = -1; // already purged or not existing
706 $this->unlock(array('version', 'recent', 'nonempty', 'page', 'link'));
712 * on DEBUG: delete old, deleted links from page
714 function set_links($pagename, $links)
716 // Update link table.
717 // FIXME: optimize: mysql can do this all in one big INSERT/REPLACE.
720 extract($this->_table_names);
722 $this->lock(array('link'));
723 $pageid = $this->_get_pageid($pagename, true);
726 $dbh->query("DELETE FROM $link_tbl WHERE linkfrom=$pageid");
727 foreach ($links as $link) {
728 if (isset($linkseen[$link]))
730 $linkseen[$link] = true;
731 $linkid = $this->_get_pageid($link, true);
733 $dbh->query("INSERT INTO $link_tbl (linkfrom, linkto)"
734 . " VALUES ($pageid, $linkid)");
737 // purge page table: delete all non-referenced pages
738 // for all previously linked pages...
739 $sth = $dbh->prepare("SELECT $link_tbl.linkto as id FROM $link_tbl" .
740 " WHERE linkfrom=$pageid");
742 foreach ($sth->fetchAll(PDO::FETCH_NUM) as $id) {
743 // ...check if the page is empty and has no version
744 $sth1 = $dbh->prepare("SELECT $page_tbl.id FROM $page_tbl"
745 . " LEFT JOIN $nonempty_tbl USING (id) "
746 . " LEFT JOIN $version_tbl USING (id)"
747 . " WHERE ISNULL($nonempty_tbl.id) AND"
748 . " ISNULL($version_tbl.id) AND $page_tbl.id=$id");
750 if ($sth1->fetchColumn()) {
751 $dbh->query("DELETE FROM $page_tbl WHERE id=$id"); // this purges the link
752 $dbh->query("DELETE FROM $recent_tbl WHERE id=$id"); // may fail
755 $dbh->query("DELETE FROM $link_tbl WHERE linkfrom=$pageid");
757 $this->unlock(array('link'));
762 * Find pages which link to or are linked from a page.
764 * Optimization: save request->_dbi->_iwpcache[] to avoid further iswikipage checks
765 * (linkExistingWikiWord or linkUnknownWikiWord)
766 * This is called on every page header GleanDescription, so we can store all the
769 function get_links($pagename, $reversed = true, $include_empty = false,
770 $sortby = '', $limit = '', $exclude = '')
773 extract($this->_table_names);
776 list($have, $want) = array('linkee', 'linker');
778 list($have, $want) = array('linker', 'linkee');
779 $orderby = $this->sortby($sortby, 'db', array('pagename'));
780 if ($orderby) $orderby = " ORDER BY $want." . $orderby;
781 if ($exclude) // array of pagenames
782 $exclude = " AND $want.pagename NOT IN " . $this->_sql_set($exclude);
785 $limit = $this->_limit_sql($limit);
787 $sth = $dbh->prepare("SELECT $want.id AS id, $want.pagename AS pagename,"
788 . " $want.hits AS hits"
789 . " FROM $link_tbl, $page_tbl linker, $page_tbl linkee"
790 . (!$include_empty ? ", $nonempty_tbl" : '')
791 . " WHERE linkfrom=linker.id AND linkto=linkee.id"
792 . " AND $have.pagename=?"
793 . (!$include_empty ? " AND $nonempty_tbl.id=$want.id" : "")
794 //. " GROUP BY $want.id"
798 $sth->bindParam(1, $pagename, PDO::PARAM_STR, 100);
800 $result = $sth->fetch(PDO::FETCH_BOTH);
801 return new WikiDB_backend_PDO_iter($this, $result, $this->page_tbl_field_list);
805 * Find if a page links to another page
807 function exists_link($pagename, $link, $reversed = false)
810 extract($this->_table_names);
813 list($have, $want) = array('linkee', 'linker');
815 list($have, $want) = array('linker', 'linkee');
816 $sth = $dbh->prepare("SELECT CASE WHEN $want.pagename THEN 1 ELSE 0 END"
817 . " FROM $link_tbl, $page_tbl linker, $page_tbl linkee, $nonempty_tbl"
818 . " WHERE linkfrom=linker.id AND linkto=linkee.id"
819 . " AND $have.pagename=?"
820 . " AND $want.pagename=?");
821 $sth->bindParam(1, $pagename, PDO::PARAM_STR, 100);
822 $sth->bindParam(2, $link, PDO::PARAM_STR, 100);
824 return $sth->fetchColumn();
827 public function get_all_pages($include_empty = false,
828 $sortby = '', $limit = '', $exclude = '')
831 extract($this->_table_names);
832 $orderby = $this->sortby($sortby, 'db');
833 if ($orderby) $orderby = ' ORDER BY ' . $orderby;
834 if ($exclude) // array of pagenames
835 $exclude = " AND $page_tbl.pagename NOT IN " . $this->_sql_set($exclude);
838 $limit = $this->_limit_sql($limit);
840 if (strstr($orderby, 'mtime ')) { // was ' mtime'
841 if ($include_empty) {
843 . $this->page_tbl_fields
844 . " FROM $page_tbl, $recent_tbl, $version_tbl"
845 . " WHERE $page_tbl.id=$recent_tbl.id"
846 . " AND $page_tbl.id=$version_tbl.id AND latestversion=version"
851 . $this->page_tbl_fields
852 . " FROM $nonempty_tbl, $page_tbl, $recent_tbl, $version_tbl"
853 . " WHERE $nonempty_tbl.id=$page_tbl.id"
854 . " AND $page_tbl.id=$recent_tbl.id"
855 . " AND $page_tbl.id=$version_tbl.id AND latestversion=version"
860 if ($include_empty) {
862 . $this->page_tbl_fields
864 . ($exclude ? " WHERE $exclude" : '')
868 . $this->page_tbl_fields
869 . " FROM $nonempty_tbl, $page_tbl"
870 . " WHERE $nonempty_tbl.id=$page_tbl.id"
875 $sth = $dbh->prepare($sql . $limit);
877 $result = $sth->fetch(PDO::FETCH_BOTH);
878 return new WikiDB_backend_PDO_iter($this, $result, $this->page_tbl_field_list);
882 * Title and fulltext search.
884 public function text_search($search, $fulltext = false,
885 $sortby = '', $limit = '', $exclude = '')
888 extract($this->_table_names);
889 $orderby = $this->sortby($sortby, 'db');
890 if ($orderby) $orderby = ' ORDER BY ' . $orderby;
891 $limit = $this->_limit_sql($limit);
893 $table = "$nonempty_tbl, $page_tbl";
894 $join_clause = "$nonempty_tbl.id=$page_tbl.id";
895 $fields = $this->page_tbl_fields;
896 $field_list = $this->page_tbl_field_list;
897 $searchobj = new WikiDB_backend_PDO_search($search, $dbh);
900 $table .= ", $recent_tbl";
901 $join_clause .= " AND $page_tbl.id=$recent_tbl.id";
903 $table .= ", $version_tbl";
904 $join_clause .= " AND $page_tbl.id=$version_tbl.id AND latestversion=version";
906 $fields .= ", $page_tbl.pagedata as pagedata, " . $this->version_tbl_fields;
907 $field_list = array_merge($field_list, array('pagedata'), $this->version_tbl_field_list);
908 $callback = new WikiMethodCb($searchobj, "_fulltext_match_clause");
910 $callback = new WikiMethodCb($searchobj, "_pagename_match_clause");
912 $search_clause = $search->makeSqlClauseObj($callback);
913 $sth = $dbh->prepare("SELECT $fields FROM $table"
914 . " WHERE $join_clause"
915 . " AND ($search_clause)"
919 $result = $sth->fetch(PDO::FETCH_NUM);
920 $iter = new WikiDB_backend_PDO_iter($this, $result, $field_list);
921 $iter->stoplisted = $searchobj->stoplisted;
926 * TODO: efficiently handle wildcards exclusion: exclude=Php* => 'Php%',
927 * not sets. See above, but the above methods find too much.
928 * This is only for already resolved wildcards:
929 * " WHERE $page_tbl.pagename NOT IN ".$this->_sql_set(array('page1','page2'));
931 function _sql_set(&$pagenames)
934 foreach ($pagenames as $p) {
935 $s .= ($this->_dbh->qstr($p) . ",");
937 return substr($s, 0, -1) . ")";
941 * Find highest or lowest hit counts.
943 public function most_popular($limit = 20, $sortby = '-hits')
946 extract($this->_table_names);
953 $where = " AND hits > 0";
955 if ($sortby != '-hits') {
956 if ($order = $this->sortby($sortby, 'db')) $orderby = " ORDER BY " . $order;
959 $orderby = " ORDER BY hits $order";
962 . $this->page_tbl_fields
963 . " FROM $nonempty_tbl, $page_tbl"
964 . " WHERE $nonempty_tbl.id=$page_tbl.id"
968 $sth = $dbh->prepare($sql . $this->_limit_sql($limit));
970 $sth = $dbh->prepare($sql);
973 $result = $sth->fetch(PDO::FETCH_NUM);
974 return new WikiDB_backend_PDO_iter($this, $result, $this->page_tbl_field_list);
978 * Find recent changes.
980 public function most_recent($params)
984 $include_minor_revisions = false;
985 $exclude_major_revisions = false;
986 $include_all_revisions = false;
990 extract($this->_table_names);
994 $pick[] = "mtime >= $since";
996 if ($include_all_revisions) {
997 // Include all revisions of each page.
998 $table = "$page_tbl, $version_tbl";
999 $join_clause = "$page_tbl.id=$version_tbl.id";
1001 if ($exclude_major_revisions) {
1002 // Include only minor revisions
1003 $pick[] = "minor_edit <> 0";
1004 } elseif (!$include_minor_revisions) {
1005 // Include only major revisions
1006 $pick[] = "minor_edit = 0";
1009 $table = "$page_tbl, $recent_tbl";
1010 $join_clause = "$page_tbl.id=$recent_tbl.id";
1011 $table .= ", $version_tbl";
1012 $join_clause .= " AND $version_tbl.id=$page_tbl.id";
1014 if ($exclude_major_revisions) {
1015 // Include only most recent minor revision
1016 $pick[] = 'version=latestminor';
1017 } elseif (!$include_minor_revisions) {
1018 // Include only most recent major revision
1019 $pick[] = 'version=latestmajor';
1021 // Include only the latest revision (whether major or minor).
1022 $pick[] = 'version=latestversion';
1030 $where_clause = $join_clause;
1032 $where_clause .= " AND " . join(" AND ", $pick);
1034 . $this->page_tbl_fields . ", " . $this->version_tbl_fields
1036 . " WHERE $where_clause"
1037 . " ORDER BY mtime $order";
1039 $sth = $dbh->prepare($sql . $this->_limit_sql($limit));
1041 $sth = $dbh->prepare($sql);
1044 $result = $sth->fetch(PDO::FETCH_NUM);
1045 return new WikiDB_backend_PDO_iter($this, $result,
1046 array_merge($this->page_tbl_field_list, $this->version_tbl_field_list));
1050 * Find referenced empty pages.
1052 function wanted_pages($exclude_from = '', $exclude = '', $sortby = '', $limit = '')
1054 $dbh = &$this->_dbh;
1055 extract($this->_table_names);
1056 if ($orderby = $this->sortby($sortby, 'db', array('pagename', 'wantedfrom')))
1057 $orderby = 'ORDER BY ' . $orderby;
1059 if ($exclude_from) // array of pagenames
1060 $exclude_from = " AND linked.pagename NOT IN " . $this->_sql_set($exclude_from);
1061 if ($exclude) // array of pagenames
1062 $exclude = " AND $page_tbl.pagename NOT IN " . $this->_sql_set($exclude);
1065 all empty pages, independent of linkstatus:
1066 select pagename as empty from page left join nonempty using(id) where isnull(nonempty.id);
1067 only all empty pages, which have a linkto:
1068 select page.pagename, linked.pagename as wantedfrom from link, page as linked
1069 left join page on(link.linkto=page.id) left join nonempty on(link.linkto=nonempty.id)
1070 where isnull(nonempty.id) and linked.id=link.linkfrom;
1072 $sql = "SELECT p.pagename, pp.pagename AS wantedfrom"
1073 . " FROM $page_tbl p JOIN $link_tbl linked"
1074 . " LEFT JOIN $page_tbl pp ON linked.linkto = pp.id"
1075 . " LEFT JOIN $nonempty_tbl ne ON linked.linkto = ne.id"
1076 . " WHERE ne.id IS NULL"
1077 . " AND p.id = linked.linkfrom"
1082 $sth = $dbh->prepare($sql . $this->_limit_sql($limit));
1084 $sth = $dbh->prepare($sql);
1087 $result = $sth->fetch(PDO::FETCH_NUM);
1088 return new WikiDB_backend_PDO_iter($this, $result, array('pagename', 'wantedfrom'));
1092 * Rename page in the database.
1094 function rename_page($pagename, $to)
1096 $dbh = &$this->_dbh;
1097 extract($this->_table_names);
1099 $this->lock(array('page', 'version', 'recent', 'nonempty', 'link'));
1100 if (($id = $this->_get_pageid($pagename, false))) {
1101 if ($new = $this->_get_pageid($to, false)) {
1103 // This page does not exist (already verified before), but exists in the page table.
1104 // So we delete this page.
1105 $dbh->query("DELETE FROM $page_tbl WHERE id=$new");
1106 $dbh->query("DELETE FROM $version_tbl WHERE id=$new");
1107 $dbh->query("DELETE FROM $recent_tbl WHERE id=$new");
1108 $dbh->query("DELETE FROM $nonempty_tbl WHERE id=$new");
1109 // We have to fix all referring tables to the old id
1110 $dbh->query("UPDATE $link_tbl SET linkfrom=$id WHERE linkfrom=$new");
1111 $dbh->query("UPDATE $link_tbl SET linkto=$id WHERE linkto=$new");
1113 $sth = $dbh->prepare("UPDATE $page_tbl SET pagename=? WHERE id=?");
1114 $sth->bindParam(1, $to, PDO::PARAM_STR, 100);
1115 $sth->bindParam(2, $id, PDO::PARAM_INT);
1118 $this->unlock(array('page'));
1122 function _update_recent_table($pageid = false)
1124 $dbh = &$this->_dbh;
1125 extract($this->_table_names);
1126 extract($this->_expressions);
1128 $pageid = (int)$pageid;
1130 // optimize: mysql can do this with one REPLACE INTO.
1131 $backend_type = $this->backendType();
1132 if (substr($backend_type, 0, 5) == 'mysql') {
1133 $sth = $dbh->prepare("REPLACE INTO $recent_tbl"
1134 . " (id, latestversion, latestmajor, latestminor)"
1135 . " SELECT id, $maxversion, $maxmajor, $maxminor"
1136 . " FROM $version_tbl"
1137 . ($pageid ? " WHERE id=$pageid" : "")
1141 $this->lock(array('recent'));
1142 $sth = $dbh->prepare("DELETE FROM $recent_tbl"
1143 . ($pageid ? " WHERE id=$pageid" : ""));
1145 $sth = $dbh->prepare("INSERT INTO $recent_tbl"
1146 . " (id, latestversion, latestmajor, latestminor)"
1147 . " SELECT id, $maxversion, $maxmajor, $maxminor"
1148 . " FROM $version_tbl"
1149 . ($pageid ? " WHERE id=$pageid" : "")
1152 $this->unlock(array('recent'));
1156 function _update_nonempty_table($pageid = false)
1158 $dbh = &$this->_dbh;
1159 extract($this->_table_names);
1160 extract($this->_expressions);
1162 $pageid = (int)$pageid;
1164 extract($this->_expressions);
1165 $this->lock(array('nonempty'));
1166 $dbh->query("DELETE FROM $nonempty_tbl"
1167 . ($pageid ? " WHERE id=$pageid" : ""));
1168 $dbh->query("INSERT INTO $nonempty_tbl (id)"
1169 . " SELECT $recent_tbl.id"
1170 . " FROM $recent_tbl, $version_tbl"
1171 . " WHERE $recent_tbl.id=$version_tbl.id"
1172 . " AND version=latestversion"
1173 // We have some specifics here (Oracle)
1174 //. " AND content<>''"
1175 . " AND content $notempty" // On Oracle not just "<>''"
1176 . ($pageid ? " AND $recent_tbl.id=$pageid" : ""));
1177 $this->unlock(array('nonempty'));
1181 * Grab a write lock on the tables in the SQL database.
1183 * Calls can be nested. The tables won't be unlocked until
1184 * _unlock_database() is called as many times as _lock_database().
1186 public function lock($tables = array(), $write_lock = true)
1188 if ($this->_lock_count++ == 0) {
1189 $this->_current_lock = $tables;
1190 if (!$this->_hasTransactions)
1191 $this->_lock_tables($tables, $write_lock);
1196 * Overridden by non-transaction safe backends.
1198 protected function _lock_tables($tables, $write_lock = true)
1200 $lock_type = $write_lock ? "WRITE" : "READ";
1201 foreach ($this->_table_names as $key => $table) {
1202 $locks[] = "$table $lock_type";
1204 $this->_dbh->query("LOCK TABLES " . join(",", $locks));
1208 * Release a write lock on the tables in the SQL database.
1210 * @param array $tables
1211 * @param bool $force Unlock even if not every call to lock() has been matched
1212 * by a call to unlock().
1214 * @see _lock_database
1216 public function unlock($tables = array(), $force = false)
1218 if ($this->_lock_count == 0) {
1219 $this->_current_lock = false;
1222 if (--$this->_lock_count <= 0 || $force) {
1223 if (!$this->_hasTransactions)
1224 $this->_unlock_tables();
1225 $this->_current_lock = false;
1226 $this->_lock_count = 0;
1231 * overridden by non-transaction safe backends
1233 protected function _unlock_tables()
1235 $this->_dbh->query("UNLOCK TABLES");
1241 function _serialize($data)
1245 assert(is_array($data));
1246 return serialize($data);
1252 function _unserialize($data)
1254 return empty($data) ? array() : unserialize($data);
1257 /* some variables and functions for DB backend abstraction (action=upgrade) */
1260 return $this->_dbh->database;
1263 function backendType()
1265 return $this->_dbh->databaseType;
1268 function connection()
1270 trigger_error("PDO: connectionID unsupported", E_USER_ERROR);
1274 function listOfTables()
1276 trigger_error("PDO: virtual listOfTables", E_USER_ERROR);
1280 function listOfFields($database, $table)
1282 trigger_error("PDO: virtual listOfFields", E_USER_ERROR);
1287 * LIMIT with OFFSET is not SQL specified.
1288 * mysql: LIMIT $offset, $count
1289 * pgsql,sqlite: LIMIT $count OFFSET $offset
1290 * InterBase,FireBird: ROWS $offset TO $last
1291 * mssql: TOP $rows => TOP $last
1293 * IBM DB2: FetchFirst
1294 * See http://search.cpan.org/dist/SQL-Abstract-Limit/lib/SQL/Abstract/Limit.pm
1296 SELECT field_list FROM $table X WHERE where_clause AND
1298 SELECT COUNT(*) FROM $table WHERE $pk > X.$pk
1300 BETWEEN $offset AND $last
1301 ORDER BY $pk $asc_desc
1303 function _limit_sql($limit = false)
1306 list($offset, $count) = $this->limit($limit);
1308 $limit = " LIMIT $count";
1309 trigger_error("unsupported OFFSET in SQL ignored", E_USER_WARNING);
1311 $limit = " LIMIT $count";
1317 function write_accesslog(&$entry)
1319 $dbh = &$this->_dbh;
1320 $log_tbl = $entry->_accesslog->logtable;
1321 $dbh->prepare("INSERT INTO $log_tbl"
1322 . " (time_stamp,remote_host,remote_user,request_method,request_line,request_args,"
1323 . "request_file,request_uri,request_time,status,bytes_sent,referer,agent,request_duration)"
1324 . " VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)");
1325 // Either use unixtime as %d (long), or the native timestamp format.
1326 $sth->bindParam(1, $entry->time, PDO::PARAM_INT);
1327 $sth->bindParam(2, $entry->host, PDO::PARAM_STR, 100);
1328 $sth->bindParam(3, $entry->user, PDO::PARAM_STR, 50);
1329 $sth->bindParam(4, $entry->request_method, PDO::PARAM_STR, 10);
1330 $sth->bindParam(5, $entry->request, PDO::PARAM_STR, 255);
1331 $sth->bindParam(6, $entry->request_args, PDO::PARAM_STR, 255);
1332 $sth->bindParam(7, $entry->request_uri, PDO::PARAM_STR, 255);
1333 $sth->bindParam(8, $entry->_ncsa_time($entry->time), PDO::PARAM_STR, 28);
1334 $sth->bindParam(9, $entry->time, PDO::PARAM_INT);
1335 $sth->bindParam(10, $entry->status, PDO::PARAM_INT);
1336 $sth->bindParam(11, $entry->size, PDO::PARAM_INT);
1337 $sth->bindParam(12, $entry->referer, PDO::PARAM_STR, 255);
1338 $sth->bindParam(13, $entry->user_agent, PDO::PARAM_STR, 255);
1339 $sth->bindParam(14, $entry->duration, PDO::PARAM_FLOAT);
1344 class WikiDB_backend_PDO_generic_iter
1345 extends WikiDB_backend_iterator
1347 function __construct($backend, $query_result, $field_list = NULL)
1349 $this->_backend = &$backend;
1350 $this->_result = $query_result;
1351 //$this->_fields = $field_list;
1356 if (!is_object($this->_result)) {
1359 $count = $this->_result->rowCount();
1365 $result = &$this->_result;
1366 if (!is_object($result)) {
1369 return $result->fetch(PDO::FETCH_BOTH);
1374 if ($this->_result) {
1375 unset($this->_result);
1380 class WikiDB_backend_PDO_iter
1381 extends WikiDB_backend_PDO_generic_iter
1385 $result = &$this->_result;
1386 if (!is_object($result)) {
1389 $this->_backend = &$backend;
1390 $rec = $result->fetch(PDO::FETCH_ASSOC);
1392 if (isset($rec['pagedata']))
1393 $rec['pagedata'] = $backend->_extract_page_data($rec['pagedata'], $rec['hits']);
1394 if (!empty($rec['version'])) {
1395 $rec['versiondata'] = $backend->_extract_version_data_assoc($rec);
1401 class WikiDB_backend_PDO_search extends WikiDB_backend_search_sql
1405 // Following function taken from Pear::DB (prev. from adodb-pear.inc.php).
1406 // Eventually, change index.php to provide the relevant information
1409 * Parse a data source name.
1411 * Additional keys can be added by appending a URI query string to the
1414 * The format of the supplied DSN is in its fullest form:
1416 * phptype(dbsyntax)://username:password@protocol+hostspec/database?option=8&another=true
1419 * Most variations are allowed:
1421 * phptype://username:password@protocol+hostspec:110//usr/db_file.db?mode=0644
1422 * phptype://username:password@hostspec/database_name
1423 * phptype://username:password@hostspec
1424 * phptype://username@hostspec
1425 * phptype://hostspec/database
1426 * phptype://hostspec
1431 * @param string $dsn Data Source Name to be parsed
1433 * @return array an associative array with the following keys:
1434 * + phptype: Database backend used in PHP (mysql, odbc etc.)
1435 * + dbsyntax: Database used with regards to SQL syntax etc. (ignored with PDO)
1436 * + protocol: Communication protocol to use (tcp, unix, pipe etc.)
1437 * + hostspec: Host specification (hostname[:port])
1438 * + database: Database to use on the DBMS server
1439 * + username: User name for login
1440 * + password: Password for login
1442 * @author Tomas V.V.Cox <cox@idecnet.com>
1444 function parseDSN($dsn)
1448 'dbsyntax' => false,
1449 'username' => false,
1450 'password' => false,
1451 'protocol' => false,
1452 'hostspec' => false,
1455 'database' => false,
1458 if (is_array($dsn)) {
1459 $dsn = array_merge($parsed, $dsn);
1460 if (!$dsn['dbsyntax']) {
1461 $dsn['dbsyntax'] = $dsn['phptype'];
1466 // Find phptype and dbsyntax
1467 if (($pos = strpos($dsn, '://')) !== false) {
1468 $str = substr($dsn, 0, $pos);
1469 $dsn = substr($dsn, $pos + 3);
1475 // Get phptype and dbsyntax
1476 // $str => phptype(dbsyntax)
1477 if (preg_match('|^(.+?)\((.*?)\)$|', $str, $arr)) {
1478 $parsed['phptype'] = $arr[1];
1479 $parsed['dbsyntax'] = !$arr[2] ? $arr[1] : $arr[2];
1481 $parsed['phptype'] = $str;
1482 $parsed['dbsyntax'] = $str;
1489 // Get (if found): username and password
1490 // $dsn => username:password@protocol+hostspec/database
1491 if (($at = strrpos($dsn, '@')) !== false) {
1492 $str = substr($dsn, 0, $at);
1493 $dsn = substr($dsn, $at + 1);
1494 if (($pos = strpos($str, ':')) !== false) {
1495 $parsed['username'] = rawurldecode(substr($str, 0, $pos));
1496 $parsed['password'] = rawurldecode(substr($str, $pos + 1));
1498 $parsed['username'] = rawurldecode($str);
1502 // Find protocol and hostspec
1504 // $dsn => proto(proto_opts)/database
1505 if (preg_match('|^([^(]+)\((.*?)\)/?(.*?)$|', $dsn, $match)) {
1507 $proto_opts = $match[2] ? $match[2] : false;
1510 // $dsn => protocol+hostspec/database (old format)
1512 if (strpos($dsn, '+') !== false) {
1513 list($proto, $dsn) = explode('+', $dsn, 2);
1515 if (strpos($dsn, '/') !== false) {
1516 list($proto_opts, $dsn) = explode('/', $dsn, 2);
1523 // process the different protocol options
1524 $parsed['protocol'] = (!empty($proto)) ? $proto : 'tcp';
1525 $proto_opts = rawurldecode($proto_opts);
1526 if ($parsed['protocol'] == 'tcp') {
1527 if (strpos($proto_opts, ':') !== false) {
1528 list($parsed['hostspec'], $parsed['port']) = explode(':', $proto_opts);
1530 $parsed['hostspec'] = $proto_opts;
1532 } elseif ($parsed['protocol'] == 'unix') {
1533 $parsed['socket'] = $proto_opts;
1536 // Get database if any
1540 if (($pos = strpos($dsn, '?')) === false) {
1541 $parsed['database'] = $dsn;
1542 // /database?param1=value1¶m2=value2
1544 $parsed['database'] = substr($dsn, 0, $pos);
1545 $dsn = substr($dsn, $pos + 1);
1546 if (strpos($dsn, '&') !== false) {
1547 $opts = explode('&', $dsn);
1548 } else { // database?param1=value1
1549 $opts = array($dsn);
1551 foreach ($opts as $opt) {
1552 list($key, $value) = explode('=', $opt);
1553 if (!isset($parsed[$key])) {
1554 // don't allow params overwrite
1555 $parsed[$key] = rawurldecode($value);
1567 // c-basic-offset: 4
1568 // c-hanging-comment-ender-p: nil
1569 // indent-tabs-mode: nil