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 WikiDB_backend_PDO ($dbparams) {
34 $this->_dbparams = $dbparams;
35 if (strstr($dbparams['dsn'], "://")) { // pear DB syntax
36 $parsed = parseDSN($dbparams['dsn']);
37 $this->_parsedDSN = $parsed;
38 /* Need to convert the DSN
39 * dbtype(dbsyntax)://username:password@protocol+hostspec/database?option=value&option2=value2
40 * to dbtype:option1=value1;option2=value2
41 * PDO passes the DSN verbatim to the driver. But we need to extract the username + password
42 * and cannot rely that the parseDSN keys have the same name as needed by the driver.
43 * e.g: odbc:DSN=SAMPLE;UID=db2inst1;PWD=ibmdb2, mysql:host=127.0.0.1;dbname=testdb
45 $driver = $parsed['phptype'];
46 unset($parsed['phptype']);
47 unset($parsed['dbsyntax']);
48 $dbparams['dsn'] = $driver . ":";
49 $this->_dbh->database = $parsed['database'];
50 // mysql needs to map database=>dbname, hostspec=>host. TODO for the others.
51 $dsnmap = array('mysql' => array('database'=>'dbname', 'hostspec' => 'host')
53 foreach (array('protocol','hostspec','port','socket','database') as $option) {
54 if (!empty($parsed[$option])) {
55 $optionname = (isset($dsnmap[$driver][$option]) and !isset($parsed[$optionname]))
56 ? $dsnmap[$driver][$option]
58 $dbparams['dsn'] .= ($optionname . "=" . $parsed[$option] . ";");
59 unset($parsed[$optionname]);
61 unset($parsed[$option]);
63 unset($parsed['username']);
64 unset($parsed['password']);
65 // pass the rest verbatim to the driver.
66 foreach ($parsed as $option => $value) {
67 $dbparams['dsn'] .= ($option . "=" . $value . ";");
70 list($driver, $dsn) = explode(":", $dbparams['dsn'], 2);
71 foreach (explode(";", trim($dsn)) as $pair) {
73 list($option, $value) = explode("=", $pair, 2);
74 $this->_parsedDSN[$option] = $value;
77 $this->_dbh->database = isset($this->_parsedDSN['database'])
78 ? $this->_parsedDSN['database']
79 : $this->_parsedDSN['dbname'];
81 if (empty($this->_parsedDSN['password'])) $this->_parsedDSN['password'] = '';
84 // try to load it dynamically (unix only)
85 if (!loadPhpExtension("pdo_$driver")) {
86 echo $GLOBALS['php_errormsg'], "<br>\n";
87 trigger_error(sprintf("dl() problem: Required extension '%s' could not be loaded!",
92 // persistent is defined as DSN option, or with a config value.
93 // phptype://username:password@hostspec/database?persistent=false
94 $this->_dbh = new PDO($dbparams['dsn'],
95 $this->_parsedDSN['username'],
96 $this->_parsedDSN['password'],
97 array(PDO_ATTR_AUTOCOMMIT => true,
98 PDO_ATTR_TIMEOUT => DATABASE_TIMEOUT,
99 PDO_ATTR_PERSISTENT => !empty($parsed['persistent'])
100 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();
128 catch (PDOException $e) {
129 $this->_hasTransactions = false;
131 $sth = $this->_dbh->prepare("SELECT version()");
133 $this->_serverinfo['version'] = $sth->fetchSingle();
134 $this->commit(); // required to match the try catch block above!
136 $prefix = isset($dbparams['prefix']) ? $dbparams['prefix'] : '';
138 = array('page_tbl' => $prefix . 'page',
139 'version_tbl' => $prefix . 'version',
140 'link_tbl' => $prefix . 'link',
141 'recent_tbl' => $prefix . 'recent',
142 'nonempty_tbl' => $prefix . 'nonempty');
143 $page_tbl = $this->_table_names['page_tbl'];
144 $version_tbl = $this->_table_names['version_tbl'];
145 $this->page_tbl_fields = "$page_tbl.id AS id, $page_tbl.pagename AS pagename, "
146 . "$page_tbl.hits hits";
147 $this->page_tbl_field_list = array('id', 'pagename', 'hits');
148 $this->version_tbl_fields = "$version_tbl.version AS version, "
149 . "$version_tbl.mtime AS mtime, "
150 . "$version_tbl.minor_edit AS minor_edit, $version_tbl.content AS content, "
151 . "$version_tbl.versiondata AS versiondata";
152 $this->version_tbl_field_list = array('version', 'mtime', 'minor_edit', 'content',
156 = array('maxmajor' => "MAX(CASE WHEN minor_edit=0 THEN version END)",
157 'maxminor' => "MAX(CASE WHEN minor_edit<>0 THEN version END)",
158 'maxversion' => "MAX(version)",
159 'notempty' => "<>''",
160 'iscontent' => "$version_tbl.content<>''");
161 $this->_lock_count = 0;
164 function beginTransaction() {
165 if ($this->_hasTransactions)
166 $this->_dbh->beginTransaction();
169 if ($this->_hasTransactions)
170 $this->_dbh->commit();
172 function rollback() {
173 if ($this->_hasTransactions)
174 $this->_dbh->rollback();
177 function query($sql) {
178 $sth = $this->_dbh->prepare($sql);
179 return $sth->execute();
181 /* with one result row */
182 function getRow($sql) {
183 $sth = $this->_dbh->prepare($sql);
185 return $sth->fetch(PDO_FETCH_BOTH);
191 * Close database connection.
196 if ($this->_lock_count) {
197 trigger_error("WARNING: database still locked " .
198 '(lock_count = $this->_lock_count)' . "\n<br />",
201 $this->unlock(false, 'force');
207 * Fast test for wikipage.
209 function is_wiki_page($pagename) {
211 extract($this->_table_names);
212 $sth = $dbh->prepare("SELECT $page_tbl.id AS id"
213 . " FROM $nonempty_tbl, $page_tbl"
214 . " WHERE $nonempty_tbl.id=$page_tbl.id"
215 . " AND pagename=?");
216 $sth->bindParam(1, $pagename, PDO_PARAM_STR, 100);
218 return $sth->fetchSingle();
223 function get_all_pagenames() {
225 extract($this->_table_names);
226 $sth = $dbh->exec("SELECT pagename"
227 . " FROM $nonempty_tbl, $page_tbl"
228 . " WHERE $nonempty_tbl.id=$page_tbl.id"
230 return $sth->fetchAll(PDO_FETCH_NUM);
233 function numPages($filter=false, $exclude='') {
235 extract($this->_table_names);
236 $sth = $dbh->exec("SELECT count(*)"
237 . " FROM $nonempty_tbl, $page_tbl"
238 . " WHERE $nonempty_tbl.id=$page_tbl.id");
239 return $sth->fetchSingle();
242 function increaseHitCount($pagename) {
244 $page_tbl = $this->_table_names['page_tbl'];
245 $sth = $dbh->prepare("UPDATE $page_tbl SET hits=hits+1"
246 . " WHERE pagename=?"
248 $sth->bindParam(1, $pagename, PDO_PARAM_STR, 100);
253 * Read page information from database.
255 function get_pagedata($pagename) {
257 $page_tbl = $this->_table_names['page_tbl'];
258 $sth = $dbh->prepare("SELECT id,pagename,hits,pagedata FROM $page_tbl"
259 ." WHERE pagename=? LIMIT 1");
260 $sth->bindParam(1, $pagename, PDO_PARAM_STR, 100);
262 $row = $sth->fetch(PDO_FETCH_NUM);
263 return $row ? $this->_extract_page_data($row[3], $row[2]) : false;
266 function _extract_page_data($data, $hits) {
268 return array('hits' => $hits);
270 return array_merge(array('hits' => $hits), $this->_unserialize($data));
273 function update_pagedata($pagename, $newdata) {
275 $page_tbl = $this->_table_names['page_tbl'];
277 // Hits is the only thing we can update in a fast manner.
278 if (count($newdata) == 1 && isset($newdata['hits'])) {
279 // Note that this will fail silently if the page does not
280 // have a record in the page table. Since it's just the
281 // hit count, who cares?
282 $sth = $dbh->prepare("UPDATE $page_tbl SET hits=? WHERE pagename=? LIMIT 1");
283 $sth->bindParam(1, $newdata['hits'], PDO_PARAM_INT);
284 $sth->bindParam(2, $pagename, PDO_PARAM_STR, 100);
288 $this->beginTransaction();
289 $data = $this->get_pagedata($pagename);
292 $this->_get_pageid($pagename, true); // Creates page record
295 $hits = (empty($data['hits'])) ? 0 : (int)$data['hits'];
296 unset($data['hits']);
298 foreach ($newdata as $key => $val) {
301 else if (empty($val))
306 $sth = $dbh->prepare("UPDATE $page_tbl"
307 . " SET hits=?, pagedata=?"
308 . " WHERE pagename=?"
310 $sth->bindParam(1, $hits, PDO_PARAM_INT);
311 $sth->bindParam(2, $this->_serialize($data), PDO_PARAM_LOB);
312 $sth->bindParam(3, $pagename, PDO_PARAM_STR, 100);
313 if ($sth->execute()) {
322 function get_cached_html($pagename) {
324 $page_tbl = $this->_table_names['page_tbl'];
325 $sth = $dbh->prepare("SELECT cached_html FROM $page_tbl WHERE pagename=? LIMIT 1");
326 $sth->bindParam(1, $pagename, PDO_PARAM_STR, 100);
328 return $sth->fetchSingle(PDO_FETCH_NUM);
331 function set_cached_html($pagename, $data) {
333 $page_tbl = $this->_table_names['page_tbl'];
334 if (empty($data)) $data = '';
335 $sth = $dbh->prepare("UPDATE $page_tbl"
336 . " SET cached_html=?"
337 . " WHERE pagename=?"
339 $sth->bindParam(1, $data, PDO_PARAM_STR);
340 $sth->bindParam(2, $pagename, PDO_PARAM_STR, 100);
344 function _get_pageid($pagename, $create_if_missing = false) {
347 $cache =& $request->_dbi->_cache->_id_cache;
348 if (isset($cache[$pagename])) {
349 if ($cache[$pagename] or !$create_if_missing) {
350 return $cache[$pagename];
354 // attributes play this game.
355 if ($pagename === '') return 0;
358 $page_tbl = $this->_table_names['page_tbl'];
359 $sth = $dbh->prepare("SELECT id FROM $page_tbl WHERE pagename=? LIMIT 1");
360 $sth->bindParam(1, $pagename, PDO_PARAM_STR, 100);
361 $id = $sth->fetchSingle();
362 if (! $create_if_missing ) {
366 //mysql, mysqli or mysqlt
367 if (substr($dbh->databaseType,0,5) == 'mysql') {
368 // have auto-incrementing, atomic version
369 $sth = $dbh->prepare("INSERT INTO $page_tbl"
371 . " VALUES (NULL,?)");
372 $sth->bindParam(1, $pagename, PDO_PARAM_STR, 100);
374 $id = $dbh->lastInsertId();
376 $this->beginTransaction();
377 $sth = $dbh->prepare("SELECT MAX(id) FROM $page_tbl");
379 $id = $sth->fetchSingle();
380 $sth = $dbh->prepare("INSERT INTO $page_tbl"
381 . " (id,pagename,hits)"
382 . " VALUES (?,?,0)");
384 $sth->bindParam(1, $id, PDO_PARAM_INT);
385 $sth->bindParam(2, $pagename, PDO_PARAM_STR, 100);
396 function get_latest_version($pagename) {
398 extract($this->_table_names);
399 $sth = $dbh->prepare("SELECT latestversion"
400 . " FROM $page_tbl, $recent_tbl"
401 . " WHERE $page_tbl.id=$recent_tbl.id"
404 $sth->bindParam(1, $pagename, PDO_PARAM_STR, 100);
406 return $sth->fetchSingle();
409 function get_previous_version($pagename, $version) {
411 extract($this->_table_names);
412 $sth = $dbh->prepare("SELECT version"
413 . " FROM $version_tbl, $page_tbl"
414 . " WHERE $version_tbl.id=$page_tbl.id"
417 . " ORDER BY version DESC"
419 $sth->bindParam(1, $pagename, PDO_PARAM_STR, 100);
420 $sth->bindParam(2, $version, PDO_PARAM_INT);
422 return $sth->fetchSingle();
428 * @param $version int Which version to get.
430 * @return hash The version data, or false if specified version does not
433 function get_versiondata($pagename, $version, $want_content = false) {
435 extract($this->_table_names);
436 extract($this->_expressions);
438 assert(is_string($pagename) and $pagename != '');
439 assert($version > 0);
441 // FIXME: optimization: sometimes don't get page data?
443 $fields = $this->page_tbl_fields . ", $page_tbl.pagedata AS pagedata"
444 . ', ' . $this->version_tbl_fields;
446 $fields = $this->page_tbl_fields . ", '' AS pagedata"
447 . ", $version_tbl.version AS version, $version_tbl.mtime AS mtime, "
448 . "$version_tbl.minor_edit AS minor_edit, $iscontent AS have_content, "
449 . "$version_tbl.versiondata as versiondata";
451 $sth = $dbh->prepare("SELECT $fields"
452 . " FROM $page_tbl, $version_tbl"
453 . " WHERE $page_tbl.id=$version_tbl.id"
457 $sth->bindParam(1, $pagename, PDO_PARAM_STR, 100);
458 $sth->bindParam(2, $version, PDO_PARAM_INT);
460 $row = $sth->fetch(PDO_FETCH_NUM);
461 return $row ? $this->_extract_version_data_num($row, $want_content) : false;
464 function _extract_version_data_num($row, $want_content) {
469 //$pagename &= $row[1];
470 $data = empty($row[8]) ? array() : $this->_unserialize($row[8]);
471 $data['mtime'] = $row[5];
472 $data['is_minor_edit'] = !empty($row[6]);
474 $data['%content'] = $row[7];
476 $data['%content'] = !empty($row[7]);
478 if (!empty($row[3])) {
479 $data['%pagedata'] = $this->_extract_page_data($row[3], $row[2]);
484 function _extract_version_data_assoc($row) {
489 $data = empty($versiondata) ? array() : $this->_unserialize($versiondata);
490 $data['mtime'] = $mtime;
491 $data['is_minor_edit'] = !empty($minor_edit);
493 $data['%content'] = $content;
494 elseif ($have_content)
495 $data['%content'] = true;
497 $data['%content'] = '';
498 if (!empty($pagedata)) {
499 $data['%pagedata'] = $this->_extract_page_data($pagedata, $hits);
505 * Create a new revision of a page.
507 function set_versiondata($pagename, $version, $data) {
509 $version_tbl = $this->_table_names['version_tbl'];
511 $minor_edit = (int) !empty($data['is_minor_edit']);
512 unset($data['is_minor_edit']);
514 $mtime = (int)$data['mtime'];
515 unset($data['mtime']);
516 assert(!empty($mtime));
518 @$content = (string) $data['%content'];
519 unset($data['%content']);
520 unset($data['%pagedata']);
522 $this->lock(array('page','recent','version','nonempty'));
523 $this->beginTransaction();
524 $id = $this->_get_pageid($pagename, true);
525 $backend_type = $this->backendType();
526 // optimize: mysql can do this with one REPLACE INTO.
527 if (substr($backend_type,0,5) == 'mysql') {
528 $sth = $dbh->prepare("REPLACE INTO $version_tbl"
529 . " (id,version,mtime,minor_edit,content,versiondata)"
530 . " VALUES(?,?,?,?,?,?)");
531 $sth->bindParam(1, $id, PDO_PARAM_INT);
532 $sth->bindParam(2, $version, PDO_PARAM_INT);
533 $sth->bindParam(3, $mtime, PDO_PARAM_INT);
534 $sth->bindParam(4, $minor_edit, PDO_PARAM_INT);
535 $sth->bindParam(5, $content, PDO_PARAM_STR, 100);
536 $sth->bindParam(6, $this->_serialize($data), PDO_PARAM_STR, 100);
537 $rs = $sth->execute();
539 $sth = $dbh->prepare("DELETE FROM $version_tbl"
540 . " WHERE id=? AND version=?");
541 $sth->bindParam(1, $id, PDO_PARAM_INT);
542 $sth->bindParam(2, $version, PDO_PARAM_INT);
544 $sth = $dbh->prepare("INSERT INTO $version_tbl"
545 . " (id,version,mtime,minor_edit,content,versiondata)"
546 . " VALUES(?,?,?,?,?,?)");
547 $sth->bindParam(1, $id, PDO_PARAM_INT);
548 $sth->bindParam(2, $version, PDO_PARAM_INT);
549 $sth->bindParam(3, $mtime, PDO_PARAM_INT);
550 $sth->bindParam(4, $minor_edit, PDO_PARAM_INT);
551 $sth->bindParam(5, $content, PDO_PARAM_STR, 100);
552 $sth->bindParam(6, $this->_serialize($data), PDO_PARAM_STR, 100);
553 $rs = $sth->execute();
555 $this->_update_recent_table($id);
556 $this->_update_nonempty_table($id);
557 if ($rs) $this->commit( );
558 else $this->rollBack( );
559 $this->unlock(array('page','recent','version','nonempty'));
563 * Delete an old revision of a page.
565 function delete_versiondata($pagename, $version) {
567 extract($this->_table_names);
569 $this->lock(array('version'));
570 if ( ($id = $this->_get_pageid($pagename)) ) {
571 $dbh->query("DELETE FROM $version_tbl"
572 . " WHERE id=$id AND version=$version");
573 $this->_update_recent_table($id);
574 // This shouldn't be needed (as long as the latestversion
575 // never gets deleted.) But, let's be safe.
576 $this->_update_nonempty_table($id);
578 $this->unlock(array('version'));
582 * Delete page from the database with backup possibility.
583 * i.e save_page('') and DELETE nonempty id
585 * deletePage increments latestversion in recent to a non-existent version,
586 * and removes the nonempty row,
587 * so that get_latest_version returns id+1 and get_previous_version returns prev id
588 * and page->exists returns false.
590 function delete_page($pagename) {
592 extract($this->_table_names);
594 $this->beginTransaction();
595 //$dbh->CommitLock($recent_tbl);
596 if (($id = $this->_get_pageid($pagename, false)) === false) {
601 $user =& $GLOBALS['request']->_user;
602 $meta = array('author' => $user->getId(),
603 'author_id' => $user->getAuthenticatedId(),
605 $this->lock(array('version','recent','nonempty','page','link'));
606 $version = $this->get_latest_version($pagename);
607 if ($dbh->query("UPDATE $recent_tbl SET latestversion=latestversion+1,"
608 . "latestmajor=latestversion+1,latestminor=NULL WHERE id=$id"))
610 $insert = $dbh->prepare("INSERT INTO $version_tbl"
611 . " (id,version,mtime,minor_edit,content,versiondata)"
612 . " VALUES(?,?,?,?,?,?)");
613 $insert->bindParam(1, $id, PDO_PARAM_INT);
614 $insert->bindParam(2, $version + 1, PDO_PARAM_INT);
615 $insert->bindParam(3, $mtime, PDO_PARAM_INT);
616 $insert->bindParam(4, 0, PDO_PARAM_INT);
617 $insert->bindParam(5, '', PDO_PARAM_STR, 100);
618 $insert->bindParam(6, $this->_serialize($meta), PDO_PARAM_STR, 100);
619 if ($insert->execute()
620 and $dbh->query("DELETE FROM $nonempty_tbl WHERE id=$id")
621 and $this->set_links($pagename, false)) {
622 // need to keep perms and LOCKED, otherwise you can reset the perm
623 // by action=remove and re-create it with default perms
624 // keep hits but delete meta-data
625 //and $dbh->Execute("UPDATE $page_tbl SET pagedata='' WHERE id=$id")
626 $this->unlock(array('version','recent','nonempty','page','link'));
631 $this->unlock(array('version','recent','nonempty','page','link'));
637 function purge_page($pagename) {
639 extract($this->_table_names);
641 $this->lock(array('version','recent','nonempty','page','link'));
642 if ( ($id = $this->_get_pageid($pagename, false)) ) {
643 $dbh->query("DELETE FROM $version_tbl WHERE id=$id");
644 $dbh->query("DELETE FROM $recent_tbl WHERE id=$id");
645 $dbh->query("DELETE FROM $nonempty_tbl WHERE id=$id");
646 $this->set_links($pagename, false);
647 $sth = $dbh->prepare("SELECT COUNT(*) FROM $link_tbl WHERE linkto=$id");
649 if ($sth->fetchSingle()) {
650 // We're still in the link table (dangling link) so we can't delete this
652 $dbh->query("UPDATE $page_tbl SET hits=0, pagedata='' WHERE id=$id");
656 $dbh->query("DELETE FROM $page_tbl WHERE id=$id");
660 $result = -1; // already purged or not existing
662 $this->unlock(array('version','recent','nonempty','page','link'));
666 // The only thing we might be interested in updating which we can
667 // do fast in the flags (minor_edit). I think the default
668 // update_versiondata will work fine...
669 //function update_versiondata($pagename, $version, $data) {
672 function set_links($pagename, $links) {
673 // Update link table.
674 // FIXME: optimize: mysql can do this all in one big INSERT/REPLACE.
677 extract($this->_table_names);
679 $this->lock(array('link'));
680 $pageid = $this->_get_pageid($pagename, true);
683 $dbh->query("DELETE FROM $link_tbl WHERE linkfrom=$pageid");
684 foreach ($links as $link) {
685 if (isset($linkseen[$link]))
687 $linkseen[$link] = true;
688 $linkid = $this->_get_pageid($link, true);
690 $dbh->query("INSERT INTO $link_tbl (linkfrom, linkto)"
691 . " VALUES ($pageid, $linkid)");
694 // purge page table: delete all non-referenced pages
695 // for all previously linked pages...
696 $sth = $dbh->prepare("SELECT $link_tbl.linkto as id FROM $link_tbl".
697 " WHERE linkfrom=$pageid");
699 foreach ($sth->fetchAll(PDO_FETCH_NUM) as $id) {
700 // ...check if the page is empty and has no version
701 $sth1 = $dbh->prepare("SELECT $page_tbl.id FROM $page_tbl"
702 . " LEFT JOIN $nonempty_tbl USING (id) "
703 . " LEFT JOIN $version_tbl USING (id)"
704 . " WHERE ISNULL($nonempty_tbl.id) AND"
705 . " ISNULL($version_tbl.id) AND $page_tbl.id=$id");
707 if ($sth1->fetchSingle()) {
708 $dbh->query("DELETE FROM $page_tbl WHERE id=$id"); // this purges the link
709 $dbh->query("DELETE FROM $recent_tbl WHERE id=$id"); // may fail
712 $dbh->query("DELETE FROM $link_tbl WHERE linkfrom=$pageid");
714 $this->unlock(array('link'));
719 * Find pages which link to or are linked from a page.
721 * Optimization: save request->_dbi->_iwpcache[] to avoid further iswikipage checks
722 * (linkExistingWikiWord or linkUnknownWikiWord)
723 * This is called on every page header GleanDescription, so we can store all the existing links.
725 function get_links($pagename, $reversed=true, $include_empty=false,
726 $sortby='', $limit='', $exclude='') {
728 extract($this->_table_names);
731 list($have,$want) = array('linkee', 'linker');
733 list($have,$want) = array('linker', 'linkee');
734 $orderby = $this->sortby($sortby, 'db', array('pagename'));
735 if ($orderby) $orderby = " ORDER BY $want." . $orderby;
736 if ($exclude) // array of pagenames
737 $exclude = " AND $want.pagename NOT IN ".$this->_sql_set($exclude);
740 $limit = $this->_limit_sql($limit);
742 $sth = $dbh->prepare("SELECT $want.id AS id, $want.pagename AS pagename,"
743 . " $want.hits AS hits"
744 . " FROM $link_tbl, $page_tbl linker, $page_tbl linkee"
745 . (!$include_empty ? ", $nonempty_tbl" : '')
746 . " WHERE linkfrom=linker.id AND linkto=linkee.id"
747 . " AND $have.pagename=?"
748 . (!$include_empty ? " AND $nonempty_tbl.id=$want.id" : "")
749 //. " GROUP BY $want.id"
753 $sth->bindParam(1, $pagename, PDO_PARAM_STR, 100);
755 $result = $sth->fetch(PDO_FETCH_BOTH);
756 return new WikiDB_backend_PDO_iter($this, $result, $this->page_tbl_field_list);
760 * Find if a page links to another page
762 function exists_link($pagename, $link, $reversed=false) {
764 extract($this->_table_names);
767 list($have, $want) = array('linkee', 'linker');
769 list($have, $want) = array('linker', 'linkee');
770 $sth = $dbh->prepare("SELECT CASE WHEN $want.pagename THEN 1 ELSE 0 END"
771 . " FROM $link_tbl, $page_tbl linker, $page_tbl linkee, $nonempty_tbl"
772 . " WHERE linkfrom=linker.id AND linkto=linkee.id"
773 . " AND $have.pagename=?"
774 . " AND $want.pagename=?");
775 $sth->bindParam(1, $pagename, PDO_PARAM_STR, 100);
776 $sth->bindParam(2, $link, PDO_PARAM_STR, 100);
778 return $sth->fetchSingle();
781 function get_all_pages($include_empty=false, $sortby='', $limit='', $exclude='') {
783 extract($this->_table_names);
784 $orderby = $this->sortby($sortby, 'db');
785 if ($orderby) $orderby = ' ORDER BY ' . $orderby;
786 if ($exclude) // array of pagenames
787 $exclude = " AND $page_tbl.pagename NOT IN ".$this->_sql_set($exclude);
790 $limit = $this->_limit_sql($limit);
792 if (strstr($orderby, 'mtime ')) { // was ' mtime'
793 if ($include_empty) {
795 . $this->page_tbl_fields
796 ." FROM $page_tbl, $recent_tbl, $version_tbl"
797 . " WHERE $page_tbl.id=$recent_tbl.id"
798 . " AND $page_tbl.id=$version_tbl.id AND latestversion=version"
804 . $this->page_tbl_fields
805 . " FROM $nonempty_tbl, $page_tbl, $recent_tbl, $version_tbl"
806 . " WHERE $nonempty_tbl.id=$page_tbl.id"
807 . " AND $page_tbl.id=$recent_tbl.id"
808 . " AND $page_tbl.id=$version_tbl.id AND latestversion=version"
813 if ($include_empty) {
815 . $this->page_tbl_fields
817 . ($exclude ? " WHERE $exclude" : '')
821 . $this->page_tbl_fields
822 . " FROM $nonempty_tbl, $page_tbl"
823 . " WHERE $nonempty_tbl.id=$page_tbl.id"
828 $sth = $dbh->prepare($sql . $limit);
830 $result = $sth->fetch(PDO_FETCH_BOTH);
831 return new WikiDB_backend_PDO_iter($this, $result, $this->page_tbl_field_list);
837 function text_search($search, $fullsearch=false, $sortby='', $limit='', $exclude='') {
839 extract($this->_table_names);
840 $orderby = $this->sortby($sortby, 'db');
841 if ($orderby) $orderby = ' ORDER BY ' . $orderby;
842 $limit = $this->_limit_sql($limit);
844 $table = "$nonempty_tbl, $page_tbl";
845 $join_clause = "$nonempty_tbl.id=$page_tbl.id";
846 $fields = $this->page_tbl_fields;
847 $field_list = $this->page_tbl_field_list;
848 $searchobj = new WikiDB_backend_PDO_search($search, $dbh);
851 $table .= ", $recent_tbl";
852 $join_clause .= " AND $page_tbl.id=$recent_tbl.id";
854 $table .= ", $version_tbl";
855 $join_clause .= " AND $page_tbl.id=$version_tbl.id AND latestversion=version";
857 $fields .= ",$page_tbl.pagedata as pagedata," . $this->version_tbl_fields;
858 $field_list = array_merge($field_list, array('pagedata'), $this->version_tbl_field_list);
859 $callback = new WikiMethodCb($searchobj, "_fulltext_match_clause");
861 $callback = new WikiMethodCb($searchobj, "_pagename_match_clause");
864 $search_clause = $search->makeSqlClauseObj($callback);
865 $sth = $dbh->prepare("SELECT $fields FROM $table"
866 . " WHERE $join_clause"
867 . " AND ($search_clause)"
871 $result = $sth->fetch(PDO_FETCH_NUM);
872 $iter = new WikiDB_backend_PDO_iter($this, $result, $field_list);
873 $iter->stoplisted = $searchobj->stoplisted;
878 * TODO: efficiently handle wildcards exclusion: exclude=Php* => 'Php%',
879 * not sets. See above, but the above methods find too much.
880 * This is only for already resolved wildcards:
881 * " WHERE $page_tbl.pagename NOT IN ".$this->_sql_set(array('page1','page2'));
883 function _sql_set(&$pagenames) {
885 foreach ($pagenames as $p) {
886 $s .= ($this->_dbh->qstr($p).",");
888 return substr($s,0,-1).")";
892 * Find highest or lowest hit counts.
894 function most_popular($limit=20, $sortby='-hits') {
896 extract($this->_table_names);
903 $where = " AND hits > 0";
905 if ($sortby != '-hits') {
906 if ($order = $this->sortby($sortby, 'db')) $orderby = " ORDER BY " . $order;
909 $orderby = " ORDER BY hits $order";
911 . $this->page_tbl_fields
912 . " FROM $nonempty_tbl, $page_tbl"
913 . " WHERE $nonempty_tbl.id=$page_tbl.id"
917 $sth = $dbh->prepare($sql . $this->_limit_sql($limit));
919 $sth = $dbh->prepare($sql);
922 $result = $sth->fetch(PDO_FETCH_NUM);
923 return new WikiDB_backend_PDO_iter($this, $result, $this->page_tbl_field_list);
927 * Find recent changes.
929 function most_recent($params) {
932 $include_minor_revisions = false;
933 $exclude_major_revisions = false;
934 $include_all_revisions = false;
938 extract($this->_table_names);
942 $pick[] = "mtime >= $since";
944 if ($include_all_revisions) {
945 // Include all revisions of each page.
946 $table = "$page_tbl, $version_tbl";
947 $join_clause = "$page_tbl.id=$version_tbl.id";
949 if ($exclude_major_revisions) {
950 // Include only minor revisions
951 $pick[] = "minor_edit <> 0";
953 elseif (!$include_minor_revisions) {
954 // Include only major revisions
955 $pick[] = "minor_edit = 0";
959 $table = "$page_tbl, $recent_tbl";
960 $join_clause = "$page_tbl.id=$recent_tbl.id";
961 $table .= ", $version_tbl";
962 $join_clause .= " AND $version_tbl.id=$page_tbl.id";
964 if ($exclude_major_revisions) {
965 // Include only most recent minor revision
966 $pick[] = 'version=latestminor';
968 elseif (!$include_minor_revisions) {
969 // Include only most recent major revision
970 $pick[] = 'version=latestmajor';
973 // Include only the latest revision (whether major or minor).
974 $pick[] ='version=latestversion';
982 $where_clause = $join_clause;
984 $where_clause .= " AND " . join(" AND ", $pick);
986 . $this->page_tbl_fields . ", " . $this->version_tbl_fields
988 . " WHERE $where_clause"
989 . " ORDER BY mtime $order";
991 $sth = $dbh->prepare($sql . $this->_limit_sql($limit));
993 $sth = $dbh->prepare($sql);
996 $result = $sth->fetch(PDO_FETCH_NUM);
997 return new WikiDB_backend_PDO_iter($this, $result,
998 array_merge($this->page_tbl_field_list, $this->version_tbl_field_list));
1002 * Find referenced empty pages.
1004 function wanted_pages($exclude_from='', $exclude='', $sortby='', $limit='') {
1005 $dbh = &$this->_dbh;
1006 extract($this->_table_names);
1007 if ($orderby = $this->sortby($sortby, 'db', array('pagename','wantedfrom')))
1008 $orderby = 'ORDER BY ' . $orderby;
1010 if ($exclude_from) // array of pagenames
1011 $exclude_from = " AND linked.pagename NOT IN ".$this->_sql_set($exclude_from);
1012 if ($exclude) // array of pagenames
1013 $exclude = " AND $page_tbl.pagename NOT IN ".$this->_sql_set($exclude);
1016 all empty pages, independent of linkstatus:
1017 select pagename as empty from page left join nonempty using(id) where isnull(nonempty.id);
1018 only all empty pages, which have a linkto:
1019 select page.pagename, linked.pagename as wantedfrom from link, page as linked
1020 left join page on(link.linkto=page.id) left join nonempty on(link.linkto=nonempty.id)
1021 where isnull(nonempty.id) and linked.id=link.linkfrom;
1023 $sql = "SELECT p.pagename, pp.pagename as wantedfrom"
1024 . " FROM $page_tbl p JOIN $link_tbl linked"
1025 . " LEFT JOIN $page_tbl pp ON linked.linkto = pp.id"
1026 . " LEFT JOIN $nonempty_tbl ne ON linked.linkto = ne.id"
1027 . " WHERE ne.id is NULL"
1028 . " AND p.id = linked.linkfrom"
1033 $sth = $dbh->prepare($sql . $this->_limit_sql($limit));
1035 $sth = $dbh->prepare($sql);
1038 $result = $sth->fetch(PDO_FETCH_NUM);
1039 return new WikiDB_backend_PDO_iter($this, $result, array('pagename','wantedfrom'));
1043 * Rename page in the database.
1045 function rename_page($pagename, $to) {
1046 $dbh = &$this->_dbh;
1047 extract($this->_table_names);
1049 $this->lock(array('page','version','recent','nonempty','link'));
1050 if ( ($id = $this->_get_pageid($pagename, false)) ) {
1051 if ($new = $this->_get_pageid($to, false)) {
1053 // This page does not exist (already verified before), but exists in the page table.
1054 // So we delete this page.
1055 $dbh->query("DELETE FROM $page_tbl WHERE id=$new");
1056 $dbh->query("DELETE FROM $version_tbl WHERE id=$new");
1057 $dbh->query("DELETE FROM $recent_tbl WHERE id=$new");
1058 $dbh->query("DELETE FROM $nonempty_tbl WHERE id=$new");
1059 // We have to fix all referring tables to the old id
1060 $dbh->query("UPDATE $link_tbl SET linkfrom=$id WHERE linkfrom=$new");
1061 $dbh->query("UPDATE $link_tbl SET linkto=$id WHERE linkto=$new");
1063 $sth = $dbh->prepare("UPDATE $page_tbl SET pagename=? WHERE id=?");
1064 $sth->bindParam(1, $to, PDO_PARAM_STR, 100);
1065 $sth->bindParam(2, $id, PDO_PARAM_INT);
1068 $this->unlock(array('page'));
1072 function _update_recent_table($pageid = false) {
1073 $dbh = &$this->_dbh;
1074 extract($this->_table_names);
1075 extract($this->_expressions);
1077 $pageid = (int)$pageid;
1079 // optimize: mysql can do this with one REPLACE INTO.
1080 $backend_type = $this->backendType();
1081 if (substr($backend_type,0,5) == 'mysql') {
1082 $sth = $dbh->prepare("REPLACE INTO $recent_tbl"
1083 . " (id, latestversion, latestmajor, latestminor)"
1084 . " SELECT id, $maxversion, $maxmajor, $maxminor"
1085 . " FROM $version_tbl"
1086 . ( $pageid ? " WHERE id=$pageid" : "")
1090 $this->lock(array('recent'));
1091 $sth = $dbh->prepare("DELETE FROM $recent_tbl"
1092 . ( $pageid ? " WHERE id=$pageid" : ""));
1094 $sth = $dbh->prepare( "INSERT INTO $recent_tbl"
1095 . " (id, latestversion, latestmajor, latestminor)"
1096 . " SELECT id, $maxversion, $maxmajor, $maxminor"
1097 . " FROM $version_tbl"
1098 . ( $pageid ? " WHERE id=$pageid" : "")
1101 $this->unlock(array('recent'));
1105 function _update_nonempty_table($pageid = false) {
1106 $dbh = &$this->_dbh;
1107 extract($this->_table_names);
1108 extract($this->_expressions);
1110 $pageid = (int)$pageid;
1112 extract($this->_expressions);
1113 $this->lock(array('nonempty'));
1114 $dbh->query("DELETE FROM $nonempty_tbl"
1115 . ( $pageid ? " WHERE id=$pageid" : ""));
1116 $dbh->query("INSERT INTO $nonempty_tbl (id)"
1117 . " SELECT $recent_tbl.id"
1118 . " FROM $recent_tbl, $version_tbl"
1119 . " WHERE $recent_tbl.id=$version_tbl.id"
1120 . " AND version=latestversion"
1121 // We have some specifics here (Oracle)
1122 //. " AND content<>''"
1123 . " AND content $notempty"
1124 . ( $pageid ? " AND $recent_tbl.id=$pageid" : ""));
1125 $this->unlock(array('nonempty'));
1129 * Grab a write lock on the tables in the SQL database.
1131 * Calls can be nested. The tables won't be unlocked until
1132 * _unlock_database() is called as many times as _lock_database().
1136 function lock($tables, $write_lock = true) {
1137 if ($this->_lock_count++ == 0) {
1138 $this->_current_lock = $tables;
1139 if (!$this->_hasTransactions)
1140 $this->_lock_tables($tables, $write_lock);
1145 * Overridden by non-transaction safe backends.
1147 function _lock_tables($tables, $write_lock) {
1148 $lock_type = $write_lock ? "WRITE" : "READ";
1149 foreach ($this->_table_names as $key => $table) {
1150 $locks[] = "$table $lock_type";
1152 $this->_dbh->query("LOCK TABLES " . join(",", $locks));
1156 * Release a write lock on the tables in the SQL database.
1160 * @param $force boolean Unlock even if not every call to lock() has been matched
1161 * by a call to unlock().
1163 * @see _lock_database
1165 function unlock($tables = false, $force = false) {
1166 if ($this->_lock_count == 0) {
1167 $this->_current_lock = false;
1170 if (--$this->_lock_count <= 0 || $force) {
1171 if (!$this->_hasTransactions)
1172 $this->_unlock_tables($tables);
1173 $this->_current_lock = false;
1174 $this->_lock_count = 0;
1179 * overridden by non-transaction safe backends
1181 function _unlock_tables($tables) {
1182 $this->_dbh->query("UNLOCK TABLES");
1188 function _serialize($data) {
1191 assert(is_array($data));
1192 return serialize($data);
1198 function _unserialize($data) {
1199 return empty($data) ? array() : unserialize($data);
1202 /* some variables and functions for DB backend abstraction (action=upgrade) */
1203 function database () {
1204 return $this->_dbh->database;
1206 function backendType() {
1207 return $this->_dbh->databaseType;
1209 function connection() {
1210 trigger_error("PDO: connectionID unsupported", E_USER_ERROR);
1213 function listOfTables() {
1214 trigger_error("PDO: virtual listOfTables", E_USER_ERROR);
1217 function listOfFields($database, $table) {
1218 trigger_error("PDO: virtual listOfFields", E_USER_ERROR);
1223 * LIMIT with OFFSET is not SQL specified.
1224 * mysql: LIMIT $offset, $count
1225 * pgsql,sqlite: LIMIT $count OFFSET $offset
1226 * InterBase,FireBird: ROWS $offset TO $last
1227 * mssql: TOP $rows => TOP $last
1229 * IBM DB2: FetchFirst
1230 * See http://search.cpan.org/dist/SQL-Abstract-Limit/lib/SQL/Abstract/Limit.pm
1232 SELECT field_list FROM $table X WHERE where_clause AND
1234 SELECT COUNT(*) FROM $table WHERE $pk > X.$pk
1236 BETWEEN $offset AND $last
1237 ORDER BY $pk $asc_desc
1239 function _limit_sql($limit = false) {
1241 list($offset, $count) = $this->limit($limit);
1243 $limit = " LIMIT $count";
1244 trigger_error("unsupported OFFSET in SQL ignored", E_USER_WARNING);
1246 $limit = " LIMIT $count";
1252 function write_accesslog(&$entry) {
1254 $dbh = &$this->_dbh;
1255 $log_tbl = $entry->_accesslog->logtable;
1256 $dbh->prepare("INSERT INTO $log_tbl"
1257 . " (time_stamp,remote_host,remote_user,request_method,request_line,request_args,"
1258 . "request_file,request_uri,request_time,status,bytes_sent,referer,agent,request_duration)"
1259 . " VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)");
1260 // Either use unixtime as %d (long), or the native timestamp format.
1261 $sth->bindParam(1, $entry->time, PDO_PARAM_INT);
1262 $sth->bindParam(2, $entry->host, PDO_PARAM_STR, 100);
1263 $sth->bindParam(3, $entry->user, PDO_PARAM_STR, 50);
1264 $sth->bindParam(4, $entry->request_method, PDO_PARAM_STR, 10);
1265 $sth->bindParam(5, $entry->request, PDO_PARAM_STR, 255);
1266 $sth->bindParam(6, $entry->request_args, PDO_PARAM_STR, 255);
1267 $sth->bindParam(7, $entry->request_uri, PDO_PARAM_STR, 255);
1268 $sth->bindParam(8, $entry->_ncsa_time($entry->time), PDO_PARAM_STR, 28);
1269 $sth->bindParam(9, $entry->time, PDO_PARAM_INT);
1270 $sth->bindParam(10,$entry->status, PDO_PARAM_INT);
1271 $sth->bindParam(11,$entry->size, PDO_PARAM_INT);
1272 $sth->bindParam(12,$entry->referer, PDO_PARAM_STR, 255);
1273 $sth->bindParam(13,$entry->user_agent, PDO_PARAM_STR, 255);
1274 $sth->bindParam(14,$entry->duration, PDO_PARAM_FLOAT);
1279 class WikiDB_backend_PDO_generic_iter
1280 extends WikiDB_backend_iterator
1282 function WikiDB_backend_PDO_generic_iter($backend, $query_result, $field_list = NULL) {
1283 $this->_backend = &$backend;
1284 $this->_result = $query_result;
1285 //$this->_fields = $field_list;
1289 if (!is_object($this->_result)) {
1292 $count = $this->_result->rowCount();
1297 $result = &$this->_result;
1298 if (!is_object($result)) {
1301 return $result->fetch(PDO_FETCH_BOTH);
1305 if ($this->_result) {
1306 unset($this->_result);
1311 class WikiDB_backend_PDO_iter
1312 extends WikiDB_backend_PDO_generic_iter
1315 $result = &$this->_result;
1316 if (!is_object($result)) {
1319 $this->_backend = &$backend;
1320 $rec = $result->fetch(PDO_FETCH_ASSOC);
1322 if (isset($rec['pagedata']))
1323 $rec['pagedata'] = $backend->_extract_page_data($rec['pagedata'], $rec['hits']);
1324 if (!empty($rec['version'])) {
1325 $rec['versiondata'] = $backend->_extract_version_data_assoc($rec);
1331 class WikiDB_backend_PDO_search extends WikiDB_backend_search_sql {}
1333 // Following function taken from Pear::DB (prev. from adodb-pear.inc.php).
1334 // Eventually, change index.php to provide the relevant information
1337 * Parse a data source name.
1339 * Additional keys can be added by appending a URI query string to the
1342 * The format of the supplied DSN is in its fullest form:
1344 * phptype(dbsyntax)://username:password@protocol+hostspec/database?option=8&another=true
1347 * Most variations are allowed:
1349 * phptype://username:password@protocol+hostspec:110//usr/db_file.db?mode=0644
1350 * phptype://username:password@hostspec/database_name
1351 * phptype://username:password@hostspec
1352 * phptype://username@hostspec
1353 * phptype://hostspec/database
1354 * phptype://hostspec
1359 * @param string $dsn Data Source Name to be parsed
1361 * @return array an associative array with the following keys:
1362 * + phptype: Database backend used in PHP (mysql, odbc etc.)
1363 * + dbsyntax: Database used with regards to SQL syntax etc. (ignored with PDO)
1364 * + protocol: Communication protocol to use (tcp, unix, pipe etc.)
1365 * + hostspec: Host specification (hostname[:port])
1366 * + database: Database to use on the DBMS server
1367 * + username: User name for login
1368 * + password: Password for login
1370 * @author Tomas V.V.Cox <cox@idecnet.com>
1372 function parseDSN($dsn)
1376 'dbsyntax' => false,
1377 'username' => false,
1378 'password' => false,
1379 'protocol' => false,
1380 'hostspec' => false,
1383 'database' => false,
1386 if (is_array($dsn)) {
1387 $dsn = array_merge($parsed, $dsn);
1388 if (!$dsn['dbsyntax']) {
1389 $dsn['dbsyntax'] = $dsn['phptype'];
1394 // Find phptype and dbsyntax
1395 if (($pos = strpos($dsn, '://')) !== false) {
1396 $str = substr($dsn, 0, $pos);
1397 $dsn = substr($dsn, $pos + 3);
1403 // Get phptype and dbsyntax
1404 // $str => phptype(dbsyntax)
1405 if (preg_match('|^(.+?)\((.*?)\)$|', $str, $arr)) {
1406 $parsed['phptype'] = $arr[1];
1407 $parsed['dbsyntax'] = !$arr[2] ? $arr[1] : $arr[2];
1409 $parsed['phptype'] = $str;
1410 $parsed['dbsyntax'] = $str;
1417 // Get (if found): username and password
1418 // $dsn => username:password@protocol+hostspec/database
1419 if (($at = strrpos($dsn,'@')) !== false) {
1420 $str = substr($dsn, 0, $at);
1421 $dsn = substr($dsn, $at + 1);
1422 if (($pos = strpos($str, ':')) !== false) {
1423 $parsed['username'] = rawurldecode(substr($str, 0, $pos));
1424 $parsed['password'] = rawurldecode(substr($str, $pos + 1));
1426 $parsed['username'] = rawurldecode($str);
1430 // Find protocol and hostspec
1432 // $dsn => proto(proto_opts)/database
1433 if (preg_match('|^([^(]+)\((.*?)\)/?(.*?)$|', $dsn, $match)) {
1435 $proto_opts = $match[2] ? $match[2] : false;
1438 // $dsn => protocol+hostspec/database (old format)
1440 if (strpos($dsn, '+') !== false) {
1441 list($proto, $dsn) = explode('+', $dsn, 2);
1443 if (strpos($dsn, '/') !== false) {
1444 list($proto_opts, $dsn) = explode('/', $dsn, 2);
1451 // process the different protocol options
1452 $parsed['protocol'] = (!empty($proto)) ? $proto : 'tcp';
1453 $proto_opts = rawurldecode($proto_opts);
1454 if ($parsed['protocol'] == 'tcp') {
1455 if (strpos($proto_opts, ':') !== false) {
1456 list($parsed['hostspec'], $parsed['port']) = explode(':', $proto_opts);
1458 $parsed['hostspec'] = $proto_opts;
1460 } elseif ($parsed['protocol'] == 'unix') {
1461 $parsed['socket'] = $proto_opts;
1464 // Get dabase if any
1468 if (($pos = strpos($dsn, '?')) === false) {
1469 $parsed['database'] = $dsn;
1470 // /database?param1=value1¶m2=value2
1472 $parsed['database'] = substr($dsn, 0, $pos);
1473 $dsn = substr($dsn, $pos + 1);
1474 if (strpos($dsn, '&') !== false) {
1475 $opts = explode('&', $dsn);
1476 } else { // database?param1=value1
1477 $opts = array($dsn);
1479 foreach ($opts as $opt) {
1480 list($key, $value) = explode('=', $opt);
1481 if (!isset($parsed[$key])) {
1482 // don't allow params overwrite
1483 $parsed[$key] = rawurldecode($value);
1495 // c-basic-offset: 4
1496 // c-hanging-comment-ender-p: nil
1497 // indent-tabs-mode: nil