]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - wiki_mysql.php3
bringing documentation up to date
[SourceForge/phpwiki.git] / wiki_mysql.php3
1 <?php rcs_id('$Id: wiki_mysql.php3,v 1.15 2000-09-20 19:26:36 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
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       $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, $pagestore) {
78       $pagename = addslashes($pagename);
79       if ($res = mysql_query("select * from $pagestore 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       if (!mysql_query("replace into $dbi[table] ($COLUMNS) values ($VALUES)",
102                         $dbi['dbc'])) {
103             echo "error writing page '$pagename' ";
104             echo mysql_error();
105             exit();
106       }
107    }
108
109
110    // for archiving pages to a seperate dbm
111    function SaveCopyToArchive($dbi, $pagename, $pagehash) {
112       global $ArchivePageStore;
113
114       $adbi = OpenDataBase($ArchivePageStore);
115       InsertPage($adbi, $pagename, $pagehash);
116    }
117
118
119    function IsWikiPage($dbi, $pagename) {
120       $pagename = addslashes($pagename);
121       if ($res = mysql_query("select count(*) from $dbi[table] where pagename='$pagename'", $dbi['dbc'])) {
122          return(mysql_result($res, 0));
123       }
124       return 0;
125    }
126
127    function IsInArchive($dbi, $pagename) {
128       global $ArchivePageStore;
129
130       $pagename = addslashes($pagename);
131       if ($res = mysql_query("select count(*) from $ArchivePageStore where pagename='$pagename'", $dbi['dbc'])) {
132          return(mysql_result($res, 0));
133       }
134       return 0;
135    }
136
137
138    function IncreaseHitCount($dbi, $pagename)
139    {
140       $res = mysql_query("update hitcount set hits=hits+1 where pagename='$pagename'", $dbi['dbc']);
141
142       if (!mysql_affected_rows($dbi['dbc'])) {
143          $res = mysql_query("insert into hitcount (pagename, hits) values ('$pagename', 1)", $dbi['dbc']);
144       }
145
146       return $res;
147    }
148
149    function GetHitCount($dbi, $pagename)
150    {
151       $res = mysql_query("select hits from hitcount where pagename='$pagename'", $dbi['dbc']);
152       if (mysql_num_rows($res))
153          $hits = mysql_result($res, 0);
154       else
155          $hits = "0";
156
157       return $hits;
158    }
159
160
161    // setup for title-search
162    function InitTitleSearch($dbi, $search) {
163       $search = addslashes($search);
164       $res = mysql_query("select pagename from $dbi[table] where pagename like '%$search%' order by pagename", $dbi["dbc"]);
165
166       return $res;
167    }
168
169
170    // iterating through database
171    function TitleSearchNextMatch($dbi, $res) {
172       if($o = mysql_fetch_object($res)) {
173          return $o->pagename;
174       }
175       else {
176          return 0;
177       }
178    }
179
180
181    // setup for full-text search
182    function InitFullSearch($dbi, $search) {
183       $search = addslashes($search);
184       $res = mysql_query("select * from $dbi[table] where content like '%$search%'", $dbi["dbc"]);
185
186       return $res;
187    }
188
189    // iterating through database
190    function FullSearchNextMatch($dbi, $res) {
191       if($hash = mysql_fetch_array($res)) {
192          return MakePageHash($hash);
193       }
194       else {
195          return 0;
196       }
197    }
198
199    function InitMostPopular($dbi, $limit) {
200       $res = mysql_query("select * from hitcount order by hits desc, pagename limit $limit");
201       
202       return $res;
203    }
204
205    function MostPopularNextMatch($dbi, $res) {
206       if ($hits = mysql_fetch_array($res))
207          return $hits;
208       else
209          return 0;
210    }
211
212    function GetAllWikiPageNames($dbi) {
213       $res = mysql_query("select pagename from wiki");
214       $rows = mysql_num_rows($res);
215       for ($i = 0; $i < $rows; $i++) {
216          $pages[$i] = mysql_result($res, $i);
217       }
218       return $pages;
219    }
220 ?>