Community Documentation

importing vocabularies into url_alias

Last updated May 21, 2010. Created by hutch on May 21, 2010.
Log in to edit this page.

I recently needed to create a number of url aliases based on a taxonomy vocabulary, doing it one at a time was out of the question so I wrote an external script to do it for me.

The script is designed to run from the command line and is not in any way integrated with Drupal.
You will need to edit some things, see the comments in the code.
You will want to prepend the code with something like #!/usr/bin/php. Alter this to suit your platform. Tested on Linux only.

This code assumes you have a reasonable working knowledge of php and sql, if you don't know what it does by reading it then you should leave it alone. YOU HAVE BEEN WARNED.

Here is the code:

<?php
// mysql EDIT THIS
$mysqldb = "test";
$mysqltable1 = "term_data";
$mysqltable2 = "term_hierarchy";
$mysqltable3 = "url_alias";
$mysqlhost = "localhost";
$mysqluser = "mysqluser";
$mysqlpw = "mysqlpass";

// which vocabulary EDIT THIS
$vid = 1;

// connect
$link = mysql_connect($mysqlhost, $mysqluser, $mysqlpw);
if (!
$link) { die('Could not connect: ' . mysql_error() . "\n"); }

// make $mysqldb the current db
$db_selected = mysql_select_db($mysqldb, $link);
if (!
$db_selected) { die("Cannot use $mysqldb\n"); }

// fetch this vocab and put it into $term_data
$sql = "SELECT * FROM $mysqltable1 WHERE `vid` = $vid ORDER BY `tid`";
#print $sql . "\n";
$result = mysql_query($sql, $link);
while (
$row = mysql_fetch_array($result)) {
 
$term_data[$row['tid']] = preg_replace("/\s/", '_', $row['name']);
}

$keys = array_keys($term_data);
foreach (
$keys AS $tid) {
 
$src = "taxonomy/term/$tid";
 
$parent = TRUE;
 
$thistid = $tid;
  while (
$parent) {
   
$parent = get_parent($thistid);
   
$data[] = $thistid;
   
$thistid = $parent;
  }
 
$data = array_reverse($data);
  for (
$ct = 0; $ct < count($data); $ct++) {
   
$t = $data[$ct];
    if (
$ct == 0) {
     
$dst = $term_data[$t];
    }
    else {
     
$dst .= '/' . $term_data[$t];
    }
  }
  unset(
$data);
 
#print "$src - " . $dst . "\n";
  # comment this section out for testing purpose
  #/*
  // check that this entry does not exist already
 
$numrows = FALSE;
 
$sql = "SELECT * FROM $mysqltable3 WHERE `src` = '$src'";
 
#print $sql . "\n";
 
if ($result = mysql_query($sql, $link)) {
   
$numrows = mysql_num_rows($result);
  }
  if (!
$numrows) {
   
$sql = "INSERT INTO $mysqltable3 SET ";
   
$sql .= "`src` = '$src', ";
   
$sql .= "`dst` = '$dst'";
   
#print $sql . "\n";
   
$result2 = mysql_query($sql, $link);
  }
 
#else {
  #  print "insert skipped, $src already exists\n";
  #}

  #*/
}

function
get_parent($tid) {
  global
$mysqltable2, $link;
 
$sql = "SELECT `parent` FROM $mysqltable2 WHERE `tid` = $tid";
 
#print $sql . "\n";
 
$result = mysql_query($sql, $link);
 
$row = mysql_fetch_array($result);
  return
$row['parent'];
}
?>

The code has only been run against a single vocabulary and written to an empty copy of the url_alias table from Drupal 6. It should handle a table which already has entries but you should run it againast a test copy nonetheless. The vocabulary is was run against had terms with 3 levels deep but should handle any depth. It assumes that each entry can only exist once and will skip duplicates of "taxonomy/term/n"
It replace spaces with '_' in term names to avoid spaces in url paths. If you have any other 'weird' characters in your term names then you should alter the code accordingly. eg '&'

I created a new database 'test' and copied the tables term_data, term_hierarchy and url_alias from the drupal instance concerned, ran the script on that, eyeballed the contents of url_alias in phpmyadmin and once I was satisfied copied that over to the Drupal instance it came from.

I give no guarantees that it will work for you, YMMV.

Hope this helps someone.

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 6.x
Audience
Developers and coders

Tutorials and site recipes

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.