Exemple #1
0
define('VUI_TEMPLATE_PLUGIN_PATH', VUI_APP_PATH . DIRECTORY_SEPARATOR . "plugins");
define('VUI_TEMPLATE_ALADDIN_TEMPLATE_TYPE', "aladdin");
$GLOBALS['ECOM_TEMPLATE_TYPE'] = 'ecom';
require VUI_APP_PATH . '/libs/Smarty/Smarty.class.php';
require VUI_APP_PATH . '/utils/CSmarty.class.php';
require VUI_APP_PATH . '/plugins/modifier.common_render.php';
require VUI_APP_PATH . '/libs/log/CLog.class.php';
require VUI_APP_PATH . '/utils/Util.php';
// require (dirname(__FILE__) . '/phplib/bd/Conf.php');
require VUI_APP_PATH . '/libs/log/TplLog.php';
// require (VUI_APP_PATH . '/utils/base.php');
require VUI_APP_PATH . '/utils/Volatile.php';
// 读取cssUI
$cssUI = json_decode(file_get_contents(dirname(__FILE__) . '/cssUI.json'), true);
$smartyConf = array('language' => 'zh-CN', 'type' => 'ecom', 'platform' => 'pc');
$smarty = CSmarty::getInstance($smartyConf);
$res = array('left' => array(), 'right' => array());
function render($tplName, $tplItem)
{
    global $smarty;
    global $smartyConf;
    CSmarty::clearError();
    // 这里,$data有三种数据格式,第一种是我定义的最原始的数据格式,第二种是有的同学在display里面进行了细分,加了extData,第三种是平台的数据格式
    // 所以,在这里先进行一次处理
    // $data = preprocess(CJSON::decode(file_get_contents($tplItem['data'])));
    $data = preprocess(json_decode(file_get_contents($tplItem['data']), TRUE));
    $result = $smarty->do_render($data, $tplName);
    // 有错误发生
    $errors = CSmarty::getError();
    if (count($errors) > 0) {
        return $errors[0];
/**
 *
 *
 * @file modifier.real.php
 * @package plugins
 * @author liyudong@baidu.com
 * @date 2011-11-03 10:47
 */
function smarty_modifier_aladdin_render($data, $templateName, $curr_sort = false)
{
    $time_start = Volatile::microtime(true) * 1000;
    $logArr['smarty_modifier'] = "modifier_aladdin_render";
    /**
     * render config
     */
    $conf = CSmarty::getConf();
    /**
     * template type
     * @var string 
     */
    $type = VUI_TEMPLATE_ALADDIN_TEMPLATE_TYPE;
    $language = $conf['language'];
    $platform = $conf['platform'];
    $logArr['template_type'] = $type;
    $logArr['template_name'] = $templateName;
    $logArr['language'] = $language;
    $logArr['platform'] = $platform;
    $log_key = $data['StdStg'] . '_' . $templateName;
    /**
     * template type cannot be empty
     */
    if (strlen($type) == 0) {
        CLog::warning("template type is empty", -1, $logArr, 1);
        return false;
    }
    /**
     * template name cannot be empty
     */
    if (empty($templateName)) {
        CLog::warning("template name is empty", -1, $logArr, 1);
        return false;
    }
    /**
     * aladdin's tplData 
     */
    if (empty($data['resultData']['tplData']) || !is_array($data['resultData']['tplData'])) {
        CLog::warning("tplData is empty", -1, $logArr, 1);
        return false;
    }
    /**
     * set queryInfo
     */
    $data['resultData']['queryInfo'] = CSmarty::getQueryInfo();
    /**
     * set templateConfig
     */
    $data['resultData']['templateConfig'] = CSmarty::getTemplateConfig();
    /**
     * set result current sort of result page
     */
    if ($curr_sort === false) {
        $curr_sort = 0;
    } else {
        $curr_sort += 1;
    }
    $data['resultData']['extData']['curr_sort'] = $curr_sort;
    /**
     * page renderer 
     * @var Smarty
     */
    $smarty = CSmarty::getInstance(array('language' => $language, 'type' => $type, 'platform' => $platform));
    if (false === $smarty) {
        $status = -1;
        $errors = CSmarty::getError();
        $errors_str = implode(";", $errors);
        $logArr['ala_smarty_error'] = $errors_str;
        CLog::warning("fail to get instance of smarty, type: {$type}", $status, $logArr, 1);
        return false;
    }
    /**
     * encrypt page's url
     */
    backend_encrypt_url($data, $platform, $language, $type, $templateName);
    /**
     * render aladdin's page
     */
    $page = $smarty->do_render($data, $templateName, 1);
    if ($page === false) {
        $status = -1;
        $errors = CSmarty::getError();
        $errors_str = implode(";", $errors);
        CLog::warning("fail to render aladdin's page, errors[{$errors_str}]", $status, $logArr, 1);
        return false;
    }
    if (strlen($page) == 0) {
        $status = -1;
        CLog::warning("aladdin's page is empty", $status, $logArr, 1);
        return false;
    }
    $GLOBALS['logArr']["aladdin_page_size_{$log_key}"] = strlen($page);
    $GLOBALS['logArr']["time_aladdin_{$log_key}"] = round(Volatile::microtime(true) * 1000 - $time_start, 2);
    return $page;
}
/**
 * 通用的单条渲染插件,可渲染aladdin、result、ecom等目录下;
 * 不能渲染midpage、xml、data和baidu下的完整页面模板
 *
 * @package plugins
 * @author jiachunxin@baidu.com
 */
function smarty_modifier_common_render($data, $type, $templateName, $curr_sort = false)
{
    if ($GLOBALS['mulit_render_flag'] === 1) {
        $key = $data['multi_render_key'];
        if (empty($key)) {
            CLog::warning("empty_key!", 1);
        }
        return $key;
    }
    $logArr['smarty_modifier'] = "common_render";
    $conf = CSmarty::getConf();
    $language = $conf['language'];
    $platform = $conf['platform'];
    $logArr['template_type'] = $type;
    $logArr['template_name'] = $templateName;
    $logArr['language'] = $language;
    $logArr['platform'] = $platform;
    $logArr['StdStg'] = $data['StdStg'];
    // result目录下的模板不统计性能
    $time_start = Volatile::microtime(true) * 1000;
    if ($type != $GLOBALS['STRUCT_TEMPLATE_TYPE']) {
        $log_key = $data['StdStg'] . '_' . $templateName;
    } else {
        CSmarty::addStructTplRenderNumCount();
    }
    if (empty($type)) {
        CLog::warning("template type is empty", -1, $logArr, 1);
        return false;
    }
    if (empty($templateName)) {
        CLog::warning("template name is empty", -1, $logArr, 1);
        return false;
    }
    if (empty($data['resultData']['tplData']) || !is_array($data['resultData']['tplData'])) {
        CLog::warning("tplData is empty", -1, $logArr, 1);
        return false;
    }
    $data['resultData']['queryInfo'] = CSmarty::getQueryInfo();
    $data['resultData']['templateConfig'] = CSmarty::getTemplateConfig();
    if ($curr_sort === false) {
        $curr_sort = 0;
    } else {
        $curr_sort += 1;
    }
    $data['resultData']['extData']['curr_sort'] = $curr_sort;
    // page renderer
    $smarty = CSmarty::getInstance(array('language' => $language, 'type' => $type, 'platform' => $platform));
    if (false === $smarty) {
        $status = -1;
        $errors = CSmarty::getError();
        $errors_str = implode(";", $errors);
        $logArr[$type . '_smarty_error'] = $errors_str;
        CLog::warning("fail to get instance of smarty, type: {$type}", $status, $logArr, 1);
        return false;
    }
    // encrypt page's url
    backend_encrypt_url($data, $platform, $language, $type, $templateName);
    // render page
    $page = $smarty->do_render($data, $templateName, 1);
    if (empty($page)) {
        $status = -1;
        $errors = CSmarty::getError();
        $errors_str = implode(";", $errors);
        CLog::warning("fail to render " . $type . " page, errors[{$errors_str}],info[{$log_key}]", $status, $logArr, 1);
        return false;
    }
    $tm_cost = round(Volatile::microtime(true) * 1000 - $time_start, 2);
    // result目录下的模板不统计性能
    if ($type != $GLOBALS['STRUCT_TEMPLATE_TYPE']) {
        $GLOBALS['logArr']["aladdin_page_size_{$log_key}"] = strlen($page);
        $GLOBALS['logArr']["time_aladdin_{$log_key}"] = $tm_cost;
        $GLOBALS['time_count_temp']['time_aladdin_total'] += $tm_cost;
        $GLOBALS['time_count_temp']['render_aladdin_num']++;
    } else {
        CSmarty::addStructTplRenderTimeCount($tm_cost);
        // result模板也统计性能,合并打印
        CSmarty::recordResultTplRenderTime($tm_cost);
    }
    return $page;
}
/**
 *
 *
 * @file modifier.render.php
 * @package plugins
 * @author fengfei02@baidu.com
 * @date 2012-10-29 10:47
 */
function smarty_modifier_render($data, $templateName, $curr_sort = false)
{
    $logArr['smarty_modifier'] = "modifier_render";
    /**
     * render config
     */
    $conf = CSmarty::getConf();
    /**
     * template type
     * @var string 
     */
    $type = $GLOBALS['STRUCT_TEMPLATE_TYPE'];
    $language = $conf['language'];
    $platform = $conf['platform'];
    $logArr['template_name'] = $templateName;
    $logArr['template_type'] = $type;
    $logArr['language'] = $language;
    $logArr['platform'] = $platform;
    /**
     * template type cannot be empty
     * 
     * 模板是否修改类型的目录?
     * 
     */
    if (strlen($type) == 0) {
        CLog::warning("template type is empty", -1, $logArr, 1);
        return false;
    }
    /**
     * template name cannot be empty
     */
    if (empty($templateName)) {
        CLog::warning("template name is empty", -1, $logArr, 1);
        return false;
    }
    /**
     * ac's data 
     */
    if (empty($data) || !is_array($data) || empty($data['resultData'])) {
        CLog::warning("data or resultData is empty", -1, $logArr, 1);
        return false;
    }
    /**
     * set queryInfo,这个字段在baidu的global变量和函数有用,迁移时有用
     */
    $data['resultData']['queryInfo'] = CSmarty::getQueryInfo();
    /**
     * set templateConfig,同queryInfo
     */
    $data['resultData']['templateConfig'] = CSmarty::getTemplateConfig();
    /**
     * set result current sort of result page,同queryInfo
     */
    if ($curr_sort === false) {
        $curr_sort = 0;
    } else {
        $curr_sort += 1;
    }
    $data['resultData']['extData']['curr_sort'] = $curr_sort;
    /**
     * page renderer 
     * @var Smarty
     */
    $smarty = CSmarty::getInstance(array('language' => $language, 'type' => $type, 'platform' => $platform));
    if (false === $smarty) {
        $status = -1;
        $errors = CSmarty::getError();
        $errors_str = implode(";", $errors);
        $logArr['ac_smarty_error'] = $errors_str;
        CLog::warning("fail to get instance of smarty, type: {$type}", $status, $logArr, 1);
        return false;
    }
    /**
     * render ac's page
     */
    $page = $smarty->do_render($data, $templateName, 1);
    if ($page === false) {
        $status = -1;
        $errors = CSmarty::getError();
        $errors_str = implode(";", $errors);
        //CLog::warning("fail to render ac's page, errors[$errors_str]", $status, $logArr, 1);
        CLog::warning("fail to render ac's page", $status, $logArr, 1);
        return false;
    }
    if (strlen($page) == 0) {
        $status = -1;
        CLog::warning("ac's page is empty", $status, $logArr, 1);
        return false;
    }
    return $page;
}