-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPDO.php
223 lines (126 loc) · 5.13 KB
/
PDO.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
header('Content-Type: text/html; charset=UTF-8');
$outputfilename = 'exported.xml';//Writes to the same directory as .php file located
$dbhost = 'localhost';//Host
$dbuname = '';//Database Username
$dbpass = '';//Database Password
$dbname = '';//Database Name
$prefix = 'nuke';//Table Prefix
$nukeurl = '';//No trailing slash here (ie: http://www.clanthemes.com)
$module_name = 'News';//Module Name
//Some Modifications
function disqushtml($context)
{
//Could add some more filters here if you need theme
//Can comment out if you don't want to strip out any html.
$context = strip_tags($context, '<a><blockquote>');
return $context;
}
//Let's connect
$dbh = new PDO('mysql:host='.$dbhost.';dbname='.$dbname, $dbuname, $dbpass);
//XML Head
$dom = new DOMDocument('1.0', 'UTF-8');
$rss = $dom->createElement('rss');
$rss->setAttribute('version', '2.0');
$rss->setAttribute('xmlns:content', 'http://purl.org/rss/1.0/modules/content/');
$rss->setAttribute('xmlns:dsq', 'http://www.disqus.com/');
$rss->setAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/');
$rss->setAttribute('xmlns:wp', 'http://wordpress.org/export/1.0/');
$channel = $dom->createElement('channel');
//Query for Stories
$sqlstory ='SELECT sid, title, time FROM '.$prefix.'_stories ORDER BY sid';
$querystory = $dbh->prepare($sqlstory);
$querystory->execute();
$newsloop = $querystory->fetchAll();
//Loop Stories to build Items
foreach($newsloop as $news)
{
$sid = intval($news['sid']);
$title = $news['title'];
$time = $news['time'];
//Modify this to fit you're specific needs
$pageurl = $nukeurl.'/modules.php?name='.$module_name.'&file=article&sid='.$sid;
//Query Comments table based on sid(story id)
$sqlcomm = 'SELECT tid, pid, date, name, email, url, host_name, comment FROM '.$prefix.'_comments WHERE sid = '.$sid.' ORDER BY tid';
$querycomm = $dbh->prepare($sqlcomm);
$querycomm->execute();
$countcomm = $querycomm->rowCount();
//Let's not build items if the article has no comments
if($countcomm > 0)
{
$item = $dom->createElement('item');
$ttitle = $dom->createElement('title', htmlspecialchars($title, ENT_NOQUOTES, 'UTF-8'));
$link = $dom->createElement('link', htmlentities($pageurl));
//Be sure to modify the Identifier if you need to
$dsqthread = $dom->createElement('dsq:thread_identifier', $module_name.'-'.$sid);
$postdate = $dom->createElement('wp:post_date_gmt', $time);
$status = $dom->createElement('wp:comment_status', 'open');
$channel->appendChild($item);
$item->appendChild($ttitle);
$item->appendChild($link);
$item->appendChild($dsqthread);
$item->appendChild($postdate);
$item->appendChild($status);
//Loop Comment Results
foreach($querycomm as $comms)
{
$cid = intval($comms['tid']);
$parentid = intval($comms['pid']);
$ctime = $comms['date'];
$cname = $comms['name'];
$email = $comms['email'];
$url = $comms['url'];
$ipaddy = $comms['host_name'];
//Filter function
$comment = disqushtml($comms['comment']);
//I decided to query the users table since some of comments were missing urls and emails.
//That being said, matching by username is hit and miss.
if(empty($email) OR empty($url))
{
$sqluser = 'SELECT user_email, user_website FROM '.$prefix.'_users WHERE username = :cname LIMIT 1';
$queryuser = $dbh->prepare($sqluser);
$queryuser->execute(array('cname' => $cname));
$resultuser = $queryuser->fetch();
if(empty($email))
{
$email = $resultuser['user_email'];
}
if(empty($url))
{
$url = $resultuser['user_website'];
}
}
$wpcom = $dom->createElement('wp:comment');
$commid = $dom->createElement('wp:comment_id', $cid);
$cauthor = $dom->createElement('wp:comment_author', $cname);
$cemail = $dom->createElement('wp:comment_author_email', $email);
$cauthurl = $dom->createElement('wp:comment_author_url', $url);
$cauthip = $dom->createElement('wp:comment_author_IP', $ipaddy);
$cdate = $dom->createElement('wp:comment_date_gmt', $ctime);
$ccontent = $dom->createElement('wp:comment_content');
$ccontentdata = $dom->createCDATASection(htmlentities($comment, ENT_NOQUOTES, 'UTF-8'));
$ccontentdata = $ccontent->appendChild($ccontentdata);
$capproved = $dom->createElement('wp:comment_approved', '1');
$cparent = $dom->createElement('wp:comment_parent', $parentid);
$item->appendChild($wpcom);
$wpcom->appendChild($commid);
$wpcom->appendChild($cauthor);
$wpcom->appendChild($cemail);
$wpcom->appendChild($cauthurl);
$wpcom->appendChild($cauthip);
$wpcom->appendChild($cdate);
$wpcom->appendChild($ccontent);
$wpcom->appendChild($capproved);
$wpcom->appendChild($cparent);
} //Close Comments Loop
} //End If
} //Close Article Loop
$rss->appendChild($channel);
$dom->appendChild($rss);
$dom->formatOutput = true;
//Writes file to directory and prints bytes written.
echo 'Successfully Wrote ' .$dom->save($outputfilename). ' bytes';
//Close
$dbh = null;