登录 |

OpenKM安装

2013年05月29日 下午 06:01 | 作者:

# install software
apt-get install ttf-wqy-microhei
apt-get install libreoffice-core
apt-get install imagemagick

#download
http://sourceforge.net/projects/openkm/files/
#install to /opt/openkm-6.2.3-community/
./openkm-6.2.3-community-linux-x64-installer.run
# login
user = okmAdmin
pass = admin

# administration - Config
system.swftools.pdf2swf  = /opt/openkm-6.2.3-community/tomcat/bin/pdf2swf -T 9 -f ${fileIn} -o ${fileOut}
system.openoffice.path     = /usr/lib/libreoffice
system.imagemagick.convert = /usr/bin/convert

# start on boot
vi /etc/rc.local
/opt/openkm-6.2.3-community/tomcat/bin/startup.sh

# shutdown
/opt/openkm-6.2.3-community/tomcat/bin/shutdown.sh

#log
/opt/openkm-6.2.3-community/tomcat/logs/catalina.log

PHP实现crontab

2013年05月6日 下午 34:39 | 作者:

1. 准备一个标准crontab文件 ./crontab

# m h dom mon dow command
* * * * * date > /tmp/cron.date.run

2. crontab -e 将此cron.php脚本加入系统cron

* * * * * /usr/bin/php cron.php

3. cron.php 源码

// 从./crontab读取cron项,也可以从其他持久存储(mysql、redis)读取
$crontab = file('./crontab');
$now = $_SERVER['REQUEST_TIME'];

foreach ( $crontab as $cron ) {
	$slices = preg_split("/[\s]+/", $cron, 6);
	if( count($slices) !== 6 ) continue;
	
	$cmd       = array_pop($slices);
	$cron_time = implode(' ', $slices);
	$next_time = Crontab::parse($cron_time, $now);
	if ( $next_time !== $now ) continue;	

	$pid = pcntl_fork();
	if ($pid == -1) {
		die('could not fork');
	} else if ($pid) {
		// we are the parent
		pcntl_wait($status, WNOHANG); //Protect against Zombie children
	} else {
	     // we are the child
		`$cmd`;
		exit;
	}
}

/* https://github.com/jkonieczny/PHP-Crontab */
class Crontab {
   /**
	* Finds next execution time(stamp) parsin crontab syntax,
	* after given starting timestamp (or current time if ommited)
	*
	* @param string $_cron_string:
	*
	* 0 1 2 3 4
	* * * * * *
	* - - - - -
	* | | | | |
	* | | | | +----- day of week (0 - 6) (Sunday=0)
	* | | | +------- month (1 - 12)
	* | | +--------- day of month (1 - 31)
	* | +----------- hour (0 - 23)
	* +------------- min (0 - 59)
	* @param int $_after_timestamp timestamp [default=current timestamp]
	* @return int unix timestamp - next execution time will be greater
	* than given timestamp (defaults to the current timestamp)
	* @throws InvalidArgumentException
	*/
    public static function parse($_cron_string,$_after_timestamp=null)
    {
        if(!preg_match('/^((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+((\*(\/[0-9]+)?)|[0-9\-\,\/]+)$/i',trim($_cron_string))){
            throw new InvalidArgumentException("Invalid cron string: ".$_cron_string);
        }
        if($_after_timestamp && !is_numeric($_after_timestamp)){
            throw new InvalidArgumentException("\$_after_timestamp must be a valid unix timestamp ($_after_timestamp given)");
        }
        $cron = preg_split("/[\s]+/i",trim($_cron_string));
        $start = empty($_after_timestamp)?time():$_after_timestamp;

        $date = array( 'minutes' =>self::_parseCronNumbers($cron[0],0,59),
                            'hours' =>self::_parseCronNumbers($cron[1],0,23),
                            'dom' =>self::_parseCronNumbers($cron[2],1,31),
                            'month' =>self::_parseCronNumbers($cron[3],1,12),
                            'dow' =>self::_parseCronNumbers($cron[4],0,6),
                        );
        // limited to time()+366 - no need to check more than 1year ahead
        for($i=0;$i<=60*60*24*366;$i+=60){
            if( in_array(intval(date('j',$start+$i)),$date['dom']) &&
                in_array(intval(date('n',$start+$i)),$date['month']) &&
                in_array(intval(date('w',$start+$i)),$date['dow']) &&
                in_array(intval(date('G',$start+$i)),$date['hours']) &&
                in_array(intval(date('i',$start+$i)),$date['minutes'])

                ){
                    return $start+$i;
            }
        }
        return null;
    }
	
    /**
	* get a single cron style notation and parse it into numeric value
	*
	* @param string $s cron string element
	* @param int $min minimum possible value
	* @param int $max maximum possible value
	* @return int parsed number
	*/
    protected static function _parseCronNumbers($s,$min,$max)
    {
        $result = array();

        $v = explode(',',$s);
        foreach($v as $vv){
            $vvv = explode('/',$vv);
            $step = empty($vvv[1])?1:$vvv[1];
            $vvvv = explode('-',$vvv[0]);
            $_min = count($vvvv)==2?$vvvv[0]:($vvv[0]=='*'?$min:$vvv[0]);
            $_max = count($vvvv)==2?$vvvv[1]:($vvv[0]=='*'?$max:$vvv[0]);

            for($i=$_min;$i<=$_max;$i+=$step){
                $result[$i]=intval($i);
            }
        }
        ksort($result);
        return $result;
    }
}

Go练习(A Tour of Go)答案

2013年05月3日 上午 36:33 | 作者:

Sqrt
用牛顿法实现开方函数。

package main

import (
    "fmt"
    "math"
)

func Sqrt(x float64) float64 {
    z := 1.0
    z_last := 1.0
    z_limit := 0.0000001
    for {
        z = (z+x/z)/2
        if math.Abs(z_last - z) < z_limit {
            break
        }
        z_last = z
    }
    return z
}

func main() {
    fmt.Println(Sqrt(5)==math.Sqrt(5))
}

Map
实现 `WordCount`。它应当返回一个含有 s 中每个 “词” 个数的 map。函数 wc.Test 针对这个函数执行一个测试用例,并打印成功或者失败。

package main

import (
    "code.google.com/p/go-tour/wc"
    "strings"
)

func WordCount(s string) map[string]int {
    m := make(map[string]int)
    words := strings.Fields(s)
    var word string
    
    for _,word = range(words) {
        m[word]++
    }
    
    return m
}

func main() {
    wc.Test(WordCount)
}

Fibonacci
实现一个 fibonacci 函数,返回一个函数(一个闭包)可以返回连续的斐波纳契数。

package main

import "fmt"

// 使用数组
func fibonacci() func() int {
    n := 0
    last := []int{1,1}
    return func () int {
        n++
        i := n%2
        if n > 2 {
            last[i] = last[0]+last[1]
        }
        
        return last[i]
    }
}
// 使用a/b两个变量
func fibonacci2() func() int {
    a, b := 0, 1
    return func () int {
        a = a+b
        b = a-b
        return a
    }
}

func main() {
    f := fibonacci()
    for i := 0; i < 10; i++ {
        fmt.Println(f())
    }
}

错误
从之前的练习中复制 Sqrt 函数,并修改使其返回 error 值。

package main

import (
    "fmt"
    "math"
)

type ErrNegativeSqrt float64

func (e ErrNegativeSqrt) Error() string {
	return fmt.Sprintf("cantnot Sqrt negative number: %v", float64(e))
}

func Sqrt(x float64) (float64, error) {
    if x < 0 {
        err := ErrNegativeSqrt(x)
        return 0, err
    }
    
    return math.Sqrt(x), nil
}

func main() {
    fmt.Println(Sqrt(2))
    fmt.Println(Sqrt(-2))
}

HTTP 处理
实现下面的类型,并在其上定义 ServeHTTP 方法。在 web 服务器中注册它们来处理指定的路径。

package main

import (
    "fmt"
    "net/http"
)

type Hello struct{}

type String string

type Struct struct {
    Greeting string
    Punct    string
    Who      string
}

func (s String) ServeHTTP(
    w http.ResponseWriter,
    r *http.Request) {
	response := fmt.Sprintf("%v", s)
    fmt.Fprint(w, response)
}

func (s Struct) ServeHTTP(
    w http.ResponseWriter,
    r *http.Request) {
	response := fmt.Sprintf("%v%v%v", s.Greeting,s.Punct,s.Who)
    fmt.Fprint(w, response)
}



func main() {
    http.Handle("/string", String("I'm a frayed knot."))
    http.Handle("/struct", &Struct{"Hello", ":", "Gophers!"})
    http.ListenAndServe("localhost:4000", nil)
}