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