forked from NoahY/q2a-network
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqa-network-migrate.php
More file actions
339 lines (270 loc) · 10 KB
/
qa-network-migrate.php
File metadata and controls
339 lines (270 loc) · 10 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
class qa_network_migrate {
private $directory;
private $urltoroot;
public function load_module($directory, $urltoroot)
{
$this->directory = $directory;
$this->urltoroot = $urltoroot;
}
public function suggest_requests()
{
return array(
array(
'title' => 'Network Migrate',
'request' => 'network-migrate',
'nav' => 'footer',
),
);
}
public function match_request($request)
{
// validates the postid so we don't need to do this later
return ($request == 'network-migrate');
}
public function process_request($request)
{
$qa_content=qa_content_prepare();
$qa_content['title']='Network Migrate';
$user_level = qa_get_logged_in_level();
if($user_level<QA_USER_LEVEL_MODERATOR)
{
if($user_level==null)
$qa_content['error']="Nothing Yet, Try Logging In, Come back, Ask me again!";
else
$qa_content['error']="You Don't Have The Permissions. Ask Me When you Grow Up.";
return $qa_content;
}
require_once QA_INCLUDE_DIR.'qa-app-posts.php';
$ok = null;
// get sites
$idx = 0;
$sites = array();
while(qa_opt('network_site_'.$idx.'_url')) {
$sites[qa_opt('network_site_'.$idx.'_prefix')] = qa_opt('network_site_'.$idx++.'_title');
}
if (qa_clicked('network_site_migrate')) {
$pids =explode(",", qa_post_text('network_site_migrate_id'));
$prefix = qa_db_escape_string(qa_post_text('network_site_migrate_site'));
$cat = qa_post_text('network_site_migrate_cat');
$keep_post_copy = (bool)qa_post_text('keep_post_copy');
// migrate question to get new id
foreach($pids as $pid)
{
$pid = trim($pid);
$post = qa_db_read_one_assoc(
qa_db_query_sub(
'SELECT * FROM ^posts WHERE postid=#',
$pid
),
true
);
$nid = $this->post_migrate($prefix,$post,null,$cat);
// migrate children
$query = qa_db_query_sub(
'SELECT * FROM ^posts WHERE parentid=#',
$pid
);
$children = 0;
while(($child = qa_db_read_one_assoc($query,true)) !== null) {
// migrate child (comment or answer to question)
$ncid = $this->post_migrate($prefix,$child,$nid);
$children++;
if(strpos($child['type'],'A') === 0) {
// update selchildid if selected
if($child['postid'] == $post['selchildid']) {
qa_db_query_sub(
'UPDATE '.$prefix.'posts SET selchildid=# WHERE postid=#',
$ncid,$nid
);
}
// check for grandchildren
$query2 = qa_db_query_sub (
'SELECT * FROM ^posts WHERE parentid=#',
$child['postid']
);
while(($gchild = qa_db_read_one_assoc($query2,true)) !== null) {
// unrelate related questions... any other choice?
if(strpos($gchild['type'],'Q') === 0) {
qa_db_query_sub(
'UPDATE ^posts SET parentid=NULL WHERE postid=#',
$gchild['postid']
);
}
else { // migrate comments to answers
$this->post_migrate($prefix,$gchild,$ncid);
$children++;
}
//qa_post_delete($gchild['postid']);
}
mysqli_free_result($query2);
}
if(!$keep_post_copy)
qa_post_delete($child['postid']);
}
mysqli_free_result($query);
qa_db_query_sub(
'INSERT INTO '.$prefix.'postmeta (post_id,meta_key,meta_value) VALUES (#,$,$)',
$nid,'migrated',QA_MYSQL_TABLE_PREFIX.'|'.time().'|'.qa_get_logged_in_handle()
);
// qa_post_delete($post['postid']);
if($ok) $ok.="<br>";
$ok .= 'Post '.$pid.($children?' and '.$children.' child posts':'').' migrated to '.$sites[qa_post_text('network_site_migrate_site')].'.';
}
}
// Create the form for display
$fields = array();
$fields[] = array(
'label' => 'Post IDs to migrate',
'tags' => 'NAME="network_site_migrate_id"',
'note' => 'Warning: will migrate all child posts as well.',
'type' => 'number',
);
$fields[] = array(
'label' => 'Migrate to site:',
'tags' => 'NAME="network_site_migrate_site"',
'type' => 'select',
'options' => $sites,
);
$fields[] = array(
'label' => 'Category ID on new site',
'tags' => 'NAME="network_site_migrate_cat"',
'note' => 'Optional - cat ID must exist on new site',
'type' => 'number',
);
$fields[] = array(
'label' => 'Keep post copy on current site',
'tags' => 'NAME="keep_post_copy"',
'type' => 'checkbox',
);
$qa_content['form'] = array(
'tags' => 'method="post" action="'.qa_self_html().'"',
'style' => 'wide',
'ok' => ($ok && !isset($error)) ? $ok : null,
'fields' => $fields,
'buttons' => array(
array(
'label' => 'Migrate Post',
'tags' => 'NAME="network_site_migrate"',
),
),
);
return $qa_content;
}
function post_migrate($prefix,$post,$parentid=null,$cat=null) {
require_once QA_INCLUDE_DIR.'qa-app-post-update.php';
// get new parent id
$result = qa_db_query_sub("SHOW TABLE STATUS LIKE '".$prefix."posts'");
$row = mysqli_fetch_array($result);
// $nid = $row['Auto_increment'];
// copy post to new site
qa_db_query_sub(
'INSERT INTO '.$prefix.'posts (type,parentid,categoryid,catidpath1,catidpath2,catidpath3,acount,amaxvote,selchildid,closedbyid,userid,cookieid,createip,lastuserid,lastip,upvotes,downvotes,netvotes,lastviewip,views,hotness,flagcount,format,created,updated,updatetype,title,content,tags,notify) VALUES($,'.($parentid?qa_db_escape_string($parentid):'NULL').','.($cat?qa_db_escape_string($cat):'NULL').',NULL,NULL,NULL,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,$,#,#,$,$,$,$,$)',
$post['type'],($post['acount']?$post['acount']:0),($post['amaxvote']?$post['amaxvote']:0),$post['selchildid'],$post['closedbyid'],$post['userid'],$post['cookieid'],$post['createip'],$post['lastuserid'],$post['lastip'],$post['upvotes'],$post['downvotes'],$post['netvotes'],$post['lastviewip'],($post['views']?$post['views']:0),$post['hotness'],$post['flagcount'],($post['format']?$post['format']:''),$post['created'],$post['updated'],$post['updatetype'],$post['title'],$post['content'],$post['tags'],$post['notify']
);
$nid = mysqli_insert_id(qa_db_connection());
mysqli_free_result($result);
//update answer key
$query = qa_db_query_sub(
'SELECT answer_str,userid,created,edited,editedby FROM ^ec_answers WHERE postid=#',
$post['postid']
);
while(($answer = qa_db_read_one_assoc($query,true)) !== null) {
qa_db_query_sub(
'INSERT INTO '.$prefix.'ec_answers (postid,answer_str,userid,created,edited,editedby) VALUES(#,$,#,$,$,#)',
$nid,$answer['answer_str'],$answer['userid'],$answer['created'],$answer['edited'],$answer['editedby']
);
}
// get old uservotes
$query = qa_db_query_sub(
'SELECT * FROM ^uservotes WHERE postid=#',
$post['postid']
);
while(($vote = qa_db_read_one_assoc($query,true)) !== null) {
// add new uservote
qa_db_query_sub(
'INSERT INTO '.$prefix.'uservotes (postid,userid,vote,flag,votecreated,voteupdated) VALUES(#,#,#,#,$,$)',
$nid,$vote['userid'],$vote['vote'],$vote['flag'],$vote['votecreated'],$vote['voteupdated']
);
}
mysqli_free_result($query);
// make remote request for update
$idx = 0;
$url = '';
while($idx <= (int)qa_opt('network_site_number')) {
if(qa_opt('network_site_'.$idx.'_prefix') == $prefix) {
$url = qa_opt('network_site_'.$idx.'_url');
break;
}
$idx++;
}
// set migrate prefix to invoke override, changing the set of tables temporarily -- yikes!
global $migrate_change_db;
$migrate_change_db = $prefix;
require_once QA_INCLUDE_DIR.'qa-db-post-create.php';
require_once QA_INCLUDE_DIR.'qa-db-post-update.php';
require_once QA_INCLUDE_DIR.'qa-db-points.php';
require_once QA_INCLUDE_DIR.'qa-db-votes.php';
$post = qa_db_read_one_assoc(
qa_db_query_sub(
'SELECT * FROM ^posts WHERE postid=#',
$nid
),
true
);
qa_db_posts_calc_category_path($post['postid']);
$text=qa_post_content_to_text($post['content'], $post['format']);
if($post['type'] == 'Q') {
$tagstring=qa_post_tags_to_tagstring($post['tags']);
qa_db_category_path_qcount_update(qa_db_post_get_category_path($post['postid']));
qa_db_hotness_update($post['postid']);
qa_post_index($post['postid'], 'Q', $post['postid'], null, $post['title'], $post['content'], $post['format'], $text, $tagstring,($cat?qa_db_escape_string($cat):'NULL'));
qa_db_points_update_ifuser($post['userid'], array('qposts', 'aselects', 'qvoteds', 'upvoteds', 'downvoteds'));
qa_db_qcount_update();
qa_db_unaqcount_update();
qa_db_unselqcount_update();
qa_db_unupaqcount_update();
}
else if($post['type'] == 'A') {
$question = qa_db_read_one_assoc(
qa_db_query_sub(
'SELECT * FROM ^posts WHERE postid=#',
$parentid
),
true
);
if ($question['type']=='Q')
qa_post_index($post['postid'], 'A', $question['postid'], $question['postid'], null, $post['content'], $post['format'], $text, null,($cat?qa_db_escape_string($cat):'NULL'));
qa_db_post_acount_update($question['postid']);
qa_db_hotness_update($question['postid']);
qa_db_points_update_ifuser($post['userid'], array('aposts', 'aselecteds', 'avoteds', 'upvoteds', 'downvoteds'));
qa_db_acount_update();
qa_db_unaqcount_update();
qa_db_unupaqcount_update();
}
else if($post['type'] == 'C') {
$parent = qa_db_read_one_assoc(
qa_db_query_sub(
'SELECT * FROM ^posts WHERE postid=#',
$parentid
),
true
);
if(strpos($parent['type'],'A') === 0)
$question = qa_db_read_one_assoc(
qa_db_query_sub(
'SELECT * FROM ^posts WHERE postid=#',
$parent['postid']
),
true
);
else $question = $parent;
if ( ($question['type']=='Q') && (($parent['type']=='Q') || ($parent['type']=='A')) ) // only index if antecedents fully visible
qa_post_index($post['postid'], 'C', $question['postid'], $parent['postid'], null, $post['content'], $post['format'], $text, null,($cat?qa_db_escape_string($cat):'NULL'));
qa_db_points_update_ifuser($post['userid'], array('cposts'));
qa_db_ccount_update();
}
$migrate_change_db = null;
return $nid;
}
}