Merge lp:~jacekn/wordpress/wp-plugin-swift-storage-migration into lp:~canonical-sysadmins/wordpress/wp-plugin-swift-storage

Proposed by Jacek Nykis
Status: Merged
Merged at revision: 3
Proposed branch: lp:~jacekn/wordpress/wp-plugin-swift-storage-migration
Merge into: lp:~canonical-sysadmins/wordpress/wp-plugin-swift-storage
Diff against target: 102 lines (+98/-0)
1 file modified
migrate.php (+98/-0)
To merge this branch: bzr merge lp:~jacekn/wordpress/wp-plugin-swift-storage-migration
Reviewer Review Type Date Requested Status
The Canonical Sysadmins Pending
Review via email: mp+246724@code.launchpad.net

Description of the change

Add migration script that copies old media files to object storage

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== added file 'migrate.php'
--- migrate.php 1970-01-01 00:00:00 +0000
+++ migrate.php 2015-01-16 15:18:23 +0000
@@ -0,0 +1,98 @@
1<?php
2
3/*
4This PHP cli helper script will upload existing media files
5to OpenStack object store and update links witin posts
6
7Berore running it make sure that:
81. You have database and media files backups
92. swift-storage plugin is configured and can reach swift.
10 Good test is to check if swift buckets are showing on
11 the plugin configuration page
123. Swift bucket is publically readable ie.
13 swift post -r '.r:*' WordPress
144. Swift URLs are reachable from outside world
15
16The script must be run from the top level directory holding
17wordpress code for example:
18
19cd /srv/wordpress
20php wp-content/plugins/swift-storage/migrate.php
21
22*/
23
24@include "wp-config.php";
25@include_once "wp-includes/option.php";
26@include_once "wp-includes/functions.php";
27@include_once "wp-includes/post.php";
28
29$plugin_path = dirname($argv[0]);
30require_once $plugin_path . "/classes/swift.php";
31
32if($argv[1] != "--break-wordpress") {
33 print "Make sure you have backups and you know what you are doing.\n";
34 print "If you really want to migrate data rerun the script with --break-wordpress switch\n";
35 exit;
36}
37
38$uploaded_files = 0;
39global $wpdb;
40$siteurl = $wpdb->get_var("select option_value from wp_options where option_name = 'siteurl'");
41$last_post = $wpdb->get_var("SELECT MAX(ID) from wp_posts");
42$swift = new Swift( __FILE__ );
43
44function fix_space($post_id) {
45 $filename = get_post_meta( $post_id, $key = '_wp_attached_file', true);
46 $filename = str_replace( ' ', '+', $filename);
47 delete_post_meta($post_id, '_wp_attached_file');
48 add_post_meta($post_id, '_wp_attached_file', $filename);
49 $meta = get_post_meta( $post_id, $key = '_wp_attachment_metadata', true);
50 $meta['file'] = str_replace( ' ', '+', $meta['file']);
51 delete_post_meta($post_id, '_wp_attachment_metadata');
52 add_post_meta($post_id, '_wp_attachment_metadata', $meta);
53 $swift_info = get_post_meta( $post_id, $key = 'swift_info', true);
54 $swift_info['key'] = str_replace( ' ', '+', $swift_info['key']);
55 delete_post_meta($post_id, 'swift_info');
56 add_post_meta($post_id, 'swift_info', $swift_info);
57}
58
59for($i=0; $i<=$last_post; $i++) {
60 $filename = get_post_meta( $i, $key = '_wp_attached_file', true);
61 $meta = get_post_meta( $i, $key = '_wp_attachment_metadata', true);
62
63 // Some files can have full paths, we have to normalize it
64 if(preg_match('/\/srv\//', $filename)) {
65 print "\tUpdating path for file: " . $filename . "\n";
66 $filename = preg_replace( '/.*\/wp-content\/uploads\//', '', $filename);
67 delete_post_meta($i, '_wp_attached_file');
68 add_post_meta($i, '_wp_attached_file', $filename);
69 }
70 if(!empty($meta)) {
71 if(preg_match('/\/srv\//', $meta['file'])) {
72 $meta['file'] = preg_replace( '/.*\/wp-content\/uploads\//', '', $meta['file']);
73 delete_post_meta($i, '_wp_attachment_metadata');
74 add_post_meta($i, '_wp_attachment_metadata', $meta);
75 }
76 }
77 if(!empty($filename) && !empty($meta)){
78 $old_url = $siteurl . "/wp-content/uploads/" . $filename;
79 $swift->wp_update_attachment_metadata($meta, $i);
80 if (strpos($filename, ' ') !== FALSE) {
81 print "\tDetected space in filename: " . $filename . " replacing with \"+\"\n";
82 fix_space($i);
83 }
84 $new_url = $swift->swift_get_attachment_url($i);
85 $_tmp = @file_get_contents($new_url);
86 if(empty($_tmp)){
87 die("\nERROR: Can't verify upload for " . $filename . " (url: " . $new_url . "). Giving up!\n");
88 }
89 print $old_url . " => " . $new_url . "\n";
90 $uploaded_files++;
91 $sql = "UPDATE wp_posts SET post_content = REPLACE(post_content, '" . $old_url . "', '" . $new_url . "')";
92 //print "SQL query: " . $sql . "\n";
93 $wpdb->get_var($sql);
94 }
95}
96
97print "\n\n#################################\n# Number of files uploaded: " . $uploaded_files . "\n\n";
98

Subscribers

People subscribed via source and target branches