手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表分类:PHP

Yii的又一个BUG?

先申明一下,这个可能不是BUG。只是算起来是实现的方式不一样而已。
场景:我有一个MYSQL数据库,但是现在容量越来越大,主要是因为其中有大字段,多个大字段,所以在查询的时候会特别特别慢。所以想到用mongo来存储一些查询用的结构。单条的时候,我还是准备采用MYSQL。当然其实单条用mongo也合适。不过因为迁移数据有点麻烦,所以还是先忍忍,一步一步来。

测试阶段:因为MYSQL to MONGO有点小麻烦,没有现成的工具。官方说有mongoimport,我看过了,确实OK,但是只支持一些基础的结构,比如:json,csv等。其他的则需要第三方工具。但我自己先在测试中,所以就直接写了个脚本,先将数据用AR取出来。然后插入到mongo中。结果发现,在排序的时候。。9999比10000还大。问了一下神仙 ,他说这应该是按字符串排序的方式来做的。

于是做了一个测试,写进了一个数字,果然就对了。。难道是yii的AR的BUG?想想这不太应该啊。于是我写了一个小的Demo:

PHP代码
  1. $db = mysql_connect("localhost","root","123456");  
  2. mysql_select_db("feed");  
  3. $query = mysql_query("select id from feeds_group limit 1");  
  4. while($rs = mysql_fetch_array($query)){  
  5.     $result = $rs;  
  6.     echo "<pre>";  
  7.     var_dump($result);  
  8.     echo "</pre>";  
  9. }  
  10. echo "<hr />";  
  11. $dsn = 'mysql:host=localhost;dbname=feed';  
  12. $user = 'root';  
  13. $password = '123456';  
  14. $dbh = new PDO($dsn$user$password);  
  15. $sth = $dbh->prepare("select id from feeds_group limit 1");  
  16. $sth->execute();  
  17. $result = $sth->fetchAll();  
  18. echo "<pre>";  
  19. var_dump($result);  
  20. echo "</pre>";  

打印出来一看:

XML/HTML代码
  1. array(2) {  
  2.   [0]=>  
  3.   string(1) "1"  
  4.   ["id"]=>  
  5.   string(1) "1"  
  6. }  
  7.   
  8. array(1) {  
  9.   [0]=>  
  10.   array(2) {  
  11.     ["id"]=>  
  12.     string(1) "1"  
  13.     [0]=>  
  14.     string(1) "1"  
  15.   }  
  16. }  

 

果然出来就是字符串了,最后只能自己在插到mongo的时候写了一个小函数,对于数值型字段做了转换,问题就这样解决了。

 

 

 

 

Tags: yii, mongodb, mysql, string

HtmlPurifier的一个小问题:处理flash

在没有TIdy库的情况下,修复HTML我们是用htmlPurifier类库,但这个类库有点小问题。。。比如,对flash支持不好,虽然官网上有例子,可以支持yotube,不过,国内的flash视频站很多,我们不可能一一写扩展啦。怎么办?

后来有人说:

PHP代码
  1. $config->set('HTML.SafeObject', true);  
  2. $config->set('Output.FlashCompat', true);  
  3. $config->set('HTML.Allowed''object[width|height|data],param[name|value],embed[src|type|allowscriptaccess|allowfullscreen|width|height]');  

但经过测试,发现这样也不行。。不信?你去官网的Demo.php中测试一下就知道了。

测试代码可以是:
http://htmlpurifier.org/demo.php?filter[HTML.SafeObject]=1&filter[Output.FlashCompat]=1&filter[HTML.Allowed]=object[width|height|data],param[name|value],embed[src|type|allowscriptaccess|allowfullscreen|width|height]&html=%3Cp%3E%3C%2Fp%3E%3Cp%3E%3Cembed+src%3D%22http%3A%2F%2Fplayer.youku.com%2Fplayer.php%2Fsid%2FXMzIwMjQ5NTk2%2Fv.swf%22+allowfullscreen%3D%22true%22+quality%3D%22high%22+width%3D%22480%22+height%3D%22400%22+align%3D%22middle%22+allowscriptaccess%3D%22always%22+type%3D%22application%2Fx-shockwave-flash%22%2F%3E%3C%2Fp%3E&submit=Submit

所以。如果stripos($data,"embed")!==false的话,还是不要用htmlpurifier过滤了。

参考:
1、http://htmlpurifier.org/docs/enduser-youtube.html(文中有为什么不支持embed的说明)

  1. <embed> is not recognized by W3C, so if you want standards-compliant code, you'll have to get rid of it.
  2. The code is exactly the same for all instances, except for the identifier AyPzM5WK8ys which tells us which movie file to retrieve.

2、http://stackoverflow.com/questions/1368809/allow-embed-object-param-html-tags-with-htmlpurifier(即使设置了allowed,还是不行)
3、http://htmlpurifier.org/phorum/read.php?5,2239,page=3 (在这里,也还是没有解决。寄希望于下一版了。。。)

Tags: htmlpurifier

PHPer下一步应该学go?

其实看到现在,很多人都在推荐go语言,理由有很多种,但对于PHPer来说,不一定就是适合的。go是google推出的语言,到现在为止也已经有很长时间了,再加上go又能底层,又能WEB,所以。。。
但是,究竟应该不应该学,还是会有点想法,所以看到这篇全英文的介绍的时候就蛋定不了了。
原文是来自于:Why you PHP guys should learn Golang

Go or Golang, is a open source, community supported, fast, consistent, scalable, productive language, and conceived by Google. More and more applications were build with it. Although Rob Pike has said that “… we expected C++ programmers to see Go as an alternative …”, but I really think of that: You PHP guys should learn Golang! Let’s talk about WHY.

 

Easy to learn

PHP is easy to learn. Golang is as easy as PHP!

The syntax of PHP is similar to C family’s programming languages(C/C++/Java etc.). If you have some experiences with those languages, it is not too hard to write some codes when you first met PHP.

Golang is also a C family’s programming language. Eh… with a litte syntax difference. For example, the keyword “for” is similar with PHP’s, but without the pair of parentheses. And the condition keyword “if” is without the pair of parentheses either. You can read Effective Go for more things.

Golang has only 30 keywords and 47 operators, delimiters or other special tokens. There is no need to work hard for remembering all the literals. The type system is exquisite and easy to use. No huge OO but a practical struct type with methods on it. The interface design is one of my favorate parts of Golang. When you have finished this A Tour of Go, based on your PHP experiences, some jobs could be done with Golang immediately.

Easy to Use

Your PHP scripts will be evaluated by SAPI components: web server module, PHP-fpm or CLI. All needed for PHP deploying is a SAPI environment. Deployment of the environment is the most difficult part to understand for a PHP beginner.

All the Golang’s codes will be compiled and linked into native code. So there is no need to deploy a environment for it executing but a build environment. Compaire with deployment of the PHP environment, it is not so much complex. Do you really think of the deployment of the PHP environment is really complex? I don’t think so. Really!

To deploy the build environment, just need 3 steps:

  • Download Golang‘s source code.
  • Setup the environment variables. Here’s a great blog post: Go environment setup
  • Run the ‘all.bash’ in the ‘src’ directory.

Or only one step: Use the binary package to setup.

Then a toolset called “go” can be used. Using the go tool is as easy as using PHP’s CLI tool. “The Go Tool” has already explained it.

A myth of PHP

If a programming language is easy to learn and use, shall we learn and use it? Lots of programming languages are easy to learning and use. Shall we learn and use all of them? The answer is straightforward: NO!

But why you PHP guys should learn Golang? Just because it is cool! Ja, I’m kidding, but it is true. Anyway, let’s talk about PHP itself first.

PHP is “a general-purpose server-side scripting language originally designed for Web development to produce dynamic Web pages.(Wikipedia)”. The most important feature of PHP is embedding with HTML. PHP code write in “<?php … ?>” tag; HTML write outside. It also has a powerful extension system. The extension can be writen by C with Zend API. Data processing was actually done by those extensions. IMO, PHP must be the best template programming language.

But if you have years experiences with PHP and began face to some complex web application. You must have already felt some powerlessness to PHP. It dosen’t have concurrent mechanism build-in, no threads, multiprocessing or X-routines. A slow data source will be blocked entire page’s processing. A powerful web server in the front of PHP is always needed. Backend programes were writen to process data. Message Queue, Cache, Proxy … The system is not only about PHP, but also about a lot of services and system components. This time, PHP is really be a template language with few logical jobs.

PHPers are always looking for solutions, like “PHP multithread” or a concurrent RPC framework for PHP. I could not say witch one is better. But I’m sure that you must choose an other programming language for the backend job. In my own case, I tried C(struggle with malloc/free)/Java(stumble in the jar hell)/Python(never be pythonic but type lost)..If I get profermance, I must struggle with the memory management; If I use GC, I must deploy and turning VM; If I get convenient, careless would kill me….Eche has advantages, each also has a problem.

OK! Let’s back to Golang now!

Golang has GC, there is no need to care about memory management(or could pay less attention to it). And the code are compiled into native machine code. So, “cp” and “mv” are all needed for deploying the application writen by Golang. Oh, as I just said, Golang is a compiled language with a static type system. You have no chance to confuse veriable types. Of course, one of the most important thing is PHPer who moves to Golang “don’t have to surrender much expressiveness, but gain performance and get to play with concurrency(by Rob Pike)”. “Why Not Go?” talked about this deeply.

I would show parts of my own experience: a Gearman worker works for us to process backend data. PHP connected to the worker though Gearman Job Server with its Gearman API. The original worker was writen by python. There are lots of problems with that version(They are our own problems, but python’s). But it works for us. Then we rewrite the worker with Golang. It was amazing. I wrote a Gearman API for Golang, and a Golang package using Zend API(cgo is another amazing thing) for evaluating the PHP script in Golang. Then combined them together – A Gearman worker witch can execute PHP scripts. It works about 3 mounth in our production enverionment and looks great! Oh, I also wrote a RPC-combinator for combining the RPC call in PHP script. It will be used in our next product.

Everything goes right. Thanks Golang!

In any case, most of PHPers alwasy need to learn other languages for the backend development. If you have began looking for, or already tasted some of other languages. Why don’t you try Golang? It would make your life happy and easy. You could spend more time with your family and friends, eat your favorite foods and visit the places you love.

That’s why you PHP guys should learn Golang! Is it enough? I think so!

This is my writing exercise. If you find any grammatical or spelling error, plz tell me!

---EOF---

其实我也挺纠结的,要知道当时我刚跟着tour做了30多篇的时候,突然golang的网页就再也打不开了。再苦了,墙就是我们学习的最大障碍。想想这些年我们遇到了什么。。
sf.net被墙
groups.google.com被墙
python.org被墙
很多。。。。。这些都是我们的障碍啊

LAMP练级指南

亲,玩过大波罗没。玩过魔兽没,那么应该了解技能树的一些特征吧。
当一定的等级达到的时候,可以新开一个技能树。然后已经点亮的技能树可能还要升级,因为前面的技能树也可能会需要初级技能的升级版。

说的有点乱了。NND,直接复制一份百科的技能树的说明算了:(靠,百科居然没有。。。。连知识树也没有,只能用企业知识树模型来代替了):

XML/HTML代码
  1. 企业知识树模型本质上就是一个层级式知识图,它表达了为实现某一组织目标的所有相关组织知识间的因果关系或从属关系。层级式知识图由于具有对称性、良好图形、连续性等特点,而得到广泛的应用。企业知识树模型在层级式知识图的基础上拓展了更多的管理功能。考虑一个为完成某一组织目标的知识构成的层次知识结构。它是若干个关联的知识结点集K,知识结点间的关系集r,构成了一个关系集R。  

意思差不多。。。
下面的文章就是来自于对LAMP技能的知识树的的提炼。您看好了:(http://blog.cunsheng.me/?p=42)

俗话说温故而知新,也正好培训需要,我最近也整理了一张LAMP程序员技能树,相比我所见过的技能树Flash开发者,这张图有以下特点:

  1. 由于同一门知识在不同阶段有不同的要求,为了具有更好的参考性,技能树将所有知识和技能点按1、初中级(工作1-2年)2、中高级(工作3-4年)和3、高级(工作3年+)分成三级, 但除了网络与架构外其余的知识和技能点都只罗列到了第二级,原因是大多知识和技能到了一定程度后,都着眼于非常具体、非常细节的问题,很难全面罗列,也可 能也因为我自身有限的眼界,至于网络与架构有第三级倒不是因为这些知识有多高深,而是相比其余知识,这些知识可以晚一些再掌握
  2. 知识点附带资料与书籍推荐,蓝勾表示有网页资料,绿手表示有书籍推荐(查看flash版可直接链接到资料页),毕竟每个知识点只是简单几个字,人背景不同理解也不同,对着资料或参考书籍来看能更好的表达我的意图,同时也更能帮助到所需之人
  3. 全面性是这棵技能树的主要考虑之一,但不得不承认要完全掌握所有内容是非常不容易的事情,有些理想化,现实中往往还要考虑如你上级的要求、公司的需要等实际因素,能作为checklist发现问题,能让读者更了解自己的能力现状,那么制作技能树的目的也就达到了

由于技能树涉及到多种编程语言,所以把语言的共通部分以及编程基础单独提炼了出来:

http://cunsheng-wordpress.stor.sinaapp.com/uploads/2012/08/LAMPer%E6%8A%80%E8%83%BD%E6%A0%91.swf

------

值得一观啊

 

在新浪SAE上使用htmlpurifier

修复HTML页面,以前一直用的是tidy庫,但SAE上其實是無法使用的

所以,我選擇了htmlPurifier庫,不過如果直接使用的話,是會報錯的,因為HtmlPurifier默認在修復頁面的時候會使用緩存,這樣可以加快速度。因此代碼就變成了這樣

PHP代码
  1. $htmlPurifier = new HTMLPurifier(array(  
  2.         'AutoFormat.AutoParagraph' => TRUE,  
  3.         'HTML.TidyLevel' => 'medium',  
  4.         'Cache.DefinitionImpl' => NULL,  
  5.     ));  

本来,我是将Cache.DefinitionImpl设置为SAE_TMP_PATH的,但HTMLPurifier中会chmod这个目录的权限,而tmppath是无法设置权限的,因此我干脆设置为NULL。

这样一来,问题就解决了。哈哈

Tags: sae, tidy