5 Copyright 2002,2004,2005,2006 $ThePhpWikiProgrammingTeam
7 This file is part of PhpWiki.
9 PhpWiki is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 PhpWiki is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with PhpWiki; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * Based on PearDB.php.
27 * @author: Lawrence Akka, Reini Urban
29 * Now (since phpwiki-1.3.10) with adodb-4.22, by Reini Urban:
30 * 1) Extended to use all available database backends, not only mysql.
31 * 2) It uses the ultra-fast binary adodb extension if loaded.
32 * 3) We use FETCH_NUM instead of FETCH_ASSOC (faster and more generic)
33 * 4) To support generic iterators which return ASSOC fields, and to support queries with
34 * variable columns, some trickery was needed to use recordset specific fetchMode.
35 * The first Execute uses the global fetchMode (ASSOC), then it's resetted back to NUM
36 * and the recordset fetchmode is set to ASSOC.
37 * 5) Transaction support, and locking as fallback.
38 * 6) 2004-12-10 added extra page.cached_html
40 * phpwiki-1.3.11, by Philippe Vanhaesendonck
41 * - pass column list to iterators so we can FETCH_NUM in all cases
42 * phpwiki-1.3.12: get rid of ISNULL
43 * phpwiki-1.3.13: tsearch2 and stored procedures
45 * ADODB basic differences to PearDB: It pre-fetches the first row into fields,
46 * is dirtier in style, layout and more low-level ("worse is better").
47 * It has less needed basic features (modifyQuery, locks, ...), but some more
48 * unneeded features included: paging, monitoring and sessions, and much more drivers.
49 * No locking (which PearDB supports in some backends), and sequences are very
50 * bad compared to PearDB.
52 * Old Comments, by Lawrence Akka:
53 * 1) ADODB's GetRow() is slightly different from that in PEAR. It does not
54 * accept a fetchmode parameter
55 * That doesn't matter too much here, since we only ever use FETCHMODE_ASSOC
56 * 2) No need for ''s around strings in sprintf arguments - qstr puts them
58 * 3) ADODB has a version of GetOne, but it is difficult to use it when
59 * FETCH_ASSOC is in effect.
60 * Instead, use $rs = Execute($query); $value = $rs->fields["$colname"]
61 * 4) No error handling yet - could use ADOConnection->raiseErrorFn
62 * 5) It used to be faster then PEAR/DB at the beginning of 2002.
63 * Now at August 2002 PEAR/DB with our own page cache added,
64 * performance is comparable.
67 require_once('lib/WikiDB/backend.php');
68 // Error handling - calls trigger_error. NB - does not close the connection. Does it need to?
69 include_once('lib/WikiDB/adodb/adodb-errorhandler.inc.php');
70 // include the main adodb file
71 require_once('lib/WikiDB/adodb/adodb.inc.php');
73 class WikiDB_backend_ADODB
74 extends WikiDB_backend
77 function WikiDB_backend_ADODB ($dbparams) {
78 $parsed = parseDSN($dbparams['dsn']);
79 $this->_dbparams = $dbparams;
80 $this->_parsedDSN =& $parsed;
81 $this->_dbh = &ADONewConnection($parsed['phptype']);
82 if (DEBUG & _DEBUG_SQL) {
83 $this->_dbh->debug = true;
84 $GLOBALS['ADODB_OUTP'] = '_sql_debuglog';
86 $this->_dsn = $parsed;
87 // persistent is defined as DSN option, or with a config value.
88 // phptype://username:password@hostspec/database?persistent=false
90 //FIXME: how to catch connection errors for dbamin_user?
91 if (!empty($parsed['persistent']) or DATABASE_PERSISTENT)
92 $conn = $this->_dbh->PConnect($parsed['hostspec'],$parsed['username'],
93 $parsed['password'], $parsed['database']);
95 $conn = $this->_dbh->Connect($parsed['hostspec'],$parsed['username'],
96 $parsed['password'], $parsed['database']);
99 // Since 1.3.10 we use the faster ADODB_FETCH_NUM,
100 // with some ASSOC based recordsets.
101 $GLOBALS['ADODB_FETCH_MODE'] = ADODB_FETCH_NUM;
102 $this->_dbh->SetFetchMode(ADODB_FETCH_NUM);
103 $GLOBALS['ADODB_COUNTRECS'] = false;
105 $prefix = isset($dbparams['prefix']) ? $dbparams['prefix'] : '';
107 = array('page_tbl' => $prefix . 'page',
108 'version_tbl' => $prefix . 'version',
109 'link_tbl' => $prefix . 'link',
110 'recent_tbl' => $prefix . 'recent',
111 'nonempty_tbl' => $prefix . 'nonempty');
112 $page_tbl = $this->_table_names['page_tbl'];
113 $version_tbl = $this->_table_names['version_tbl'];
114 $this->page_tbl_fields = "$page_tbl.id AS id, $page_tbl.pagename AS pagename, "
115 . "$page_tbl.hits AS hits";
116 $this->links_field_list = array('id', 'pagename');
117 $this->page_tbl_field_list = array('id', 'pagename', 'hits');
118 $this->version_tbl_fields = "$version_tbl.version AS version, "
119 . "$version_tbl.mtime AS mtime, "
120 . "$version_tbl.minor_edit AS minor_edit, $version_tbl.content AS content, "
121 . "$version_tbl.versiondata AS versiondata";
122 $this->version_tbl_field_list = array('version', 'mtime', 'minor_edit', 'content',
126 = array('maxmajor' => "MAX(CASE WHEN minor_edit=0 THEN version END)",
127 'maxminor' => "MAX(CASE WHEN minor_edit<>0 THEN version END)",
128 'maxversion' => "MAX(version)",
129 'notempty' => "<>''",
130 'iscontent' => "$version_tbl.content<>''");
131 $this->_lock_count = 0;
135 * Close database connection.
140 if ($this->_lock_count) {
141 trigger_error("WARNING: database still locked " .
142 '(lock_count = $this->_lock_count)' . "\n<br />",
145 // $this->_dbh->setErrorHandling(PEAR_ERROR_PRINT); // prevent recursive loops.
146 $this->unlock(false,'force');
148 $this->_dbh->close();
153 * Fast test for wikipage.
155 function is_wiki_page($pagename) {
157 extract($this->_table_names);
158 $row = $dbh->GetRow(sprintf("SELECT $page_tbl.id AS id"
159 . " FROM $nonempty_tbl, $page_tbl"
160 . " WHERE $nonempty_tbl.id=$page_tbl.id"
161 . " AND pagename=%s",
162 $dbh->qstr($pagename)));
163 return $row ? $row[0] : false;
166 function get_all_pagenames() {
168 extract($this->_table_names);
169 $result = $dbh->Execute("SELECT pagename"
170 . " FROM $nonempty_tbl, $page_tbl"
171 . " WHERE $nonempty_tbl.id=$page_tbl.id");
172 return $result->GetArray();
176 * filter (nonempty pages) currently ignored
178 function numPages($filter=false, $exclude='') {
180 extract($this->_table_names);
181 $result = $dbh->getRow("SELECT count(*)"
182 . " FROM $nonempty_tbl, $page_tbl"
183 . " WHERE $nonempty_tbl.id=$page_tbl.id");
187 function increaseHitCount($pagename) {
189 // Hits is the only thing we can update in a fast manner.
190 // Note that this will fail silently if the page does not
191 // have a record in the page table. Since it's just the
192 // hit count, who cares?
193 $dbh->Execute(sprintf("UPDATE %s SET hits=hits+1 WHERE pagename=%s",
194 $this->_table_names['page_tbl'],
195 $dbh->qstr($pagename)));
200 * Read page information from database.
202 function get_pagedata($pagename) {
204 $row = $dbh->GetRow(sprintf("SELECT id,pagename,hits,pagedata FROM %s WHERE pagename=%s",
205 $this->_table_names['page_tbl'],
206 $dbh->qstr($pagename)));
207 return $row ? $this->_extract_page_data($row[3], $row[2]) : false;
210 function _extract_page_data($data, $hits) {
212 return array('hits' => $hits);
214 return array_merge(array('hits' => $hits), $this->_unserialize($data));
217 function update_pagedata($pagename, $newdata) {
219 $page_tbl = $this->_table_names['page_tbl'];
221 // Hits is the only thing we can update in a fast manner.
222 if (count($newdata) == 1 && isset($newdata['hits'])) {
223 // Note that this will fail silently if the page does not
224 // have a record in the page table. Since it's just the
225 // hit count, who cares?
226 $dbh->Execute(sprintf("UPDATE $page_tbl SET hits=%d WHERE pagename=%s",
227 $newdata['hits'], $dbh->qstr($pagename)));
230 $where = sprintf("pagename=%s", $dbh->qstr($pagename));
232 $dbh->RowLock($page_tbl,$where);
234 $data = $this->get_pagedata($pagename);
237 $this->_get_pageid($pagename, true); // Creates page record
240 $hits = (empty($data['hits'])) ? 0 : (int)$data['hits'];
241 unset($data['hits']);
243 foreach ($newdata as $key => $val) {
246 else if (empty($val))
251 if ($dbh->Execute("UPDATE $page_tbl"
252 . " SET hits=?, pagedata=?"
253 . " WHERE pagename=?",
254 array($hits, $this->_serialize($data), $pagename))) {
255 $dbh->CommitTrans( );
258 $dbh->RollbackTrans( );
263 function get_cached_html($pagename) {
265 $page_tbl = $this->_table_names['page_tbl'];
266 $row = $dbh->GetRow(sprintf("SELECT cached_html FROM $page_tbl WHERE pagename=%s",
267 $dbh->qstr($pagename)));
268 return $row ? $row[0] : false;
271 function set_cached_html($pagename, $data) {
273 $page_tbl = $this->_table_names['page_tbl'];
274 if (empty($data)) $data = '';
275 $rs = $dbh->Execute("UPDATE $page_tbl"
276 . " SET cached_html=?"
277 . " WHERE pagename=?",
278 array($data, $pagename));
281 function _get_pageid($pagename, $create_if_missing = false) {
285 $cache =& $request->_dbi->_cache->_id_cache;
286 if (isset($cache[$pagename])) {
287 if ($cache[$pagename] or !$create_if_missing) {
288 return $cache[$pagename];
291 // attributes play this game.
292 if ($pagename === '') return 0;
295 $page_tbl = $this->_table_names['page_tbl'];
296 $query = sprintf("SELECT id FROM $page_tbl WHERE pagename=%s",
297 $dbh->qstr($pagename));
298 if (! $create_if_missing ) {
299 $row = $dbh->GetRow($query);
300 return $row ? $row[0] : false;
302 $row = $dbh->GetRow($query);
304 //TODO: Does the DBM has subselects? Then we can do it with select max(id)+1
305 // $this->lock(array('page'));
307 $dbh->CommitLock($page_tbl);
308 if (0 and $dbh->hasGenID) {
309 // requires create permissions
310 $id = $dbh->GenID($page_tbl."_id");
312 // Better generic version than with adodb::genID
313 $row = $dbh->GetRow("SELECT MAX(id) FROM $page_tbl");
316 $rs = $dbh->Execute(sprintf("INSERT INTO $page_tbl"
317 . " (id,pagename,hits)"
318 . " VALUES (%d,%s,0)",
319 $id, $dbh->qstr($pagename)));
320 if ($rs) $dbh->CommitTrans( );
321 else $dbh->RollbackTrans( );
322 // $this->unlock(array('page'));
330 function get_latest_version($pagename) {
332 extract($this->_table_names);
333 $row = $dbh->GetRow(sprintf("SELECT latestversion"
334 . " FROM $page_tbl, $recent_tbl"
335 . " WHERE $page_tbl.id=$recent_tbl.id"
336 . " AND pagename=%s",
337 $dbh->qstr($pagename)));
338 return $row ? (int)$row[0] : false;
341 function get_previous_version($pagename, $version) {
343 extract($this->_table_names);
344 // Use SELECTLIMIT for maximum portability
345 $rs = $dbh->SelectLimit(sprintf("SELECT version"
346 . " FROM $version_tbl, $page_tbl"
347 . " WHERE $version_tbl.id=$page_tbl.id"
349 . " AND version < %d"
350 . " ORDER BY version DESC",
351 $dbh->qstr($pagename),
354 return $rs->fields ? (int)$rs->fields[0] : false;
360 * @param $version int Which version to get.
362 * @return hash The version data, or false if specified version does not
365 function get_versiondata($pagename, $version, $want_content = false) {
367 extract($this->_table_names);
368 extract($this->_expressions);
370 assert(is_string($pagename) and $pagename != '');
371 assert($version > 0);
373 // FIXME: optimization: sometimes don't get page data?
375 $fields = $this->page_tbl_fields . ", $page_tbl.pagedata AS pagedata"
376 . ', ' . $this->version_tbl_fields;
378 $fields = $this->page_tbl_fields . ", '' AS pagedata"
379 . ", $version_tbl.version AS version, $version_tbl.mtime AS mtime, "
380 . "$version_tbl.minor_edit AS minor_edit, $iscontent AS have_content, "
381 . "$version_tbl.versiondata as versiondata";
383 $row = $dbh->GetRow(sprintf("SELECT $fields"
384 . " FROM $page_tbl, $version_tbl"
385 . " WHERE $page_tbl.id=$version_tbl.id"
388 $dbh->qstr($pagename), $version));
389 return $row ? $this->_extract_version_data_num($row, $want_content) : false;
392 function _extract_version_data_num($row, $want_content) {
397 //$pagename &= $row[1];
398 $data = empty($row[8]) ? array() : $this->_unserialize($row[8]);
399 $data['mtime'] = $row[5];
400 $data['is_minor_edit'] = !empty($row[6]);
402 $data['%content'] = $row[7];
404 $data['%content'] = !empty($row[7]);
406 if (!empty($row[3])) {
407 $data['%pagedata'] = $this->_extract_page_data($row[3], $row[2]);
412 function _extract_version_data_assoc($row) {
417 $data = empty($versiondata) ? array() : $this->_unserialize($versiondata);
418 $data['mtime'] = $mtime;
419 $data['is_minor_edit'] = !empty($minor_edit);
421 $data['%content'] = $content;
422 elseif ($have_content)
423 $data['%content'] = true;
425 $data['%content'] = '';
426 if (!empty($pagedata)) {
427 // hmm, $pagedata = is already extracted by WikiDB_backend_ADODB_iter
428 //$data['%pagedata'] = $this->_extract_page_data($pagedata, $hits);
429 $data['%pagedata'] = $pagedata;
435 * Create a new revision of a page.
437 function set_versiondata($pagename, $version, $data) {
439 $version_tbl = $this->_table_names['version_tbl'];
441 $minor_edit = (int) !empty($data['is_minor_edit']);
442 unset($data['is_minor_edit']);
444 $mtime = (int)$data['mtime'];
445 unset($data['mtime']);
446 assert(!empty($mtime));
448 @$content = (string) $data['%content'];
449 unset($data['%content']);
450 unset($data['%pagedata']);
452 $this->lock(array('page','recent','version','nonempty'));
454 $dbh->CommitLock($version_tbl);
455 $id = $this->_get_pageid($pagename, true);
456 $backend_type = $this->backendType();
457 $dbh->Execute(sprintf("DELETE FROM $version_tbl"
458 . " WHERE id=%d AND version=%d",
460 $rs = $dbh->Execute("INSERT INTO $version_tbl"
461 . " (id,version,mtime,minor_edit,content,versiondata)"
462 . " VALUES(?,?,?,?,?,?)",
463 array($id, $version, $mtime, $minor_edit,
464 $content, $this->_serialize($data)));
465 $this->_update_recent_table($id);
466 $this->_update_nonempty_table($id);
467 if ($rs) $dbh->CommitTrans( );
468 else $dbh->RollbackTrans( );
469 $this->unlock(array('page','recent','version','nonempty'));
473 * Delete an old revision of a page.
475 function delete_versiondata($pagename, $version) {
477 extract($this->_table_names);
479 $this->lock(array('version'));
480 if ( ($id = $this->_get_pageid($pagename)) ) {
481 $dbh->Execute("DELETE FROM $version_tbl"
482 . " WHERE id=$id AND version=$version");
483 $this->_update_recent_table($id);
484 // This shouldn't be needed (as long as the latestversion
485 // never gets deleted.) But, let's be safe.
486 $this->_update_nonempty_table($id);
488 $this->unlock(array('version'));
492 * Delete page from the database with backup possibility.
493 * i.e save_page('') and DELETE nonempty id
495 * deletePage increments latestversion in recent to a non-existent version,
496 * and removes the nonempty row,
497 * so that get_latest_version returns id+1 and get_previous_version returns prev id
498 * and page->exists returns false.
500 function delete_page($pagename) {
502 extract($this->_table_names);
505 $dbh->CommitLock($recent_tbl);
506 if (($id = $this->_get_pageid($pagename, false)) === false) {
507 $dbh->RollbackTrans( );
511 $user =& $GLOBALS['request']->_user;
512 $meta = array('author' => $user->getId(),
513 'author_id' => $user->getAuthenticatedId(),
515 $this->lock(array('version','recent','nonempty','page','link'));
516 $version = $this->get_latest_version($pagename);
517 if ($dbh->Execute("UPDATE $recent_tbl SET latestversion=latestversion+1,"
518 . "latestmajor=latestversion+1,latestminor=NULL WHERE id=$id")
519 and $dbh->Execute("INSERT INTO $version_tbl"
520 . " (id,version,mtime,minor_edit,content,versiondata)"
521 . " VALUES(?,?,?,?,?,?)",
522 array($id, $version+1, $mtime, 0,
523 '', $this->_serialize($meta)))
524 and $dbh->Execute("DELETE FROM $nonempty_tbl WHERE id=$id")
525 and $this->set_links($pagename, false)
526 // need to keep perms and LOCKED, otherwise you can reset the perm
527 // by action=remove and re-create it with default perms
528 // keep hits but delete meta-data
529 //and $dbh->Execute("UPDATE $page_tbl SET pagedata='' WHERE id=$id")
532 $this->unlock(array('version','recent','nonempty','page','link'));
533 $dbh->CommitTrans( );
536 $this->unlock(array('version','recent','nonempty','page','link'));
537 $dbh->RollbackTrans( );
542 function purge_page($pagename) {
544 extract($this->_table_names);
546 $this->lock(array('version','recent','nonempty','page','link'));
547 if ( ($id = $this->_get_pageid($pagename, false)) ) {
548 $dbh->Execute("DELETE FROM $nonempty_tbl WHERE id=$id");
549 $dbh->Execute("DELETE FROM $recent_tbl WHERE id=$id");
550 $dbh->Execute("DELETE FROM $version_tbl WHERE id=$id");
551 $this->set_links($pagename, false);
552 $row = $dbh->GetRow("SELECT COUNT(*) FROM $link_tbl WHERE linkto=$id");
553 if ($row and $row[0]) {
554 // We're still in the link table (dangling link) so we can't delete this
556 $dbh->Execute("UPDATE $page_tbl SET hits=0, pagedata='' WHERE id=$id");
560 $dbh->Execute("DELETE FROM $page_tbl WHERE id=$id");
564 $result = -1; // already purged or not existing
566 $this->unlock(array('version','recent','nonempty','page','link'));
571 // The only thing we might be interested in updating which we can
572 // do fast in the flags (minor_edit). I think the default
573 // update_versiondata will work fine...
574 //function update_versiondata($pagename, $version, $data) {
579 * on DEBUG: delete old, deleted links from page
581 function set_links($pagename, $links) {
582 // FIXME: optimize: mysql can do this all in one big INSERT/REPLACE.
585 extract($this->_table_names);
587 $this->lock(array('link'));
588 $pageid = $this->_get_pageid($pagename, true);
590 $oldlinks = $dbh->getAssoc("SELECT $link_tbl.linkto as id, page.pagename FROM $link_tbl"
591 ." JOIN page ON ($link_tbl.linkto = page.id)"
592 ." WHERE linkfrom=$pageid");
593 // Delete current links,
594 $dbh->Execute("DELETE FROM $link_tbl WHERE linkfrom=$pageid");
595 // and insert new links. Faster than checking for all single links
597 foreach ($links as $link) {
598 $linkto = $link['linkto'];
599 if (isset($link['relation']))
600 $relation = $this->_get_pageid($link['relation'], true);
603 if ($linkto === "") { // ignore attributes
607 if (isset($linkseen[$linkto]) and !$relation) {
611 $linkseen[$linkto] = true;
613 $linkid = $this->_get_pageid($linkto, true);
616 $dbh->Execute("INSERT INTO $link_tbl (linkfrom, linkto, relation)"
617 . " VALUES ($pageid, $linkid, $relation)");
619 $dbh->Execute("INSERT INTO $link_tbl (linkfrom, linkto)"
620 . " VALUES ($pageid, $linkid)");
622 if ($oldlinks and array_key_exists($linkid, $oldlinks)) {
623 // This was also in the previous page
624 unset($oldlinks[$linkid]);
628 // purge page table: delete all non-referenced pages
629 // for all previously linked pages, which have no other linkto links
630 if (DEBUG and $oldlinks) {
631 // trigger_error("purge page table: delete all non-referenced pages...", E_USER_NOTICE);
632 foreach ($oldlinks as $id => $name) {
633 // ...check if the page is empty and has no version
634 $result = $dbh->getRow("SELECT $page_tbl.id FROM $page_tbl"
635 . " LEFT JOIN $nonempty_tbl USING (id) "
636 . " LEFT JOIN $version_tbl USING (id)"
637 . " WHERE $nonempty_tbl.id is NULL"
638 . " AND $version_tbl.id is NULL"
639 . " AND $page_tbl.id=$id");
640 $linkto = $dbh->getRow("SELECT linkfrom FROM $link_tbl WHERE linkto=$id");
641 if ($result and empty($linkto))
643 trigger_error("delete empty and non-referenced link $name ($id)", E_USER_NOTICE);
644 $dbh->Execute("DELETE FROM $recent_tbl WHERE id=$id"); // may fail
645 $dbh->Execute("DELETE FROM $link_tbl WHERE linkto=$id");
646 $dbh->Execute("DELETE FROM $page_tbl WHERE id=$id"); // this purges the link
650 $this->unlock(array('link'));
654 /* get all oldlinks in hash => id, relation
655 check for all new links
657 function set_links1($pagename, $links) {
660 extract($this->_table_names);
662 $this->lock(array('link'));
663 $pageid = $this->_get_pageid($pagename, true);
665 $oldlinks = $dbh->getAssoc("SELECT $link_tbl.linkto as linkto, $link_tbl.relation, page.pagename"
667 ." JOIN page ON ($link_tbl.linkto = page.id)"
668 ." WHERE linkfrom=$pageid");
670 * X => [1,0 2,0 1,1] X => [1,1 3,0]
671 * => delete 1,0 2,0 + insert 3,0
674 foreach ($links as $link) {
675 $linkto = $link['linkto'];
676 if ($link['relation'])
677 $relation = $this->_get_pageid($link['relation'], true);
681 if (isset($linkseen[$linkto]) and !$relation) {
685 $linkseen[$linkto] = true;
687 $linkid = $this->_get_pageid($linkto, true);
690 // find linkfrom,linkto,relation triple in oldlinks
691 foreach ($oldlinks as $l) {
692 if ($relation) { // relation NOT NULL
693 if ($l['linkto'] == $linkid and $l['relation'] == $relation) {
703 $dbh->Execute("INSERT INTO $link_tbl (linkfrom, linkto, relation)"
704 . " VALUES ($pageid, $linkid, $relation)");
706 $dbh->Execute("INSERT INTO $link_tbl (linkfrom, linkto)"
707 . " VALUES ($pageid, $linkid)");
711 if (array_key_exists($linkid, $oldlinks)) {
712 // This was also in the previous page
713 unset($oldlinks[$linkid]);
717 // purge page table: delete all non-referenced pages
718 // for all previously linked pages...
719 if (DEBUG and $oldlinks) {
720 // trigger_error("purge page table: delete all non-referenced pages...", E_USER_NOTICE);
721 foreach ($oldlinks as $id => $name) {
722 // ...check if the page is empty and has no version
723 if ($dbh->getRow("SELECT $page_tbl.id FROM $page_tbl"
724 . " LEFT JOIN $nonempty_tbl USING (id) "
725 . " LEFT JOIN $version_tbl USING (id)"
726 . " WHERE $nonempty_tbl.id is NULL"
727 . " AND $version_tbl.id is NULL"
728 . " AND $page_tbl.id=$id"))
730 trigger_error("delete empty and non-referenced link $name ($id)", E_USER_NOTICE);
731 $dbh->Execute("DELETE FROM $page_tbl WHERE id=$id"); // this purges the link
732 $dbh->Execute("DELETE FROM $recent_tbl WHERE id=$id"); // may fail
736 $this->unlock(array('link'));
741 * Find pages which link to or are linked from a page.
743 * Optimization: save request->_dbi->_iwpcache[] to avoid further iswikipage checks
744 * (linkExistingWikiWord or linkUnknownWikiWord)
745 * This is called on every page header GleanDescription, so we can store all the
748 * relations: $backend->get_links is responsible to add the relation to the pagehash
749 * as 'linkrelation' key as pagename. See WikiDB_PageIterator::next
750 * if (isset($next['linkrelation']))
752 function get_links($pagename, $reversed=true, $include_empty=false,
753 $sortby='', $limit='', $exclude='',
754 $want_relations = false)
757 extract($this->_table_names);
760 list($have,$want) = array('linkee', 'linker');
762 list($have,$want) = array('linker', 'linkee');
763 $orderby = $this->sortby($sortby, 'db', array('pagename'));
764 if ($orderby) $orderby = " ORDER BY $want." . $orderby;
765 if ($exclude) // array of pagenames
766 $exclude = " AND $want.pagename NOT IN ".$this->_sql_set($exclude);
770 $qpagename = $dbh->qstr($pagename);
771 // removed ref to FETCH_MODE in next line
772 $sql = "SELECT $want.id AS id, $want.pagename AS pagename, "
773 . ($want_relations ? " related.pagename as linkrelation" : " $want.hits AS hits")
775 . (!$include_empty ? "$nonempty_tbl, " : '')
776 . " $page_tbl linkee, $page_tbl linker, $link_tbl "
777 . ($want_relations ? " JOIN $page_tbl related ON ($link_tbl.relation=related.id)" : '')
778 . " WHERE linkfrom=linker.id AND linkto=linkee.id"
779 . " AND $have.pagename=$qpagename"
780 . (!$include_empty ? " AND $nonempty_tbl.id=$want.id" : "")
781 //. " GROUP BY $want.id"
785 echo "SELECT linkee.id AS id, linkee.pagename AS pagename, related.pagename as linkrelation FROM link, page linkee, page linker JOIN page related ON (link.relation=related.id) WHERE linkfrom=linker.id AND linkto=linkee.id AND linker.pagename='SanDiego'" | mysql phpwiki
786 id pagename linkrelation
787 2268 California located_in
790 // extract from,count from limit
791 list($offset,$count) = $this->limit($limit);
792 $result = $dbh->SelectLimit($sql, $count, $offset);
794 $result = $dbh->Execute($sql);
796 $fields = $this->links_field_list;
797 if ($want_relations) // instead of hits
798 $fields[2] = 'linkrelation';
799 return new WikiDB_backend_ADODB_iter($this, $result, $fields);
803 * Find if a page links to another page
805 function exists_link($pagename, $link, $reversed=false) {
807 extract($this->_table_names);
810 list($have, $want) = array('linkee', 'linker');
812 list($have, $want) = array('linker', 'linkee');
813 $qpagename = $dbh->qstr($pagename);
814 $qlink = $dbh->qstr($link);
815 $row = $dbh->GetRow("SELECT CASE WHEN $want.pagename=$qlink THEN 1 ELSE 0 END"
816 . " FROM $link_tbl, $page_tbl linker, $page_tbl linkee, $nonempty_tbl"
817 . " WHERE linkfrom=linker.id AND linkto=linkee.id"
818 . " AND $have.pagename=$qpagename"
819 . " AND $want.pagename=$qlink");
826 function get_all_pages($include_empty=false, $sortby='', $limit='', $exclude='') {
828 extract($this->_table_names);
829 $orderby = $this->sortby($sortby, 'db');
830 if ($orderby) $orderby = ' ORDER BY ' . $orderby;
832 if ($exclude) {// array of pagenames
834 $exclude = " $page_tbl.pagename NOT IN ".$this->_sql_set($exclude);
839 //$dbh->SetFetchMode(ADODB_FETCH_ASSOC);
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"
852 . $this->page_tbl_fields
853 . " FROM $nonempty_tbl, $page_tbl, $recent_tbl, $version_tbl"
854 . " WHERE $nonempty_tbl.id=$page_tbl.id"
855 . " AND $page_tbl.id=$recent_tbl.id"
856 . " AND $page_tbl.id=$version_tbl.id AND latestversion=version"
861 if ($include_empty) {
863 . $this->page_tbl_fields
865 . ($exclude ? " WHERE $exclude" : '')
869 . $this->page_tbl_fields
870 . " FROM $nonempty_tbl, $page_tbl"
871 . " WHERE $nonempty_tbl.id=$page_tbl.id"
877 // extract from,count from limit
878 list($offset,$count) = $this->limit($limit);
879 $result = $dbh->SelectLimit($sql, $count, $offset);
881 $result = $dbh->Execute($sql);
883 //$dbh->SetFetchMode(ADODB_FETCH_NUM);
884 return new WikiDB_backend_ADODB_iter($this, $result, $this->page_tbl_field_list);
888 * Title and fulltext search.
890 function text_search($search, $fullsearch=false,
891 $sortby='', $limit='', $exclude='')
894 extract($this->_table_names);
895 $orderby = $this->sortby($sortby, 'db');
896 if ($orderby) $orderby = ' ORDER BY ' . $orderby;
898 $table = "$nonempty_tbl, $page_tbl";
899 $join_clause = "$nonempty_tbl.id=$page_tbl.id";
900 $fields = $this->page_tbl_fields;
901 $field_list = $this->page_tbl_field_list;
902 $searchobj = new WikiDB_backend_ADODB_search($search, $dbh);
905 $table .= ", $recent_tbl";
906 $join_clause .= " AND $page_tbl.id=$recent_tbl.id";
908 $table .= ", $version_tbl";
909 $join_clause .= " AND $page_tbl.id=$version_tbl.id AND latestversion=version";
911 $fields .= ",$page_tbl.pagedata as pagedata," . $this->version_tbl_fields;
912 $field_list = array_merge($field_list, array('pagedata'),
913 $this->version_tbl_field_list);
914 $callback = new WikiMethodCb($searchobj, "_fulltext_match_clause");
916 $callback = new WikiMethodCb($searchobj, "_pagename_match_clause");
919 $search_clause = $search->makeSqlClauseObj($callback);
920 $sql = "SELECT $fields FROM $table"
921 . " WHERE $join_clause"
922 . " AND ($search_clause)"
925 // extract from,count from limit
926 list($offset,$count) = $this->limit($limit);
927 $result = $dbh->SelectLimit($sql, $count, $offset);
929 $result = $dbh->Execute($sql);
931 $iter = new WikiDB_backend_ADODB_iter($this, $result, $field_list);
933 $iter->stoplisted = $searchobj->stoplisted;
938 * TODO: efficiently handle wildcards exclusion: exclude=Php* => 'Php%',
939 * not sets. See above, but the above methods find too much.
940 * This is only for already resolved wildcards:
941 * " WHERE $page_tbl.pagename NOT IN ".$this->_sql_set(array('page1','page2'));
943 function _sql_set(&$pagenames) {
945 foreach ($pagenames as $p) {
946 $s .= ($this->_dbh->qstr($p).",");
948 return substr($s,0,-1).")";
952 * Find highest or lowest hit counts.
954 function most_popular($limit=20, $sortby='-hits') {
956 extract($this->_table_names);
963 $where = " AND hits > 0";
965 if ($sortby != '-hits') {
966 if ($order = $this->sortby($sortby, 'db')) $orderby = " ORDER BY " . $order;
969 $orderby = " ORDER BY hits $order";
971 . $this->page_tbl_fields
972 . " FROM $nonempty_tbl, $page_tbl"
973 . " WHERE $nonempty_tbl.id=$page_tbl.id"
977 // extract from,count from limit
978 list($offset,$count) = $this->limit($limit);
979 $result = $dbh->SelectLimit($sql, $count, $offset);
981 $result = $dbh->Execute($sql);
983 return new WikiDB_backend_ADODB_iter($this, $result, $this->page_tbl_field_list);
987 * Find recent changes.
989 function most_recent($params) {
992 $include_minor_revisions = false;
993 $exclude_major_revisions = false;
994 $include_all_revisions = false;
998 extract($this->_table_names);
1002 $pick[] = "mtime >= $since";
1004 if ($include_all_revisions) {
1005 // Include all revisions of each page.
1006 $table = "$page_tbl, $version_tbl";
1007 $join_clause = "$page_tbl.id=$version_tbl.id";
1009 if ($exclude_major_revisions) {
1010 // Include only minor revisions
1011 $pick[] = "minor_edit <> 0";
1013 elseif (!$include_minor_revisions) {
1014 // Include only major revisions
1015 $pick[] = "minor_edit = 0";
1019 $table = "$page_tbl, $recent_tbl";
1020 $join_clause = "$page_tbl.id=$recent_tbl.id";
1021 $table .= ", $version_tbl";
1022 $join_clause .= " AND $version_tbl.id=$page_tbl.id";
1024 if ($exclude_major_revisions) {
1025 // Include only most recent minor revision
1026 $pick[] = 'version=latestminor';
1028 elseif (!$include_minor_revisions) {
1029 // Include only most recent major revision
1030 $pick[] = 'version=latestmajor';
1033 // Include only the latest revision (whether major or minor).
1034 $pick[] ='version=latestversion';
1042 $where_clause = $join_clause;
1044 $where_clause .= " AND " . join(" AND ", $pick);
1046 . $this->page_tbl_fields . ", " . $this->version_tbl_fields
1048 . " WHERE $where_clause"
1049 . " ORDER BY mtime $order";
1050 // FIXME: use SQL_BUFFER_RESULT for mysql?
1052 // extract from,count from limit
1053 list($offset,$count) = $this->limit($limit);
1054 $result = $dbh->SelectLimit($sql, $count, $offset);
1056 $result = $dbh->Execute($sql);
1058 //$result->fields['version'] = $result->fields[6];
1059 return new WikiDB_backend_ADODB_iter($this, $result,
1060 array_merge($this->page_tbl_field_list, $this->version_tbl_field_list));
1064 * Find referenced empty pages.
1066 function wanted_pages($exclude_from='', $exclude='', $sortby='', $limit='') {
1067 $dbh = &$this->_dbh;
1068 extract($this->_table_names);
1069 if ($orderby = $this->sortby($sortby, 'db', array('pagename','wantedfrom')))
1070 $orderby = 'ORDER BY ' . $orderby;
1072 if ($exclude_from) // array of pagenames
1073 $exclude_from = " AND pp.pagename NOT IN ".$this->_sql_set($exclude_from);
1074 if ($exclude) // array of pagenames
1075 $exclude = " AND p.pagename NOT IN ".$this->_sql_set($exclude);
1078 all empty pages, independent of linkstatus:
1079 select pagename as empty from page left join nonempty using(id) where is null(nonempty.id);
1080 only all empty pages, which have a linkto:
1081 select page.pagename, linked.pagename as wantedfrom from link, page linked
1082 left join page on link.linkto=page.id left join nonempty on link.linkto=nonempty.id
1083 where nonempty.id is null and linked.id=link.linkfrom;
1085 $sql = "SELECT p.pagename, pp.pagename as wantedfrom"
1086 . " FROM $page_tbl p, $link_tbl linked"
1087 . " LEFT JOIN $page_tbl pp ON (linked.linkto = pp.id)"
1088 . " LEFT JOIN $nonempty_tbl ne ON (linked.linkto = ne.id)"
1089 . " WHERE ne.id is NULL"
1090 . " AND (p.id = linked.linkfrom)"
1095 // extract from,count from limit
1096 list($offset,$count) = $this->limit($limit);
1097 $result = $dbh->SelectLimit($sql, $count, $offset);
1099 $result = $dbh->Execute($sql);
1101 return new WikiDB_backend_ADODB_iter($this, $result, array('pagename','wantedfrom'));
1105 * Rename page in the database.
1107 function rename_page($pagename, $to) {
1108 $dbh = &$this->_dbh;
1109 extract($this->_table_names);
1111 $this->lock(array('page','version','recent','nonempty','link'));
1112 if ( ($id = $this->_get_pageid($pagename, false)) ) {
1113 if ($new = $this->_get_pageid($to, false)) {
1115 // This page does not exist (already verified before), but exists in the page table.
1116 // So we delete this page.
1117 $dbh->query("DELETE FROM $page_tbl WHERE id=$new");
1118 $dbh->query("DELETE FROM $version_tbl WHERE id=$new");
1119 $dbh->query("DELETE FROM $recent_tbl WHERE id=$new");
1120 $dbh->query("DELETE FROM $nonempty_tbl WHERE id=$new");
1121 // We have to fix all referring tables to the old id
1122 $dbh->query("UPDATE $link_tbl SET linkfrom=$id WHERE linkfrom=$new");
1123 $dbh->query("UPDATE $link_tbl SET linkto=$id WHERE linkto=$new");
1125 $dbh->query(sprintf("UPDATE $page_tbl SET pagename=%s WHERE id=$id",
1128 $this->unlock(array('page'));
1132 function _update_recent_table($pageid = false) {
1133 $dbh = &$this->_dbh;
1134 extract($this->_table_names);
1135 extract($this->_expressions);
1137 $pageid = (int)$pageid;
1139 // optimize: mysql can do this with one REPLACE INTO.
1140 $backend_type = $this->backendType();
1141 if (substr($backend_type,0,5) == 'mysql') {
1142 $dbh->Execute("REPLACE INTO $recent_tbl"
1143 . " (id, latestversion, latestmajor, latestminor)"
1144 . " SELECT id, $maxversion, $maxmajor, $maxminor"
1145 . " FROM $version_tbl"
1146 . ( $pageid ? " WHERE id=$pageid" : "")
1149 $this->lock(array('recent'));
1150 $dbh->Execute("DELETE FROM $recent_tbl"
1151 . ( $pageid ? " WHERE id=$pageid" : ""));
1152 $dbh->Execute( "INSERT INTO $recent_tbl"
1153 . " (id, latestversion, latestmajor, latestminor)"
1154 . " SELECT id, $maxversion, $maxmajor, $maxminor"
1155 . " FROM $version_tbl"
1156 . ( $pageid ? " WHERE id=$pageid" : "")
1158 $this->unlock(array('recent'));
1162 function _update_nonempty_table($pageid = false) {
1163 $dbh = &$this->_dbh;
1164 extract($this->_table_names);
1165 extract($this->_expressions);
1167 $pageid = (int)$pageid;
1169 extract($this->_expressions);
1170 $this->lock(array('nonempty'));
1171 $dbh->Execute("DELETE FROM $nonempty_tbl"
1172 . ( $pageid ? " WHERE id=$pageid" : ""));
1173 $dbh->Execute("INSERT INTO $nonempty_tbl (id)"
1174 . " SELECT $recent_tbl.id"
1175 . " FROM $recent_tbl, $version_tbl"
1176 . " WHERE $recent_tbl.id=$version_tbl.id"
1177 . " AND version=latestversion"
1178 // We have some specifics here (Oracle)
1179 //. " AND content<>''"
1180 . " AND content $notempty" // On Oracle not just "<>''"
1181 . ( $pageid ? " AND $recent_tbl.id=$pageid" : ""));
1182 $this->unlock(array('nonempty'));
1186 * Grab a write lock on the tables in the SQL database.
1188 * Calls can be nested. The tables won't be unlocked until
1189 * _unlock_database() is called as many times as _lock_database().
1193 function lock($tables, $write_lock = true) {
1194 $this->_dbh->StartTrans();
1195 if ($this->_lock_count++ == 0) {
1196 $this->_current_lock = $tables;
1197 $this->_lock_tables($tables, $write_lock);
1202 * Overridden by non-transaction safe backends.
1204 function _lock_tables($tables, $write_lock) {
1205 return $this->_current_lock;
1209 * Release a write lock on the tables in the SQL database.
1213 * @param $force boolean Unlock even if not every call to lock() has been matched
1214 * by a call to unlock().
1216 * @see _lock_database
1218 function unlock($tables = false, $force = false) {
1219 if ($this->_lock_count == 0) {
1220 $this->_current_lock = false;
1223 if (--$this->_lock_count <= 0 || $force) {
1224 $this->_unlock_tables($tables, $force);
1225 $this->_current_lock = false;
1226 $this->_lock_count = 0;
1228 $this->_dbh->CompleteTrans(! $force);
1232 * overridden by non-transaction safe backends
1234 function _unlock_tables($tables, $write_lock=false) {
1241 function _serialize($data) {
1244 assert(is_array($data));
1245 return serialize($data);
1251 function _unserialize($data) {
1252 return empty($data) ? array() : unserialize($data);
1255 /* some variables and functions for DB backend abstraction (action=upgrade) */
1256 function database () {
1257 return $this->_dbh->database;
1259 function backendType() {
1260 return $this->_dbh->databaseType;
1262 function connection() {
1263 return $this->_dbh->_connectionID;
1265 function getRow($query) {
1266 return $this->_dbh->getRow($query);
1269 function listOfTables() {
1270 return $this->_dbh->MetaTables();
1273 // other database needs another connection and other privileges.
1274 function listOfFields($database, $table) {
1275 $field_list = array();
1276 $old_db = $this->database();
1277 if ($database != $old_db) {
1278 $conn = $this->_dbh->Connect($this->_parsedDSN['hostspec'],
1279 DBADMIN_USER ? DBADMIN_USER : $this->_parsedDSN['username'],
1280 DBADMIN_PASSWD ? DBADMIN_PASSWD : $this->_parsedDSN['password'],
1283 foreach ($this->_dbh->MetaColumns($table, false) as $field) {
1284 $field_list[] = $field->name;
1286 if ($database != $old_db) {
1287 $this->_dbh->close();
1288 $conn = $this->_dbh->Connect($this->_parsedDSN['hostspec'],
1289 $this->_parsedDSN['username'],
1290 $this->_parsedDSN['password'],
1298 class WikiDB_backend_ADODB_generic_iter
1299 extends WikiDB_backend_iterator
1301 function WikiDB_backend_ADODB_generic_iter($backend, $query_result, $field_list = NULL) {
1302 $this->_backend = &$backend;
1303 $this->_result = $query_result;
1305 if (is_null($field_list)) {
1306 // No field list passed, retrieve from DB
1307 // WikiLens is using the iterator behind the scene
1308 $field_list = array();
1309 $fields = $query_result->FieldCount();
1310 for ($i = 0; $i < $fields ; $i++) {
1311 $field_info = $query_result->FetchField($i);
1312 array_push($field_list, $field_info->name);
1316 $this->_fields = $field_list;
1320 if (!$this->_result) {
1323 $count = $this->_result->numRows();
1324 //$this->_result->Close();
1329 $result = &$this->_result;
1330 $backend = &$this->_backend;
1331 if (!$result || $result->EOF) {
1336 // Convert array to hash
1338 $rec_num = $result->fields;
1339 foreach ($this->_fields as $field) {
1340 $rec_assoc[$field] = $rec_num[$i++];
1342 // check if the cache can be populated here?
1344 $result->MoveNext();
1349 if ($this->_result) {
1350 $this->_result->MoveFirst();
1353 function asArray () {
1355 while ($page = $this->next())
1361 if ($this->_result) {
1362 /* call mysql_free_result($this->_queryID) */
1363 $this->_result->Close();
1364 $this->_result = false;
1369 class WikiDB_backend_ADODB_iter
1370 extends WikiDB_backend_ADODB_generic_iter
1373 $result = &$this->_result;
1374 $backend = &$this->_backend;
1375 if (!$result || $result->EOF) {
1380 // Convert array to hash
1382 $rec_num = $result->fields;
1383 foreach ($this->_fields as $field) {
1384 $rec_assoc[$field] = $rec_num[$i++];
1387 $result->MoveNext();
1388 if (isset($rec_assoc['pagedata']))
1389 $rec_assoc['pagedata'] = $backend->_extract_page_data($rec_assoc['pagedata'], $rec_assoc['hits']);
1390 if (!empty($rec_assoc['version'])) {
1391 $rec_assoc['versiondata'] = $backend->_extract_version_data_assoc($rec_assoc);
1393 if (!empty($rec_assoc['linkrelation'])) {
1394 $rec_assoc['linkrelation'] = $rec_assoc['linkrelation']; // pagename enough?
1400 class WikiDB_backend_ADODB_search extends WikiDB_backend_search_sql
1402 // no surrounding quotes because we know it's a string
1403 // function _quote($word) { return $this->_dbh->escapeSimple($word); }
1406 // Following function taken from Pear::DB (prev. from adodb-pear.inc.php).
1407 // Eventually, change index.php to provide the relevant information
1410 * Parse a data source name.
1412 * Additional keys can be added by appending a URI query string to the
1415 * The format of the supplied DSN is in its fullest form:
1417 * phptype(dbsyntax)://username:password@protocol+hostspec/database?option=8&another=true
1420 * Most variations are allowed:
1422 * phptype://username:password@protocol+hostspec:110//usr/db_file.db?mode=0644
1423 * phptype://username:password@hostspec/database_name
1424 * phptype://username:password@hostspec
1425 * phptype://username@hostspec
1426 * phptype://hostspec/database
1427 * phptype://hostspec
1432 * @param string $dsn Data Source Name to be parsed
1434 * @return array an associative array with the following keys:
1435 * + phptype: Database backend used in PHP (mysql, odbc etc.)
1436 * + dbsyntax: Database used with regards to SQL syntax etc.
1437 * + protocol: Communication protocol to use (tcp, unix etc.)
1438 * + hostspec: Host specification (hostname[:port])
1439 * + database: Database to use on the DBMS server
1440 * + username: User name for login
1441 * + password: Password for login
1443 * @author Tomas V.V.Cox <cox@idecnet.com>
1445 function parseDSN($dsn)
1449 'dbsyntax' => false,
1450 'username' => false,
1451 'password' => false,
1452 'protocol' => false,
1453 'hostspec' => false,
1456 'database' => false,
1459 if (is_array($dsn)) {
1460 $dsn = array_merge($parsed, $dsn);
1461 if (!$dsn['dbsyntax']) {
1462 $dsn['dbsyntax'] = $dsn['phptype'];
1467 // Find phptype and dbsyntax
1468 if (($pos = strpos($dsn, '://')) !== false) {
1469 $str = substr($dsn, 0, $pos);
1470 $dsn = substr($dsn, $pos + 3);
1476 // Get phptype and dbsyntax
1477 // $str => phptype(dbsyntax)
1478 if (preg_match('|^(.+?)\((.*?)\)$|', $str, $arr)) {
1479 $parsed['phptype'] = $arr[1];
1480 $parsed['dbsyntax'] = !$arr[2] ? $arr[1] : $arr[2];
1482 $parsed['phptype'] = $str;
1483 $parsed['dbsyntax'] = $str;
1490 // Get (if found): username and password
1491 // $dsn => username:password@protocol+hostspec/database
1492 if (($at = strrpos($dsn,'@')) !== false) {
1493 $str = substr($dsn, 0, $at);
1494 $dsn = substr($dsn, $at + 1);
1495 if (($pos = strpos($str, ':')) !== false) {
1496 $parsed['username'] = rawurldecode(substr($str, 0, $pos));
1497 $parsed['password'] = rawurldecode(substr($str, $pos + 1));
1499 $parsed['username'] = rawurldecode($str);
1503 // Find protocol and hostspec
1505 // $dsn => proto(proto_opts)/database
1506 if (preg_match('|^([^(]+)\((.*?)\)/?(.*?)$|', $dsn, $match)) {
1508 $proto_opts = $match[2] ? $match[2] : false;
1511 // $dsn => protocol+hostspec/database (old format)
1513 if (strpos($dsn, '+') !== false) {
1514 list($proto, $dsn) = explode('+', $dsn, 2);
1516 if (strpos($dsn, '/') !== false) {
1517 list($proto_opts, $dsn) = explode('/', $dsn, 2);
1524 // process the different protocol options
1525 $parsed['protocol'] = (!empty($proto)) ? $proto : 'tcp';
1526 $proto_opts = rawurldecode($proto_opts);
1527 if ($parsed['protocol'] == 'tcp') {
1528 if (strpos($proto_opts, ':') !== false) {
1529 list($parsed['hostspec'], $parsed['port']) = explode(':', $proto_opts);
1531 $parsed['hostspec'] = $proto_opts;
1533 } elseif ($parsed['protocol'] == 'unix') {
1534 $parsed['socket'] = $proto_opts;
1537 // Get dabase if any
1541 if (($pos = strpos($dsn, '?')) === false) {
1542 $parsed['database'] = $dsn;
1543 // /database?param1=value1¶m2=value2
1545 $parsed['database'] = substr($dsn, 0, $pos);
1546 $dsn = substr($dsn, $pos + 1);
1547 if (strpos($dsn, '&') !== false) {
1548 $opts = explode('&', $dsn);
1549 } else { // database?param1=value1
1550 $opts = array($dsn);
1552 foreach ($opts as $opt) {
1553 list($key, $value) = explode('=', $opt);
1554 if (!isset($parsed[$key])) {
1555 // don't allow params overwrite
1556 $parsed[$key] = rawurldecode($value);
1568 // c-basic-offset: 4
1569 // c-hanging-comment-ender-p: nil
1570 // indent-tabs-mode: nil