在向Drupal网站中导入大量数据的时候,需要对页面进行归类以便分类展示,所谓归类也就是让Node在Taxonomy系统中属于某个Vocabulary的某个Term,这可以通过调用Drupal的API来批量实现,有两种调用办法都可以:
一是修改$node->taxonomy[] = $tid,然后调用node_save($node)来保存;
另外一种是先组成$terms,然后调用taxonomy_node_save($node, $terms)来保存。
这两种办法都可以实现,效果也是一样的,我后来用的第二种办法。在编写这个PHP程序前我还真的没有怎么接触PHP程序、Drupal API函数,所以花了很多时间不断编写、测试、修改,直到最后可用,其实也只是不长的一段代码。
现在列出来供感兴趣的朋友参考:
<?php
$_SERVER['HTTP_HOST'] = 'guizhou.youbianku.com';
$_SERVER['SCRIPT_NAME'] = '/'.'refresh_category.php';
$drupal_path = '/usr/local/apache2/htdocs/drupal.youbianku.com/';
chdir($drupal_path);
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
$node_type = "minglu";
$list_no = 100;
$offset = 0;
$sql = "SELECT node.title, node.type, node.nid FROM {node} WHERE node.type = '$node_type' LIMIT $list_no OFFSET $offset";
$result = db_query($sql);
while ($anode = db_fetch_object($result)) {
/*
print_r ($anode);
echo '<br>\n';
*/
$node=node_load($anode->nid);
/*
print_r ($node);
echo '<br>';
*/
//将CCK数据库字段读出赋值给变量
$postcode=$node->field_postcode[0]['value'];
$area_number=$node->field_area_number[0]['value'];
/*
echo '$postcode='.$postcode.'<br>';
echo '$area_number='.$area_number.'<br>';
*/
//从行政区划编号前2位判断省份
switch (substr($area_number,0,2))
{
case "52":
$province="贵州省";
break;
case "";
$province="没有行政区号";
break;
default:
$province="贵州省以外";
}
//从行政区划编号前4位判断地市
switch (substr($area_number,0,4))
{
case "5200":
$city="贵州省未细分";
break;
case "5201";
$city="贵阳市";
$areacode="0851";
break;
case "5202";
$city="六盘水市";
$areacode="0858";
break;
case "5203";
$city="遵义市";
$areacode="0852";
break;
case "5222";
$city="铜仁地区";
$areacode="0856";
break;
case "5223";
$city="黔西南布依族苗族自治州";
$areacode="0859";
break;
case "5224";
$city="毕节地区";
$areacode="0857";
break;
case "5225";
$city="安顺地区";
$areacode="0853";
break;
case "5226";
$city="黔东南苗族侗族自治州";
$areacode="0855";
break;
case "5227";
$city="黔南布依族苗族自治州";
$areacode="0854";
break;
default:
$city="默认行政区号";
}
//从行政区划编号全部6位判断区县
switch ($area_number)
{
case "520101":
$county="市辖区";
break;
case "520102":
$county="南明区";
break;
case "520103":
$county="云岩区";
break;
case "520111":
$county="花溪区";
break;
case "520112":
$county="乌当区";
break;
case "520113":
$county="白云区";
break;
case "520121":
$county="开阳县";
break;
case "520122":
$county="息烽县";
break;
case "520123":
$county="修文县";
break;
case "520181":
$county="清镇市";
break;
case "520201":
$county="钟山区";
break;
case "520202":
$county="盘县特区";
break;
case "520203":
$county="六枝特区";
break;
case "520221":
$county="水城县";
break;
case "520301":
$county="市辖区";
break;
case "520302":
$county="红花岗区";
break;
case "520321":
$county="遵义县";
break;
case "520322":
$county="桐梓县";
break;
case "520323":
$county="绥阳县";
break;
case "520324":
$county="正安县";
break;
case "520325":
$county="道真仡佬族苗族自治县";
break;
case "520326":
$county="务川仡佬族苗族自治县";
break;
case "520327":
$county="凤冈县";
break;
case "520328":
$county="湄潭县";
break;
case "520329":
$county="余庆县";
break;
case "520330":
$county="习水县";
break;
case "520381":
$county="赤水市";
break;
case "520382":
$county="仁怀市";
break;
case "522201":
$county="铜仁市";
break;
case "522222":
$county="江口县";
break;
case "522223":
$county="玉屏侗族自治县";
break;
case "522224":
$county="石阡县";
break;
case "522225":
$county="思南县";
break;
case "522226":
$county="印江土家族苗族自治县";
break;
case "522227":
$county="德江县";
break;
case "522228":
$county="沿河土家族自治县";
break;
case "522229":
$county="松桃苗族自治县";
break;
case "522230":
$county="万山特区";
break;
case "522301":
$county="兴义市";
break;
case "522322":
$county="兴仁县";
break;
case "522323":
$county="普安县";
break;
case "522324":
$county="晴隆县";
break;
case "522325":
$county="贞丰县";
break;
case "522326":
$county="望谟县";
break;
case "522327":
$county="册亨县";
break;
case "522328":
$county="安龙县";
break;
case "522401":
$county="毕节市";
break;
case "522422":
$county="大方县";
break;
case "522423":
$county="黔西县";
break;
case "522424":
$county="金沙县";
break;
case "522425":
$county="织金县";
break;
case "522426":
$county="纳雍县";
break;
case "522427":
$county="威宁彝族回族苗族自治县";
break;
case "522428":
$county="赫章县";
break;
case "522501":
$county="安顺市";
break;
case "522526":
$county="平坝县";
break;
case "522527":
$county="普定县";
break;
case "522528":
$county="关岭布依族苗族自治县";
break;
case "522529":
$county="镇宁布依族苗族自治县";
break;
case "522530":
$county="紫云苗族布依族自治县";
break;
case "522601":
$county="凯里市";
break;
case "522622":
$county="黄平县";
break;
case "522623":
$county="施秉县";
break;
case "522624":
$county="三穗县";
break;
case "522625":
$county="镇远县";
break;
case "522626":
$county="岑巩县";
break;
case "522627":
$county="天柱县";
break;
case "522628":
$county="锦屏县";
break;
case "522629":
$county="剑河县";
break;
case "522630":
$county="台江县";
break;
case "522631":
$county="黎平县";
break;
case "522632":
$county="榕江县";
break;
case "522633":
$county="从江县";
break;
case "522634":
$county="雷山县";
break;
case "522635":
$county="麻江县";
break;
case "522636":
$county="丹寨县";
break;
case "522701":
$county="都匀市";
break;
case "522702":
$county="福泉市";
break;
case "522722":
$county="荔波县";
break;
case "522723":
$county="贵定县";
break;
case "522725":
$county="瓮安县";
break;
case "522726":
$county="独山县";
break;
case "522727":
$county="平塘县";
break;
case "522728":
$county="罗甸县";
break;
case "522729":
$county="长顺县";
break;
case "522730":
$county="龙里县";
break;
case "522731":
$county="惠水县";
break;
case "522732":
$county="三都水族自治县";
break;
default:
$county="";
}
/*
echo '$province='.$province.'<br>';
echo '$city='.$city.'<br>';
echo '$county='.$county.'<br>';
*/
$term_province = taxonomy_get_term_by_name ($province);
$tid_province=$term_province[0]->tid;
$term_city = taxonomy_get_term_by_name ($city);
$tid_city=$term_city[0]->tid;
$term_county = taxonomy_get_term_by_name ($county);
$tid_county=$term_county[0]->tid;
$term_postcode = taxonomy_get_term_by_name ($postcode);
$tid_postcode=$term_postcode[0]->tid;
/*
echo '$term_province=';
print_r ($term_province);
echo '$term_city=';
print_r ($term_city);
echo '$term_county=';
print_r ($term_county);
echo '$term_postcode=';
print_r ($term_postcode);
echo '<br>';
echo '$tid_province='.$tid_province,'$tid_city='.$tid_city,'$tid_county='.$tid_county,'$tid_postcode='.$tid_postcode;
echo '<br>';
*/
echo '$nid='.$anode->nid.
', $tid_province='.$tid_province.
', $tid_city='.$tid_city.
'. $tid_county='.$tid_county.
', $tid_postcode='.$tid_postcode.
"<br>\n";
taxonomy_node_save($node,array($tid_province,$tid_city,$tid_county,$tid_postcode));
}
echo "<br>Done!<br>\n"
?>
上面的程序是以贵州为例,将行政区号代码读出,转换为省份、地市、区县三级名称,再找到对应的Term的ID,另外找到邮编对应的Term的ID,然后将该Node关联这些分类并保存到数据库中。上面的代码中有些只是调试用的,我也没有做注释,仅供参考。
评论