Created
March 16, 2020 08:36
-
-
Save egore/d936a1aee83eca2b8b9480f809354eb3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare (strict_types = 1); | |
/** | |
* Create SQL scripts to add images to the Wordpress media library. Note that this does not create thumbnails (etc.) | |
* Run using 'php rebuild.php' in a copy of your wordpress folder | |
*/ | |
const BASE_URI = 'http://YOUR_IP_OR_HOSTNAME_GOES_HERE/'; | |
function clearname(string $name) { | |
$name = strtolower($name); | |
$name = str_replace('@', '', $name); | |
return $name; | |
} | |
function startsWith(string $haystack, string $needle) { | |
$length = strlen($needle); | |
return (substr($haystack, 0, $length) === $needle); | |
} | |
function cutExtesion(string $basename) { | |
$pos = strrpos($basename, '.'); | |
if ($pos === FALSE) { | |
return $basename; | |
} | |
return substr($basename, 0, $pos); | |
} | |
function render(string $date, string $basename, string $dir, string $mimetype) { | |
echo 'INSERT INTO wp_posts(post_author, post_date, post_date_gmt, post_content, post_title, post_excerpt, post_status, comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified, post_modified_gmt, post_content_filtered, post_parent, guid, menu_order, post_type, post_mime_type, comment_count) | |
SELECT 2, \''.$date.'\', \''.$date.'\', \'\', \''.cutExtesion($basename).'\', \'\', \'inherit\', \'open\', \'closed\', \'\', \'\', \''.clearname($basename).'\', \'\', \''.$date.'\', \''.$date.'\', \'\', 0, \''.BASE_URI.'wp-content/uploads/'.$dir.'/'.$basename.'\', 0, \'attachment\', \''.$mimetype.'\', 0 | |
FROM dual | |
WHERE NOT EXISTS (SELECT * FROM wp_posts WHERE guid = \''.BASE_URI.'wp-content/uploads/'.$dir.'/'.$basename.'\'); | |
'; | |
echo 'INSERT INTO wp_postmeta (post_id, meta_key, meta_value) | |
SELECT id, \'_wp_attached_file\', \''.$dir.'/'.$basename.'\' FROM wp_posts WHERE guid = \''.BASE_URI.'wp-content/uploads/'.$dir.'/'.$basename.'\' AND NOT EXISTS (SELECT * FROM wp_postmeta WHERE meta_key = \'_wp_attached_file\' AND meta_value = \''.$dir.'/'.$basename.'\'); | |
'; | |
echo 'INSERT INTO wp_icl_translations (element_id, element_type, language_code, trid) | |
SELECT id, \'post_attachment\', \'de\', (SELECT max(trid) + 1 FROM wp_icl_translations) FROM wp_posts WHERE guid = \''.BASE_URI.'wp-content/uploads/'.$dir.'/'.$basename.'\' AND NOT EXISTS (SELECT * FROM wp_icl_translations WHERE element_id = (SELECT id FROM wp_posts WHERE guid = \'http://10.0.2.120/wp-content/uploads/'.$dir.'/'.$basename.'\')); | |
'; | |
} | |
if ($handle = opendir('wp-content/uploads')) { | |
while (false !== ($entry = readdir($handle))) { | |
if (is_dir('wp-content/uploads/'.$entry) && startsWith($entry, '20')) { | |
if ($handle2 = opendir('wp-content/uploads/'.$entry)) { | |
while (false !== ($entry2 = readdir($handle2))) { | |
if (is_dir('wp-content/uploads/'.$entry.'/'.$entry2) && !startsWith($entry2, '.')) { | |
if ($handle3 = opendir('wp-content/uploads/'.$entry.'/'.$entry2)) { | |
while (false !== ($entry3 = readdir($handle3))) { | |
if (is_file('wp-content/uploads/'.$entry.'/'.$entry2.'/'.$entry3) && !startsWith($entry3, '.') && preg_match('/.*[0-9]+x[0-9]+.(png|jpg)/', $entry3, $matches, PREG_OFFSET_CAPTURE) == 0) { | |
$date = date("Y-m-d H:i:s", filemtime("wp-content/uploads/$entry/$entry2/$entry3")); | |
render($date, $entry3, $entry.'/'.$entry2, mime_content_type("wp-content/uploads/$entry/$entry2/$entry3")); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment