Skip to content

Instantly share code, notes, and snippets.

@Quilted
Last active December 12, 2015 01:38
Show Gist options
  • Save Quilted/4692732 to your computer and use it in GitHub Desktop.
Save Quilted/4692732 to your computer and use it in GitHub Desktop.
Drupal 7: Setting up bulk WordPress to Drupal migration redirects

Drupal 7: Setting up bulk WordPress to Drupal migration redirects

When would I use this?

If you have a database of WordPress URLs that need to be redirected in Drupal 7.

What do I do?

  1. Install Redirect module.
  2. Install path_redirect_import module.
  3. Export 2 CSVs from the database: aliased and unaliased URLs.
  4. Import both CSVs into separate sheets in Google Docs.
  5. Create a script in the Google Spreadsheet.
  6. Paste in the scripts and save.
  7. Run the createDestinationURL() script on the sheet that imported the post_name column.
  8. Run the createOriginURL() script on the sheet that imported the guid column.
  9. Export both sheets to CSV.
  10. In Drupal, go to admin/config/search/redirect/import and import both CSVs exported from Google Docs.
/**
* https://developers.google.com/apps-script/service_spreadsheet
*/
function createDestinationUrl() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
for (var i = 0; i <= numRows - 1; i++) {
var cell = rows.getCell(i+1, 2);
cell.clearContent();
// Replace with desired destination URL.
cell.setValue('http://journalism.howlround.com/' + rows.getCell(i+1, 1).getValue());
// Logger.log(row);
}
};
function createOriginUrl() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
rows.copyTo(sheet.getRange(1, 2));
for (var i = 0; i <= numRows - 1; i++) {
var cell = rows.getCell(i+1, 1);
var value = cell.getValue();
// Replace with origin URL. This should match the contents of the CSV.
value = value.replace('http://www.howlround.com/', '');
cell.setValue(value);
}
}
/**
* Adds a custom menu to the active spreadsheet, containing a single menu item
* for invoking the readRows() function specified above.
* The onOpen() function, when defined, is automatically invoked whenever the
* spreadsheet is opened.
* For more information on using the Spreadsheet API, see
* https://developers.google.com/apps-script/service_spreadsheet
*/
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Create destination URL",
functionName : "createDestinationUrl"
},
{
name : "Create origin URL",
functionName : "createOriginUrl"
}
];
sheet.addMenu("Scripts", entries);
};
--- These are the queries that you need for exporting from WordPress.
--- This query returns the post_name column.
--- Depending on the WordPress URL alias setting, this can help with exporting all the URL aliases.
SELECT `post_name`
FROM `wp_posts`
WHERE `guid` NOT REGEXP '.*(revision|autosave|wp-content|post_type=slideshow).*'
AND `post_name` NOT REGEXP '.*(autosave|revision).*'
AND `post_status` = 'publish'
ORDER BY `wp_posts`.`post_name` ASC
--- This query returns the guid column.
--- It will include entries containing "?p=" and "?page_id=", which is what we want.
SELECT `guid`
FROM `wp_posts`
WHERE `guid` NOT REGEXP '.*(revision|autosave|wp-content|post_type=slideshow).*'
AND `post_name` NOT REGEXP '.*(autosave|revision).*'
AND `post_status` = 'publish'
ORDER BY `wp_posts`.`post_name` ASC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment