Archive for八月, 2005

说说Blog中的Trackback(引用)功能 (zz )

//这篇文章不错,帖一下。

什么是Trackback呢?

简单说来,Trackback是网站与网站之间互相通告的一种方法。它能让甲对乙说:“这篇文章可能是你感兴趣的”,要实现这一动作,甲向乙发送一个Trackback Ping(引用通告)即可。在这种情况下,一个ping就是一条从一个web服务器发送到另一个服务器的短消息(a small message)。

Trackback Ping是由Moveable Type发明的规范,他们说Trackback是”a framework for peer-to-peer communication and notifications between web sites”。这里是他们的Trackback技术规范文档:http://www.movabletype.org/docs/mttrackback.html

举个例子来说明一下。比如我在鱼头的Blog中看了一篇我感兴趣的文章,对这篇文章我自己有一些看法,如果按照传统BBS的做法,我需要在鱼头这篇文章下发表自己的评论,但这样做的话我的文字只能存在于鱼头的网站上,我无法再维护自己的这篇评论。另一个情况是我在自己的Blog中也在写一篇相类似的文章,我希望鱼头也能来看一看我的这篇,传统BBS的习惯还是得到鱼头的文章下发一篇回复,把我的URL贴过去。

但有了Trackback Ping,我们的Blog不再需要这样做。

Blog跟BBS有点重要的不同,Blog是一种个人创作,用Blog来发表自己的东西,保存自己的东西,即使是对他人Blog文章的评论也要在自己的Blog中永久保留下来。通过Trackbak,我就可以在自己的Blog中发表文章,同时把自己这篇文章的URL地址Ping到鱼头的那篇文章上去。这样,所有阅读鱼头文章的人也能通过Trackback顺藤摸瓜地来我的Blog看我的文章。

所以,当我们的Blog有了Trackback Ping功能,那么谁都可以通过Trackback Ping来发表意见和评论了。这样,多家Blog网站就通过相关话题而联接起来。各种评论在Internet上相互连接而织成一张大网。因此,可以说,Trackback创造出了Blog与BBS、Diary完全不同的文化,Blog的世界通过Trackback而变成真正的无限互连。

作为Blogger,让我们习惯于这样讨论问题和做评论吧:把内容写在自己的Blog里面,Trackback Ping到别人的Blog。这个概念是论坛模式里从来没有的,称为Remote Commenting。

如何在Blog中实现Trackback?

在有Trackback功能的Blog系统中,每篇Blog文章都有两个URL,一个是要访问这篇文章所使用的URL,另一个就是Trackback Ping URL(引用通告地址),它是用来接受来自其他Blog网站Trackback Ping的程序。当我发表文章的时候,想要通知鱼头的话,只需要把鱼头那篇文章的Trackback Ping URL贴到我这边文章中来,我提交文章时,系统就会按照这个URL发送一个Ping给鱼头的那篇文章。而鱼头则将在自己的文章下看到类似这样的一个引用通告:

标题: Joyhero的文章
来自: Joyhero的Blog
摘要: Joyhero的文章内容摘要…
地址: http://joyhero的文章地址

这个Trackback Ping是通过标准的HTTP协议从我的Blog发送到鱼头的Blog的,我的Blog发送一个POST格式的HTTP请求到鱼头那篇文章的Trackback Ping URL。这个请求的content type是application/x-www-form-URLencoded,它大概是这个样子的:

POST http://鱼头文章的TrackbackPingURL
Content-Type: application/x-www-form-URLencoded
title=Joyhero的文章&url=http://Joyhero的文章地址&excerpt=Joyhero的文章摘要&blog_name=Joyhero的Blog

早期版本的Trackback规范中,Ping是GET方式的HTTP请求,现在不再支持GET方式,只能用POST方式。参数包括:

title – 文章的标题
excerpt – 文章的摘要。在Movable Type系统中,如果摘录信息超过255个字符将会被截断为252个字符,并在后面增加…三个字符
url – 文章的永久连接。象其它永久连接一样,这个连接应可能准确地在页面中定位文章的入口,因有疑问时这个链接会用到
blog_name – 发表文章的blog的名称
在上述的参数中只有url是必须的。如果title没有提供,url的值将被用作标题。

当我的Blog发出这个Trackback Ping后,将接收一个简单的XML格式应答,如果Ping成功,那么应答的格式如下:

0

而失败应答的格式为:

1
The error message

Comments (1)

Smarty vs. XML/XSLT

//摘要: 此文将 xml/xslt 与传统的php presentation 引擎作了一番比较,作者是偏向于 xml/xslt
//smarty 的语法其实比 xslt 要简单地多,它有cache 的功能,执行效率比xslt高,当然如果xslt由客户端执行
//就是另外一回事了,xslt正在标准化的过程中,xml/xslt 在应用似乎还不是很多,但在可预见的将来,
//xml/xslt 肯定会是主流。

by Sergey Makogon 11/10/03 Rating:

Synopsis:

The article gives a very short comparison of currently most used ways to separate data and its representation in PHP solutions. We compared Smarty and XML as two competitors and most often used methods to ease the process of code development and design implementation for PHP projects.

The Article

There are many ways to separate business-logic and data presentation of an Internet solution in PHP. But the most popular one is usage of templates. The idea behind the method is to use some kind of “engine” that binds visual data representation with the data of a page. There are many advantages in using templates:

• Ease of design change
• Ease of interface localization
• Possibility to work separately on design and code by different people at one and the same time
• Etc.

But the goal of this article is not to make templates even more popular. We want to briefly compare two of the most popular methods of separating data and its presentation – templates and XML/XSLT. We will use Smarty as a representative of the template method.

Smarty is a PHP class that can be used either as an extension or with the use of “include”:

As Smarty authors say, Smarty is a “template compiler”. And what exactly is this Smarty’s “compilation”? If we carefully study the files produces by Smarty (it needs a folder with permissions to write files into it), we will notice Smarty’s compilation is a simple replacement of the {$var} variables into PHP code like:

.

Actually, the replacement operation is done only once, when Smarty finds changes in the original template file. This template engine uses the “compiled” template file to generate pages after the initial replacement. Smarty uses caching (which can be turned off) to increase performance of results generation.

A developer basically needs to study a new language (another one :) ) to use Smarty in a project.
The Smarty “language” is carefully described in a PDF documentation of around 150 pages.

In around 80% of cases, developers use only a few standard operators and functions out of the huge amount of ones offered by the “engine”.

One should use the “assign” method to send the value of a variable to the template:

$oSmarty = new Smarty();
$oSmarty->assign(“article”, ”Article”);
$oSmarty ->display(“template.html”);

The “display” method generates a page with data based on the “template.html” template file.

And to show the variable, one should use the following code in the right place of the template:

Some code

{$article}

Some code

One should use “assign” to display more than one variable:

$oSmarty = new Smarty();
$oSmarty->assign(
“var1″=>”Article”,
“var2″=>”Article2”,
“var3″=>”Article3”,
);

And the template file should contain the following code:

Some code

{$var1}

Some code

{$var2}

Some code

{$var3}

Some code

One should use associative arrays for complex data structures:

$oSmarty = new Smarty();
$article = array(
“key1”=>”value1”,
“key2”=>”value2”,
“key3”=>”value3”
)
$oSmarty->assign(“article”, $article);

And the following code in the template file:

Some code

{$ article.key1}

Some code

{$ article.key2}

Some code

{$ article.key3}

Some code

Smarty supports conditional constructions “if…else” in the following way:

{if $var >0 }
Some code
{else}
Some code
{/if}

One should use the “section” method to display repetitive data blocks:

{section name=article loop=$articles}
{$articles[article].title}
{/section}

We will stop dwelling on Smarty now. And we can recommend reading the documentation of Smarty (you can find one with the distributive) to those interested in the engine.

Basically, Smarty is a jumble of PHP code and HTML tags. It only slightly eases developers’ work. The “dirty” work to place the PHP code into template is done by the parser. In exchange for the dirty work done, the developer has to study a new, non-standard “programming” language. And though the language is powerful enough, it still has its own limitations, as any other language has.

Let us look at the major Smarty competitor – XML/XSLT.

XML/XSLT is a specification of a language, offered by the W3C. As a result, XML/XSLT is supported by a much huger amount of parsers, than by one (Smarty), even very good one.

That is why, XML/XSLT support in PHP is possible with different parsers. We will take Sablotron parser as one of the representatives of many parsers for XML/XSLT for our example.
But there are other ones. And as soon as there is another parser (e.g., a faster than Sablotron one) that me might like more, we can easily switch to the new parser without any need in studying a new language and modifying the code for new functions or operators. We will simply have to change a few instructions that call the parser. Due to the “standardization”, one can create platform-independent templates for porting an application to another development language (e.g., from PHP to Java, or ASP.Net).

There is no need to show the XML/XSLT specification in this article. Anyone can access it from the W3C web site. The only thing that we can mention is that the parser of our choice – Sablotron – does not support the specification fully nowadays. However, a new release of Sablotron or another parser can easily support the specification better.

It is not more difficult to use XML/XSLT than to use Smarty, as typically one uses only a few typical constructions to develop new applications.

One should install the Sablotron parser on the server and compile the PHP with the –enable-xslt –with-xslt-sablot options.

Here is a simple example of XML/XSLT work in PHP with the usage of “XSLTransformer” class:

getOutput();

$t->destructXSLTansform();
?>

The ”test.xml” file contains the following data:

Jack’s CV
Jack has 4 years experience teaching in university and 3 years in the industry. He has interest in distributed and parallel computing and parallel processing.

The ”test.xsl” file contains the following code:

One can use dynamically generated data instead of files:

Jack’s CV
Jack has 4 years experience teaching in university and 3 years in the industry. He has interest in distributed and parallel computing and parallel processing.

”;

$xsl=”

”;

$arguments = array(
‘/_xml’ => $xml,
‘/_xsl’ => $xsl
);

// Allocate a new XSLT processor
$xh = xslt_create();

// Process the document
$result = xslt_process($xh, ‘arg:/_xml’, ‘arg:/_xsl’, NULL, $arguments);
if ($result)
{
print $result;
}
else
{
print “Sorry, xml could not be transformed xsl into”;
print ” the \$result variable the reason is that ” . xslt_error($xh) .
print ” and the error code is ” . xslt_errno($xh);
}
xslt_free($xh);
?>

The major draw-backs of XML/XSLT code and appearance separation via Sablotron are slow speed of transformation and a need to re-compile PHP to support the parser. However, usage of some other parser can eliminate these draw-backs.

As a conclusion, we want to tell that it is basically a matter of preference on whether to use Smarty (or any other template “engine”) or XML/XSLT, as both the two major methods have their pros and cons. In some cases, it is better to use Smarty, especially if you do not have full access to your server and cannot compile XSLT support into PHP. In many other cases, the power and flexibility of XML/XSLT beats Smarty (and other dozens of currently available template “engines”) easily in most of the aspects of its usage.

评论

过几天就要回家了

前几天一直在改 blog,改到最后终于认输了。我得承认, web 方面我没天份。
看着那些乱七八zao的代码,头都大了,修改又很麻烦,下载,本地修改,再上传,测试,出问题,再改。
还是干脆用旧的界面吧,虽然难看了点,但也很朴素和稳定啊。呵呵。

找到了 plog 的源代码,据说博客中国就是使用 plog 架设的。 当初修改 vsftpd 源代码的那份劲头又上来了。 昨天花了点时间看了下 plog 部分源代码,感觉就是不一样,所有功能都用类封装,每个类及其函数变量都有完备的注释,不得不再次叹服老外做开源项目的水平。

过几天就要回家了,家里催的实在太紧 。

评论

Next entries »