/
import_wordpress.py
829 lines (737 loc) · 34.7 KB
/
import_wordpress.py
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
# -*- coding: utf-8 -*-
# Copyright © 2012-2015 Roberto Alsina and others.
# Permission is hereby granted, free of charge, to any
# person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the
# Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the
# Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice
# shall be included in all copies or substantial portions of
# the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from __future__ import unicode_literals, print_function
import os
import re
import sys
import datetime
import io
import json
import requests
from lxml import etree
from collections import defaultdict
try:
from urlparse import urlparse
from urllib import unquote
except ImportError:
from urllib.parse import urlparse, unquote # NOQA
try:
import phpserialize
except ImportError:
phpserialize = None # NOQA
from nikola.plugin_categories import Command
from nikola import utils
from nikola.utils import req_missing
from nikola.plugins.basic_import import ImportMixin, links
from nikola.nikola import DEFAULT_TRANSLATIONS_PATTERN
from nikola.plugins.command.init import SAMPLE_CONF, prepare_config, format_default_translations_config
LOGGER = utils.get_logger('import_wordpress', utils.STDERR_HANDLER)
class CommandImportWordpress(Command, ImportMixin):
"""Import a WordPress dump."""
name = "import_wordpress"
needs_config = False
doc_usage = "[options] wordpress_export_file"
doc_purpose = "import a WordPress dump"
cmd_options = ImportMixin.cmd_options + [
{
'name': 'exclude_drafts',
'long': 'no-drafts',
'short': 'd',
'default': False,
'type': bool,
'help': "Don't import drafts",
},
{
'name': 'exclude_privates',
'long': 'exclude-privates',
'default': False,
'type': bool,
'help': "Don't import private posts",
},
{
'name': 'include_empty_items',
'long': 'include-empty-items',
'default': False,
'type': bool,
'help': "Include empty posts and pages",
},
{
'name': 'squash_newlines',
'long': 'squash-newlines',
'default': False,
'type': bool,
'help': "Shorten multiple newlines in a row to only two newlines",
},
{
'name': 'no_downloads',
'long': 'no-downloads',
'default': False,
'type': bool,
'help': "Do not try to download files for the import",
},
{
'name': 'download_auth',
'long': 'download-auth',
'default': None,
'type': str,
'help': "Specify username and password for HTTP authentication (separated by ':')",
},
{
'name': 'separate_qtranslate_content',
'long': 'qtranslate',
'default': False,
'type': bool,
'help': "Look for translations generated by qtranslate plugin",
# WARNING: won't recover translated titles that actually
# don't seem to be part of the wordpress XML export at the
# time of writing :(
},
{
'name': 'translations_pattern',
'long': 'translations_pattern',
'default': None,
'type': str,
'help': "The pattern for translation files names",
},
{
'name': 'export_categories_as_categories',
'long': 'export-categories-as-categories',
'default': False,
'type': bool,
'help': "Export categories as categories, instead of treating them as tags",
},
{
'name': 'export_comments',
'long': 'export-comments',
'default': False,
'type': bool,
'help': "Export comments as .wpcomment files",
},
]
all_tags = set([])
def _read_options(self, options, args):
options['filename'] = args.pop(0)
if args and ('output_folder' not in args or
options['output_folder'] == 'new_site'):
options['output_folder'] = args.pop(0)
if args:
LOGGER.warn('You specified additional arguments ({0}). Please consider '
'putting these arguments before the filename if you '
'are running into problems.'.format(args))
self.import_into_existing_site = False
self.url_map = {}
self.timezone = None
self.wordpress_export_file = options['filename']
self.squash_newlines = options.get('squash_newlines', False)
self.output_folder = options.get('output_folder', 'new_site')
self.exclude_drafts = options.get('exclude_drafts', False)
self.exclude_privates = options.get('exclude_privates', False)
self.no_downloads = options.get('no_downloads', False)
self.import_empty_items = options.get('include_empty_items', False)
self.export_categories_as_categories = options.get('export_categories_as_categories', False)
self.read_comments = options.get('read_comments', False)
self.auth = None
if options.get('download_auth') is not None:
username_password = options.get('download_auth')
self.auth = tuple(username_password.split(':', 1))
if len(self.auth) < 2:
print("Please specify HTTP authentication credentials in the form username:password.")
return False
self.separate_qtranslate_content = options.get('separate_qtranslate_content')
self.translations_pattern = options.get('translations_pattern')
return True
def _prepare(self, channel):
self.context = self.populate_context(channel)
self.base_dir = urlparse(self.context['BASE_URL']).path
if self.export_categories_as_categories:
wordpress_namespace = channel.nsmap['wp']
cat_map = dict()
for cat in channel.findall('{{{0}}}category'.format(wordpress_namespace)):
# cat_id = get_text_tag(cat, '{{{0}}}term_id'.format(wordpress_namespace), None)
cat_slug = get_text_tag(cat, '{{{0}}}category_nicename'.format(wordpress_namespace), None)
cat_parent_slug = get_text_tag(cat, '{{{0}}}category_parent'.format(wordpress_namespace), None)
cat_name = get_text_tag(cat, '{{{0}}}cat_name'.format(wordpress_namespace), None)
cat_path = [cat_name]
if cat_parent_slug in cat_map:
cat_path = cat_map[cat_parent_slug] + cat_path
cat_map[cat_slug] = cat_path
self._category_paths = dict()
for cat, path in cat_map.items():
self._category_paths[cat] = utils.join_hierarchical_category_path(path)
def _adjust_config_template(self, channel, rendered_template):
rendered_template = re.sub('# REDIRECTIONS = ', 'REDIRECTIONS = ',
rendered_template)
if self.timezone:
rendered_template = re.sub('# TIMEZONE = \'UTC\'',
'TIMEZONE = \'' + self.timezone + '\'',
rendered_template)
if self.export_categories_as_categories:
rendered_template = re.sub('(# |)CATEGORY_ALLOW_HIERARCHIES = (True|False)', 'CATEGORY_ALLOW_HIERARCHIES = True',
rendered_template)
rendered_template = re.sub('(# |)CATEGORY_OUTPUT_FLAT_HIERARCHY = (True|False)', 'CATEGORY_OUTPUT_FLAT_HIERARCHY = True',
rendered_template)
return rendered_template
def _execute(self, options={}, args=[]):
"""Import a WordPress blog from an export file into a Nikola site."""
if not args:
print(self.help())
return False
if not self._read_options(options, args):
return False
# A place holder where extra language (if detected) will be stored
self.extra_languages = set()
if not self.no_downloads:
def show_info_about_mising_module(modulename):
LOGGER.error(
'To use the "{commandname}" command, you have to install '
'the "{package}" package or supply the "--no-downloads" '
'option.'.format(
commandname=self.name,
package=modulename)
)
if phpserialize is None:
req_missing(['phpserialize'], 'import WordPress dumps without --no-downloads')
channel = self.get_channel_from_file(self.wordpress_export_file)
self._prepare(channel)
conf_template = self.generate_base_site()
# If user has specified a custom pattern for translation files we
# need to fix the config
if self.translations_pattern:
self.context['TRANSLATIONS_PATTERN'] = self.translations_pattern
self.import_posts(channel)
self.context['TRANSLATIONS'] = format_default_translations_config(
self.extra_languages)
self.context['REDIRECTIONS'] = self.configure_redirections(
self.url_map)
# Add tag redirects
for tag in self.all_tags:
try:
tag_str = tag.decode('utf8')
except AttributeError:
tag_str = tag
tag = utils.slugify(tag_str)
src_url = '{}tag/{}'.format(self.context['SITE_URL'], tag)
dst_url = self.site.link('tag', tag)
if src_url != dst_url:
self.url_map[src_url] = dst_url
self.write_urlmap_csv(
os.path.join(self.output_folder, 'url_map.csv'), self.url_map)
rendered_template = conf_template.render(**prepare_config(self.context))
rendered_template = self._adjust_config_template(channel, rendered_template)
self.write_configuration(self.get_configuration_output_path(),
rendered_template)
@classmethod
def read_xml_file(cls, filename):
xml = []
with open(filename, 'rb') as fd:
for line in fd:
# These explode etree and are useless
if b'<atom:link rel=' in line:
continue
xml.append(line)
return b'\n'.join(xml)
@classmethod
def get_channel_from_file(cls, filename):
tree = etree.fromstring(cls.read_xml_file(filename))
channel = tree.find('channel')
return channel
@staticmethod
def populate_context(channel):
wordpress_namespace = channel.nsmap['wp']
context = SAMPLE_CONF.copy()
context['DEFAULT_LANG'] = get_text_tag(channel, 'language', 'en')[:2]
context['TRANSLATIONS_PATTERN'] = DEFAULT_TRANSLATIONS_PATTERN
context['BLOG_TITLE'] = get_text_tag(channel, 'title',
'PUT TITLE HERE')
context['BLOG_DESCRIPTION'] = get_text_tag(
channel, 'description', 'PUT DESCRIPTION HERE')
context['BASE_URL'] = get_text_tag(channel, 'link', '#')
if not context['BASE_URL']:
base_site_url = channel.find('{{{0}}}author'.format(wordpress_namespace))
context['BASE_URL'] = get_text_tag(base_site_url,
None,
"http://foo.com/")
if not context['BASE_URL'].endswith('/'):
context['BASE_URL'] += '/'
context['SITE_URL'] = context['BASE_URL']
author = channel.find('{{{0}}}author'.format(wordpress_namespace))
context['BLOG_EMAIL'] = get_text_tag(
author,
'{{{0}}}author_email'.format(wordpress_namespace),
"joe@example.com")
context['BLOG_AUTHOR'] = get_text_tag(
author,
'{{{0}}}author_display_name'.format(wordpress_namespace),
"Joe Example")
context['POSTS'] = '''(
("posts/*.rst", "posts", "post.tmpl"),
("posts/*.txt", "posts", "post.tmpl"),
("posts/*.md", "posts", "post.tmpl"),
)'''
context['PAGES'] = '''(
("stories/*.rst", "stories", "story.tmpl"),
("stories/*.txt", "stories", "story.tmpl"),
("stories/*.md", "stories", "story.tmpl"),
)'''
context['COMPILERS'] = '''{
"rest": ('.txt', '.rst'),
"markdown": ('.md', '.mdown', '.markdown'),
"html": ('.html', '.htm')
}
'''
return context
def download_url_content_to_file(self, url, dst_path):
if self.no_downloads:
return
try:
request = requests.get(url, auth=self.auth)
if request.status_code >= 400:
LOGGER.warn("Downloading {0} to {1} failed with HTTP status code {2}".format(url, dst_path, request.status_code))
return
with open(dst_path, 'wb+') as fd:
fd.write(request.content)
except requests.exceptions.ConnectionError as err:
LOGGER.warn("Downloading {0} to {1} failed: {2}".format(url, dst_path, err))
def import_attachment(self, item, wordpress_namespace):
url = get_text_tag(
item, '{{{0}}}attachment_url'.format(wordpress_namespace), 'foo')
link = get_text_tag(item, '{{{0}}}link'.format(wordpress_namespace),
'foo')
path = urlparse(url).path
dst_path = os.path.join(*([self.output_folder, 'files'] + list(path.split('/'))))
dst_dir = os.path.dirname(dst_path)
utils.makedirs(dst_dir)
LOGGER.info("Downloading {0} => {1}".format(url, dst_path))
self.download_url_content_to_file(url, dst_path)
dst_url = '/'.join(dst_path.split(os.sep)[2:])
links[link] = '/' + dst_url
links[url] = '/' + dst_url
return [path] + self.download_additional_image_sizes(
item,
wordpress_namespace,
os.path.dirname(url)
)
def download_additional_image_sizes(self, item, wordpress_namespace, source_path):
if phpserialize is None:
return []
additional_metadata = item.findall('{{{0}}}postmeta'.format(wordpress_namespace))
if additional_metadata is None:
return []
result = []
for element in additional_metadata:
meta_key = element.find('{{{0}}}meta_key'.format(wordpress_namespace))
if meta_key is not None and meta_key.text == '_wp_attachment_metadata':
meta_value = element.find('{{{0}}}meta_value'.format(wordpress_namespace))
if meta_value is None:
continue
# Someone from Wordpress thought it was a good idea
# serialize PHP objects into that metadata field. Given
# that the export should give you the power to insert
# your blogging into another site or system its not.
# Why don't they just use JSON?
if sys.version_info[0] == 2:
try:
metadata = phpserialize.loads(utils.sys_encode(meta_value.text))
except ValueError:
# local encoding might be wrong sometimes
metadata = phpserialize.loads(meta_value.text.encode('utf-8'))
else:
metadata = phpserialize.loads(meta_value.text.encode('utf-8'))
size_key = b'sizes'
file_key = b'file'
if size_key not in metadata:
continue
for filename in [metadata[size_key][size][file_key] for size in metadata[size_key]]:
url = '/'.join([source_path, filename.decode('utf-8')])
path = urlparse(url).path
dst_path = os.path.join(*([self.output_folder, 'files'] + list(path.split('/'))))
dst_dir = os.path.dirname(dst_path)
utils.makedirs(dst_dir)
LOGGER.info("Downloading {0} => {1}".format(url, dst_path))
self.download_url_content_to_file(url, dst_path)
dst_url = '/'.join(dst_path.split(os.sep)[2:])
links[url] = '/' + dst_url
result.append(path)
return result
code_re1 = re.compile(r'\[code.* lang.*?="(.*?)?".*\](.*?)\[/code\]', re.DOTALL | re.MULTILINE)
code_re2 = re.compile(r'\[sourcecode.* lang.*?="(.*?)?".*\](.*?)\[/sourcecode\]', re.DOTALL | re.MULTILINE)
code_re3 = re.compile(r'\[code.*?\](.*?)\[/code\]', re.DOTALL | re.MULTILINE)
code_re4 = re.compile(r'\[sourcecode.*?\](.*?)\[/sourcecode\]', re.DOTALL | re.MULTILINE)
def transform_code(self, content):
# http://en.support.wordpress.com/code/posting-source-code/. There are
# a ton of things not supported here. We only do a basic [code
# lang="x"] -> ```x translation, and remove quoted html entities (<,
# >, &, and ").
def replacement(m, c=content):
if len(m.groups()) == 1:
language = ''
code = m.group(0)
else:
language = m.group(1) or ''
code = m.group(2)
code = code.replace('&', '&')
code = code.replace('>', '>')
code = code.replace('<', '<')
code = code.replace('"', '"')
return '```{language}\n{code}\n```'.format(language=language, code=code)
content = self.code_re1.sub(replacement, content)
content = self.code_re2.sub(replacement, content)
content = self.code_re3.sub(replacement, content)
content = self.code_re4.sub(replacement, content)
return content
@staticmethod
def transform_caption(content):
new_caption = re.sub(r'\[/caption\]', '', content)
new_caption = re.sub(r'\[caption.*\]', '', new_caption)
return new_caption
def transform_multiple_newlines(self, content):
"""Replaces multiple newlines with only two."""
if self.squash_newlines:
return re.sub(r'\n{3,}', r'\n\n', content)
else:
return content
def transform_content(self, content, post_format):
if post_format == 'wp':
content = self.transform_code(content)
content = self.transform_caption(content)
content = self.transform_multiple_newlines(content)
return content, 'md'
elif post_format == 'markdown':
return content, 'md'
elif post_format == 'none':
return content, 'html'
else:
return None
def _extract_comment(self, comment, wordpress_namespace):
id = int(get_text_tag(comment, "{{{0}}}comment_id".format(wordpress_namespace), None))
author = get_text_tag(comment, "{{{0}}}comment_author".format(wordpress_namespace), None)
author_email = get_text_tag(comment, "{{{0}}}comment_author_email".format(wordpress_namespace), None)
author_url = get_text_tag(comment, "{{{0}}}comment_author_url".format(wordpress_namespace), None)
author_IP = get_text_tag(comment, "{{{0}}}comment_author_IP".format(wordpress_namespace), None)
# date = get_text_tag(comment, "{{{0}}}comment_date".format(wordpress_namespace), None)
date_gmt = get_text_tag(comment, "{{{0}}}comment_date_gmt".format(wordpress_namespace), None)
content = get_text_tag(comment, "{{{0}}}comment_content".format(wordpress_namespace), None)
approved = get_text_tag(comment, "{{{0}}}comment_approved".format(wordpress_namespace), '0')
if approved == '0':
approved = 'hold'
elif approved == '1':
approved = 'approved'
elif approved == 'spam' or approved == 'trash':
pass
else:
LOGGER.warn("Unknown comment approved status: " + str(approved))
parent = int(get_text_tag(comment, "{{{0}}}comment_parent".format(wordpress_namespace), 0))
if parent == 0:
parent = None
user_id = int(get_text_tag(comment, "{{{0}}}comment_user_id".format(wordpress_namespace), 0))
if user_id == 0:
user_id = None
if approved == 'trash' or approved == 'spam':
return None
return {"id": id, "status": str(approved), "approved": approved == "approved",
"author": author, "email": author_email, "url": author_url, "ip": author_IP,
"date": date_gmt, "content": content, "parent": parent, "user_id": user_id}
def _write_comment(self, filename, comment):
def write_header_line(fd, header_field, header_content):
if header_content is None:
return
header_content = str(header_content).replace('\n', ' ')
line = '.. ' + header_field + ': ' + header_content + '\n'
fd.write(line.encode('utf8'))
with open(filename, "wb+") as fd:
write_header_line(fd, "id", comment["id"])
write_header_line(fd, "status", comment["status"])
write_header_line(fd, "approved", comment["approved"])
write_header_line(fd, "author", comment["author"])
write_header_line(fd, "author_email", comment["email"])
write_header_line(fd, "author_url", comment["url"])
write_header_line(fd, "author_IP", comment["ip"])
write_header_line(fd, "date_utc", comment["date"])
write_header_line(fd, "parent_id", comment["parent"])
write_header_line(fd, "wordpress_user_id", comment["user_id"])
fd.write(('\n' + comment['content']).encode('utf8'))
def _create_metadata(self, status, excerpt, tags, categories, post_name=None):
other_meta = {'wp-status': status}
if excerpt is not None:
other_meta['excerpt'] = excerpt
if self.export_categories_as_categories:
cats = []
for text in categories:
if text in self._category_paths:
cats.append(self._category_paths[text])
else:
cats.append(utils.join_hierarchical_category_path([text]))
other_meta['categories'] = ','.join(cats)
if len(cats) > 0:
other_meta['category'] = cats[0]
if len(cats) > 1:
LOGGER.warn(('Post "{0}" has more than one category! ' +
'Will only use the first one.').format(post_name))
tags_cats = tags
else:
tags_cats = tags + categories
return tags_cats, other_meta
def import_item(self, item, wordpress_namespace, out_folder=None):
"""Takes an item from the feed and creates a post file."""
if out_folder is None:
out_folder = 'posts'
title = get_text_tag(item, 'title', 'NO TITLE')
# link is something like http://foo.com/2012/09/01/hello-world/
# So, take the path, utils.slugify it, and that's our slug
link = get_text_tag(item, 'link', None)
parsed = urlparse(link)
path = unquote(parsed.path.strip('/'))
try:
path = path.decode('utf8')
except AttributeError:
pass
# Cut out the base directory.
if path.startswith(self.base_dir.strip('/')):
path = path.replace(self.base_dir.strip('/'), '', 1)
pathlist = path.split('/')
if parsed.query: # if there are no nice URLs and query strings are used
out_folder = os.path.join(*([out_folder] + pathlist))
slug = get_text_tag(
item, '{{{0}}}post_name'.format(wordpress_namespace), None)
if not slug: # it *may* happen
slug = get_text_tag(
item, '{{{0}}}post_id'.format(wordpress_namespace), None)
if not slug: # should never happen
LOGGER.error("Error converting post:", title)
return False
else:
if len(pathlist) > 1:
out_folder = os.path.join(*([out_folder] + pathlist[:-1]))
slug = utils.slugify(pathlist[-1])
description = get_text_tag(item, 'description', '')
post_date = get_text_tag(
item, '{{{0}}}post_date'.format(wordpress_namespace), None)
try:
dt = utils.to_datetime(post_date)
except ValueError:
dt = datetime.datetime(1970, 1, 1, 0, 0, 0)
LOGGER.error('Malformed date "{0}" in "{1}" [{2}], assuming 1970-01-01 00:00:00 instead.'.format(post_date, title, slug))
post_date = dt.strftime('%Y-%m-%d %H:%M:%S')
if dt.tzinfo and self.timezone is None:
self.timezone = utils.get_tzname(dt)
status = get_text_tag(
item, '{{{0}}}status'.format(wordpress_namespace), 'publish')
content = get_text_tag(
item, '{http://purl.org/rss/1.0/modules/content/}encoded', '')
excerpt = get_text_tag(
item, '{http://wordpress.org/export/1.2/excerpt/}encoded', None)
if excerpt is not None:
if len(excerpt) == 0:
excerpt = None
tags = []
categories = []
if status == 'trash':
LOGGER.warn('Trashed post "{0}" will not be imported.'.format(title))
return False
elif status == 'private':
tags.append('private')
is_draft = False
is_private = True
elif status != 'publish':
tags.append('draft')
is_draft = True
is_private = False
else:
is_draft = False
is_private = False
for tag in item.findall('category'):
text = tag.text
type = 'category'
if 'domain' in tag.attrib:
type = tag.attrib['domain']
if text == 'Uncategorized' and type == 'category':
continue
self.all_tags.add(text)
if type == 'category':
categories.append(type)
else:
tags.append(text)
if '$latex' in content:
tags.append('mathjax')
# Find post format if it's there
post_format = 'wp'
format_tag = [x for x in item.findall('*//{%s}meta_key' % wordpress_namespace) if x.text == '_tc_post_format']
if format_tag:
post_format = format_tag[0].getparent().find('{%s}meta_value' % wordpress_namespace).text
if post_format == 'wpautop':
post_format = 'wp'
if is_draft and self.exclude_drafts:
LOGGER.notice('Draft "{0}" will not be imported.'.format(title))
return False
elif is_private and self.exclude_privates:
LOGGER.notice('Private post "{0}" will not be imported.'.format(title))
return False
elif content.strip() or self.import_empty_items:
# If no content is found, no files are written.
self.url_map[link] = (self.context['SITE_URL'] +
out_folder.rstrip('/') + '/' + slug +
'.html').replace(os.sep, '/')
if hasattr(self, "separate_qtranslate_content") \
and self.separate_qtranslate_content:
content_translations = separate_qtranslate_content(content)
else:
content_translations = {"": content}
default_language = self.context["DEFAULT_LANG"]
for lang, content in content_translations.items():
try:
content, extension = self.transform_content(content, post_format)
except:
LOGGER.error(('Cannot interpret post "{0}" (language {1}) with post ' +
'format {2}!').format(os.path.join(out_folder, slug), lang, post_format))
return False
if lang:
out_meta_filename = slug + '.meta'
if lang == default_language:
out_content_filename = slug + '.' + extension
else:
out_content_filename \
= utils.get_translation_candidate(self.context,
slug + "." + extension, lang)
self.extra_languages.add(lang)
meta_slug = slug
else:
out_meta_filename = slug + '.meta'
out_content_filename = slug + '.' + extension
meta_slug = slug
tags, other_meta = self._create_metadata(status, excerpt, tags, categories,
post=os.path.join(out_folder, slug))
self.write_metadata(os.path.join(self.output_folder, out_folder,
out_meta_filename),
title, meta_slug, post_date, description, tags, **other_meta)
self.write_content(
os.path.join(self.output_folder,
out_folder, out_content_filename),
content)
if self.read_comments:
comments = []
for tag in item.findall('{{{0}}}comment'.format(wordpress_namespace)):
comment = self._extract_comment(tag, wordpress_namespace)
if comment is not None:
comments.append(comment)
for comment in comments:
comment_filename = out_folder_slug[1] + "." + str(comment['id']) + ".wpcomment"
self._write_comment(os.path.join(self.output_folder, out_folder_slug[0], comment_filename), comment)
return (out_folder, slug)
else:
LOGGER.warn(('Not going to import "{0}" because it seems to contain'
' no content.').format(title))
return False
def process_item(self, item):
# The namespace usually is something like:
# http://wordpress.org/export/1.2/
wordpress_namespace = item.nsmap['wp']
post_type = get_text_tag(
item, '{{{0}}}post_type'.format(wordpress_namespace), 'post')
post_id = int(get_text_tag(
item, '{{{0}}}post_id'.format(wordpress_namespace), "0"))
parent_id = get_text_tag(
item, '{{{0}}}post_parent'.format(wordpress_namespace), None)
if post_type == 'attachment':
files = self.import_attachment(item, wordpress_namespace)
# If parent was found, store relation with imported files
if parent_id is not None:
self.attachments[int(parent_id)][post_id] = files
else:
LOGGER.warn("Attachment #{0} ({1}) has no parent!".format(post_id, files))
else:
if post_type == 'post':
out_folder_slug = self.import_item(item, wordpress_namespace, 'posts')
else:
post_type = 'page'
out_folder_slug = self.import_item(item, wordpress_namespace, 'stories')
# If post was exported, store data
if out_folder_slug:
self.posts_pages[post_id] = (post_type, out_folder_slug[0], out_folder_slug[1])
def write_attachments_info(self, path, attachments):
with io.open(path, "wb") as file:
file.write(json.dumps(attachments).encode('utf-8'))
def import_posts(self, channel):
self.posts_pages = {}
self.attachments = defaultdict(dict)
for item in channel.findall('item'):
self.process_item(item)
# Assign attachments to posts
for post_id in self.attachments:
if post_id in self.posts_pages:
destination = os.path.join(self.output_folder, self.posts_pages[post_id][1],
self.posts_pages[post_id][2] + ".attachments.json")
self.write_attachments_info(destination, self.attachments[post_id])
else:
LOGGER.warn(("Found attachments for post or page #{0}, but didn't find post or page. " +
"(Attachments: {1})").format(post_id, [e[0] for _, e in self.attachments[post_id].items()]))
def get_text_tag(tag, name, default):
if tag is None:
return default
t = tag.find(name)
if t is not None and t.text is not None:
return t.text
else:
return default
def separate_qtranslate_content(text):
"""Parse the content of a wordpress post or page and separate
the various language specific contents when they are delimited
with qtranslate tags: <!--:LL-->blabla<!--:-->"""
# TODO: uniformize qtranslate tags <!--/en--> => <!--:-->
qt_start = "<!--:"
qt_end = "-->"
qt_end_with_lang_len = 5
qt_chunks = text.split(qt_start)
content_by_lang = {}
common_txt_list = []
for c in qt_chunks:
if not c.strip():
continue
if c.startswith(qt_end):
# just after the end of a language specific section, there may
# be some piece of common text or tags, or just nothing
lang = "" # default language
c = c.lstrip(qt_end)
if not c:
continue
elif c[2:].startswith(qt_end):
# a language specific section (with language code at the begining)
lang = c[:2]
c = c[qt_end_with_lang_len:]
else:
# nowhere specific (maybe there is no language section in the
# currently parsed content)
lang = "" # default language
if not lang:
common_txt_list.append(c)
for l in content_by_lang.keys():
content_by_lang[l].append(c)
else:
content_by_lang[lang] = content_by_lang.get(lang, common_txt_list) + [c]
# in case there was no language specific section, just add the text
if common_txt_list and not content_by_lang:
content_by_lang[""] = common_txt_list
# Format back the list to simple text
for l in content_by_lang.keys():
content_by_lang[l] = " ".join(content_by_lang[l])
return content_by_lang