Created
December 13, 2011 23:28
-
-
Save zerolab/1474455 to your computer and use it in GitHub Desktop.
Detect taxonomy term loops in Drupal 6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Detect taxonomy term loops in Drupal 6 | |
* http://davidherron.com/content/save-your-server-detect-taxonomy-term-loops-drupal-6 | |
*/ | |
$result = db_query('SELECT tid, name FROM {term_data}'); | |
while ($term = db_fetch_object($result)) { | |
drush_print( '>>> '. $term->name); | |
$parents = safe_get_parents_all($term->tid); | |
$related = taxonomy_get_related($term->tid); | |
$synonyms = taxonomy_get_synonyms($term->tid); | |
drush_print( $term->name .'('. $term->tid .'): parents='. count($parents) .' related='. count($related) .' synonyms='. count($synonyms)); | |
} | |
function safe_get_parents_all($tid) { | |
$parents = array(); | |
if ($tid) { | |
$parents[] = taxonomy_get_term($tid); | |
$n = 0; | |
while ($parent = taxonomy_get_parents($parents[$n]->tid)) { | |
foreach ($parent as $p) { | |
if (! parents_has_term($parents, $p)) { | |
array_push($parents, $p); | |
//drush_print_r('... '. count($parents)); | |
} else { | |
drush_print_r('DETECTED LOOP tid='. $tid .' has parent '. $p->tid .' multiple times'); | |
drush_print_r($parents); | |
} | |
} | |
$n++; | |
} | |
} | |
return $parents; | |
} | |
function parents_has_term($parents, $term) { | |
foreach ($parents as $p) { | |
if ($p->tid === $term->tid) { | |
return TRUE; | |
} else { | |
return FALSE; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment