2013年11月

[Laravel4] 使用command执行任务

场景

  1. 比如使用redis作为计数器,然后在每天的凌晨将计数保存到MySQL
  2. 比如定时执行一些批量的任务

扩展artisan的command

例子:批量将文字题目生成为图片

artisan command:make QuestionCommand
# app/command/QuestionCommand.php

生成php文件之后,首先需要修改name为你的命令

protected $name = 'question:gen';

getArguments, getOptions 分别为获取参数和选项的值

getArguments // command arg1 arg2
getOptions // command --opt1=val1 --opt2=val2
fire // 执行命令的时候具体要做的事情

较为完整的例子

<?php

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class QuestionCommand extends Command {

    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'question:gen';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '生成question图片';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function fire()
    {
        $questions = $this->option('all') ? Question::all() : Question::whereThumb(0)->get();
        // 初始化markdown解析引擎
        $ciconia = new Ciconia\Ciconia();
        $ciconia->addExtension(new Ciconia\Extension\Gfm\FencedCodeBlockExtension());
        $ciconia->addExtension(new Ciconia\Extension\Gfm\TaskListExtension());
        $ciconia->addExtension(new Ciconia\Extension\Gfm\InlineStyleExtension());
        $ciconia->addExtension(new Ciconia\Extension\Gfm\WhiteSpaceExtension());
        $ciconia->addExtension(new Ciconia\Extension\Gfm\TableExtension());
        foreach ($questions as $question) {
            $item = array(
                'id' => $question->id,
                'absPath' => public_path('upload/question/' . $question->id . '.png'),
                'relPath' => 'question/' . $question->id . '.png',
                'question' => $ciconia->render($question->question),
            );
            Queue::push('code2png', $item, 'code2png');
            $this->info('Question ' . $question->id . ' has been added to the queue.');
        }
    }

    /**
     * Get the console command arguments.
     *
     * @return array
     */
    protected function getArguments()
    {
        return array(
            // array('example', InputArgument::REQUIRED, 'An example argument.'),
        );
    }

    /**
     * Get the console command options.
     *
     * @return array
     */
    protected function getOptions()
    {
        return array(
            array('all', null, InputOption::VALUE_OPTIONAL, '全部重新生成', null),
        );
    }

}

需要在app/start/artisan.php里面添加下面一行才会生效

// 4.0
$artisan->add(new QuestionCommand);
// 4.1
Artisan::add(new QuestionCommand);

命令行中这样执行

artisan question:gen --all=1

如果要在Controller里面调用,可以这样

Artisan::call('question:gen', array('--all' => 1));

[Laravel4] 从数据库读取配置

再次验证时发现下面的代码有问题,有待修复

场景

很多时候我们的网站的配置都是存在数据库里面的,这样后台也方便配置修改。而Laravel4默认的是将数组直接写在配置文件里面的。

解决

Get Config from database 这个还不错,但是有个问题,就是不支持这样的$register = Config::get('site.register'); var_dump($register['invite']);类似于多级数组的意思

数据库里面直接这样写:

|       key       | value |
|-----------------|-------|
| register.invite |   1   |

我们改造一下

<?php
// app/config/site.php
class DBconfiguratorObject implements ArrayAccess, Serializable {
    protected $config = array();
    protected $table = null;

    private static $_instance = null;

    public static function instance($tableName = 'site'){
        if(self::$_instance === null){
            self::$_instance = new self($tableName);
        }
        return self::$_instance;
    }

    private function __construct($tableName = 'site'){
        $this->table = DB::table($tableName);
        $config = $this->table->lists('value', 'key');
        $this->config = array();
        foreach ($config as $key => $value) {
            array_set($this->config, $key, $value);
        }
    }

    public function offsetGet($key){
        // return $this->config[$key];
        return array_get($this->config, $key);
    }

    public function offsetSet($key, $value){
        if($this->offsetExists($key)){
            $this->table->where('key', $key)->update(array(
                'value' => $value
            ));
        } else {
            $this->table->insert(array(
                'key' => $key,
                'value' => $value
            ));
        }
        $this->config[$key] = $value;
    }

    public function offsetExists($key){
        return isset($this->config[$key]);
    }

    public function offsetUnset($key){
        unset($this->config[$key]);
        $this->table->where('key', $key)->delete();
    }

    public function serialize(){
        return serialize($this->config);
    }

    public function unserialize($serialized){
        $config = unserialize($serialized);
        foreach($config as $key => $value){
            $this[$key] = $value;
        }
    }

    public function toJson(){
        return json_encode($this->config);
    }
}

return DBconfiguratorObject::instance();

php正则匹配出错的问题

发现

因为用了laravel4的

Response::json(array())->setCallback('callback')

然后始终报

The callback name is not valid.

然后单独写了个test.php文件测试该语句在不同的机器上测下来的结果不一样。

var_dump(preg_match('/^[$_\p{L}][$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\x{200C}\x{200D}]*+$/u', 'sad'));

解决

本以为是系统哪边的设置的问题,于是将envphp -i的结果都比对了一遍,都一样。后来想到看一下pcre的版本是否一致pcre-config --version,结果都是6.6;最后没办法rmp -qa | grep pcre,好家伙最后的小版本不一样;再然后你懂的,升级了一下pcre就正常了。

附注

  • 有问题的版本: pcre-6.6-2.el5_1.7
  • 升级后的版本: pcre-6.6-9.el5

在CENTOS上编译pidgin-lwqq,配置qq机器人

首先,yum安装各种需要的东西,比如pidgin,以及后面需要用到的依赖包,其中dbus是用来通信的

yum install pidgin-devel finch screen dbus dbus-devel dbus-x11 libev-devel zlib-devel glibc-devel cmake sqlite-devel libcurl-devel

其次,通过git clone pidgin-lwqq来进行编译,详细的见https://github.com/xiehuc/pidgin-lwqq,不过这里需要注意两点:

  1. 切换到dev分支,不然没法编译,master分支需要依赖一个mozjs的东西,centos上可以搞,但是相当麻烦
  2. 因为是在一个没有图形界面的系统(比如VPS)上编译,需要将验证码输出到一个web可访问的路径去,否则登录不了。修改方法大致如下:
diff --git a/lib/login.c b/lib/login.c
index 8fe25c0..a1ae11b 100644
--- a/lib/login.c
+++ b/lib/login.c
@@ -141,6 +141,7 @@ static LwqqAsyncEvent* check_need_verify(LwqqClient *lc,const char* appid)
 static int request_captcha_back(LwqqHttpRequest* req,LwqqVerifyCode* code)
 {
     int err = 0;
+       FILE *captcha_fp;
     if(req->http_code!=200){
         err = -1;
         goto done;
@@ -148,6 +149,9 @@ static int request_captcha_back(LwqqHttpRequest* req,LwqqVerifyCode* code)
     LwqqClient* lc = req->lc;
     code->data = req->response;
     code->size = req->resp_len;
+       captcha_fp = fopen("/path/to/qqcaptcha.png", "wb");
+       fwrite(code->data, code->size, 1, captcha_fp);
+       fclose(captcha_fp);
     req->response = NULL;
     lwqq_call_action(lc,need_verify2)(lc,code);
 done:

然后,编译好之后,就可以先试试登录了,直接敲入finch试试,目测就能看到登录界面了
1.png

选择webqq进行登录,使用快捷键还是很方便的,不会使用可以man一下

下面就是机器人部分了,pidgin支持dbus通信,于是我们可以用利用dbus的接口来监听qq的消息,并作出回应,抑或是主动发出消息。要使用dbus,那么finch需要运行在screen下,这也是前面安装screen的原因,简单点,在命令行里敲入

dbus-launch screen

即可启动dbus,并进入screen,然后在screen下运行finch即可。友情提示,某些terminal里,screen下的finch排版混乱,至今没找到解决方案。另外,screen的使用方法可以man一下,你的通信脚本也必须运行在screen里。

dbus通信可以用多个语言实现,c、python、php等等都可以,个人熟悉php,因此使用php进行编写机器人脚本。首先安装php扩展dbus:

pecl install dbus-0.1.1

装完记得在php.ini里加入dbus的配置

[dbus]
extension=dbus.so

网上能找到很多示例代码,比如PHP官网就有很多example:传送门在此

下面贴个简单自动重复消息的机器人代码

<?php

Robot::getInstance()->run(200);
/**
 * class Robot
 * @author Baiqiang Dong<qiyuuu@gmail.com>
 */
class Robot {

    protected $interface = 'im.pidgin.purple.PurpleInterface';
    protected $signals = array(
        'ReceivedImMsg',
        'ReceivedChatMsg',
    );

    private static $_instance;
    private $_dbus;
    private $_proxy;

    public static function getInstance() {
        if (self::$_instance === null) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    private function __construct() {
        //connect to dbus
        $this->_dbus = new IDBus(DBus::BUS_SESSION);
        $this->_proxy = $this->_dbus->createProxy(
            'im.pidgin.purple.PurpleService',
            '/im/pidgin/purple/PurpleObject',
            'im.pidgin.purple.PurpleInterface'
        );
        //listen to specified signals
        foreach ($this->signals as $signal) {
            $this->_dbus->addSignalReceiver(array($this, lcfirst($signal)), $this->interface, $signal);
        }
    }

    public function receivedImMsg($signal) {
        $this->responseMessage($signal);
    }

    public function receivedChatMsg($signal) {
        $this->responseMessage($signal);
    }

    public function responseMessage($signal) {
        list($receiver, $sender, $message, $conversation, $flags) = $signal->getData()->getData();
        //send what you received
        $this->sendMessage($conversation, $message, $sender);
    }

    public function sendMessage($conversation, $message) {
        $proxy = $this->_proxy;
        try {
            $type = $proxy->PurpleConversationGetType($conversation);
            switch ($type) {
                //im
                case 1:
                    $im = $proxy->PurpleConvIm($conversation);
                    $proxy->PurpleConvImSend($im, $message);
                    break;
                //chat
                case 2:
                    $chat = $proxy->PurpleConvChat($conversation);
                    $proxy->PurpleConvChatSend($chat, $message);
                    break;
                
                default:
                    # code...
                    break;
            }
        } catch (Exception $e) {
            echo $e->getMessage(), "\n";
            return false;
        }
        return true;
    }

    public function run($time = 1000) {
        $this->_dbus->mainLoop($time);
    }

}

/**
 * extend the DBus class to implement methods like mainLoop and addSignalReceiver
 */
class IDBus extends DBus {
    private $_signalReceivers = array();

    public function __construct($type) {
        parent::__construct($type);
    }

    public function addSignalReceiver($callback, $interface, $signal) {
        if (!is_callable($callback)) {
            return;
        }
        if (!isset($this->_signalReceivers[$interface])) {
            $this->addWatch($interface);
        }
        $this->_signalReceivers[$interface][$signal] = $callback;
    }

    public function mainLoop($time) {
        while (true) {
            $signal = parent::waitLoop($time);
            $called = false;
            if ($signal instanceof DbusSignal) {
                foreach ($this->_signalReceivers as $interface=>$callbacks) {
                    foreach ($callbacks as $method=>$callback) {
                        if ($signal->matches($interface, $method) && is_callable($callback)) {
                            $called = call_user_func($callback, $signal);
                            //break to main loop if callback return true
                            if ($called) {
                                break 2;
                            }
                        }
                    }
                }
            }
        }
        return $signal;
    }
}