]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - wiki_mysql.php3
added support for HTML templates
[SourceForge/phpwiki.git] / wiki_mysql.php3
1 <!-- $Id: wiki_mysql.php3,v 1.5 2000-06-08 22:11:05 ahollosi Exp $ -->
2 <?
3
4    /*
5       Database functions:
6
7       OpenDataBase($dbname)
8       CloseDataBase($dbi)
9       RetrievePage($dbi, $pagename)
10       InsertPage($dbi, $pagename, $pagehash)
11       IsWikiPage($dbi, $pagename)
12       InitTitleSearch($dbi, $search)
13       TitleSearchNextMatch($dbi, &$pos)
14       InitFullSearch($dbi, $search)
15       FullSearchNextMatch($dbi, &$pos)
16    */
17
18
19    // open a database and return the handle
20    // ignores MAX_DBM_ATTEMPTS
21
22    function OpenDataBase($dbname) {
23       global $mysql_server, $mysql_user, $mysql_pwd, $mysql_db;
24
25       if (!($dbc = mysql_pconnect($mysql_server, $mysql_user, $mysql_pwd))) {
26          echo "Cannot establish connection to database, giving up.";
27          exit();
28       }
29       if (!mysql_select_db($mysql_db, $dbc)) {
30          echo "Cannot open database, giving up.";
31          exit();
32       }
33
34       $dbi['dbc'] = $dbc;
35       $dbi['table'] = $dbname;
36       return $dbi;
37    }
38
39
40    function CloseDataBase($dbi) {
41       // NOP function
42       // mysql connections are established as persistant
43       // they cannot be closed through mysql_close()
44    }
45
46
47    // Return hash of page + attributes or default
48    function RetrievePage($dbi, $pagename) {
49       $pagename = addslashes($pagename);
50       if ($res = mysql_query("select hash from $dbi[table] where page='$pagename'", $dbi['dbc'])) {
51          if ($o = mysql_fetch_object($res)) {
52             // unserialize data into a hash
53             $pagehash = unserialize($o->hash);
54             return $pagehash;
55          }
56       }
57
58       return -1;
59    }
60
61
62    // Either insert or replace a key/value (a page)
63    function InsertPage($dbi, $pagename, $pagehash) {
64       $pagename = addslashes($pagename);
65       $pagedata = addslashes(serialize($pagehash));
66
67       if (!mysql_query("replace into $dbi[table] (page, hash) values ('$pagename', '$pagedata')", $dbi['dbc'])) {
68             echo "error writing value";
69             exit();
70       }
71    }
72
73
74
75    function IsWikiPage($dbi, $pagename) {
76       $pagename = addslashes($pagename);
77       if ($res = mysql_query("select count(*) from $dbi[table] where page='$pagename'", $dbi['dbc'])) {
78          return(mysql_result($res, 0));
79       }
80    }
81
82
83    // setup for title-search
84    function InitTitleSearch($dbi, $search) {
85       $search = addslashes($search);
86       $res = mysql_query("select page from $dbi[table] where page like '%$search%' order by page", $dbi["dbc"]);
87
88       return $res;
89    }
90
91
92    // iterating through database
93    function TitleSearchNextMatch($dbi, $res) {
94       if($o = mysql_fetch_object($res)) {
95          return $o->page;
96       }
97       else {
98          return 0;
99       }
100    }
101
102
103    // setup for full-text search
104    function InitFullSearch($dbi, $search) {
105       $search = addslashes($search);
106       $res = mysql_query("select page,hash from $dbi[table] where hash like '%$search%'", $dbi["dbc"]);
107
108       return $res;
109    }
110
111    // iterating through database
112    function FullSearchNextMatch($dbi, $res) {
113       if($o = mysql_fetch_object($res)) {
114          $page['name'] = $o->page;
115          $page['hash'] = unserialize($o->hash);
116          return $page;
117       }
118       else {
119          return 0;
120       }
121    }
122
123
124 ?>