2011 OSDC Day 1 筆記

Update: 補上 OSDC 紀錄影片 2011.06.26 今年很高興可以北上參加 OSDC 2011 (Open Source Developers Conference),由於之前都在南部唸書及工作,沒有機會北上參加聚會,現在人在新竹,終於有機會可以參加了,雖然早上六點就要起床趕電車了,不過到現場聽課感覺就是不同,也可以認識很多新朋友,底下來紀錄上課筆記 微軟與 jQuery 社群的親密接觸 講者: Eric Shangkuan (Microsoft) Slide: 微軟與 jQuery 社群的親密接觸 這是 OSDC 第一場演講,早上九點就開始了,雖然人不多,但是蠻多人還是為了講者而來,首先介紹什麼是 jQuery,以及 jQuery 一些基本用法,像是 CSS selector,如何在 Windows Visual Studio 上面開發 jQuery 及撰寫 plugin 整合進去 ASP.Net,最後介紹三個不錯用的 jQuery Plugin: Templeate, Datalink, Globalzation。 Templeate: 這搭配 Facebook api 可以直接做個人頁面,請參考這裡 Globalzation: 前端多國語系實做 Datalink: 可以快速處理 form,利用 object 跟 jQuery 搭配 如果要研究上述三個 jQuery Plugin 可以參考底下: jQuery Datalink: https://github.com/jquery/jquery-datalink jQuery Templeate: https://github.com/jquery/jquery-tmpl jQuery Globalzation: https://github. [Read More]

如何使用 PPA 升級 Ubunut Firefox 瀏覽器到 4.0

FireFox_4_about 作者目前使用 Ubuntu 10.10 (maverick) 桌面環境,也是台灣 MozTW 成員之一,這次 FireFox 4.0 Release 介面有些改變,使用上來也非常順手,記憶體好像吃的比較少了?(有待商榷),現在就來升級 FireFox 吧,兩種升級方式,如果不熟悉 Command Line 就用 GUI 升級,另一種升級方式就是用 apt-get upgrade 啦。 如果用 Windows 請到這裡下載

利用 GUI 介面升級 (Install firefox 4 using GUI) 我的環境是英文,所以底下寫的是英文安裝方式: 按照底下步驟進行

Applications > Ubuntu Software Center > Edit > Software Sources 
之後點選 "Other Software" 選擇左下角 "Add" 按鈕,接著把底下文字輸入
ppa:mozillateam/firefox-stable
最後到底下升級,就可以開始使用 FireFox 4 了
System > Administration > Update Manager

文字介面升級 (Install firefox 4 using terminal) 只要鍵入三行指令即可

$ sudo add-apt-repository ppa:mozillateam/firefox-stable
$ sudo apt-get update
$ sudo apt-get upgrade
沒圖沒真相啦,底下附上桌面截圖

firefox_4 Reference: How to install firefox 4 in ubuntu using PPA Firefox 4 正式版現已推出,帶給您更棒的網路體驗

CodeIgniter 2.0.1 Release 發佈了

CodeIgniter
很高興可以聽到這消息,那就是

CodeIgniter Release 2.0.1 版本了,原本大家都很擔心為什麼 CodeIgniter 每發佈 Release 版本都要等個好幾個月甚至到半年以上,現在不用這麼久了,因為自從官方新增了 CodeIgniter Reactor 加速版本開發及修正,所以更多人貢獻了自己的程式碼及回報問題,我相信 CodeIgniter 會越來越好,希望有更多台灣的朋友來使用。 2.0.1 版本新增了 ENVIRONMENT 這環境變數,讓程式開發者可以任意調整環境狀況,最主要是改變 PHP error reporting 狀態:

/*
 * production => error_reporting(0)
 * development => error_reporting(E_ALL)
 */
define('ENVIRONMENT', 'production');
當您設定為 production,表示網站不需要任意輸出錯誤訊息,如果調整成 development,系統就會打開全部錯誤訊息,這對開發者相當重要。如果想瞭解更多,請參考

Handling Multiple Environments。 歡迎大家下載最新版本: http://www.codeigniter.org.tw/downloads 如果想加入翻譯團隊,可以參考這裡: https://github.com/appleboy/PHP-CodeIgniter-Framework-Taiwan

Ubuntu (Debian) 架設 apache mpm worker mod_fcgid 筆記

最近想架設 RedmineUbuntu 伺服器上面,架設之前要先搞定 apache 搭配 mpm worker 及 mod_fcgi module,安裝步驟其實不難,就搭配懶人指令 apt 就可以了。

安裝 apache mpm worker 由於怕安裝過程會叫你把 apache2-mpm-worker 移除,改裝 apache2-mpm-prefork,所以安裝順序上面有些變化,請參考底下:

# 先安裝
$ apt-get install apache2.2-bin apache2.2-common apache2-mpm-worker libapache2-mod-fcgid php5-cli php5-cgi php5-common
#後安裝
$ apt-get install apache2 php5 php5-gd php5-curl
至於 PHP 5 套件就看你需要什麼就裝什麼吧,搜尋一下 php5-* 看看,apache 裝好預設看不到 PHP 網頁,也就是認不得 php type,請在 apache config 檔案加入底下 [Read More]

[Linux Kernel] 讀取 /proc 底下資料最佳方法: seq_file interface

前言 最近在整合公司內部

Atheros(被高通買下) 晶片的 Router,從原本 2.6.15 升級到 2.6.34.7,升級過程遇到很多困難,其中一項升級 Wireless Driver 部份,發現在 Kernel Socket 與 User Space 溝通之間出了問題,利用 Ioctl 來取得目前在 AP 上面所有 Client 資料(包含 mac address, 處於 N or G mode…等),在 User Space 上會掉資料,後來利用 /proc 底下檔案來跟 User 之間溝通,才沒有發生任何問題,由於輸出的檔案比較多,就偏向用 2.6 Kernel 提供的 seq_file 介面( interface )建立虛擬檔案 (virtual file) 與 User Space 溝通(此方法為 Alexander Viro 所設計),此功能其實在 2.4.15 已經實做了,只是在 2.6 版本才被大量使用。 程式設計師可以透過引入 <linux/seq_file.h> 來實做 seq_file interface,seq_file 最大優勢就是讀取完全沒有4k boundry 的限制,也就是不用管會不會超出 output buffer。

The iterator interface 為了能夠讓 iterator 正常運作,我們必須實做 4 個 function (start, next, stop, show),跑得過程為 start -> show -> next -> show -> next -> stop,為了方便講解,參考

Linux Kernel(4)- seq_file 裡面範例如下:

#include 
#include 
#include  /* Necessary because we use proc fs */
#include  /* for seq_file */
#include 

MODULE_LICENSE("GPL");

#define MAX_LINE 1000
static uint32_t *lines;

/**
 * seq_start() takes a position as an argument and returns an iterator which
 * will start reading at that position.
 */
static void* seq_start(struct seq_file *s, loff_t *pos)
{
    uint32_t *lines;

    if (*pos >= MAX_LINE) {
        return NULL; // no more data to read
    }

    lines = kzalloc(sizeof(uint32_t), GFP_KERNEL);
    if (!lines) {
        return NULL;
    }

    *lines = *pos + 1;

    return lines;
}

/**
 * move the iterator forward to the next position in the sequence
 */
static void* seq_next(struct seq_file *s, void *v, loff_t *pos)
{
    uint32_t *lines = v;
    *pos = ++(*lines);
    if (*pos >= MAX_LINE) {
        return NULL; // no more data to read
    }
    return lines;
}

/**
 * stop() is called when iteration is complete (clean up)
 */
static void seq_stop(struct seq_file *s, void *v)
{
    kfree(v);
}

/**
 * success return 0, otherwise return error code
 */
static int seq_show(struct seq_file *s, void *v)
{
    seq_printf(s, "Line #%d: This is Brook's demo\n", *((uint32_t*)v));
    return 0;
}

static struct seq_operations seq_ops = {
    .start = seq_start,
    .next  = seq_next,
    .stop  = seq_stop,
    .show  = seq_show
};

static int proc_open(struct inode *inode, struct file *file)
{
    return seq_open(file, &seq_ops);
}

static struct file_operations proc_ops = {
    .owner   = THIS_MODULE, // system
    .open    = proc_open,
    .read    = seq_read,    // system
    .llseek  = seq_lseek,   // system
    .release = seq_release  // system
};

static int __init init_modules(void)
{
    struct proc_dir_entry *ent;

    ent = create_proc_entry("brook", 0, NULL);
    if (ent) {
        ent->proc_fops = &proc_ops;
    }
    return 0;
}

static void __exit exit_modules(void)
{
    if (lines) {
        kfree(lines);
    }
    remove_proc_entry("brook", NULL);
}

module_init(init_modules);
module_exit(exit_modules);
[Read More]

用 js 或 php 判斷 Android 手機上網

之前寫了一篇 jQuery 偵測瀏覽器版本, 作業系統(OS detection),這次可以來加上 Android 手機版本,其實就是分析瀏覽器 User Agent 來達到目的,底下分享 PHP 跟 Javascript 作法

PHP 方法

if(stripos($_SERVER['HTTP_USER_AGENT'],'Android') !== false) 
{
	header('Location: http://android.xxx.com');
	exit();
}

Javascript 方法

if(navigator.userAgent.match(/Android/i))
{
	window.location = 'http://android.xxx.com';
}

Apache .htaccess 方法 用

Apache mod rewrite 方法

RewriteCond %{HTTP_USER_AGENT} ^.*Android.*$
RewriteRule ^(.*)$ http://android.xxx.com [R=301]

Git 版本控制 branch model 分支模組基本介紹

我相信大家對於 Git 版本控制不陌生了,Git 最大強項就是可以任意建立 branch,讓您開發程式不需要擔心原本的程式碼會被動到,造成不知道該怎麼恢復原來的狀態。為了不影響產品發展,branch 對於大型工作團隊就顯得更重要了,今天在網路上看到一篇 A successful Git branching model 文章,裡面把 branch 使用方式寫得非常清楚,底下我會透過指令來說明如何使用簡單 branch 指令,當然請大家先去 github 註冊申請帳號,如果不想申請帳號,也可以自己在 local 端去執行。

底下所引用的圖片都是經由 A successful Git branching model 文章所提供。

git-flow

看到這張圖其實就說明了 branch 最重要的精神:『無限建立分支』,大家也不用害怕看不懂這張圖,底下說明 branch 分支狀況

[Read More]

利用 mb_strwidth 取代 mb_strlen 計算 Multi-byte 字數

之前寫了一篇如何切割中文標題,裡面計算中文字數,這樣才不會直接切到中文字,但是 PHP 有很多函式可以算出字串有多少字元,我們看看底下例子,使用了 strlen、mb_strlen、mb_strwidth 分別下去測試,看看會把中文字算成幾個字元: 看到這結果並不意外,大家可以看到 strlen 把中文字元算成3個字元,mb\_strlen 不管是中文還是英文就都算成單一字元,mb\_strwidth 則是把中文算成 2 字元,mb_strwidth 算出來正是我想要的,如果是想要在 Web 上面切割中文,建議大家用 mb_substr 即可。因為作者本人在弄跟 BBS 相關技術,所以必須江中文字算成2字元,底下節錄 mb_strwidth 如何算字元長度: Chars => Width U+0000 - U+0019 => 0 U+0020 - U+1FFF => 1 U+2000 - U+FF60 => 2 U+FF61 - U+FF9F => 1 U+FFA0 - => 2 PS: 測試環境 PHP 5. [Read More]
php 

切換 Ubuntu apt 的 mirror site

Update: 國網內部員工建議用 http://ftp.twaren.net 這台

最近常常會發生 apt-get update 指令失敗,台大這台 tw.archive.ubuntu.com 似乎常常掛點,所以網路上找一下其他的 mirror site,看到似乎很多人都在用國網的 Server ( http://free.nchc.org.tw ),要換的話,請更改 /etc/apt/sources.list,將全部 tw.archive.ubuntu.com 都取代成 free.nchc.org.tw,其實還有另一個 domain 就是 opensource.nchc.org.tw,這些都可以用,沒有 apt 的 UbuntuDebian 簡直就不是 Server 了…XD

WordPress plugin 加強網址 SEO

由於網站 SEO 在大家心中都是非常重要,現在製作網站也都考慮了很多 SEO 的問題,其中一個功能就是可不可以自訂網址,Wordpress 很早之前就支援了此功能,站長我呢,在創站的時候使用 blog.wu-boy.com/2011/02/17/2542,為了使搜尋引擎更可以快速找到本站,所以打算將網址改成 blog.wu-boy.com/2011/02/php-codeigniter-google-url-shortener-api-library/,在後台 Permalink Settings 可以自訂部落格網址,將網址格式改成 /%year%/%monthnum%/%postname%/,可是改了之後,之前搜尋引擎及別人引用的網址就會變成 404 NOT Found,為瞭解決此問題,必須寫一支 Mapping Url 程式,讓之前的舊網址轉到新網址,剛好在 Roga Blog 找到一篇 加強部落格的 SEO,提供了轉換的 plugin,底下是該程式碼:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
/*
Plugin Name: roga's url hotfix
Plugin URI: http://blog.roga.tw/2011/02/%E5%8A%A0%E5%BC%B7%E9%83%A8%E8%90%BD%E6%A0%BC%E7%9A%84-seo/
Description: redirect http requests.
Version: 0.1
Author: roga
Author URI: http://blog.roga.tw
License: GPL v2
*/

function roga_wrap()
{
	GLOBAL $wpdb;
	$request_uri = getenv('REQUEST_URI');

	$array = explode('/', $request_uri);

	$status = TRUE;

	foreach($array as $row)
	{
		if( ! is_numeric($row) && ! empty($row)) $status = FALSE;
	}

	if(count($array) != 5 || $status != TRUE)
		return NULL;

	$post_id = (int) $array[4]; // http://blog.roga.tw/2011/02/16/2484

	$wp_result = $wpdb->get_row("SELECT `post_type`, `post_name`, `post_date` FROM `$wpdb->posts` WHERE `ID` = $post_id ");

	if( ! isset($wp_result))
		return NULL;

	$post_type = $wp_result->post_type;
	$post_name = $wp_result->post_name;
	$post_date = $wp_result->post_date;

	if($post_type == 'revision')
		return NULL;

	$time = strtotime($post_date);
	$year = date('Y', $time);
	$month = date('m', $time);
	//  old:   /%year%/%monthnum%/%day%/%post_id%
	//  new:   /%year%/%monthnum%/%postname%/
	$new_request_uri = "/$year/$month/$post_name";
	$http_host = getenv('HTTP_HOST');

	$logfile = WP_CONTENT_DIR . "/cache/wp-roga-redirect.log";
	if(file_exists($logfile))
		file_put_contents($logfile, sprintf("[%s] %s -> %s / %s " . PHP_EOL, date_i18n("Y-m-d H:i:s"), $request_uri, urldecode($new_request_uri), getenv('HTTP_USER_AGENT')), FILE_APPEND);

	header("Status: 301 Moved Permanently");
	header("Location: http:/$http_host$new_request_uri");
	exit();
}

add_action('init', 'roga_wrap');
[Read More]