]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/mysql.php
Updated the list of functions in the header comment.
[SourceForge/phpwiki.git] / lib / mysql.php
1 <?php rcs_id('$Id: mysql.php,v 1.5 2000-11-09 02:57:51 wainstead Exp $');
2
3    /*
4       Database functions:
5       OpenDataBase($dbname)
6       CloseDataBase($dbi)
7       MakeDBHash($pagename, $pagehash)
8       MakePageHash($dbhash)
9       RetrievePage($dbi, $pagename, $pagestore)
10       InsertPage($dbi, $pagename, $pagehash)
11       SaveCopyToArchive($dbi, $pagename, $pagehash)
12       IsWikiPage($dbi, $pagename)
13       IsInArchive($dbi, $pagename)
14       RemovePage($dbi, $pagename)
15       IncreaseHitCount($dbi, $pagename)
16       GetHitCount($dbi, $pagename)
17       InitTitleSearch($dbi, $search)
18       TitleSearchNextMatch($dbi, $res)
19       InitFullSearch($dbi, $search)
20       FullSearchNextMatch($dbi, $res)
21       InitMostPopular($dbi, $limit)
22       MostPopularNextMatch($dbi, $res)
23       GetAllWikiPageNames($dbi)
24       GetWikiPageLinks($dbi, $pagename)
25       SetWikiPageLinks($dbi, $pagename, $linklist)
26    */
27
28    // open a database and return the handle
29    // ignores MAX_DBM_ATTEMPTS
30
31    function OpenDataBase($dbname) {
32       global $mysql_server, $mysql_user, $mysql_pwd, $mysql_db;
33
34       if (!($dbc = mysql_pconnect($mysql_server, $mysql_user, $mysql_pwd))) {
35          $msg = gettext ("Cannot establish connection to database, giving up.");
36          $msg .= "<BR>";
37          $msg .= sprintf(gettext ("MySQL error: %s"), mysql_error());
38          ExitWiki($msg);
39       }
40       if (!mysql_select_db($mysql_db, $dbc)) {
41          $msg =  sprintf(gettext ("Cannot open database %s, giving up."), $mysql_db);
42          $msg .= "<BR>";
43          $msg .= sprintf(gettext ("MySQL error: %s"), mysql_error());
44          ExitWiki($msg);
45       }
46       $dbi['dbc'] = $dbc;
47       $dbi['table'] = $dbname;
48       return $dbi;
49    }
50
51
52    function CloseDataBase($dbi) {
53       // NOP function
54       // mysql connections are established as persistant
55       // they cannot be closed through mysql_close()
56    }
57
58
59    function MakeDBHash($pagename, $pagehash)
60    {
61       $pagehash["pagename"] = addslashes($pagename);
62       if (!isset($pagehash["flags"]))
63          $pagehash["flags"] = 0;
64       $pagehash["author"] = addslashes($pagehash["author"]);
65       $pagehash["content"] = implode("\n", $pagehash["content"]);
66       $pagehash["content"] = addslashes($pagehash["content"]);
67       $pagehash["refs"] = serialize($pagehash["refs"]);
68  
69       return $pagehash;
70    }
71
72    function MakePageHash($dbhash)
73    {
74       // unserialize/explode content
75       $dbhash['refs'] = unserialize($dbhash['refs']);
76       $dbhash['content'] = explode("\n", $dbhash['content']);
77       return $dbhash;
78    }
79
80
81    // Return hash of page + attributes or default
82    function RetrievePage($dbi, $pagename, $pagestore) {
83       $pagename = addslashes($pagename);
84       if ($res = mysql_query("select * from $pagestore where pagename='$pagename'", $dbi['dbc'])) {
85          if ($dbhash = mysql_fetch_array($res)) {
86             return MakePageHash($dbhash);
87          }
88       }
89       return -1;
90    }
91
92
93    // Either insert or replace a key/value (a page)
94    function InsertPage($dbi, $pagename, $pagehash)
95    {
96       global $WikiPageStore; // ugly hack
97
98       if ($dbi['table'] == $WikiPageStore) { // HACK
99          $linklist = ExtractWikiPageLinks($pagehash['content']);
100          SetWikiPageLinks($dbi, $pagename, $linklist);
101       }
102
103       $pagehash = MakeDBHash($pagename, $pagehash);
104
105       $COLUMNS = "author, content, created, flags, " .
106                  "lastmodified, pagename, refs, version";
107
108       $VALUES =  "'$pagehash[author]', '$pagehash[content]', " .
109                  "$pagehash[created], $pagehash[flags], " .
110                  "$pagehash[lastmodified], '$pagehash[pagename]', " .
111                  "'$pagehash[refs]', $pagehash[version]";
112
113       if (!mysql_query("replace into $dbi[table] ($COLUMNS) values ($VALUES)",
114                         $dbi['dbc'])) {
115             $msg = sprintf(gettext ("Error writing page '%s'"), $pagename);
116             $msg .= "<BR>";
117             $msg .= sprintf(gettext ("MySQL error: %s"), mysql_error());
118             ExitWiki($msg);
119       }
120    }
121
122
123    // for archiving pages to a seperate dbm
124    function SaveCopyToArchive($dbi, $pagename, $pagehash) {
125       global $ArchivePageStore;
126       $adbi = OpenDataBase($ArchivePageStore);
127       InsertPage($adbi, $pagename, $pagehash);
128    }
129
130
131    function IsWikiPage($dbi, $pagename) {
132       $pagename = addslashes($pagename);
133       if ($res = mysql_query("select count(*) from $dbi[table] where pagename='$pagename'", $dbi['dbc'])) {
134          return(mysql_result($res, 0));
135       }
136       return 0;
137    }
138
139    function IsInArchive($dbi, $pagename) {
140       global $ArchivePageStore;
141
142       $pagename = addslashes($pagename);
143       if ($res = mysql_query("select count(*) from $ArchivePageStore where pagename='$pagename'", $dbi['dbc'])) {
144          return(mysql_result($res, 0));
145       }
146       return 0;
147    }
148
149
150    function RemovePage($dbi, $pagename) {
151       global $WikiPageStore, $ArchivePageStore;
152
153       $pagename = addslashes($pagename);
154       $msg = gettext ("Cannot delete '%s' from table '%s'");
155       $msg .= "<br>\n";
156       $msg .= gettext ("MySQL error: %s");
157
158       if (!mysql_query("delete from $WikiPageStore where pagename='$pagename'", $dbi['dbc']))
159          ExitWiki(sprintf($msg, $pagename, $WikiPageStore, mysql_error()));
160
161       if (!mysql_query("delete from $ArchivePageStore where pagename='$pagename'", $dbi['dbc']))
162          ExitWiki(sprintf($msg, $pagename, $ArchivePageStore, mysql_error()));
163
164       if (!mysql_query("delete from wikilinks where frompage='$pagename'", $dbi['dbc']))
165          ExitWiki(sprintf($msg, $pagename, 'wikilinks', mysql_error()));
166
167       if (!mysql_query("delete from hitcount where pagename='$pagename'", $dbi['dbc']))
168          ExitWiki(sprintf($msg, $pagename, 'hitcount', mysql_error()));
169
170       if (!mysql_query("delete from wikiscore where pagename='$pagename'", $dbi['dbc']))
171          ExitWiki(sprintf($msg, $pagename, 'wikiscore', mysql_error()));
172    }
173
174
175    function IncreaseHitCount($dbi, $pagename)
176    {
177       $res = mysql_query("update hitcount set hits=hits+1 where pagename='$pagename'", $dbi['dbc']);
178
179       if (!mysql_affected_rows($dbi['dbc'])) {
180          $res = mysql_query("insert into hitcount (pagename, hits) values ('$pagename', 1)", $dbi['dbc']);
181       }
182
183       return $res;
184    }
185
186    function GetHitCount($dbi, $pagename)
187    {
188       $res = mysql_query("select hits from hitcount where pagename='$pagename'", $dbi['dbc']);
189       if (mysql_num_rows($res))
190          $hits = mysql_result($res, 0);
191       else
192          $hits = "0";
193
194       return $hits;
195    }
196
197
198    // setup for title-search
199    function InitTitleSearch($dbi, $search) {
200       $search = addslashes($search);
201       $res = mysql_query("select pagename from $dbi[table] where pagename like '%$search%' order by pagename", $dbi["dbc"]);
202
203       return $res;
204    }
205
206
207    // iterating through database
208    function TitleSearchNextMatch($dbi, $res) {
209       if($o = mysql_fetch_object($res)) {
210          return $o->pagename;
211       }
212       else {
213          return 0;
214       }
215    }
216
217
218    // setup for full-text search
219    function InitFullSearch($dbi, $search) {
220       $search = addslashes($search);
221       $res = mysql_query("select * from $dbi[table] where content like '%$search%'", $dbi["dbc"]);
222
223       return $res;
224    }
225
226    // iterating through database
227    function FullSearchNextMatch($dbi, $res) {
228       if($hash = mysql_fetch_array($res)) {
229          return MakePageHash($hash);
230       }
231       else {
232          return 0;
233       }
234    }
235
236    function InitMostPopular($dbi, $limit) {
237       $res = mysql_query("select * from hitcount order by hits desc, pagename limit $limit", $dbi["dbc"]);
238       
239       return $res;
240    }
241
242    function MostPopularNextMatch($dbi, $res) {
243       if ($hits = mysql_fetch_array($res))
244          return $hits;
245       else
246          return 0;
247    }
248
249    function GetAllWikiPageNames($dbi) {
250       $res = mysql_query("select pagename from wiki", $dbi["dbc"]);
251       $rows = mysql_num_rows($res);
252       for ($i = 0; $i < $rows; $i++) {
253          $pages[$i] = mysql_result($res, $i);
254       }
255       return $pages;
256    }
257    
258    
259    ////////////////////////////////////////
260    // functionality for the wikilinks table
261
262    // takes a page name, returns array of scored incoming and outgoing links
263    function GetWikiPageLinks($dbi, $pagename) {
264       $pagename = addslashes($pagename);
265       $res = mysql_query("select topage, score from wikilinks, wikiscore where topage=pagename and frompage='$pagename' order by score desc, topage");
266       $rows = mysql_num_rows($res);
267       for ($i = 0; $i < $rows; $i++) {
268          $out = mysql_fetch_array($res);
269          $links['out'][] = array($out['topage'], $out['score']);
270       }
271
272       $res = mysql_query("select frompage, score from wikilinks, wikiscore where frompage=pagename and topage='$pagename' order by score desc, frompage");
273       $rows = mysql_num_rows($res);
274       for ($i = 0; $i < $rows; $i++) {
275          $out = mysql_fetch_array($res);
276          $links['in'][] = array($out['frompage'], $out['score']);
277       }
278
279       $res = mysql_query("select distinct pagename, hits from wikilinks, hitcount where (frompage=pagename and topage='$pagename') or (topage=pagename and frompage='$pagename') order by hits desc, pagename");
280       $rows = mysql_num_rows($res);
281       for ($i = 0; $i < $rows; $i++) {
282          $out = mysql_fetch_array($res);
283          $links['popular'][] = array($out['pagename'], $out['hits']);
284       }
285
286       return $links;
287    }
288
289
290    // takes page name, list of links it contains
291    // the $linklist is an array where the keys are the page names
292    function SetWikiPageLinks($dbi, $pagename, $linklist) {
293       $frompage = addslashes($pagename);
294
295       // first delete the old list of links
296       mysql_query("delete from wikilinks where frompage='$frompage'",
297                 $dbi["dbc"]);
298
299       // the page may not have links, return if not
300       if (! count($linklist))
301          return;
302       // now insert the new list of links
303       while (list($topage, $count) = each($linklist)) {
304          $topage = addslashes($topage);
305          if($topage != $frompage) {
306             mysql_query("insert into wikilinks (frompage, topage) " .
307                      "values ('$frompage', '$topage')", $dbi["dbc"]);
308          }
309       }
310
311       // update pagescore
312       mysql_query("delete from wikiscore", $dbi["dbc"]);
313       mysql_query("insert into wikiscore select w1.topage, count(*) from wikilinks as w1, wikilinks as w2 where w2.topage=w1.frompage group by w1.topage", $dbi["dbc"]);
314    }
315
316 /* more mysql queries:
317
318 orphans:
319 select pagename from wiki left join wikilinks on pagename=topage where topage is NULL;
320 */
321 ?>