Convert Pigeonholes to Tags

Created by: Will, Last modification: 11 Dec 2007 (03:12 UTC)
You can use the following script if you would like to convert all your pigeonholes to tags. The script gets the content_ids for all your liberty content items at once and then steps through each one, gets its pigeonholes and saves them as tags for that content item. If you have millions or billions of records you may NOT want to get all your liberty content items at once, you may want to do that in steps or something.

This script includes parent pigeonholes as tags too - that is to say, for each content item a tag is created for the entire branch of pigeonholes it is leafed on. Proceed with caution, I only tested it minimally and ran it for my own purpose once - it worked, but all caveats apply as usual.

How to Use

Copy the code to a pigeon_to_tags.php file in your pigeonholes directory. Load it in your browser. Ding. Enjoy freshly baked tags.


<?php
require_once( '../bit_setup_inc.php' );

$bindVars = array();
$list_sql "SELECT `content_id` FROM `".BIT_DB_PREFIX."liberty_content`";

$list $gBitSystem->mDb->query($list_sql,$bindVars,99999);

$ret = array();
while (
$res $list->fetchRow()) {
    
$ret[] = $res;
}

include_once( 
TAGS_PKG_PATH.'LibertyTag.php' );
$pigeonholes = new Pigeonholes();

$log "";

if ( !empty( 
$ret ) ){
    foreach( 
$ret as &$content ) {
        if( 
$pigeons $pigeonholes->getPigeonholesFromContentId$content['content_id'] )) {
            
$pigeonData = array();
            foreach( 
$pigeons as $key => $pigeon ) {
                
$pigeonholes->mContentId $pigeon['content_id'];
                
$pigeonholes->loadTRUEFALSE );
                
$pigeonData[] = $pigeonholes->mInfo['path'];
            }

            
// strip out the tag values from the array
            
$tags = array();
            if ( !empty(
$pigeonData) ){
                foreach( 
$pigeonData as $path ){
                    foreach ( 
$path as $pigeon ){
                        
$tags[] = $pigeon['title'];
                    }
                }
            }

            if ( !empty( 
$tags ) ){
                
$tag = new LibertyTag$content['content_id'] );
                
// load up existing tags for the content item
                
$tag->load();
                
$existingTags = array();
                foreach (
$tag->mInfo['tags'] as $item){
                    
$existingTags[] = $item['tag'];
                }
                
$tagsHash = array();
                
// exclude existing tags from our storage hash or else we'll create duplicate tag records
                
$tagsHash['tags'] = array_diff($tags$existingTags);
                
$log .= "content_id:".$content['content_id']." update";
                
// store the tags
                
if ( $rslt $tag->storeTags$tagsHash ) ){
                    
$log .= " successful

"
;
                }else{
                    
$log .= " failed

"
;
                }
            }
        }
    }
}
echo 
$log;
?>