forked from moodleou/moodle-mod_oublog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigratepersonal.php
1028 lines (989 loc) · 46.4 KB
/
migratepersonal.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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Import migrated personal blogs from a 1.9 system
* These reside in dataroot - provides means to delete imported data files
*
* @package mod
* @subpackage oublog
* @copyright 2012 The open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(__FILE__) . '/../../config.php');
require_once($CFG->dirroot . '/auth/sams_soap/auth.php');
require_once($CFG->dirroot . '/mod/oublog/locallib.php');
require_once($CFG->dirroot . '/mod/oublog/lib.php');
require_login();
$context = context_system::instance();
require_capability('moodle/site:config', $context);
$datadir = $CFG->dataroot . '/oublog/migration';
$action = optional_param('action', 'NONE', PARAM_ALPHA);
$PAGE->set_context($context);
$PAGE->set_url('/mod/oublog/migratepersonal.php', array('action' => $action));
// Setup form for uploading zip files.
require_once("$CFG->libdir/formslib.php");
require_once("$CFG->libdir/uploadlib.php");
class importpersonal extends moodleform {
public function definition() {
$mform =& $this->_form;
$mform->addElement('filepicker', 'attachment', 'Import 1.9 data zip', null, array('accepted_types' => '*.zip'));
$this->add_action_buttons(false, 'Import');
}
}
$form = new importpersonal();
if ($data = $form->get_data()) {
// Upload file.
@raise_memory_limit(MEMORY_EXTRA);
@set_time_limit(0);
$file = $form->save_temp_file('attachment');
output('', true);
$fp = get_file_packer();
// Check file is vaild.
$files = $fp->list_files($file);
output('', true);
if (!isset($files[0]->pathname) || strpos($files[0]->pathname, 'pb') !== 0) {
print_error('Invalid migration zip file.');
}
$fp->extract_to_pathname($file, $datadir);
unlink($file);
output('Unzipped file ready for importing');
echo $OUTPUT->continue_button($PAGE->url);
exit;
}
print $OUTPUT->header();
print $OUTPUT->heading('Import migrated personal blog data');
if ($action == 'DELETE') {
// Delete an attempt folder.
require_sesskey();
$name = required_param('name', PARAM_PATH);
$dirname = $datadir . '/' . $name;
if (!file_exists($dirname) || $name == '' || strpos($name, 'pb') === false) {
print_error('Directory does not exist.');
}
if (optional_param('confirm', false, PARAM_BOOL)) {
if (fulldelete($dirname)) {
print 'Folder deleted.';
}
} else {
$link = $CFG->wwwroot . '/mod/oublog/migratepersonal.php';
$params = "?action=DELETE&name=$name&confirm=1&sesskey=" . sesskey();
print $OUTPUT->confirm('Are you sure you want to delete?', $link . $params, $link);
}
} else if ($action == 'GO') {
require_sesskey();
$name = required_param('name', PARAM_PATH);
$dirname = $datadir . '/' . $name;
if (!file_exists($dirname) || $name == '' || strpos($name, 'pb') === false) {
print_error('Directory does not exist.');
}
if (optional_param('confirm', false, PARAM_BOOL)) {
// Process the folder.
@raise_memory_limit(MEMORY_EXTRA);
@set_time_limit(0);
migrate_backup($dirname);
} else {
if (!file_exists($dirname . '/success.txt') || !file_exists($dirname . '/oublog.xml')) {
print_error('Data no good. Missing key files.');
}
// File checks/info.
$doc = new DOMDocument();
$doc->load($dirname . '/oublog.xml');
$instances = $doc->getElementsByTagName('INSTANCE');
print '<p>' . $instances->length . ' instances in dataset.</p>';
if (file_exists($dirname . '/imported.xml')) {
$doc = new DOMDocument();
$doc->load($dirname . '/imported.xml');
$tags = $doc->getElementsByTagName('instance');
print '<p>Have processed ' . $tags->length . ' previously.</p>';
}
$link = $CFG->wwwroot . '/mod/oublog/migratepersonal.php';
$params = "?action=GO&name=$name&confirm=1&sesskey=" . sesskey();
$form = $OUTPUT->confirm('You cannot undo this (it will attempt to rollback DB on error). Continue?',
$link . $params, $link);
// Add in server image/link match switching.
$form = str_replace('<input type="submit" value="Continue" />',
'<input id="acct" name="acct" type="checkbox">Images point to Acct?</input><br />
<input name="kill" type="checkbox" checked="checked">Trial (rollback transactions)?</input><br />
<label for="total">Instances to process in this run:</label>
<select id="total" name="total">
<option value="0">All available</option>
<option value="500">500</option>
<option value="1000">1000</option>
<option value="5000">5000</option>
</select><br />
<input name="ignore" type="checkbox">Re-process instances already added? (Really shouldn\'t select this)</input><br />
<input name="tagsonly" type="checkbox">Fix already imported posts tags (and nothing else)</input><br />
<input type="submit" value="Continue" />', $form);
$form .= '<p>Select checkbox when images and urls in data set are pointing to learnacct not live server.</p>
<p>Only select to fix tags when a version of the migration script where they were not imported correctly was used.</p>
<p>Once successfully added an instance will not be processed again and will be skipped.</p>';
echo $form;
}
}
if ($action == 'NONE') {
// SHOW MAIN INTERFACE.
// Show number of personal blog instances.
if (!$blog = $DB->get_record('oublog', array('global' => 1))) {
print_error('globalblogmissing', 'oublog');
}
$bloginsts = $DB->get_record_sql('SELECT COUNT(id) as count FROM {oublog_instances} WHERE oublogid = ?',
array($blog->id));
print "<p>There are <strong>$bloginsts->count</strong> personal blog instances on the system at present";
print '<br />Imported instances will not override their settings - posts will be added to existing instances</p>';
$form->display();
// Display list of previous attempts and provide some management of these.
$attemps = array();
// Get folder info.
if (file_exists($datadir)) {
$folders = glob($datadir . '/pb*', GLOB_ONLYDIR);
foreach ($folders as $folder) {
$info = new stdClass();
$info->path = $folder;
$info->folder = substr($folder, strrpos($folder, '/') + 1);
$info->size = 0;
$info->size = @exec("du -b $folder | awk '{print $1}'");
$info->date = str_replace('pb', '', $info->folder);
$info->ok = file_exists($folder . '/success.txt') ? 'OK' : 'Failed';
$attemps[] = $info;
}
}
// Now show.
print '<div id="existing"><p>Previous backups:</p>';
foreach ($attemps as $attempt) {
print '<p>' . userdate($attempt->date) . ' : ' . formatbytes($attempt->size) . ' (' . $attempt->ok . ') ';
// Delete.
print '<a href="' . $CFG->wwwroot . '/mod/oublog/migratepersonal.php?action=DELETE&name=' . $attempt->folder . '&sesskey=' . sesskey() . '">';
print $OUTPUT->pix_icon('t/delete', 'Delete') . '</a> ';
// Zip.
print '<a href="' . $CFG->wwwroot . '/mod/oublog/migratepersonal.php?action=GO&name=' . $attempt->folder . '&sesskey=' . sesskey() . '">';
print $OUTPUT->pix_icon('i/restore', 'Import data') . '</a>';
print '</p>';
}
print '</div>';
}
print $OUTPUT->footer();
exit;
/**
* Start the import of personal blog data
* @param string $dir path of directory containing backup data
*/
function migrate_backup($dir) {
global $DB, $CFG, $SOURCESERVER;
$SOURCESERVER = 'http://learn.open.ac.uk';
if (optional_param('acct', false, PARAM_BOOL)) {
$SOURCESERVER = 'http://learnacct.open.ac.uk';
}
if ($kill = optional_param('kill', false, PARAM_BOOL)) {
output('<b>Trial run</b>, will perform rollback on each instance.');
}
if ($tagsonly = optional_param('tagsonly', false, PARAM_BOOL)) {
output('<b>Tags only</b>, will only fix tags, not add posts.');
}
// Load in XML.
$doc = new DOMDocument();
$doc->load($dir . '/oublog.xml');
$instancetags = $doc->getElementsByTagName('INSTANCE');
output('Total = ' . $instancetags->length . ' instances ');
$total = required_param('total', PARAM_INT);
$max = $total == 0 ? $instancetags->length + 1 : $total;
output('Max instances to process: ' . $max);
// Get xml that stores old instance ID's of those instances already processed.
$donedoc = new DOMDocument();
$done = array();
$donetag = null;
if (!optional_param('ignore', false, PARAM_BOOL) && file_exists($dir . '/imported.xml')) {
$donedoc->load($dir . '/imported.xml');
$donestr = $donedoc->saveXML();
$donetag = $donedoc->getElementsByTagName('imported')->item(0);
$tags = $donetag->childNodes->item(0);
while ($tags != null) {
// Store ids in array using fast looping.
$done[$tags->nodeValue] = '';
$tags->parentNode->removeChild($tags);
$tags = $donetag->childNodes->item(0);
}
// Reinstate childnodes.
$donedoc->loadXML($donestr);
$donestr = null;
output('Have already processed ' . count($done) . ' instances');
} else {
$donedoc->appendChild($donedoc->createElement('imported'));
}
$donetag = $donedoc->getElementsByTagName('imported')->item(0);
$count = 0;
$instances = array();// Stores data (as object) about each instance in array.
$tags = $instancetags->item(0);
// Fast looping of node list by always accessing first tag (faster than foreach).
while ($tags != null && $count < $max) {
$info = childnodes_to_object($tags);
// Remove tag we just processed.
$tags->parentNode->removeChild($tags);
// Get the next tag to parse.
$tags = $instancetags->item(0);
// Have we processed this instance before? If so, skip.
if (isset($done[$info->id])) {
$info = null;
continue;
}
$instances[$info->id] = $info;
$info = null;
$count++;
}
// Throw away xml as no longer need.
$doc = null;
$instancetags = null;
$tags = null;
output("Ready to process $count instances");
// Process each blog instance - checking for existing user instance.
$blog = $DB->get_record('oublog', array('global' => 1), 'id', MUST_EXIST);
$blog->global = 1;
output('Got global blog record');
if (!$cm = get_coursemodule_from_instance('oublog', $blog->id)) {
print_error('invalidcoursemodule');
}
$blogcontext = context_module::instance($cm->id);
// Vars used to store blog user content xml data.
$xmlcontent = new DOMDocument();
$xmlfilenum = -1;
$xpath = null;// Xpath for current content xml file.
foreach ($instances as &$instance) {
// Transaction per instance (ensure any existing are cleared up).
if (isset($trans) && ($trans instanceof moodle_transaction)) {
// Commit what has been done (errors will have rolled back already).
$DB->commit_delegated_transaction($trans);
$trans = null;
}
$trans = $DB->start_delegated_transaction();
try {
// Find/create user.
if (!$instance->newuserid = oublog_search_create_user($instance)) {
output("<b>Error: Skipping blog for $instance->username</b>");
continue;
}
// Work out new instance number (either from new or existing record).
if ($bloginst = $DB->get_record('oublog_instances',
array('oublogid' => $blog->id, 'userid' => $instance->newuserid), 'id')) {
$instance->newid = $bloginst->id;
$bloginst = null;
} else {
if ($tagsonly) {
output("<b>Error: Skipping create instance for {$instance->username} as in tag only mode</b>");
continue;
}
// Create instance.
$newbloginst = new stdClass();
$newbloginst->oublogid = $blog->id;
$newbloginst->userid = $instance->newuserid;
$newbloginst->name = $instance->name;
$newbloginst->summary = blogxml_isempty($instance->summary) ? '' : $instance->summary;
$newbloginst->summary = oublog_decode_perbloglinks($newbloginst->summary, $xpath);
$newbloginst->accesstoken = $instance->accesstoken;
$newbloginst->views = $instance->views;
if (!($instance->newid = $DB->insert_record('oublog_instances', $newbloginst))) {
output("<b>Failed to add blog instance $instance->name</b>");
continue;
}
$newbloginst = null;
// Migrate any mystuff images in summary.
if (strpos($instance->summary, '@@PLUGINFILE@@')
|| stripos($instance->summary, $SOURCESERVER . '/pix/smartpix.php/ou/s/')) {
$updaterec = new stdClass();
$updaterec->id = $instance->newid;
$updaterec->summary = oublog_add_files($instance->summary, $dir, $blogcontext->id,
'summary', $instance->newid);
$DB->update_record('oublog_instances', $updaterec);
$updaterec = null;
}
}
output('Finished setup blog instance for ' . $instance->username . ' :new id=' . $instance->newid);
// What is XML file for current instance, load if not current one.
$instfilenum = get_filenumber($instance->id);
if ($instfilenum != $xmlfilenum) {
// Load our XML into current var.
if (!$xmlcontent->load($dir . '/i' . $instfilenum . '.xml')) {
output('<b>Error: Failed to load user data</b> file number ' . $instfilenum);
continue;
}
$xmlfilenum = $instfilenum;
$xpath = new DOMXPath($xmlcontent);
// Get data from XML into vars for quick access.
$alllinks = array();
$linktags = $xmlcontent->getElementsByTagName('LINK');
foreach ($linktags as $l) {
$linkob = childnodes_to_object($l);
if (!isset($alllinks[$linkob->oubloginstancesid])) {
$alllinks[$linkob->oubloginstancesid] = array();
}
$alllinks[$linkob->oubloginstancesid][] = $linkob;
$linkob = null;
}
$linktags = null;
if ($tagsonly) {
$alllinks = array();// Ignore any links in tag fix mode;
}
$allusers = array();
$usertags = $xmlcontent->getElementsByTagName('USER');
foreach ($usertags as $t) {
$allusers[$t->getAttribute('id')] = childnodes_to_object($t);
}
$usertags = null;
$allposts = array();
$posttags = $xmlcontent->getElementsByTagName('POST');
$tag = $posttags->item(0);
// Fast looping for posts as probably the most tags.
while ($tag != null) {
$linkob = childnodes_to_object($tag);
$tag->parentNode->removeChild($tag);
$tag = $posttags->item(0);
if (!isset($linkob->oubloginstancesid)) {
continue;
}
if (!isset($allposts[$linkob->oubloginstancesid])) {
$allposts[$linkob->oubloginstancesid] = array();
}
$allposts[$linkob->oubloginstancesid][] = $linkob;
$linkob = null;
}
$posttags = null;
$alltags = array();
$tagtags = $xmlcontent->getElementsByTagName('TAG');
foreach ($tagtags as $l) {
$linkob = childnodes_to_object($l);
if (!isset($linkob->postid)) {
continue;
}
if (!isset($alltags[$linkob->postid])) {
$alltags[$linkob->postid] = array();
}
$alltags[$linkob->postid][] = $linkob;
$linkob = null;
}
$tagtags = null;
$alledits = array();
$edittags = $xmlcontent->getElementsByTagName('EDIT');
foreach ($edittags as $l) {
$linkob = childnodes_to_object($l);
if (!isset($linkob->postid)) {
continue;
}
if (!isset($alledits[$linkob->postid])) {
$alledits[$linkob->postid] = array();
}
$alledits[$linkob->postid][] = $linkob;
$linkob = null;
}
$edittags = null;
if ($tagsonly) {
$alledits = array();// Ignore any edits in tag fix mode;
}
$allcomments = array();
$commenttags = $xmlcontent->getElementsByTagName('COMMENT');
foreach ($commenttags as $l) {
$linkob = childnodes_to_object($l);
if (!isset($linkob->postid)) {
continue;
}
if (!isset($allcomments[$linkob->postid])) {
$allcomments[$linkob->postid] = array();
}
$allcomments[$linkob->postid][] = $linkob;
$linkob = null;
}
$commenttags = null;
if ($tagsonly) {
$allcomments = array();// Ignore any comments in tag fix mode;
}
output('Loaded user data file ' . $xmlfilenum);
}
// Store posts info for this instance (used in link checks).
$postinfo = array();
if (isset($allposts[$instance->id])) {
// Create a clone of the array as it contains objects...
$postinfo = serialize($allposts[$instance->id]);
$postinfo = unserialize($postinfo);
}
// Create instance links.
// $links = $xpath->query("/DATA/LINKS/LINK[OUBLOGINSTANCESID=$instance->id]");
$links = array();
if (isset($alllinks[$instance->id])) {
$links = $alllinks[$instance->id];
}
$linksa = array();
// Sort by SORTORDER - so lowest first.
foreach ($links as $newlink) {
// Create new link object and add to our new array.
// $newlink = childnodes_to_object($link);
$newlink->oubloginstancesid = $instance->newid;
$newlink->oublogid = $blog->id;
unset($newlink->id);
if (!isset($newlink->sortorder)) {
$newlink->sortorder = 0;
}
$linksa[] = $newlink;
}
$links = null;
// Sort by SORTORDER - so lowest first.
usort($linksa, 'oublog_sort_links');
foreach ($linksa as $link) {
// Create link.
$link->url = oublog_decode_perbloglinks($link->url, $xpath, $postinfo, $instance->userid);
if (!oublog_add_link($link)) {
output("Error: Failed to create link for blog:$instance->newid");
}
}
$linksa = null;
// End of link creation code.
output('Finished links');
output('Processing posts');
// PROCESS Blog posts.
$instance->postmapping = array();// Store old + new ids.
// $posts = $xpath->query("/DATA/POSTS/POST[OUBLOGINSTANCESID=$instance->id]");
$posts = array();
if (isset($allposts[$instance->id])) {
$posts = $allposts[$instance->id];
}
output(count($posts) . ' posts to process');
foreach ($posts as $newpost) {
// Sort out post object ready to save.
// $newpost = childnodes_to_object($post);
$oldid = $newpost->id;
unset($newpost->id);
$newpost->groupid = 0;// Just in case!
$newpost->oubloginstancesid = $instance->newid;
if (!is_null($newpost->deletedby)) {
if ($tagsonly) {
continue;// Don't update deleted post tags.
}
if ($newpost->deletedby == $instance->userid) {
// Most of the time will be blog user.
$newpost->deletedby = $instance->newuserid;
} else {
// Find mapped user id in this system.
// $users = $xpath->query("/DATA/USERS/USER[@id=$newpost->deletedby]");
$users = isset($allusers[$newpost->deletedby]) ? $allusers[$newpost->deletedby] : false;
if (!$users) {
$newpost->deletedby = false;
} else {
$newpost->deletedby = oublog_search_create_user($users);
}
if ($newpost->deletedby == false) {
output("Error: Failed to get/make user id for deletedby, old post id: $oldid");
$newpost->deletedby = $instance->newuserid;
}
}
}
if (!$tagsonly) {
if (!is_null($newpost->lasteditedby)) {
if ($newpost->lasteditedby == $instance->userid) {
// Most of the time will be blog user.
$newpost->lasteditedby = $instance->newuserid;
} else {
// Find mapped user id in this system.
// $users = $xpath->query("/DATA/USERS/USER[@id=$newpost->lasteditedby]");
$users = isset($allusers[$newpost->lasteditedby]) ? $allusers[$newpost->lasteditedby] : false;
if (!$users) {
$newpost->lasteditedby = false;
} else {
$newpost->lasteditedby = oublog_search_create_user($users);
}
if ($newpost->lasteditedby == false) {
output("Error: Failed to get/make user id for edited by, old post id: $oldid");
$newpost->lasteditedby = $instance->newuserid;
}
}
}
$newpost->message = oublog_decode_perbloglinks($newpost->message, $xpath, $postinfo, $instance->userid);
if (!$postid = $DB->insert_record('oublog_posts', $newpost)) {
output("<b>Error: Failed to create post, old id = $oldid</b>");
continue;
}
} else {
// Tag fix only mode. Search for post previously created to use as $postid.
if (!$post = $DB->get_record_sql('select id, message from {oublog_posts} where oubloginstancesid
= :oubloginstancesid and timeposted = :timeposted and title = :title and deletedby is null',
array('oubloginstancesid' => $instance->newid, 'timeposted' => $newpost->timeposted,
'title' => $newpost->title))) {
output("<b>Error: Failed to find post (TAG FIX), old id = $oldid</b>");
continue;
}
$postid = $post->id;
$newpost->message = $post->message;// Ensure matches that on system.
// Store existing tags so any extra added not removed.
$tagnames = oublog_get_post_tags($post);
$post = null;
}
$instance->postmapping[$oldid] = $postid;// Record old/new id mapping.
output('', true);// Output dots.
// Migrate any mystuff images in message.
if (!$tagsonly && (strpos($newpost->message, '@@PLUGINFILE@@')
|| stripos($newpost->message, $SOURCESERVER . '/pix/smartpix.php/ou/s/'))) {
$updaterec = new stdClass();
$updaterec->id = $postid;
$updaterec->message = oublog_add_files($newpost->message, $dir, $blogcontext->id,
'message', $postid);
$DB->update_record('oublog_posts', $updaterec);
$updaterec = null;
}
// Add post tags (also used in search to save DB query).
$newpost->tags = array();
// $tags = $xpath->query("/DATA/TAGS/TAG[POSTID=$oldid]");
if (!$tagsonly || empty($tagnames)) {
$tagnames = array();
}
$tags = array();
if (isset($alltags[$oldid])) {
$tags = $alltags[$oldid];
}
foreach ($tags as $tag) {
$tagnames[] = $tag->tagname;
}
if (!empty($tagnames)) {
$tagnames = array_unique(array_filter($tagnames));
oublog_update_item_tags($instance->newid, $postid, $tagnames);
$newpost->tags = $tagnames;
$tagnames = null;
}
if (is_null($newpost->deletedby)) {
// Update search.
$newpost->id = $postid;
$newpost->userid = $instance->newuserid;
oublog_search_update($newpost, $cm);
output('', true);
}
$newpost = null;
}
$posts = null;
// End Blog post processing.
output('Finished Posts');
// From now on all data is got from looping through postmapping array each time.
// PROCESS Blog EDITS.
foreach ($instance->postmapping as $oldpostid => $newpostid) {
// $edits = $xpath->query("/DATA/EDITS/EDIT[POSTID=$oldpostid]");
$edits = array();
if (isset($alledits[$oldpostid])) {
$edits = $alledits[$oldpostid];
}
foreach ($edits as $newedit) {
// Sort out edit object ready to save.
// $newedit = childnodes_to_object($edit);
unset($newedit->id);
$newedit->postid = $newpostid;
if ($newedit->userid == $instance->userid) {
// Most of the time will be blog user.
$newedit->userid = $instance->newuserid;
} else {
// Find mapped user id in this system.
// $users = $xpath->query("/DATA/USERS/USER[@id=$newedit->userid]");
$users = isset($allusers[$newedit->userid]) ? $allusers[$newedit->userid] : false;
if (!$users) {
$newedit->userid = false;
} else {
$newedit->userid = oublog_search_create_user($users);
}
if ($newedit->userid == false) {
output("Error: Failed to get/make user id for edit rec, old post id: $oldpostid");
$newedit->userid = $instance->newuserid;
}
}
$newedit->oldmessage = oublog_decode_perbloglinks($newedit->oldmessage, $xpath, $postinfo, $instance->userid);
output('', true);// Output dots.
// Migrate any mystuff images in message.
if (strpos($newedit->oldmessage, '@@PLUGINFILE@@')
|| stripos($newedit->oldmessage, $SOURCESERVER . '/pix/smartpix.php/ou/s/')) {
$newedit->oldmessage = oublog_add_files($newedit->oldmessage, $dir, $blogcontext->id,
'message', $newpostid);
}
if (!$newid = $DB->insert_record('oublog_edits', $newedit)) {
output("Error: Failed to create edit for old post id: $oldpostid");
continue;
}
$newedit = null;
}
$edits = null;
}
output('Finished edits');
// End Blog EDITS processing.
// Process blog comments (Standard + Approved only).
foreach ($instance->postmapping as $oldpostid => $newpostid) {
// $comments = $xpath->query("/DATA/COMMENTS/COMMENT[POSTID=$oldpostid]");
$comments = array();
if (isset($allcomments[$oldpostid])) {
$comments = $allcomments[$oldpostid];
}
foreach ($comments as $newcomment) {
// Sort out edit object ready to save.
// $newcomment = childnodes_to_object($comment);
$newcomment->postid = $newpostid;
if (!blogxml_isempty($newcomment->userid)) {
if ($newcomment->userid == $instance->userid) {
// Most of the time will be blog user.
$newcomment->userid = $instance->newuserid;
} else {
// Find mapped user id in this system.
// $users = $xpath->query("/DATA/USERS/USER[@id='$newcomment->userid']");
$users = isset($allusers[$newcomment->userid]) ? $allusers[$newcomment->userid] : false;
if (!$users || empty($users)) {
$newcomment->userid = false;
} else {
$newcomment->userid = oublog_search_create_user($users);
}
if ($newcomment->userid == false) {
output("Error: Failed to get/make user id for comment, old id: $newcomment->id file:$xmlfilenum");
continue;// Skip this comment.
}
}
}
if (!blogxml_isempty($newcomment->deletedby)) {
if ($newcomment->deletedby == $instance->userid) {
// Most of the time will be blog user.
$newcomment->deletedby = $instance->newuserid;
} else {
// Find mapped user id in this system.
// $users = $xpath->query("/DATA/USERS/USER[@id=$newcomment->deletedby]");
$users = isset($allusers[$newcomment->deletedby]) ? $allusers[$newcomment->deletedby] : false;
if (!$users) {
$newcomment->deletedby = false;
} else {
$newcomment->deletedby = oublog_search_create_user($users);
}
if ($newcomment->deletedby == false) {
output("Error: Failed to get/make user id for comment deletedby, old id: $newcomment->id");
$newcomment->deletedby = $instance->newuserid;
}
}
}
$newcomment->message = oublog_decode_perbloglinks($newcomment->message, $xpath, $postinfo, $instance->userid);
unset($newcomment->id);
if (!$newid = $DB->insert_record('oublog_comments', $newcomment)) {
output("Error: Failed to create comment for old post id: $oldpostid");
continue;
}
output('', true);// Output dots.
// Migrate any mystuff images in message.
if (strpos($newcomment->message, '@@PLUGINFILE@@')
|| stripos($newcomment->message, $SOURCESERVER . '/pix/smartpix.php/ou/s/')) {
$updaterec = new stdClass();
$updaterec->id = $newid;
$updaterec->message = oublog_add_files($newcomment->message, $dir, $blogcontext->id,
'messagecomment', $newid);
$DB->update_record('oublog_comments', $updaterec);
$updaterec = null;
}
$newcomment = null;
}
$comments = null;
}
output('Finished comments');
// End comments.
// Commit everything in this instance.
if ($kill) {
throw new moodle_exception('kill');
} else {
// Commit and save that instance has been imported.
$trans->allow_commit();
$trans->dispose();
$donetag->appendChild($donedoc->createElement('instance', $instance->id));
if (!$donedoc->save($dir . '/imported.xml')) {
output('<b>Error saving xml record of instance, will be picked up next save</b>');
}
}
} catch (moodle_exception $e) {
output('<b>Code error:</b> ' . $e->debuginfo);
try {
output('Rollback transation');
$trans->rollback($e);
} catch (moodle_exception $e) {
output('<b>Code error:</b> ' . $e->debuginfo);
$trans = null;
continue;
} catch (Exception $e) {
output('<b>Code error:</b> ' . $e->debuginfo);
$trans = null;
continue;
}
} catch (Exception $e) {
output('<b>Code error:</b> ' . $e->debuginfo);
$trans->rollback($e);
$trans = null;
continue;
}
$trans = null;
}
output('<b>Finished</b>');
}
/**
* Searches for user based on username.
* Creates a new user if not exist, based on info sent.
* Mirrors SAMS SOAP user creation.
* @param object $info
* @return int new ID or false
*/
function oublog_search_create_user($info) {
global $DB, $CFG;
if (empty($info->username)) {
return false;
}
// Check our local static cache of users already processed.
static $users;
if (!isset($users)) {
$users = array();
} else if (!empty($users[$info->username])) {
return $users[$info->username];
}
// Does user exist in system?
if ($user = $DB->get_record('user', array('username' => $info->username), 'id')) {
$info->newuserid = $user->id;
} else {
// Create user record with min info - expecting it to be updated when user logs in.
$user = new stdClass();
$user->username = $info->username;
$user->firstname = !blogxml_isempty($info->firstname)
? $info->firstname : 'New';
$user->lastname = !blogxml_isempty($info->lastname)
? $info->lastname : 'User';
$user->idnumber = isset($info->idnumber) ? $info->idnumber : '';
$user->email = !blogxml_isempty($info->email)
? $info->email : '[email protected]';
if ($user->email == '[email protected]') {
$user->emailstop = 1;
}
$user->country = 'GB';
$user->mnethostid = $CFG->mnet_localhost_id;
$user->lang = $CFG->lang;
$user->confirmed = 1;
$user->auth = 'sams_soap';
$user->firstaccess = time();
$user->picture = 0;
$user->maildisplay = 0;
// Set a dummy password (we won't ever check it).
$user->password = hash_internal_user_password('SAMSsupplied');
$user->trackforums = 1;
try {
$user->id = $DB->insert_record('user', $user, true);
} catch (moodle_exception $e) {
output("<strong>Failed to add user $user->username</strong>");
return false;
}
// Add user to role.
try {
$auth = new auth_plugin_sams_soap();
$auth->assign_role($user);
} catch (moodle_exception $e) {
output("<strong>Failed to assign user role for $user->username<strong>");
}
$info->newuserid = $user->id;
$user = null;
}
$users[$info->username] = $info->newuserid;
return $info->newuserid;
}
/**
* Finds refs to images in text and adds files to DB and rewrite text content
* @param string $text - text to search/replace
* @param string $dir - dir to get images from
* @param int $contextid
* @param string $filearea - name of file area to add to
* @param int $itemid - number of item id to use in files table
*/
function oublog_add_files($text, $dir, $contextid, $filearea, $itemid) {
global $CFG, $OUTPUT, $SOURCESERVER;
if (strpos($text, '@@PLUGINFILE@@')) {
require_once($CFG->dirroot . '/lib/filestorage/file_storage.php');
static $knownfiles;
if (!isset($knownfiles)) {
$knownfiles = array();
}
$pattern = '#<img.*?src="(@@PLUGINFILE@@(.*?))"#';
preg_match_all($pattern, $text, $matches);
$filerecord = new stdClass();
$filerecord->filearea = $filearea;
$filerecord->itemid = $itemid;
$filerecord->contextid = $contextid;
$filerecord->component = 'mod_oublog';
$filerecord->filepath = '/';
$fs = get_file_storage();
for ($a = 0, $len = count($matches[2]); $a < $len; $a++) {
// Add MyStuff images in a folder based on user name.
$match = $matches[2][$a];
try {
$paths = explode('/', $match);
$filerecord->filename = array_pop($paths);
if (strpos($filerecord->filename, '.')) {
// Looks like we got filename OK so try and store, check if already done first.
if (!isset($knownfiles[$contextid . $filearea . $itemid . $filerecord->filename])) {
// Import file into system.
if (!$fs->file_exists($contextid, 'mod_oublog', $filearea, $itemid, '/', $filerecord->filename)) {
$fs->create_file_from_pathname($filerecord, $dir . $match);
// Store we've processed so no need to import again (saves 1 query).
$knownfiles[$contextid . $filearea . $itemid . $filerecord->filename] = 0;
}
}
// Replace full path to just filename for rewrite plugin urls.
$text = str_replace($match, '/' . $filerecord->filename, $text);
}
} catch (moodle_exception $e) {
// TODO clean up match if fail?
output('Failed to import file ' . $match . ' : ' . $e->getMessage());
}
}
$matches = null;
}
if (stripos($text, $SOURCESERVER . '/pix/smartpix.php/ou/s/')) {
// Search for hard-coded Emoticons and convert to local versions.
$pattern = '#<img.*?src="(' . $SOURCESERVER . '/pix/smartpix.php/ou/s/(.*?))"#';
preg_match_all($pattern, $text, $matches);
for ($a = 0, $len = count($matches[2]); $a < $len; $a++) {
// Add MyStuff images in a folder based on user name.
$match = $matches[2][$a];
$match = str_ireplace('.gif', '', $match);
$newurl = $OUTPUT->pix_url('/s/' . $match);
$text = str_replace($matches[1][$a], $newurl, $text);
}
}
return $text;
}
/**
* Switches any encode backup links to 'proper' form.
* Will search for user info and add as needed.
* @param string $content
* @param xpath $xpath
*/
function oublog_decode_perbloglinks($content, $xpath, $userposts = null, $olduserid = null) {
global $CFG, $SOURCESERVER;
$result = $content;
if (strpos($content, '$@OUBLOGVIEWUSER') !== false) {
// Process blog links.
$searchstring = '/\$@(OUBLOGVIEWUSER)\*([0-9]+)@\$/';
preg_match_all($searchstring, $result, $foundset);
if ($foundset[0]) {
// Iterate over foundset[2]. They are the old_ids.
foreach ($foundset[2] as $old_id) {
// We get the needed variables here (stored in users in xml)
$users = $xpath->query("/DATA/USERS/USER[@id=$old_id]");
$newid = false;
if ($users->length != 0) {
$user = childnodes_to_object($users->item(0));
if (!empty($user->username)) {
$newid = urlencode($user->username);
}
}
// Update the link to its new location.
if (!empty($newid)) {
// Now replace it.
$result = str_replace("$@OUBLOGVIEWUSER*$old_id@$", $CFG->wwwroot.'/mod/oublog/view.php?u=' . $newid, $result);
} else {
// Can't find, leave it as original.
$result = str_replace("$@OUBLOGVIEWUSER*$old_id@$", $SOURCESERVER . '/mod/oublog/view.php?user=' . $old_id, $result);
}
}
}
}
if (strpos($content, '$@OUBLOGVIEWPOST') !== false) {
// Porcess post links.
$searchstring = '/\$@(OUBLOGVIEWPOST)\*([0-9]+)@\$/';
preg_match_all($searchstring, $result, $foundset);
if ($foundset[0]) {
// Iterate over foundset[2]. They are the old_ids.
foreach ($foundset[2] as $old_id) {
$newusername = false;
$newposttime = false;
//See if this user made the post refered to, if so use their details.
if ($userposts && $olduserid) {
foreach ($userposts as $post) {
if (isset($post->id) && $post->id == $old_id && !empty($post->timeposted)) {
$newposttime = $post->timeposted;
// We get the needed variables here (stored in users in xml).
$users = $xpath->query("/DATA/USERS/USER[@id={$olduserid}]");
if ($users->length != 0) {
$user = childnodes_to_object($users->item(0));
if (!empty($user->username)) {
$newusername = urlencode($user->username);
}
}
break;;
}
}
}
// Update the link to its new location.
if (!empty($newusername) && !empty($newposttime)) {
// Now replace it.
$posturl = new moodle_url('/mod/oublog/viewpost.php', array('u' => $newusername, 'time' => $newposttime, 'post' => 0));
$result = str_replace("$@OUBLOGVIEWPOST*$old_id@$", $posturl->out(false), $result);
} else {
// Can't find, leave it as original.
$result = str_replace("$@OUBLOGVIEWPOST*$old_id@$", $SOURCESERVER . '/mod/oublog/viewpost.php?post=' . $old_id, $result);
}
}
}
}
return $result;
}
// Used by link usort.
function oublog_sort_links($a, $b) {
if ($a->sortorder == $b->sortorder) {
return 0;
}
return ($a->sortorder < $b->sortorder) ? -1 : 1;
}
function childnodes_to_object(DOMElement $node) {
$node = clone $node;// Must clone or else original will have nodes removed.
// Add all child elements as properties.
$info = new stdClass();
// Fast looping of node list by always accessing first tag (faster than foreach).
$el = $node->childNodes->item(0);
while ($el != null) {
$elname = strtolower($el->nodeName);
if ($el->hasChildNodes() && !($el->childNodes->length == 1 && $el->childNodes->item(0)->nodeType == XML_TEXT_NODE)) {
$info->{$elname} = childnodes_to_object($el);
} else if ($el->nodeType != XML_TEXT_NODE) {
if ($el->nodeValue == '$@NULL@$') {
$info->{$elname} = null;
} else {
$info->{$elname} = $el->nodeValue;
}
}
$el->parentNode->removeChild($el);
$el = $node->childNodes->item(0);
}
return $info;
}
function formatbytes($bytes, $precision = 2) {
$units = array('B', 'KB', 'MB', 'GB', 'TB');
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));