diff --git a/AUTHORS.txt b/AUTHORS.txt index 717e9d91fd..79213908b5 100644 --- a/AUTHORS.txt +++ b/AUTHORS.txt @@ -10,6 +10,7 @@ * `Aru Sahni `_ * `Arun Persaud `_ * `Aurelien Naldi `_ +* `Ayudh Haldar `_ * `Ben Mather `_ * `Bendik Knapstad `_ * `Boris Kaul `_ diff --git a/CHANGES.txt b/CHANGES.txt index f5a468f339..6d28998c96 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -39,6 +39,7 @@ Features Bugfixes -------- +* Ignore “`sizes` ⇒ false” in WordPress attachment metadata to prevent a crash in ``import_wordpress`` (Issue #3810) * Ignore errors in parsing SVG files for shrinking them, copy original file to output instead (Issue #3785) * Restore ``annotation_helper.tmpl`` with dummy content - fix themes still mentioning it @@ -47,6 +48,7 @@ Bugfixes * ``nikola serve`` now works with non-root SITE_URL. * Stack traces meaningless for end users now more reliably suppressed (Issue #3838). + Other ----- diff --git a/nikola/plugins/command/import_wordpress.py b/nikola/plugins/command/import_wordpress.py index 9a8b130bd3..685163fb1a 100644 --- a/nikola/plugins/command/import_wordpress.py +++ b/nikola/plugins/command/import_wordpress.py @@ -626,21 +626,47 @@ def add(our_key, wp_key, is_int=False, ignore_zero=False, is_float=False): if len(dst_meta) > 0: files_meta[0]['meta'] = dst_meta + # ------------------------------------------------------------------ + # Previous implementation kept only for reference: # Find other sizes of image - if size_key not in metadata: - continue + # if size_key not in metadata: + # continue - for size in metadata[size_key]: - filename = metadata[size_key][size][file_key] - url = '/'.join([source_path, filename.decode('utf-8')]) + # for size in metadata[size_key]: + # filename = metadata[size_key][size][file_key] + # url = '/'.join([source_path, filename.decode('utf-8')]) - # Construct metadata - meta = {} - meta['size'] = size.decode('utf-8') - if width_key in metadata[size_key][size] and height_key in metadata[size_key][size]: - meta['width'] = int(metadata[size_key][size][width_key]) - meta['height'] = int(metadata[size_key][size][height_key]) + # # Construct metadata + # meta = {} + # meta['size'] = size.decode('utf-8') + # if width_key in metadata[size_key][size] and height_key in metadata[size_key][size]: + # meta['width'] = int(metadata[size_key][size][width_key]) + # meta['height'] = int(metadata[size_key][size][height_key]) + # ------------------------------------------------------------------ + + # Find other sizes of image — *robust to `"sizes" => false`* + sizes_dict = metadata.get(size_key) + + # WP ≥ 6 writes `"sizes" => false` when no thumbnails exist. + # That becomes Python `False`, which is not iterable/subscriptable + # and caused TypeError: 'bool' object is not subscriptable (#3810). + if not isinstance(sizes_dict, dict): + continue # nothing more to import for this element + for size, size_info in sizes_dict.items(): + if not isinstance(size_info, dict) or file_key not in size_info: + continue # skip malformed entries + + filename = size_info[file_key] + url = "/".join([source_path, filename.decode("utf-8")]) + + # Construct metadata + meta = {"size": size.decode("utf-8")} + if width_key in size_info and height_key in size_info: + meta["width"] = int(size_info[width_key]) + meta["height"] = int(size_info[height_key]) + # ------------------------------------------------------------------ + path = urlparse(url).path dst_path = os.path.join(*([self.output_folder, 'files'] + list(path.split('/')))) if self.no_downloads: