@@ -131,6 +131,13 @@ class CommandImportWordpress(Command, ImportMixin):
131
131
'type' : bool ,
132
132
'help' : "Export categories as categories, instead of treating them as tags" ,
133
133
},
134
+ {
135
+ 'name' : 'export_comments' ,
136
+ 'long' : 'export-comments' ,
137
+ 'default' : False ,
138
+ 'type' : bool ,
139
+ 'help' : "Export comments as .wpcomment files" ,
140
+ },
134
141
]
135
142
all_tags = set ([])
136
143
@@ -160,6 +167,7 @@ def _read_options(self, options, args):
160
167
self .import_empty_items = options .get ('include_empty_items' , False )
161
168
162
169
self .export_categories_as_categories = options .get ('export_categories_as_categories' , False )
170
+ self .read_comments = options .get ('read_comments' , False )
163
171
164
172
self .auth = None
165
173
if options .get ('download_auth' ) is not None :
@@ -478,6 +486,59 @@ def transform_content(self, content, post_format):
478
486
else :
479
487
return None
480
488
489
+ def _extract_comment (self , comment , wordpress_namespace ):
490
+ id = int (get_text_tag (comment , "{{{0}}}comment_id" .format (wordpress_namespace ), None ))
491
+ author = get_text_tag (comment , "{{{0}}}comment_author" .format (wordpress_namespace ), None )
492
+ author_email = get_text_tag (comment , "{{{0}}}comment_author_email" .format (wordpress_namespace ), None )
493
+ author_url = get_text_tag (comment , "{{{0}}}comment_author_url" .format (wordpress_namespace ), None )
494
+ author_IP = get_text_tag (comment , "{{{0}}}comment_author_IP" .format (wordpress_namespace ), None )
495
+ # date = get_text_tag(comment, "{{{0}}}comment_date".format(wordpress_namespace), None)
496
+ date_gmt = get_text_tag (comment , "{{{0}}}comment_date_gmt" .format (wordpress_namespace ), None )
497
+ content = get_text_tag (comment , "{{{0}}}comment_content" .format (wordpress_namespace ), None )
498
+ approved = get_text_tag (comment , "{{{0}}}comment_approved" .format (wordpress_namespace ), '0' )
499
+ if approved == '0' :
500
+ approved = 'hold'
501
+ elif approved == '1' :
502
+ approved = 'approved'
503
+ elif approved == 'spam' or approved == 'trash' :
504
+ pass
505
+ else :
506
+ LOGGER .warn ("Unknown comment approved status: " + str (approved ))
507
+ parent = int (get_text_tag (comment , "{{{0}}}comment_parent" .format (wordpress_namespace ), 0 ))
508
+ if parent == 0 :
509
+ parent = None
510
+ user_id = int (get_text_tag (comment , "{{{0}}}comment_user_id" .format (wordpress_namespace ), 0 ))
511
+ if user_id == 0 :
512
+ user_id = None
513
+
514
+ if approved == 'trash' or approved == 'spam' :
515
+ return None
516
+
517
+ return {"id" : id , "status" : str (approved ), "approved" : approved == "approved" ,
518
+ "author" : author , "email" : author_email , "url" : author_url , "ip" : author_IP ,
519
+ "date" : date_gmt , "content" : content , "parent" : parent , "user_id" : user_id }
520
+
521
+ def _write_comment (self , filename , comment ):
522
+ def write_header_line (fd , header_field , header_content ):
523
+ if header_content is None :
524
+ return
525
+ header_content = str (header_content ).replace ('\n ' , ' ' )
526
+ line = '.. ' + header_field + ': ' + header_content + '\n '
527
+ fd .write (line .encode ('utf8' ))
528
+
529
+ with open (filename , "wb+" ) as fd :
530
+ write_header_line (fd , "id" , comment ["id" ])
531
+ write_header_line (fd , "status" , comment ["status" ])
532
+ write_header_line (fd , "approved" , comment ["approved" ])
533
+ write_header_line (fd , "author" , comment ["author" ])
534
+ write_header_line (fd , "author_email" , comment ["email" ])
535
+ write_header_line (fd , "author_url" , comment ["url" ])
536
+ write_header_line (fd , "author_IP" , comment ["ip" ])
537
+ write_header_line (fd , "date_utc" , comment ["date" ])
538
+ write_header_line (fd , "parent_id" , comment ["parent" ])
539
+ write_header_line (fd , "wordpress_user_id" , comment ["user_id" ])
540
+ fd .write (('\n ' + comment ['content' ]).encode ('utf8' ))
541
+
481
542
def _create_metadata (self , status , excerpt , tags , categories , post_name = None ):
482
543
other_meta = {'wp-status' : status }
483
544
if excerpt is not None :
@@ -648,6 +709,18 @@ def import_item(self, item, wordpress_namespace, out_folder=None):
648
709
os .path .join (self .output_folder ,
649
710
out_folder , out_content_filename ),
650
711
content )
712
+
713
+ if self .read_comments :
714
+ comments = []
715
+ for tag in item .findall ('{{{0}}}comment' .format (wordpress_namespace )):
716
+ comment = self ._extract_comment (tag , wordpress_namespace )
717
+ if comment is not None :
718
+ comments .append (comment )
719
+
720
+ for comment in comments :
721
+ comment_filename = out_folder_slug [1 ] + "." + str (comment ['id' ]) + ".wpcomment"
722
+ self ._write_comment (os .path .join (self .output_folder , out_folder_slug [0 ], comment_filename ), comment )
723
+
651
724
return (out_folder , slug )
652
725
else :
653
726
LOGGER .warn (('Not going to import "{0}" because it seems to contain'
0 commit comments