]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - wiki_mysql.php3
Added two echo statements to report mysql_error in OpenDataBase(). I
[SourceForge/phpwiki.git] / wiki_mysql.php3
1 <!-- $Id: wiki_mysql.php3,v 1.9 2000-07-02 22:12:05 wainstead 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       IncreaseHitCount($dbi, $pagename)  
17       GetHitCount($dbi, $pagename)   
18       InitMostPopular($dbi, $limit)   
19       MostPopularNextMatch($dbi, $res)
20
21    */
22
23
24    // open a database and return the handle
25    // ignores MAX_DBM_ATTEMPTS
26
27    function OpenDataBase($dbname) {
28       global $mysql_server, $mysql_user, $mysql_pwd, $mysql_db;
29
30       if (!($dbc = mysql_pconnect($mysql_server, $mysql_user, $mysql_pwd))) {
31          echo "Cannot establish connection to database, giving up.";
32          echo "MySQL error: ", mysql_error(), "<br>\n";
33          exit();
34       }
35       if (!mysql_select_db($mysql_db, $dbc)) {
36          echo "Cannot open database, giving up.";
37          echo "MySQL error: ", mysql_error(), "<br>\n";
38          exit();
39       }
40
41       $dbi['dbc'] = $dbc;
42       $dbi['table'] = $dbname;
43       return $dbi;
44    }
45
46
47    function CloseDataBase($dbi) {
48       // NOP function
49       // mysql connections are established as persistant
50       // they cannot be closed through mysql_close()
51    }
52
53
54    function MakeDBHash($pagename, $pagehash)
55    {
56       $pagehash["pagename"] = addslashes($pagename);
57       if (!isset($pagehash["flags"]))
58          $pagehash["flags"] = 0;
59       $pagehash["author"] = addslashes($pagehash["author"]);
60       $pagehash["content"] = implode("\n", $pagehash["content"]);
61       $pagehash["content"] = addslashes($pagehash["content"]);
62       $pagehash["refs"] = serialize($pagehash["refs"]);
63  
64       return $pagehash;
65    }
66
67    function MakePageHash($dbhash)
68    {
69       // unserialize/explode content
70       $dbhash['refs'] = unserialize($dbhash['refs']);
71       $dbhash['content'] = explode("\n", $dbhash['content']);
72       return $dbhash;
73    }
74
75
76    // Return hash of page + attributes or default
77    function RetrievePage($dbi, $pagename) {
78       $pagename = addslashes($pagename);
79       if ($res = mysql_query("select * from $dbi[table] where pagename='$pagename'", $dbi['dbc'])) {
80          if ($dbhash = mysql_fetch_array($res)) {
81             return MakePageHash($dbhash);
82          }
83       }
84       return -1;
85    }
86
87
88    // Either insert or replace a key/value (a page)
89    function InsertPage($dbi, $pagename, $pagehash)
90    {
91       $pagehash = MakeDBHash($pagename, $pagehash);
92
93       $COLUMNS = "author, content, created, flags, " .
94                  "lastmodified, pagename, refs, version";
95
96       $VALUES =  "'$pagehash[author]', '$pagehash[content]', " .
97                  "$pagehash[created], $pagehash[flags], " .
98                  "$pagehash[lastmodified], '$pagehash[pagename]', " .
99                  "'$pagehash[refs]', $pagehash[version]";
100
101
102       if (!mysql_query("replace into $dbi[table] ($COLUMNS) values ($VALUES)",
103                         $dbi['dbc'])) {
104             echo "error writing page '$pagename'";
105             exit();
106       }
107    }
108
109
110    function IsWikiPage($dbi, $pagename) {
111       $pagename = addslashes($pagename);
112       if ($res = mysql_query("select count(*) from $dbi[table] where pagename='$pagename'", $dbi['dbc'])) {
113          return(mysql_result($res, 0));
114       }
115    }
116
117
118    function IncreaseHitCount($dbi, $pagename)
119    {
120       $res = mysql_query("update hitcount set hits=hits+1 where pagename='$pagename'", $dbi['dbc']);
121
122       if (!mysql_affected_rows($dbi['dbc'])) {
123          $res = mysql_query("insert into hitcount (pagename, hits) values ('$pagename', 1)", $dbi['dbc']);
124       }
125
126       return $res;
127    }
128
129    function GetHitCount($dbi, $pagename)
130    {
131       $res = mysql_query("select hits from hitcount where pagename='$pagename'", $dbi['dbc']);
132       if (mysql_num_rows($res))
133          $hits = mysql_result($res, 0);
134       else
135          $hits = "0";
136
137       return $hits;
138    }
139
140
141    // setup for title-search
142    function InitTitleSearch($dbi, $search) {
143       $search = addslashes($search);
144       $res = mysql_query("select pagename from $dbi[table] where pagename like '%$search%' order by pagename", $dbi["dbc"]);
145
146       return $res;
147    }
148
149
150    // iterating through database
151    function TitleSearchNextMatch($dbi, $res) {
152       if($o = mysql_fetch_object($res)) {
153          return $o->pagename;
154       }
155       else {
156          return 0;
157       }
158    }
159
160
161    // setup for full-text search
162    function InitFullSearch($dbi, $search) {
163       $search = addslashes($search);
164       $res = mysql_query("select * from $dbi[table] where content like '%$search%'", $dbi["dbc"]);
165
166       return $res;
167    }
168
169    // iterating through database
170    function FullSearchNextMatch($dbi, $res) {
171       if($hash = mysql_fetch_array($res)) {
172          return MakePageHash($hash);
173       }
174       else {
175          return 0;
176       }
177    }
178
179    function InitMostPopular($dbi, $limit) {
180       $res = mysql_query("select * from hitcount order by hits desc, pagename limit $limit");
181       
182       return $res;
183    }
184
185    function MostPopularNextMatch($dbi, $res) {
186       if ($hits = mysql_fetch_array($res))
187          return $hits;
188       else
189          return 0;
190    }
191 ?>