Tag Archive: Drupal

调试程序往往比编写程序更浪费时间,正如一些有经验的程序员所说,软件的开发应该包括20%的程序编写时间和80%的Debug时间。并且调试程序比编写程序要难2倍。编写巧妙的程序并不一定调试方便。

程序的调试有很多方法,比如最常见的VC之类的IDE都提供加断点,逐步执行,逐段执行的功能。但是这只能针对程序某个微小的片段,对于前期bug的范围的界定并不是很方便。况且对于不满IDE的臃肿,身陷Vim, notepad++, Editplus之类的编辑器之中,我们需要找到更加高效的程序调试和测试方法。

在程序执行的关键点打印Log是一个非常高效的方法,如何打印log进行程序调试呢? 以PHP程序开发为例,需要2个步骤:

1. PHP函数中两个关键的打印变量的方法

print_r var_dump

所以我们可以在关键的地方打印需要的变量。

2. 收集我们在程序中打印的变量

我们可以把程序执行过程中打印的变量逐条写到文本文件里。在程序执行完毕之后进行分析,查找Bug,修正程序。

PHP中打印时间,和变量的程序片段:

function file_log($message) {
    global $file_log_location;
    ob_start();
    echo date("Y-m-d H:i:s \t", time());
    print_r($message);
    echo "\n";
    $var = ob_get_contents();
    ob_end_clean();
    $open=@fopen($file_log_location,"a");
    @fwrite($open,$var);
    fclose($open);
}

应用此段程序还需要对log的存储位置$file_log_location赋值。

在需要的地方插入此片段,执行PHP程序,就会得到所需信息。

但是假如是长时间运行的程序如何在程序执行的过程中就查看到log的信息?相信你已经想到了可以在shell中用

tail -f /var/debug.log

这样总可以看到程序吐出的最新的debug信息。

附件是Drupal中进行debug的一个小的module,做drupal开发的同学可以尝试一下。

Drupal debug module

虽然本文针对PHP开发所言,但是这种方法也适合python,java之类的动态语言的开发。

–EOF–

Workflow in Drupal

Workflow is the essential feature of complex modern system. Lots of automate process can be implemented if the workflow states defines.And it contributed to the ACL system.
Think of the following situations:
1. The Draft news should only be shown to moderator. After moderation, the state changed to open. And open news should be shown to All automatically.
2. In the open state, some field shows to all, and some hidden, only can be seen by some roles.
3. In the Draft state, some fields such as title can not be edited, but other fields can be edited.
4. The job can be processed just after money access. Automatically change the state of the job. (By Trigger)
5. The payment process
6. Automatically hide the article after open 1 weeks.
In Drupal, workflow module always work together with Actions*, Triggers*, VBO*. Triggers can be assigned to the workflow changes. Do something automatically when the workflow changes. Triggers fire the Actions* we defined.

How to define Actions, please see our other articles.

Important feature or bug of workflow module of Drupal:
When a new CCK field added to a content type. The workflow of this state should be saved again, or the field will not be constrained by Workflow.

Change the workflow programmingly in one line:

<?php
    workflow_execute_transition(node_load($nid), 21, 'Test comment ',TRUE);
?>

You can change the workflow state by Trigger*, by cron and conditions, by xmlrpc, etc.

1. Create new menu root for example *merchant*

2. Override function hook_preprocess_page(&$vars) in templete.php and add the following code:

global $user;
if (in_array(‘Merchant’, $user->roles)) {
$vars['nav_links'] = menu_navigation_links(“menu-merchant”);
} elseif (in_array(‘…’, $user->roles)) {

}

3. Add the following code in your tpl file:

<?php print theme(‘links’, $nav_links) ?>

That is all.