I just wanted to document one of the many really shitty scripts i write everyday to update various products and nodes in our drupal install

<?
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
define('DRUPAL_ROOT', dirname(realpath(__FILE__)));
require_once("includes/bootstrap.inc");
error_reporting(E_ALL);
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

function load_csv() {
        $handle = fopen("adobe-deletes.csv", "r");
        $csv = array();
        while(($data = fgetcsv($handle)) != false) {
                $csv[] = $data;
        }
        fclose($handle);
        return $csv;
}

function search_csv_by_sku($array, $sku) {
        foreach($array as $item) {
                if($item[0] == $sku)
                        return $item;
        }
        return null;
}

function search_csv_by_name($array, $name) {
        foreach($array as $item) {
                if($item[2] == $name)
                        return $item;
        }
        return null;
}

function update_products() {
        if((!$csv = load_csv())) {
                printf("could not load dlink csv\n");
                exit(1);
        }

        $query = db_query("SELECT node.nid, uc_products.model FROM {node}
                      LEFT JOIN uc_products ON node.nid = uc_products.nid
                          WHERE uc_products.model is not NULL");

        $count = 0;
        foreach($query as $n) {
                $node = node_load($n->nid);
                if(($result = search_csv_by_name($csv, $node->title))) {
                        printf("$node->nid $node->model $node->title $node->cost $node->sell_price\n
                                   $result[0] $result[1] $result[2] $result[3] \n\n");
                        $node->status = 0;
                        node_save($node);
                        $count++;
                }
        }
        printf("%d\n", $count);
}

update_products();
?>
Back