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