Incorrect retrival of node's taxonomy terms with revisions

nonsie - April 3, 2009 - 18:57
Project:Pathauto
Version:6.x-1.1
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:needs review
Description

Some background info:
I have a site that uses termpath-raw aliases for some node types. I noticed that sometimes when the assigned terms changed the alias did not update accordingly or reverted to a very old version. Deleting existing aliases and recreating them didn't help either.

The issue lies in pathauto_token_values() function. For a node it attempts to retrieve taxonomy related data using the following query:

$category = db_fetch_object(db_query_range("SELECT t.tid, t.name FROM {term_data} t INNER JOIN {term_node} r ON r.tid = t.tid WHERE t.vid = %d AND r.nid = %d ORDER BY weight", $vid, $object->nid, 0, 1));

This query should account for different versions of the term assignment eg. if my node was tagged with "foo", then "bar" and eventually "baz" in the same vocabulary then this query will return the term based on term weight and not the currently assigned term.
The solution to fix it is actually simple:

$category = db_fetch_object(db_query_range("SELECT t.tid, t.name FROM {term_data} t INNER JOIN {term_node} r ON r.tid = t.tid WHERE t.vid = %d AND r.nid = %d ORDER BY r.vid DESC, weight", $vid, $object->nid, 0, 1));

Fixing this query is only half of the problem - Token module borrowed this functionality in node_token_values().
Instead of

$term = db_fetch_object(db_query_range("SELECT t.tid, t.name FROM {term_data} t INNER JOIN {term_node} r ON r.tid = t.tid WHERE t.vid = %d AND r.nid = %d ORDER BY weight", $vid, $object->nid, 0, 1));

it should be

if (!isset($term->name)) {
  $term = db_fetch_object(db_query_range("SELECT t.tid, t.name FROM {term_data} t INNER JOIN {term_node} r ON r.tid = t.tid WHERE t.vid = %d AND r.nid = %d ORDER BY r.vid DESC", $vid, $object->nid, 0, 1));
}
else {
  $term = db_fetch_object(db_query_range("SELECT t.tid, t.name FROM {term_data} t INNER JOIN {term_node} r ON r.tid = t.tid WHERE t.vid = %d AND r.nid = %d ORDER BY weight", $vid, $object->nid, 0, 1));
}

Since this issue spans across two different modules I'm not sure how to submit patches. Any suggestions?

#1

greggles - April 3, 2009 - 20:47

Great find. Can you just make two issues and submit two patches?

#2

nonsie - April 3, 2009 - 23:25
Status:active» needs review

Patch for Pathauto attached.
Patch for Token can be found here - http://drupal.org/node/423322

AttachmentSize
pathauto.module_423132.patch 1.21 KB

#3

greggles - April 19, 2009 - 20:31

This makes sense, but perhaps even better would be a change like the attached. Thoughts?

AttachmentSize
423132_pathauto_term_wrong_revision.patch 1.27 KB

#4

nonsie - April 20, 2009 - 19:29
Status:needs review» reviewed & tested by the community

+1 This seems like a better solution than mine

#5

greggles - April 20, 2009 - 22:52
Title:Incorrect retrival of node's taxonomy terms» Incorrect retrival of node's taxonomy terms with revisions

Applied to

6.x-1.x: http://drupal.org/cvs?commit=199982
5.x-2.x: http://drupal.org/cvs?commit=199984
6.x-2.x: http://drupal.org/cvs?commit=199992

#6

greggles - April 20, 2009 - 22:53
Status:reviewed & tested by the community» fixed

#7

System Message - May 4, 2009 - 23:00
Status:fixed» closed

Automatically closed -- issue fixed for 2 weeks with no activity.

#8

greggles - June 8, 2009 - 14:56
Status:closed» needs work

#485210: SQL Error - Unknown column 'r.vid' in 'where clause' query

I think the patches for 5.x should be reverted. Right?

#9

greggles - June 10, 2009 - 12:47
Status:needs work» fixed

Fixed - http://drupal.org/cvs?commit=223522

#10

System Message - June 24, 2009 - 12:50
Status:fixed» closed

Automatically closed -- issue fixed for 2 weeks with no activity.

#11

lportela - August 5, 2009 - 20:59
Status:closed» active

The current implementation of pathauto_token_values() has a problem when the revisioning module is enabled.

In this case the where clause of the second query should filter by the actual node revision (the one indicated by $object->vid value).

I attached a very simple patch to resolve this issue.

Thanks for this excelent module.

Leandro

AttachmentSize
423132_pathauto_term_wrong_revisioning_compatible.patch 448 bytes

#12

greggles - August 5, 2009 - 23:03
Status:active» needs review

The revisioning module?

Seems reasonable enough. Anyone else care to check?

#13

lportela - August 6, 2009 - 15:53

Yes, the revisioning module.

In my previous patch, I only considered the bulk node alias generation, but I didn’t consider saving a pending revision. In this case, the $object->vid value corresponds to the saved revision, not to the current node revision (indicated by vid column value in the node table). For this reason, a join with node table is required.

The new node alias shouldn’t be generated until the new revision is approved. The alias could be generated using the rules module.

I attached a new version of the patch and also submited the corresponding patch to the token module (#423322: Incorrect retrival of node's taxonomy terms with revisions).

I hope this will be the good one.

Leandro

AttachmentSize
423132_pathauto_term_wrong_revisioning_compatible_v2.patch 1010 bytes

#14

eft - October 13, 2009 - 21:07

I had same issue outlined at top of this thread. I already had the patched versions of pathauto so it seems the official patch has not resolved the issue in all cases. I did find, however, that Leandro's patch in comment #13 worked for me (even though I didn't have the revisioning module installed).

#15

tseven - October 31, 2009 - 00:37

I experienced the exact same issue with versions enabled. The patch in #13 fixed the issue using the current development version 6.x-1.x-dev.

 
 

Drupal is a registered trademark of Dries Buytaert.