]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/mysql.php
added config options for all mysql table names
[SourceForge/phpwiki.git] / lib / mysql.php
1 <?php rcs_id('$Id: mysql.php,v 1.6 2000-11-13 14:54:08 ahollosi 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       global $WikiLinksStore, $HitCountStore, $WikiScoreStore;
153
154       $pagename = addslashes($pagename);
155       $msg = gettext ("Cannot delete '%s' from table '%s'");
156       $msg .= "<br>\n";
157       $msg .= gettext ("MySQL error: %s");
158
159       if (!mysql_query("delete from $WikiPageStore where pagename='$pagename'", $dbi['dbc']))
160          ExitWiki(sprintf($msg, $pagename, $WikiPageStore, mysql_error()));
161
162       if (!mysql_query("delete from $ArchivePageStore where pagename='$pagename'", $dbi['dbc']))
163          ExitWiki(sprintf($msg, $pagename, $ArchivePageStore, mysql_error()));
164
165       if (!mysql_query("delete from $WikiLinksStore where frompage='$pagename'", $dbi['dbc']))
166          ExitWiki(sprintf($msg, $pagename, $WikiLinksStore, mysql_error()));
167
168       if (!mysql_query("delete from $HitCountStore where pagename='$pagename'", $dbi['dbc']))
169          ExitWiki(sprintf($msg, $pagename, $HitCountStore, mysql_error()));
170
171       if (!mysql_query("delete from $WikiScoreStore where pagename='$pagename'", $dbi['dbc']))
172          ExitWiki(sprintf($msg, $pagename, $WikiScoreStore, mysql_error()));
173    }
174
175
176    function IncreaseHitCount($dbi, $pagename)
177    {
178       global $HitCountStore;
179
180       $res = mysql_query("update $HitCountStore set hits=hits+1 where pagename='$pagename'", $dbi['dbc']);
181
182       if (!mysql_affected_rows($dbi['dbc'])) {
183          $res = mysql_query("insert into $HitCountStore (pagename, hits) values ('$pagename', 1)", $dbi['dbc']);
184       }
185
186       return $res;
187    }
188
189    function GetHitCount($dbi, $pagename)
190    {
191       global $HitCountStore;
192
193       $res = mysql_query("select hits from $HitCountStore where pagename='$pagename'", $dbi['dbc']);
194       if (mysql_num_rows($res))
195          $hits = mysql_result($res, 0);
196       else
197          $hits = "0";
198
199       return $hits;
200    }
201
202
203    // setup for title-search
204    function InitTitleSearch($dbi, $search) {
205       $search = addslashes($search);
206       $res = mysql_query("select pagename from $dbi[table] where pagename like '%$search%' order by pagename", $dbi["dbc"]);
207
208       return $res;
209    }
210
211
212    // iterating through database
213    function TitleSearchNextMatch($dbi, $res) {
214       if($o = mysql_fetch_object($res)) {
215          return $o->pagename;
216       }
217       else {
218          return 0;
219       }
220    }
221
222
223    // setup for full-text search
224    function InitFullSearch($dbi, $search) {
225       $search = addslashes($search);
226       $res = mysql_query("select * from $dbi[table] where content like '%$search%'", $dbi["dbc"]);
227
228       return $res;
229    }
230
231    // iterating through database
232    function FullSearchNextMatch($dbi, $res) {
233       if($hash = mysql_fetch_array($res)) {
234          return MakePageHash($hash);
235       }
236       else {
237          return 0;
238       }
239    }
240
241    function InitMostPopular($dbi, $limit) {
242       global $HitCountStore;
243       $res = mysql_query("select * from $HitCountStore order by hits desc, pagename limit $limit", $dbi["dbc"]);
244       
245       return $res;
246    }
247
248    function MostPopularNextMatch($dbi, $res) {
249       if ($hits = mysql_fetch_array($res))
250          return $hits;
251       else
252          return 0;
253    }
254
255    function GetAllWikiPageNames($dbi) {
256       global $WikiPageStore;
257       $res = mysql_query("select pagename from $WikiPageStore", $dbi["dbc"]);
258       $rows = mysql_num_rows($res);
259       for ($i = 0; $i < $rows; $i++) {
260          $pages[$i] = mysql_result($res, $i);
261       }
262       return $pages;
263    }
264    
265    
266    ////////////////////////////////////////
267    // functionality for the wikilinks table
268
269    // takes a page name, returns array of scored incoming and outgoing links
270    function GetWikiPageLinks($dbi, $pagename) {
271       global $WikiLinksStore, $WikiScoreStore, $HitCountStore;
272
273       $pagename = addslashes($pagename);
274       $res = mysql_query("select topage, score from $WikiLinksStore, $WikiScoreStore where topage=pagename and frompage='$pagename' order by score desc, topage");
275       $rows = mysql_num_rows($res);
276       for ($i = 0; $i < $rows; $i++) {
277          $out = mysql_fetch_array($res);
278          $links['out'][] = array($out['topage'], $out['score']);
279       }
280
281       $res = mysql_query("select frompage, score from $WikiLinksStore, $WikiScoreStore where frompage=pagename and topage='$pagename' order by score desc, frompage");
282       $rows = mysql_num_rows($res);
283       for ($i = 0; $i < $rows; $i++) {
284          $out = mysql_fetch_array($res);
285          $links['in'][] = array($out['frompage'], $out['score']);
286       }
287
288       $res = mysql_query("select distinct pagename, hits from $WikiLinksStore, $HitCountStore where (frompage=pagename and topage='$pagename') or (topage=pagename and frompage='$pagename') order by hits desc, pagename");
289       $rows = mysql_num_rows($res);
290       for ($i = 0; $i < $rows; $i++) {
291          $out = mysql_fetch_array($res);
292          $links['popular'][] = array($out['pagename'], $out['hits']);
293       }
294
295       return $links;
296    }
297
298
299    // takes page name, list of links it contains
300    // the $linklist is an array where the keys are the page names
301    function SetWikiPageLinks($dbi, $pagename, $linklist) {
302       global $WikiLinksStore, $WikiScoreStore;
303
304       $frompage = addslashes($pagename);
305
306       // first delete the old list of links
307       mysql_query("delete from $WikiLinksStore where frompage='$frompage'",
308                 $dbi["dbc"]);
309
310       // the page may not have links, return if not
311       if (! count($linklist))
312          return;
313       // now insert the new list of links
314       while (list($topage, $count) = each($linklist)) {
315          $topage = addslashes($topage);
316          if($topage != $frompage) {
317             mysql_query("insert into $WikiLinksStore (frompage, topage) " .
318                      "values ('$frompage', '$topage')", $dbi["dbc"]);
319          }
320       }
321
322       // update pagescore
323       mysql_query("delete from $WikiScoreStore", $dbi["dbc"]);
324       mysql_query("insert into $WikiScoreStore select w1.topage, count(*) from $WikiLinksStore as w1, $WikiLinksStore as w2 where w2.topage=w1.frompage group by w1.topage", $dbi["dbc"]);
325    }
326
327 /* more mysql queries:
328
329 orphans:
330 select pagename from wiki left join wikilinks on pagename=topage where topage is NULL;
331 */
332 ?>