]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/backend/ADODB_mssqlnative.php
include [all] Include and file path should be devided with single space. File path...
[SourceForge/phpwiki.git] / lib / WikiDB / backend / ADODB_mssqlnative.php
1 <?php // -*-php-*-
2
3
4 /**
5  * MS SQL extensions for the ADODB DB backend.
6  */
7 require_once 'lib/WikiDB/backend/ADODB.php';
8
9 class WikiDB_backend_ADODB_mssqlnative
10 extends WikiDB_backend_ADODB
11 {
12     /**
13      * Constructor.
14      */
15     function WikiDB_backend_ADODB_mssqlnative($dbparams) {
16         // Lowercase Assoc arrays
17         define('ADODB_ASSOC_CASE',0);
18
19         // Backend constructor
20         $this->WikiDB_backend_ADODB($dbparams);
21
22         // Empty strings in MSSQL?  NULLS?
23         $this->_expressions['notempty'] = "NOT LIKE ''";
24         //doesn't work if content is of the "text" type http://msdn2.microsoft.com/en-us/library/ms188074.aspx
25         $this->_expressions['iscontent'] = "dbo.hasContent({$this->_table_names['version_tbl']}.content)";
26
27         $this->_prefix = isset($dbparams['prefix']) ? $dbparams['prefix'] : '';
28
29     }
30
31     /**
32      * Pack tables.
33      */
34     function optimize() {
35         // Do nothing here -- Leave that for the DB
36         // Cost Based Optimizer tuning vary from version to version
37         return 1;
38     }
39
40     // Search callabcks
41     // Page name
42     function _sql_match_clause($word) {
43         $word = preg_replace('/(?=[%_\\\\])/', "\\", $word);
44         $word = $this->_dbh->qstr("%$word%");
45         return "LOWER(pagename) LIKE $word";
46     }
47
48     // Fulltext -- case sensitive :-\
49     function _fullsearch_sql_match_clause($word) {
50         $word = preg_replace('/(?=[%_\\\\])/', "\\", $word);
51         $wordq = $this->_dbh->qstr("%$word%");
52         return "LOWER(pagename) LIKE $wordq "
53                . "OR CHARINDEX(content, '$word') > 0";
54     }
55
56     /**
57      * Serialize data
58      */
59     function _serialize($data) {
60         if (empty($data))
61             return '';
62         assert(is_array($data));
63         return addslashes(serialize($data));
64     }
65
66     /**
67      * Unserialize data
68      */
69     function _unserialize($data) {
70         return empty($data) ? array() : unserialize(stripslashes($data));
71     }
72
73     /*
74      * Update link table.
75      * on DEBUG: delete old, deleted links from page
76      */
77     function set_links($pagename, $links) {
78         // FIXME: optimize: mysql can do this all in one big INSERT/REPLACE.
79
80         $dbh = &$this->_dbh;
81         extract($this->_table_names);
82
83         $this->lock(array('link'));
84         $pageid = $this->_get_pageid($pagename, true);
85
86         $oldlinks = $dbh->getAssoc("SELECT $link_tbl.linkto as id, page.pagename FROM $link_tbl"
87                                   ." JOIN page ON ($link_tbl.linkto = page.id)"
88                                   ." WHERE linkfrom=$pageid");
89         // Delete current links,
90         $dbh->Execute("DELETE FROM $link_tbl WHERE linkfrom=$pageid");
91         // and insert new links. Faster than checking for all single links
92         if ($links) {
93             foreach ($links as $link) {
94                 $linkto = $link['linkto'];
95                 if (isset($link['relation']))
96                     $relation = $this->_get_pageid($link['relation'], true);
97                 else
98                     $relation = 0;
99                 if ($linkto === "") { // ignore attributes
100                     continue;
101                 }
102                 // avoid duplicates
103                 if (isset($linkseen[$linkto]) and !$relation) {
104                     continue;
105                 }
106                 if (!$relation) {
107                     $linkseen[$linkto] = true;
108                 }
109                 $linkid = $this->_get_pageid($linkto, true);
110                 assert($linkid);
111                 if ($relation) {
112                     $dbh->Execute("INSERT INTO $link_tbl (linkfrom, linkto, relation)"
113                                   . " VALUES ($pageid, $linkid, $relation)");
114                 } else {
115                     $dbh->Execute("INSERT INTO $link_tbl (linkfrom, linkto)"
116                                   . " VALUES ($pageid, $linkid)");
117                 }
118                 if ($oldlinks and array_key_exists($linkid, $oldlinks)) {
119                     // This was also in the previous page
120                     unset($oldlinks[$linkid]);
121                 }
122             }
123         }
124         // purge page table: delete all non-referenced pages
125         // for all previously linked pages, which have no other linkto links
126         if (DEBUG and $oldlinks) {
127             // trigger_error("purge page table: delete all non-referenced pages...", E_USER_NOTICE);
128             foreach ($oldlinks as $id => $name) {
129                 // ...check if the page is empty and has no version
130                 if($id != '') {
131                     $result = $dbh->getRow("SELECT $page_tbl.id FROM $page_tbl"
132                                            . " LEFT JOIN $nonempty_tbl ON ($nonempty_tbl.id = $page_tbl.id)"//'"id" is not a recognized table hints option'
133                                            . " LEFT JOIN $version_tbl ON ($version_tbl.id = $page_tbl.id)"//'"id" is not a recognized table hints option'
134                                            . " WHERE $nonempty_tbl.id is NULL"
135                                            . " AND $version_tbl.id is NULL"
136                                            . " AND $page_tbl.id=$id");
137                     $linkto = $dbh->getRow("SELECT linkfrom FROM $link_tbl WHERE linkto=$id");
138                     if ($result and empty($linkto)) {
139                         trigger_error("delete empty and non-referenced link $name ($id)", E_USER_NOTICE);
140                         $dbh->Execute("DELETE FROM $recent_tbl WHERE id=$id"); // may fail
141                         $dbh->Execute("DELETE FROM $link_tbl WHERE linkto=$id");
142                         $dbh->Execute("DELETE FROM $page_tbl WHERE id=$id");   // this purges the link
143                     }
144                 }
145             }
146         }
147         $this->unlock(array('link'));
148         return true;
149     }
150
151     /* get all oldlinks in hash => id, relation
152        check for all new links
153      */
154     function set_links1($pagename, $links) {
155
156         $dbh = &$this->_dbh;
157         extract($this->_table_names);
158
159         $this->lock(array('link'));
160         $pageid = $this->_get_pageid($pagename, true);
161
162         $oldlinks = $dbh->getAssoc("SELECT $link_tbl.linkto as linkto, $link_tbl.relation, page.pagename"
163                                   ." FROM $link_tbl"
164                                   ." JOIN page ON ($link_tbl.linkto = page.id)"
165                                   ." WHERE linkfrom=$pageid");
166         /*      old                  new
167          *      X => [1,0 2,0 1,1]   X => [1,1 3,0]
168          * => delete 1,0 2,0 + insert 3,0
169          */
170         if ($links) {
171             foreach ($links as $link) {
172                 $linkto = $link['linkto'];
173                 if ($link['relation'])
174                     $relation = $this->_get_pageid($link['relation'], true);
175                 else
176                     $relation = 0;
177                 // avoid duplicates
178                 if (isset($linkseen[$linkto]) and !$relation) {
179                     continue;
180                 }
181                 if (!$relation) {
182                     $linkseen[$linkto] = true;
183                 }
184                 $linkid = $this->_get_pageid($linkto, true);
185                 assert($linkid);
186                 $skip = 0;
187                 // find linkfrom,linkto,relation triple in oldlinks
188                 foreach ($oldlinks as $l) {
189                     if ($relation) { // relation NOT NULL
190                         if ($l['linkto'] == $linkid and $l['relation'] == $relation) {
191                             // found and skip
192                             $skip = 1;
193                         }
194                     }
195                 }
196                 if (! $skip ) {
197                     if ($update) {
198                     }
199                     if ($relation) {
200                         $dbh->Execute("INSERT INTO $link_tbl (linkfrom, linkto, relation)"
201                                       . " VALUES ($pageid, $linkid, $relation)");
202                     } else {
203                         $dbh->Execute("INSERT INTO $link_tbl (linkfrom, linkto)"
204                                       . " VALUES ($pageid, $linkid)");
205                     }
206                 }
207
208                 if (array_key_exists($linkid, $oldlinks)) {
209                     // This was also in the previous page
210                     unset($oldlinks[$linkid]);
211                 }
212             }
213         }
214         // purge page table: delete all non-referenced pages
215         // for all previously linked pages...
216         if (DEBUG and $oldlinks) {
217             // trigger_error("purge page table: delete all non-referenced pages...", E_USER_NOTICE);
218             foreach ($oldlinks as $id => $name) {
219                 // ...check if the page is empty and has no version
220                 if($id != '') {
221                     if ($dbh->getRow("SELECT $page_tbl.id FROM $page_tbl"
222                                      . " LEFT JOIN $nonempty_tbl ON ($nonempty_tbl.id = $page_tbl.id)"//'"id" is not a recognized table hints option'
223                                      . " LEFT JOIN $version_tbl ON ($version_tbl.id = $page_tbl.id)"//'"id" is not a recognized table hints option'
224                                      . " WHERE $nonempty_tbl.id is NULL"
225                                      . " AND $version_tbl.id is NULL"
226                                      . " AND $page_tbl.id=$id"))
227                     {
228                         trigger_error("delete empty and non-referenced link $name ($id)", E_USER_NOTICE);
229                         $dbh->Execute("DELETE FROM $page_tbl WHERE id=$id");   // this purges the link
230                         $dbh->Execute("DELETE FROM $recent_tbl WHERE id=$id"); // may fail
231                     }
232                 }
233             }
234         }
235         $this->unlock(array('link'));
236         return true;
237     }
238
239 };
240
241 // Local Variables:
242 // mode: php
243 // tab-width: 8
244 // c-basic-offset: 4
245 // c-hanging-comment-ender-p: nil
246 // indent-tabs-mode: nil
247 // End: