-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwp2hashover.php
More file actions
251 lines (237 loc) · 8.27 KB
/
Copy pathwp2hashover.php
File metadata and controls
251 lines (237 loc) · 8.27 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
namespace HashOver;
if (!is_file('config.php')) {
exit('Copy config.php.example to config.php and edit !');
}
// Include config
include('config.php');
// Do some standard HashOver setup work
require ($hashover_folder.'backend/standard-setup.php');
// Setup class autoloader
setup_autoloader ();
$crypto = new Crypto ();
// For Cli mod jump
if(php_sapi_name() == "cli") {
$jump="\n";
} else {
$jump="<br />\n";
}
echo "## wp2hashover ##".$jump.$jump;
use PDO;
use DateTime;
// Generates a slug for HashOver to use based on config and post type.
function get_slug_for_db($rowdata, $post_format, $page_format) {
$date = strtotime($rowdata['post_date']);
$searches = array(
':year',
':month',
':i_month',
':day',
':i_day',
':hour',
':minute',
':title',
':post_title',
':id',
);
$replaces = array(
date('Y', $date),
date('m', $date),
date('n', $date),
date('d', $date),
date('j', $date),
date('H', $date),
date('i', $date),
$rowdata['post_name'],
$rowdata['post_name'],
$rowdata['ID'],
);
if ($rowdata['post_type'] != 'page') {
return str_replace($searches, $replaces, $post_format);
} else {
return str_replace($searches, $replaces, $page_format);
}
}
// Connect WP
$wpDbConnect = new PDO('mysql:host='.$db_host.';dbname='.$wp_db.';charset=utf8',$db_user,$db_password);
// Connect hashover
$hashoverDbConnect = new PDO('mysql:host='.$db_host.';dbname='.$hashover_db.';charset=utf8',$db_user,$db_password);
$req = $wpDbConnect->prepare(' SELECT ID,post_date,post_title,post_name,post_type,comment_author,comment_author_IP,comment_author_email,comment_author_url,comment_date,comment_content,comment_parent
FROM '.$wp_table_prefix.'comments, '.$wp_table_prefix.'posts
WHERE '.$wp_table_prefix.'posts.ID = comment_post_ID
AND post_status = "publish"
AND comment_approved = 1
ORDER BY `'.$wp_table_prefix.'comments`.`comment_date` ASC');
$req->execute();
if($req->rowCount() > 0) {
foreach($req as $row) {
$comment_parent=null;
//~ var_dump($row);
// Prepare data for hashover DB
$data['domain']=$hashover_domain;
$data['thread']=get_slug_for_db($row, $hashover_thread_syntax_posts, $hashover_thread_syntax_pages);
$data['body']=$row['comment_content'];
$data['status']=null;
$data['date']=date(DateTime::ISO8601, strtotime($row['comment_date']));
if ($hashover_admin_wp_authorFrom == $row['comment_author']) {
$data['name']=$hashover_admin_wp_authorTo;
$data['website']=$hashover_admin_wp_website;
$emailCrypt=$crypto->encrypt($hashover_admin_wp_email);
$data['email']=$emailCrypt['encrypted'];
$data['encryption']=$emailCrypt['keys'];
$data['email_hash']=md5(mb_strtolower($hashover_admin_wp_email));
} else {
$data['name']=$row['comment_author'];
$data['website']=$row['comment_author_url'];
$emailCrypt=$crypto->encrypt($row['comment_author_email']);
$data['email']=$emailCrypt['encrypted'];
$data['encryption']=$emailCrypt['keys'];
$data['email_hash']=md5(mb_strtolower($row['comment_author_email']));
}
$data['password']=null;
$data['login_id']=null;
$data['notifications']=$hashover_notifications;
$data['ipaddr']=$row['comment_author_IP'];
$data['likes']=$hashover_likes;
$data['dislikes']=$hashover_dislikes;
// Parent / Id check...
// If no comment parent
if ($row['comment_parent'] == 0){
$req = $hashoverDbConnect->prepare('SELECT comment
FROM comments
WHERE domain = "'.$data['domain'].'"
AND thread = "'.$data['thread'].'"
AND (comment REGEXP "^[0-9]+$")
ORDER BY comment DESC
LIMIT 1');
$req->execute();
$row2=$req->fetch();
if($req->rowCount() == 0) {
// First comment
$comment_parent=1;
} else {
// Chose next id
$comment_parent=$row2['comment']+1;
}
} else {
// If have parent
//~ var_dump($row['comment_parent']);
// Search wordpress data with comment_parent
$req3 = $wpDbConnect->prepare(' SELECT post_name,comment_author,comment_date
FROM '.$wp_table_prefix.'comments, '.$wp_table_prefix.'posts
WHERE '.$wp_table_prefix.'posts.ID = comment_post_ID
AND comment_ID = '.$row['comment_parent']);
$req3->execute();
$wp_parent_data=$req3->fetch();
// Search hashover parent data (ID)
$req = $hashoverDbConnect->prepare('SELECT comment
FROM comments
WHERE domain = "'.$hashover_domain.'"
AND thread = "'.$wp_parent_data['post_name'].'"
AND name = "'.$wp_parent_data['comment_author'].'"
AND date = "'.date(DateTime::ISO8601, strtotime($wp_parent_data['comment_date'])).'"
LIMIT 1');
$req->execute();
//~ var_dump($req);
$hashover_parent_data=$req->fetch();
//~ var_dump($hashover_parent_data);
// List hashover parent data ID (if multiple child)
$req = $hashoverDbConnect->prepare('SELECT comment
FROM comments
WHERE domain = "'.$data['domain'].'"
AND thread = "'.$data['thread'].'"
AND (comment REGEXP "^'.$hashover_parent_data['comment'].'-[0-9]+$")
ORDER BY comment DESC
LIMIT 1');
$req->execute();
//~ var_dump($req);
$row2=$req->fetch();
//~ var_dump($row2);
if($req->rowCount() == 0) {
// First child
$comment_parent=$hashover_parent_data['comment'].'-1';
} else {
// No fist child
//~ var_dump($row2['comment']);
//~ // Chose next id
$id_parent_explode=explode('-', $row2['comment']);
$id_construction='';
for ($i = 0; $i < count($id_parent_explode); $i++) {
if ($i == 0) {
$id_construction = $id_parent_explode[$i];
} else {
if ($i == count($id_parent_explode)-1) {
$next=$id_parent_explode[$i]+1;
$id_construction = $id_construction.'-'.$next;
}else {
$id_construction = $id_construction.'-'.$id_parent_explode[$i];
}
}
}
$comment_parent=$id_construction;
}
//~ var_dump($id_construction);
}
$data['comment']=$comment_parent; // Id à definir... $row['comment_parent']
//~ var_dump('ID attribué : '.$data['comment']);
echo 'In '.$data['thread'].', by '.$data['name'].' at '.$data['date'].' with id '.$comment_parent.' : ';
// No doublon (check)
$req = $hashoverDbConnect->prepare(' SELECT name
FROM comments
WHERE domain = "'.$data['domain'].'"
AND thread = "'.$data['thread'].'"
AND name = "'.$data['name'].'"
AND date = "'.$data['date'].'"');
$req->execute();
if($req->rowCount() == 0) {
// Insert in hashover DB
$insertcmd = $hashoverDbConnect->prepare("INSERT INTO `comments` (domain, thread, comment, body, status, date, name, password, login_id, email, encryption, email_hash, notifications, website, ipaddr, likes, dislikes)
VALUES (:domain, :thread, :comment, :body, :status, :date, :name, :password, :login_id, :email, :encryption, :email_hash, :notifications, :website, :ipaddr, :likes, :dislikes)");
$insertcmd->execute($data);
echo 'Ok';
} else {
echo '**Already registered**';
}
echo $jump;
}
} else {
echo "No wordpress post found".$jump;
}
// ################ page-info
$reqPosts = $wpDbConnect->prepare('SELECT post_title,post_name,post_date
FROM '.$wp_table_prefix.'posts, '.$wp_table_prefix.'comments
WHERE '.$wp_table_prefix.'posts.ID = comment_post_ID
AND post_status = "publish"
AND comment_approved = 1');
$reqPosts->execute();
if($reqPosts->rowCount() > 0) {
foreach($reqPosts as $row) {
// Check doublon
$reqPostNameDoublon = $hashoverDbConnect->prepare('SELECT thread
FROM `page-info`
WHERE thread = "'.$row['post_name'].'"');
$reqPostNameDoublon->execute();
echo 'Add '.$row['post_name'].' page : ';
// If not exist
$datapi=array();
if($reqPostNameDoublon->rowCount() == 0) {
$datapi['domain']=$hashover_domain;
$datapi['thread']=$row['post_name'];
$datapi['url']=$wp_siteurl.get_slug_for_db($row, $hashover_url_syntax_posts, $hashover_url_syntax_pages);
$datapi['title']=$row['post_title'];
$insertcmd = $hashoverDbConnect->prepare("INSERT INTO `page-info` (domain, thread, url, title)
VALUES (:domain, :thread, :url, :title)");
$insertcmd->execute($datapi);
//~ var_dump($insertcmd->debugDumpParams());
//~ var_dump($datapi);
echo 'Ok';
} else {
echo '**Already registered**';
}
echo $jump;
}
} else {
echo "No wordpress post found".$jump;
}
exit();
?>