From 037709c999393b43b3c2b1723cc662654d1af5de Mon Sep 17 00:00:00 2001 From: Ayudh-M Date: Sat, 19 Apr 2025 12:51:57 +0200 Subject: [PATCH 1/4] import_wordpress: ignore 'sizes' => false in _wp_attachment_metadata (fix #3810) --- nikola/plugins/command/import_wordpress.py | 46 +++++++++++++++++----- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/nikola/plugins/command/import_wordpress.py b/nikola/plugins/command/import_wordpress.py index 9a8b130bd3..9cef37106d 100644 --- a/nikola/plugins/command/import_wordpress.py +++ b/nikola/plugins/command/import_wordpress.py @@ -626,20 +626,46 @@ 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')]) + + # # 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 - for size in metadata[size_key]: - filename = metadata[size_key][size][file_key] - url = '/'.join([source_path, filename.decode('utf-8')]) + filename = size_info[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]) + 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('/')))) From 3880ca9f5fc8a8f9650930ad41f1317213962bef Mon Sep 17 00:00:00 2001 From: Ayudh-M Date: Sat, 19 Apr 2025 13:07:47 +0200 Subject: [PATCH 2/4] =?UTF-8?q?docs:=20add=20entry=20for=20import=5Fwordpr?= =?UTF-8?q?ess=20sizes=20fix=20(Issue=C2=A0#3810)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AUTHORS.txt | 1 + CHANGES.txt | 1 + 2 files changed, 2 insertions(+) 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 f369a17f0c..aa54112f61 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -22,6 +22,7 @@ Bugfixes * Fix compatibility with watchdog 4 (Issue #3766) * ``nikola serve`` now works with non-root SITE_URL. * Stack traces meaningless for end users now more reliably suppressed (Issue #3838). +* Ignore “`sizes` ⇒ false” in WordPress attachment metadata to prevent a crash in ``import_wordpress`` (Issue #3810) Other ----- From 73b2563da6a7b47d853277b05a2cb6ebc99ad600 Mon Sep 17 00:00:00 2001 From: Ayudh-M Date: Sat, 19 Apr 2025 13:18:44 +0200 Subject: [PATCH 3/4] =?UTF-8?q?docs:=20add=20entry=20for=20import=5Fwordpr?= =?UTF-8?q?ess=20sizes=20fix=20(Issue=C2=A0#3810)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGES.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGES.txt b/CHANGES.txt index aa54112f61..d270fc3e31 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -15,6 +15,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 @@ -22,7 +23,7 @@ Bugfixes * Fix compatibility with watchdog 4 (Issue #3766) * ``nikola serve`` now works with non-root SITE_URL. * Stack traces meaningless for end users now more reliably suppressed (Issue #3838). -* Ignore “`sizes` ⇒ false” in WordPress attachment metadata to prevent a crash in ``import_wordpress`` (Issue #3810) + Other ----- From 04c29202bab8910392f5432614b5899948032952 Mon Sep 17 00:00:00 2001 From: Ayudh-M Date: Sat, 19 Apr 2025 13:22:53 +0200 Subject: [PATCH 4/4] =?UTF-8?q?docs:=20add=20entry=20for=20import=5Fwordpr?= =?UTF-8?q?ess=20sizes=20fix=20(Issue=C2=A0#3810)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nikola/plugins/command/import_wordpress.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nikola/plugins/command/import_wordpress.py b/nikola/plugins/command/import_wordpress.py index 9cef37106d..685163fb1a 100644 --- a/nikola/plugins/command/import_wordpress.py +++ b/nikola/plugins/command/import_wordpress.py @@ -665,8 +665,8 @@ def add(our_key, wp_key, is_int=False, ignore_zero=False, is_float=False): 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: