From 54132b772a806250435da081b209ef8a2e8ac39d Mon Sep 17 00:00:00 2001 From: rafa Date: Mon, 25 May 2026 14:57:40 -0400 Subject: [PATCH] =?UTF-8?q?script(avatars):=20import=20bulk=20col=5F*.png?= =?UTF-8?q?=20de=20Joomla=20=E2=86=92=20user=5Fmeta=20foto=5Fperfil?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Usado para resolver issue #39 — 736 avatares de autor importados desde joomla-php83/images/quienes_somos/col_*.png. Crea attachments WP con wp_insert_attachment + wp_generate_attachment_metadata y asigna foto_perfil a cada user_id según matching exacto (login==slug) o strong (login es token). Preserva foto_perfil ya asignado. TSV de entrada esperado en /tmp/avatar_assignments.tsv con columnas: aid, login, posts, source, filename. Ficheros pre-copiados a uploads/autores/joomla/. Co-Authored-By: Claude Opus 4.7 --- scripts/import_avatars.php | 82 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 scripts/import_avatars.php diff --git a/scripts/import_avatars.php b/scripts/import_avatars.php new file mode 100644 index 0000000..62e9f6e --- /dev/null +++ b/scripts/import_avatars.php @@ -0,0 +1,82 @@ + 0, 'reused' => 0, 'missing_file' => 0, 'missing_user' => 0, 'assigned' => 0, 'already' => 0]; +$samples_ok = []; + +global $wpdb; +$attach_by_file = []; + +foreach ($lines as $line) { + [$aid, $login, $posts, $source, $filename] = explode("\t", $line); + $aid = (int) $aid; + $abs = $avatars_dir . '/' . $filename; + if (!is_file($abs)) { $stats['missing_file']++; continue; } + if (!get_userdata($aid)) { $stats['missing_user']++; continue; } + + // Si ya tiene foto_perfil, no tocar (preservar manual assignments) + if (get_user_meta($aid, 'foto_perfil', true)) { $stats['already']++; continue; } + + // ¿Ya existe attachment para este fichero? (reutilizar) + if (!isset($attach_by_file[$filename])) { + $url = $avatars_url . '/' . $filename; + $existing = $wpdb->get_var($wpdb->prepare( + "SELECT ID FROM {$wpdb->posts} WHERE post_type='attachment' AND guid=%s LIMIT 1", + $url + )); + if ($existing) { + $attach_by_file[$filename] = (int) $existing; + $stats['reused']++; + } else { + if ($dry) { + $attach_by_file[$filename] = -1; // marcador dry + } else { + $attach_id = wp_insert_attachment([ + 'guid' => $url, + 'post_mime_type' => wp_check_filetype($abs)['type'] ?: 'image/png', + 'post_title' => pathinfo($filename, PATHINFO_FILENAME), + 'post_content' => '', + 'post_status' => 'inherit', + ], $abs); + if (is_wp_error($attach_id) || !$attach_id) { + error_log("[import_avatars] insert FAIL para $filename: " . (is_wp_error($attach_id) ? $attach_id->get_error_message() : 'unknown')); + continue; + } + $meta = wp_generate_attachment_metadata($attach_id, $abs); + wp_update_attachment_metadata($attach_id, $meta); + $attach_by_file[$filename] = $attach_id; + $stats['ok']++; + } + } + } + + $attach_id = $attach_by_file[$filename]; + if ($attach_id !== 0) { + if (!$dry && $attach_id > 0) update_user_meta($aid, 'foto_perfil', (string) $attach_id); + $stats['assigned']++; + if (count($samples_ok) < 5) { + $samples_ok[] = "user $aid ($login) → attach " . ($attach_id > 0 ? $attach_id : 'NEW') . " ($filename)"; + } + } +} + +echo ($dry ? '[DRY] ' : '') . "Stats:\n"; +foreach ($stats as $k => $v) echo " $k: $v\n"; +echo "\nSamples:\n"; +foreach ($samples_ok as $s) echo " $s\n";