Drupal
SQL Query for mapping Drupal Node Titles to URL Aliases
Submitted by Administrator on Thu, 01/13/2011 - 19:01I'm not sure how often you need to do this, but if you've ever wanted to build a cheat sheet that matches Drupal page titles to each page's clean url then here's the SQL query that'll do the trick:
select n.title as Title, u.dst as URL, u.pid as ID from (select title, concat('node/', nid) as realurl from node) as n join url_alias u on n.realurl = u.src order by n.title, u.pid desc
To avoid broken links Drupal tends to hang on to old aliases. So for better or worse you can end up with duplicate URLs for the same page and title. For that reason I've included the index id for the aliaes in the query and then used them as a secondary sort. At least in Drupal 6 and below the highest Alias ID corresponds to the most recently created alias. Which is most likely to be the "correct" one. Oh yeah, and I've included the index id for pages as well because in Drupal it's possible for two pages to have the same title.
The result's going to look something like this
| Page ID | Title | URL | Alias ID |
| 231 | 2009 - 2010 Wholesale Price List | 2009-2010-wholesale-price-list | 897 |
| 231 | 2009 - 2010 Wholesale Price List | 2009-2010_pricelist_wholesale | 837 |
| 231 | 2009 - 2010 Wholesale Price List | nursery/wholesale_price_list | 836 |
| 231 | 2009 - 2010 Wholesale Price List | pricelist | 835 |
| 242 | Availability Update | blog/2010-11/availability-update | 874 |
| 246 | Availbility Sheetnode | availbility-sheetnode | 900 |
| 202 | Before and After Photos | and-after-photos | 878 |
| 202 | Before and After Photos | before_after | 755 |
| 202 | Before and After Photos | before_after | 754 |
| 202 | Before and After Photos | beforeandafters | 753 |
| 206 | Catalog | catalog | 767 |
| 206 | Catalog | catalog | 766 |
In case you're curious I use lists like this as a link-mapping tool when I import an old website's pages into Drupal. Once the pages are imported, the titles straigthened out, and the URLs mapped into a decent hierarchy I can use this list when I go through the actual content (remember content?) and replace old urls with new correct ones.
There's probably a more intelligent...or at least less complicated way to do it, but this works for me. For a small site you wouldn't bother, but if you're importing hundreds or thousands of pages it's... kind of helpful.