2 rcs_id('$Id: ADODB.php,v 1.5 2002-02-08 20:30:48 lakka Exp $');
4 /*This file is part of PhpWiki.
6 PhpWiki is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 PhpWiki is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with PhpWiki; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 // Based on PearDB.php
24 // 1) ADODB's GetRow() is slightly different from that in PEAR. It does not accept a fetchmode parameter
25 // That doesn't matter too much here, since we only ever use FETCHMODE_ASSOC
27 // 2) No need for ''s arond strings in sprintf arguments - qstr puts them there automatically
29 // 3) ADODB has a version of GetOne, but it is difficult to use it when FETCH_ASSOC is in effect.
30 // Instead, use $rs = Execute($query); $value = $rs->fields["$colname"]
31 // 4) No error handling yet - could use ADOConnection->raiseErrorFn
33 //require_once('DB.php');
34 require_once('lib/WikiDB/backend.php');
35 //require_once('lib/FileFinder.php');
36 //require_once('lib/ErrorManager.php');
37 // Error handling - calls trigger_error. NB - does not close the connection. Does it need to?
38 include_once('lib/WikiDB/adodb/adodb-errorhandler.inc.php');
39 // include the main adodb file
40 require_once('lib/WikiDB/adodb/adodb.inc.php');
42 class WikiDB_backend_ADODB
43 extends WikiDB_backend
45 function WikiDB_backend_ADODB ($dbparams) {
46 // Find and include PEAR's DB.php.
47 //$pearFinder = new PearFileFinder;
48 //$pearFinder->includeOnce('DB.php');
50 // Install filter to handle bogus error notices from buggy DB.php's.
51 // global $ErrorManager;
52 // $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_pear_notice_filter'));
54 // Open connection to database
55 // $this->_dsn = $dbparams['dsn'];
56 /* $dboptions = array('persistent' => true,
58 $this->_dbh = DB::connect($this->_dsn, $dboptions);
60 if (DB::isError($dbh)) {
61 trigger_error(sprintf("Can't connect to database: %s",
62 $this->_pear_error_message($dbh)),
65 $dbh->setErrorHandling(PEAR_ERROR_CALLBACK,
66 array($this, '_pear_error_callback'));
67 $dbh->setFetchMode(ADODB_FETCH_ASSOC);
69 $parsed = parseDSN($dbparams['dsn']);
70 $this->_dbh = &ADONewConnection($parsed['phptype']); // Probably only MySql works just now
71 $conn = $this->_dbh->PConnect($parsed['hostspec'],$parsed['username'],
72 $parsed['password'], $parsed['database']);
74 // Error handling not needed here -all dealt with by adodb-errorhandler.inc.php
75 /* if ($conn === false) {
76 trigger_error(sprintf("Can't connect to database: %s",
77 $this->_pear_error_message($conn)),
82 // Uncomment the following line to enable debugging output (not very pretty!)
83 // $this->_dbh->debug = true;
85 $GLOBALS['ADODB_FETCH_MODE'] = ADODB_FETCH_ASSOC;
87 // The next line should speed up queries if enabled, but:
88 // 1) It only works with PHP >= 4.0.6; and
89 // 2) At the moment, I haven't figured out why thw wrong results are returned'
90 // $GLOBALS['ADODB_COUNTRECS'] = false;
92 $prefix = isset($dbparams['prefix']) ? $dbparams['prefix'] : '';
95 = array('page_tbl' => $prefix . 'page',
96 'version_tbl' => $prefix . 'version',
97 'link_tbl' => $prefix . 'link',
98 'recent_tbl' => $prefix . 'recent',
99 'nonempty_tbl' => $prefix . 'nonempty');
102 = array('maxmajor' => "MAX(CASE WHEN minor_edit=0 THEN version END)",
103 'maxminor' => "MAX(CASE WHEN minor_edit<>0 THEN version END)",
104 'maxversion' => "MAX(version)");
106 $this->_lock_count = 0;
110 * Close database connection.
115 if ($this->_lock_count) {
116 trigger_error( "WARNING: database still locked " . '(lock_count = $this->_lock_count)' . "\n<br />",
119 // $this->_dbh->setErrorHandling(PEAR_ERROR_PRINT); // prevent recursive loops.
120 $this->unlock('force');
122 $this->_dbh->close();
128 * Test fast wikipage.
130 function is_wiki_page($pagename) {
132 extract($this->_table_names);
133 $rs = $dbh->Execute(sprintf("SELECT $page_tbl.id AS id"
134 . " FROM $nonempty_tbl, $page_tbl"
135 . " WHERE $nonempty_tbl.id=$page_tbl.id"
136 . " AND pagename=%s",
137 $dbh->qstr($pagename)));
140 $result = $rs->fields["id"];
151 function get_all_pagenames() {
153 extract($this->_table_names);
154 //return $dbh->getCol("SELECT pagename"
155 // . " FROM $nonempty_tbl, $page_tbl"
156 // . " WHERE $nonempty_tbl.id=$page_tbl.id");
159 //Original code (above) return the column in an indexed array - 0 based
160 //So, hopefully, does this
161 $result = $dbh->Execute("SELECT pagename"
162 . " FROM $nonempty_tbl, $page_tbl"
163 . " WHERE $nonempty_tbl.id=$page_tbl.id");
164 return $result->GetArray();
168 * Read page information from database.
170 function get_pagedata($pagename) {
172 $page_tbl = $this->_table_names['page_tbl'];
174 //trigger_error("GET_PAGEDATA $pagename", E_USER_NOTICE);
176 $result = $dbh->GetRow(sprintf("SELECT * FROM $page_tbl WHERE pagename=%s",
177 $dbh->qstr($pagename)));
180 return $this->_extract_page_data($result);
183 function _extract_page_data(&$query_result) {
184 extract($query_result);
185 $data = empty($pagedata) ? array() : unserialize($pagedata);
186 $data['hits'] = $hits;
190 function update_pagedata($pagename, $newdata) {
192 $page_tbl = $this->_table_names['page_tbl'];
194 // Hits is the only thing we can update in a fast manner.
195 if (count($newdata) == 1 && isset($newdata['hits'])) {
196 // Note that this will fail silently if the page does not
197 // have a record in the page table. Since it's just the
198 // hit count, who cares?
199 $dbh->Execute(sprintf("UPDATE $page_tbl SET hits=%d WHERE pagename=%s",
200 $newdata['hits'], $dbh->qstr($pagename)));
205 $data = $this->get_pagedata($pagename);
208 $this->_get_pageid($pagename, true); // Creates page record
211 @$hits = (int)$data['hits'];
212 unset($data['hits']);
214 foreach ($newdata as $key => $val) {
217 else if (empty($val))
223 $dbh->Execute(sprintf("UPDATE $page_tbl"
224 . " SET hits=%d, pagedata=%s"
225 . " WHERE pagename=%s",
227 $dbh->qstr(serialize($data)),
228 $dbh->qstr($pagename)));
233 function _get_pageid($pagename, $create_if_missing = false) {
236 $page_tbl = $this->_table_names['page_tbl'];
238 $query = sprintf("SELECT id FROM $page_tbl WHERE pagename=%s",
239 $dbh->qstr($pagename));
241 if (!$create_if_missing)
243 $rs = $dbh->Execute($query);
244 return $rs->fields['id'];
247 $rs = $dbh->Execute($query);
248 $id = $rs->fields['id'];
250 // kludge necessary because an assoc array is returned with a reserved name as the key
251 $rs = $dbh->Execute("SELECT MAX(id) AS M FROM $page_tbl");
252 $id = $rs->fields['M'] + 1;
253 $dbh->Execute(sprintf("INSERT INTO $page_tbl"
254 . " (id,pagename,hits)"
255 . " VALUES (%d,%s,0)",
256 $id, $dbh->qstr($pagename)));
262 function get_latest_version($pagename) {
264 extract($this->_table_names);
265 $rs = $dbh->Execute(sprintf("SELECT latestversion"
266 . " FROM $page_tbl, $recent_tbl"
267 . " WHERE $page_tbl.id=$recent_tbl.id"
268 . " AND pagename=%s",
269 $dbh->qstr($pagename)));
270 return (int)$rs->fields['latestversion'];
273 function get_previous_version($pagename, $version) {
275 extract($this->_table_names);
276 //Use SELECTLIMIT for maximum portability
277 $rs = $dbh->SelectLimit(sprintf("SELECT version"
278 . " FROM $version_tbl, $page_tbl"
279 . " WHERE $version_tbl.id=$page_tbl.id"
281 . " AND version < %d"
282 . " ORDER BY version DESC"
283 ,$dbh->qstr($pagename),
286 return (int)$rs->fields['version'];
292 * @param $version int Which version to get.
294 * @return hash The version data, or false if specified version does not
297 function get_versiondata($pagename, $version, $want_content = false) {
299 extract($this->_table_names);
301 assert(!empty($pagename));
302 assert($version > 0);
304 //trigger_error("GET_REVISION $pagename $version $want_content", E_USER_NOTICE);
305 // FIXME: optimization: sometimes don't get page data?
311 $fields = ("$page_tbl.*,"
312 . "mtime,minor_edit,versiondata,"
313 . "content<>'' AS have_content");
315 // removed ref to FETCH_MODE in next line
316 $result = $dbh->GetRow(sprintf("SELECT $fields"
317 . " FROM $page_tbl, $version_tbl"
318 . " WHERE $page_tbl.id=$version_tbl.id"
321 $dbh->qstr($pagename), $version));
323 return $this->_extract_version_data($result);
326 function _extract_version_data(&$query_result) {
330 extract($query_result);
331 $data = empty($versiondata) ? array() : unserialize($versiondata);
333 $data['mtime'] = $mtime;
334 $data['is_minor_edit'] = !empty($minor_edit);
337 $data['%content'] = $content;
338 elseif ($have_content)
339 $data['%content'] = true;
341 $data['%content'] = '';
343 // FIXME: this is ugly.
344 if (isset($pagename)) {
345 // Query also includes page data.
346 // We might as well send that back too...
347 $data['%pagedata'] = $this->_extract_page_data($query_result);
355 * Create a new revision of a page.
357 function set_versiondata($pagename, $version, $data) {
359 $version_tbl = $this->_table_names['version_tbl'];
361 $minor_edit = (int) !empty($data['is_minor_edit']);
362 unset($data['is_minor_edit']);
364 $mtime = (int)$data['mtime'];
365 unset($data['mtime']);
366 assert(!empty($mtime));
368 @$content = (string) $data['%content'];
369 unset($data['%content']);
371 unset($data['%pagedata']);
374 $id = $this->_get_pageid($pagename, true);
376 // FIXME: optimize: mysql can do this with one REPLACE INTO (I think).
377 $dbh->Execute(sprintf("DELETE FROM $version_tbl"
378 . " WHERE id=%d AND version=%d",
381 $dbh->Execute(sprintf("INSERT INTO $version_tbl"
382 . " (id,version,mtime,minor_edit,content,versiondata)"
383 . " VALUES(%d,%d,%d,%d,%s,%s)",
384 $id, $version, $mtime, $minor_edit,
385 $dbh->qstr($content),
386 $dbh->qstr(serialize($data))));
388 $this->_update_recent_table($id);
389 $this->_update_nonempty_table($id);
395 * Delete an old revision of a page.
397 function delete_versiondata($pagename, $version) {
399 extract($this->_table_names);
402 if ( ($id = $this->_get_pageid($pagename)) ) {
403 $dbh->Execute("DELETE FROM $version_tbl"
404 . " WHERE id=$id AND version=$version");
405 $this->_update_recent_table($id);
406 // This shouldn't be needed (as long as the latestversion
407 // never gets deleted.) But, let's be safe.
408 $this->_update_nonempty_table($id);
414 * Delete page from the database.
416 function delete_page($pagename) {
418 extract($this->_table_names);
421 if ( ($id = $this->_get_pageid($pagename, 'id')) ) {
422 $dbh->Execute("DELETE FROM $version_tbl WHERE id=$id");
423 $dbh->Execute("DELETE FROM $recent_tbl WHERE id=$id");
424 $dbh->Execute("DELETE FROM $nonempty_tbl WHERE id=$id");
425 $dbh->Execute("DELETE FROM $link_tbl WHERE linkfrom=$id");
426 $rs = $dbh->Execute("SELECT COUNT(*) AS C FROM $link_tbl WHERE linkto=$id");
427 $nlinks = $rs->fields['C'];
429 // We're still in the link table (dangling link) so we can't delete this
431 $dbh->Execute("UPDATE $page_tbl SET hits=0, pagedata='' WHERE id=$id");
434 $dbh->Execute("DELETE FROM $page_tbl WHERE id=$id");
436 $this->_update_recent_table();
437 $this->_update_nonempty_table();
443 // The only thing we might be interested in updating which we can
444 // do fast in the flags (minor_edit). I think the default
445 // update_versiondata will work fine...
446 //function update_versiondata($pagename, $version, $data) {
449 function set_links($pagename, $links) {
450 // Update link table.
451 // FIXME: optimize: mysql can do this all in one big INSERT.
454 extract($this->_table_names);
457 $pageid = $this->_get_pageid($pagename, true);
459 $dbh->Execute("DELETE FROM $link_tbl WHERE linkfrom=$pageid");
462 foreach($links as $link) {
463 if (isset($linkseen[$link]))
465 $linkseen[$link] = true;
466 $linkid = $this->_get_pageid($link, true);
467 $dbh->Execute("INSERT INTO $link_tbl (linkfrom, linkto)"
468 . " VALUES ($pageid, $linkid)");
475 * Find pages which link to or are linked from a page.
477 function get_links($pagename, $reversed = true) {
479 extract($this->_table_names);
482 list($have,$want) = array('linkee', 'linker');
484 list($have,$want) = array('linker', 'linkee');
486 $qpagename = $dbh->qstr($pagename);
487 // removed ref to FETCH_MODE in next line
488 $result = $dbh->Execute("SELECT $want.*"
489 . " FROM $link_tbl, $page_tbl AS linker, $page_tbl AS linkee"
490 . " WHERE linkfrom=linker.id AND linkto=linkee.id"
491 . " AND $have.pagename=$qpagename"
492 //. " GROUP BY $want.id"
493 . " ORDER BY $want.pagename");
495 return new WikiDB_backend_ADODB_iter($this, $result);
498 function get_all_pages($include_deleted) {
500 extract($this->_table_names);
502 if ($include_deleted) {
503 $result = $dbh->Execute("SELECT * FROM $page_tbl ORDER BY pagename");
506 $result = $dbh->Execute("SELECT $page_tbl.*"
507 . " FROM $nonempty_tbl, $page_tbl"
508 . " WHERE $nonempty_tbl.id=$page_tbl.id"
509 . " ORDER BY pagename");
512 return new WikiDB_backend_ADODB_iter($this, $result);
518 function text_search($search = '', $fullsearch = false) {
520 extract($this->_table_names);
522 $table = "$nonempty_tbl, $page_tbl";
523 $join_clause = "$nonempty_tbl.id=$page_tbl.id";
524 $fields = "$page_tbl.*";
525 $callback = new WikiMethodCb($this, '_sql_match_clause');
528 $table .= ", $recent_tbl";
529 $join_clause .= " AND $page_tbl.id=$recent_tbl.id";
531 $table .= ", $version_tbl";
532 $join_clause .= " AND $page_tbl.id=$version_tbl.id AND latestversion=version";
534 $fields .= ",$version_tbl.*";
535 $callback = new WikiMethodCb($this, '_fullsearch_sql_match_clause');
538 $search_clause = $search->makeSqlClause($callback);
540 $result = $dbh->Execute("SELECT $fields FROM $table"
541 . " WHERE $join_clause"
542 . " AND ($search_clause)"
543 . " ORDER BY pagename");
545 return new WikiDB_backend_ADODB_iter($this, $result);
548 function _sql_match_clause($word) {
549 $word = preg_replace('/(?=[%_\\\\])/', "\\", $word); //not sure if we need this. ADODB may do it for us
550 // (we need it for at least % and _ --- they're the wildcard characters
551 // for the LIKE operator, and we need to quote them if we're searching
552 // for literal '%'s or '_'s. --- I'm not sure about \, but it seems to
554 $word = $this->_dbh->qstr("%$word%");
555 return "LOWER(pagename) LIKE $word";
558 function _fullsearch_sql_match_clause($word) {
559 $word = preg_replace('/(?=[%_\\\\])/', "\\", $word); //not sure if we need this
561 $word = $this->_dbh->qstr("%$word%");
562 return "LOWER(pagename) LIKE $word OR content LIKE $word";
566 * Find highest or lowest hit counts.
568 function most_popular($limit) {
570 extract($this->_table_names);
576 $limit = $limit ? $limit : -1;
577 $result = $dbh->SelectLimit("SELECT $page_tbl.*"
578 . " FROM $nonempty_tbl, $page_tbl"
579 . " WHERE $nonempty_tbl.id=$page_tbl.id"
580 . " ORDER BY hits $order"
583 return new WikiDB_backend_ADODB_iter($this, $result);
587 * Find recent changes.
589 function most_recent($params) {
592 $include_minor_revisions = false;
593 $exclude_major_revisions = false;
594 $include_all_revisions = false;
598 extract($this->_table_names);
602 $pick[] = "mtime >= $since";
604 if ($include_all_revisions) {
605 // Include all revisions of each page.
606 $table = "$page_tbl, $version_tbl";
607 $join_clause = "$page_tbl.id=$version_tbl.id";
609 if ($exclude_major_revisions) {
610 // Include only minor revisions
611 $pick[] = "minor_edit <> 0";
613 elseif (!$include_minor_revisions) {
614 // Include only major revisions
615 $pick[] = "minor_edit = 0";
619 $table = "$page_tbl, $recent_tbl";
620 $join_clause = "$page_tbl.id=$recent_tbl.id";
621 $table .= ", $version_tbl";
622 $join_clause .= " AND $version_tbl.id=$page_tbl.id";
624 if ($exclude_major_revisions) {
625 // Include only most recent minor revision
626 $pick[] = 'version=latestminor';
628 elseif (!$include_minor_revisions) {
629 // Include only most recent major revision
630 $pick[] = 'version=latestmajor';
633 // Include only the latest revision (whether major or minor).
634 $pick[] ='version=latestversion';
638 $limit = $limit ? $limit : -1;
639 $where_clause = $join_clause;
641 $where_clause .= " AND " . join(" AND ", $pick);
643 // FIXME: use SQL_BUFFER_RESULT for mysql?
644 //Use SELECTLIMIT for portability
645 $result = $dbh->SelectLimit("SELECT $page_tbl.*,$version_tbl.*"
647 . " WHERE $where_clause"
648 . " ORDER BY mtime DESC",
651 return new WikiDB_backend_ADODB_iter($this, $result);
654 function _update_recent_table($pageid = false) {
656 extract($this->_table_names);
657 extract($this->_expressions);
659 $pageid = (int)$pageid;
663 $dbh->Execute("DELETE FROM $recent_tbl"
664 . ( $pageid ? " WHERE id=$pageid" : ""));
666 $dbh->Execute( "INSERT INTO $recent_tbl"
667 . " (id, latestversion, latestmajor, latestminor)"
668 . " SELECT id, $maxversion, $maxmajor, $maxminor"
669 . " FROM $version_tbl"
670 . ( $pageid ? " WHERE id=$pageid" : "")
675 function _update_nonempty_table($pageid = false) {
677 extract($this->_table_names);
679 $pageid = (int)$pageid;
683 $dbh->Execute("DELETE FROM $nonempty_tbl"
684 . ( $pageid ? " WHERE id=$pageid" : ""));
686 $dbh->Execute("INSERT INTO $nonempty_tbl (id)"
687 . " SELECT $recent_tbl.id"
688 . " FROM $recent_tbl, $version_tbl"
689 . " WHERE $recent_tbl.id=$version_tbl.id"
690 . " AND version=latestversion"
692 . ( $pageid ? " AND $recent_tbl.id=$pageid" : ""));
699 * Grab a write lock on the tables in the SQL database.
701 * Calls can be nested. The tables won't be unlocked until
702 * _unlock_database() is called as many times as _lock_database().
706 function lock($write_lock = true) {
707 if ($this->_lock_count++ == 0)
708 $this->_lock_tables($write_lock);
712 * Actually lock the required tables.
714 function _lock_tables($write_lock) {
715 trigger_error("virtual", E_USER_ERROR);
719 * Release a write lock on the tables in the SQL database.
723 * @param $force boolean Unlock even if not every call to lock() has been matched
724 * by a call to unlock().
726 * @see _lock_database
728 function unlock($force = false) {
729 if ($this->_lock_count == 0)
731 if (--$this->_lock_count <= 0 || $force) {
732 $this->_unlock_tables();
733 $this->_lock_count = 0;
738 * Actually unlock the required tables.
740 function _unlock_tables($write_lock) {
741 trigger_error("virtual", E_USER_ERROR);
745 * Callback for PEAR (DB) errors.
749 * @param A PEAR_error object.
751 /* function _pear_error_callback($error) {
752 if ($this->_is_false_error($error))
755 $this->_dbh->setErrorHandling(PEAR_ERROR_PRINT); // prevent recursive loops.
757 trigger_error($this->_pear_error_message($error), E_USER_ERROR);
761 * Detect false errors messages from PEAR DB.
763 * The version of PEAR DB which ships with PHP 4.0.6 has a bug in that
764 * it doesn't recognize "LOCK" and "UNLOCK" as SQL commands which don't
765 * return any data. (So when a "LOCK" command doesn't return any data,
766 * DB reports it as an error, when in fact, it's not.)
769 * @return bool True iff error is not really an error.
771 /* function _is_false_error($error) {
772 if ($error->getCode() != DB_ERROR)
775 $query = $this->_dbh->last_query;
777 if (! preg_match('/^\s*"?(INSERT|UPDATE|DELETE|REPLACE|CREATE'
778 . '|DROP|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s/', $query)) {
779 // Last query was not of the sort which doesn't return any data.
780 //" <--kludge for brain-dead syntax coloring
784 if (! in_array('ismanip', get_class_methods('DB'))) {
785 // Pear shipped with PHP 4.0.4pl1 (and before, presumably)
786 // does not have the DB::isManip method.
790 if (DB::isManip($query)) {
791 // If Pear thinks it's an isManip then it wouldn't have thrown
792 // the error we're testing for....
799 /* function _pear_error_message($error) {
800 $class = get_class($this);
801 $message = "$class: fatal database error\n"
802 . "\t" . $error->getMessage() . "\n"
803 . "\t(" . $error->getDebugInfo() . ")\n";
805 // Prevent password from being exposed during a connection error
806 $safe_dsn = preg_replace('| ( :// .*? ) : .* (?=@) |xs',
807 '\\1:XXXXXXXX', $this->_dsn);
808 return str_replace($this->_dsn, $safe_dsn, $message);
812 * Filter PHP errors notices from PEAR DB code.
814 * The PEAR DB code which ships with PHP 4.0.6 produces spurious
815 * errors and notices. This is an error callback (for use with
816 * ErrorManager which will filter out those spurious messages.)
817 * @see _is_false_error, ErrorManager
820 /* function _pear_notice_filter($err) {
821 return ( $err->isNotice()
822 && preg_match('|DB[/\\\\]common.php$|', $err->errfile)
823 && $err->errline == 126
824 && preg_match('/Undefined offset: +0\b/', $err->errstr) );
829 class WikiDB_backend_ADODB_iter
830 extends WikiDB_backend_iterator
832 function WikiDB_backend_ADODB_iter(&$backend, &$query_result) {
833 // ADODB equivalent of this? May not matter, since we should never get here
834 /* if (DB::isError($query_result)) {
835 // This shouldn't happen, I thought.
836 $backend->_pear_error_callback($query_result);
839 $this->_backend = &$backend;
840 $this->_result = $query_result;
844 $result = &$this->_result;
845 if (!$result || $result->EOF) {
850 // $record = $this->_result->fetchRow(DB_FETCHMODE_ASSOC);
851 $record = $result->fields;
854 $backend = &$this->_backend;
856 $pagedata = $backend->_extract_page_data($record);
857 $rec = array('pagename' => $record['pagename'],
858 'pagedata' => $pagedata);
860 if (!empty($record['version'])) {
861 $rec['versiondata'] = $backend->_extract_version_data($record);
862 $rec['version'] = $record['version'];
869 if ($this->_result) {
870 // $this->_result->free();
871 $this->_result->Close();
872 $this->_result = false;
877 // Following function taken from adodb-pear.inc.php. Eventually, change index.php to provide the relevant information
880 * Parse a data source name
882 * @param $dsn string Data Source Name to be parsed
884 * @return array an associative array with the following keys:
886 * phptype: Database backend used in PHP (mysql, odbc etc.)
887 * dbsyntax: Database used with regards to SQL syntax etc.
888 * protocol: Communication protocol to use (tcp, unix etc.)
889 * hostspec: Host specification (hostname[:port])
890 * database: Database to use on the DBMS server
891 * username: User name for login
892 * password: Password for login
894 * The format of the supplied DSN is in its fullest form:
896 * phptype(dbsyntax)://username:password@protocol+hostspec/database
898 * Most variations are allowed:
900 * phptype://username:password@protocol+hostspec:110//usr/db_file.db
901 * phptype://username:password@hostspec/database_name
902 * phptype://username:password@hostspec
903 * phptype://username@hostspec
904 * phptype://hostspec/database
909 * @author Tomas V.V.Cox <cox@idecnet.com>
911 function parseDSN($dsn)
913 if (is_array($dsn)) {
927 // Find phptype and dbsyntax
928 if (($pos = strpos($dsn, '://')) !== false) {
929 $str = substr($dsn, 0, $pos);
930 $dsn = substr($dsn, $pos + 3);
936 // Get phptype and dbsyntax
937 // $str => phptype(dbsyntax)
938 if (preg_match('|^(.+?)\((.*?)\)$|', $str, $arr)) {
939 $parsed['phptype'] = $arr[1];
940 $parsed['dbsyntax'] = (empty($arr[2])) ? $arr[1] : $arr[2];
942 $parsed['phptype'] = $str;
943 $parsed['dbsyntax'] = $str;
950 // Get (if found): username and password
951 // $dsn => username:password@protocol+hostspec/database
952 if (($at = strpos($dsn,'@')) !== false) {
953 $str = substr($dsn, 0, $at);
954 $dsn = substr($dsn, $at + 1);
955 if (($pos = strpos($str, ':')) !== false) {
956 $parsed['username'] = urldecode(substr($str, 0, $pos));
957 $parsed['password'] = urldecode(substr($str, $pos + 1));
959 $parsed['username'] = urldecode($str);
963 // Find protocol and hostspec
964 // $dsn => protocol+hostspec/database
965 if (($pos=strpos($dsn, '/')) !== false) {
966 $str = substr($dsn, 0, $pos);
967 $dsn = substr($dsn, $pos + 1);
973 // Get protocol + hostspec
974 // $str => protocol+hostspec
975 if (($pos=strpos($str, '+')) !== false) {
976 $parsed['protocol'] = substr($str, 0, $pos);
977 $parsed['hostspec'] = urldecode(substr($str, $pos + 1));
979 $parsed['hostspec'] = urldecode($str);
985 $parsed['database'] = $dsn;
993 // (c-file-style: "gnu")
998 // c-hanging-comment-ender-p: nil
999 // indent-tabs-mode: nil