Category: Life & Work

Drupal Views PHP Code Validator

Views is one of the best module of Drupal. It provide a way to map the data in the mysql database to the web interface. You can do that even without writing any SQL query, just some clicks. I do not want to cover all the aspet of Views, but only to show how to restrict the Veiws result based on Arguments.

Arguments are parts of URL, may be a node id in /node/xxxx, or user id in /user/xxxx. Views provide some build-in arguments: See Views arguments. You can restrict the views by Node type, User ID, Term ID, etc. But these build-in features can not meet all of our requirement.

So we can use PHP Code Validator.

For example, I want to show the view only in the node where a CCK field eqs 1. And only show in the content type blog.

$nid = $argument;
$node = node_load($nid);
return ($node->type == ‘blog’ && $node->field_Myfield_type[0]['value'] == 1);

Put the php code block in the views argument, then you will find the result.

细数跨语言通信的选择

对编程语言的选择的规则其实很简单,优先选用自己或者团队熟悉的语言,优先选择项目中用到开源产品稳定版本所用的语言。经常看到某 Java 程序员抱怨 PHP 的各种问题,或者 C++ 的程序员和 JAVA 的程序员的网上吵架,或者最近知名博客 dbanotes 的博主 Fenng 和知名技术社区 Javaeye 的创始人范凯在 Twitter 上关于 PHP 或者 ROR 技术选型的争论,其实编程语言无好坏,关键是使用的人。

为了提高开发效率,需要融合不同语言。回顾以前项目中用到的几种不同语言间通信的几种方式:

1. XML-RPC 可能是应用最广泛的方法

使用 HTTP 协议,通过向远程服务发送方法请求,获取处理结果。其中请求发送的内容为 XML 格式:

<?xml version="1.0"?>
<methodCall>
  <methodName>examples.getStateName</methodName>
  <params>
    <param>
        <value><i4>40</i4></value>
    </param>
  </params>
</methodCall>

和 XML-RPC 类型的还有 JSON-RPC ,顾名思义通信内容使用 JSON 格式。

发送 XML-RPC 或者 JSON-RPC 请求可以简单的用 HTTP 发送拼接的请求内容,比如 PHP 中可以用 Curl 发送请求,接收处理结果。

2. SOAP 也是使用 HTTP 协议进行通信

SOAP 请求的例子:

POST /InStock HTTP/1.1
Host: www.example.org
Content-Type: application/soap+xml; charset=utf-8
Content-Length: 299
 
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
  <soap:Header>
  </soap:Header>
  <soap:Body>
    <m:GetStockPrice xmlns:m="http://www.example.org/stock">
      <m:StockName>IBM</m:StockName>
    </m:GetStockPrice>
  </soap:Body>
</soap:Envelope>

SOAP 和 XML-RPC 的主要区别在于可以通过调用远程 WSDL 文件获取远程服务的方法。而且扩展了对 HTTP HEADER 的使用。使用 .NET 可以轻松的创建一个 SOAP 服务以供调用。同 XML-RPC 一样,在 PHP 中可以用 CURL 直接发送 HTTP 请求。

3. Socket

用 Socket 进行通信可能是最古老的方法,也是最高效的方式。

以 PHP 为例发送 Socket 请求:

$s = @fsockopen('127.0.0.1', 1234);
fwrite($s, "addaiml\r\n");
$result= fgets($s, 10);
fclose($s);
可以用几百行 JAVA 或者其他语言轻松创建一个简单的 Socket 服务来接收 Socket 请求。这里值得提出的是 memcache 的协议设计堪称典范,以前项目的的 Socket 协议设计都参考了 memcahe 的方式。

4. Message Queue

通过消息队列来进行通信,可以用现有的 message queue 中间件产品比如支持异步处理的 gearman,或者更为强大的 Rabbit-MQ,  Zero-MQ。

也可以在数据库中自己建立消息表,构建简单的生产者-消费者模式,服务端定时查询数据库中的未处理消息,处理完后对消息进行标定,请求端在表中插入新消息,并且定期查询消息的状态。

5. CLI

这可能是比较土的调用其他服务的方式,虽然只支持同步的方式,但是也很有效,比如获取服务器的CPU MEMORY 等指标可以用这种方式

以 PHP 为例,一切在命令行使用的命令都可以通过 exec() 函数来调用,并且获取处理结果。扩展思维,一切能在命令行人工执行的命令,都可以通过此方式来执行。

常见语言对于XML-RPC的支持库

Python: http://docs.python.org/library/xmlrpclib.html
C++: http://libiqxmlrpc.sourceforge.net/
Erlang: http://erlang.stacken.kth.se/contrib/xmlrpc-1.13.tgz
JAVA: http://ws.apache.org/xmlrpc/
.NET: http://www.xml-rpc.net/
PHP: http://phpxmlrpc.sourceforge.net/
Ruby: http://www.ruby-doc.org/stdlib/libdoc/xmlrpc/rdoc/index.html

2010 Summary And 2011 Goals

为什么需要总结和计划

总结和计划总是让人喜悦或镇痛,一方面以前一段时间没有荒废,能给现在的行动以信心,另一方面看到一年的时间并不能完成很多事情,需要抓紧时间。让自己清醒总是好的。

这是一篇博客的截图:

很多年前看到这样的话:

明确地知道你的前进方向,可以增加目标完成的几率。

所以他制定了自己的年度目标,所以我制定了自己的年度目标。

这是他2010年的目标和完成情况,黑体代表完成。

他在2010年完成了: 再生一个小孩,赚到10万美元以上等等计划。足以看的计划的力量。

本来计划2011.1.1完成这个总结,但是一直拖到春节,拖到现在 :(

关于博客的总结

从  Archive for 2010 看, 在2010年发布了超过100篇博文。

Drupal开发

  1. Make your contributed drupal modules translatable in translate interface of i18n
  2. Make user inputed variables translatable in drupal
  3. Why drupal mail messy code
  4. How to switch database in Drupal
  5. Get parameters in url by arg(?) in Drupal
  6. How to set the module weight in Drupal
  7. Drupal theme block display issues
  8. Drupal Tips
  9. Drupal theme variables
  10. Drupal batch progress bar
  11. Create a progress bar in Drupal
  12. 5 tips you should know when building a Drupal theme
  13. How to show custom menu links to special roles in Drupal
  14. Drupal: Show different links to different roles
  15. Drupal: Show custom links as Primary / Secondary links.
  16. Workflow in Drupal

PHP 开发

  1. 服务器端PHP多进程编程实战
  2. Online php debug method
  3. How to get the file ext using PHP
  4. How to use include_once in PHP rightly
  5. XMLRPC example in PHP
  6. 10条测验自己PHP水平的语句
  7. PHP抓取外部资源函数fopen / file_get_contents / curl 的区别

Erlang 开发

  1. Get timestamp in Erlang
  2. How to update a value in mnesia database
  3. How to see the data in mensia database
  4. 最近-Erlang-XMPP-python
  5. HTTPCLIENT in Erlang
  6. Erlang xmlrpc example
  7. Erlang GUI tools
  8. 项目迭代:Erlang代码热替换以及Mnesia表结构修改
  9. Erlang redis library

敏捷开发

  1. Something about agility development
  2. 敏捷过程方法
  3. 如何提高敏捷开发效率
  4. Selenium进行web自动化测试
  5. Windows linux development environment settings
  6. 高效程序调试和测试方法
  7. 增强沟通技巧和提高开发效率
  8. 互联网产品交互设计需要遵循的几个原则

JAVA 开发

  1. Java程序优化过程及linux相关
  2. Java nio 实现的爬虫性能
  3. Add log4j for your servlet apps
  4. Using *.properties files in your servlet or webapp

其他

  1. How to watch the real time bandwidth in Linux
  2. 7 questions to ask yourself before starting the business online
  3. How to debug xmlrpc?
  4. Debug javascript or do automatic javascript testing
  5. 3 Tips of linux uses and 1 tip of Jquery
  6. 网站前端性能优化之CSS Sprite
  7. Some interesting stuff for you
  8. 创业团队4条箴言

关于2010年个人计划的完成

这是 2010年粗略计划:2010勇往直前 完成情况,其中灰色表示Archive。

1.增强关注领域

Marketing 完成

Finace 未完成

项目管理 完成50%

Social information 完成

Worldwide business 未完成

2.增强 时间管理 完成

3.习惯 快速做决定 完成50%

4.改善生活习惯 未完成

其他长期计划的完成

1.  领结婚证 完成

2. 贷款买房 完成

3. Body building 未完成

4. Improve my communicate skill 完成50%

5. More and more friends 未完成

从Google 搜索历史统计看

一月份到五月份第一个项目的架构逐渐稳定,只有大量的功能实施,迭代增加功能和debug的工作。所以搜索次数减少很多。

从六月份到十二月份,由于新项目的启动,需要很多研究分析,并且随着项目的展开,越来越多的问题需要解决,搜索次数逐渐增加。

工作日搜索量几乎没有很多差别,周六由于休息搜索量最低,周日稍增加。

从SVN统计看

在2010年,项目A提交次数接近1000次,项目B提交次数接近400次。

2011年目标

积极买对变化和挑战

1. 深入IOS , Android开发

2. 关注产品的网络营销

3. 项目1,2,3的开发和营销的0突破

4. Improve Algorithm and Linux C

5. 继续关注 Marketing, Social information, BI

6. 换一个拥有大数据量的环境?