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

Subscribers

People subscribed via source and target branches