網頁

2014年12月23日 星期二

Sybase學習筆記-查詢table schema

在sybase中,看來不是用describe table來查table的schema設定,google半天看起來是用三個system table來查詢。

主要是用到sysobjects、syscolumns及systypes。

sysobjects主要是記錄有哪些被建立的物件,也包含Talbe,type如下(取自From a Sybase Database, how I can get table description ( field names and types)?):

  • C – computed column
  • D – default
  • F – SQLJ function
  • L – log
  • N – partition condition
  • P – Transact-SQL or SQLJ procedure
  • PR – prepare objects (created by Dynamic SQL)
  • R – rule
  • RI – referential constraint
  • S – system table
  • TR – trigger
  • U – user table
  • V – view
  • XP – extended stored procedure

所以使用下列的sql,就可以查到帳號可以存取的table
SELECT * FROM sysobjects WHERE type = 'U'

而我想查詢的是table中的column的type,所以要合用sysobjects、syscolumns及systypes,如下我要查的是Db_subject
select st.name, sc.name, sc.length from syscolumns sc 
inner join sysobjects so on sc.id = so.id 
inner join systypes st on st.type = sc.type 
where so.name = 'Db_subject'

這樣就可以看到Table的欄位資料了。

=============2015/04/07 更新==================

底下SQL可以更準確的取得

select o.name tableName,'' tableChnName,c.name columnName, '' columnChnName, t.name columnType,
(
case when (t.name='numeric' or t.name='decimal')
then '('+CONVERT(VARCHAR(10),c.prec)+','+CONVERT(VARCHAR(10),c.scale)+')'
else CONVERT(VARCHAR,c.length)
end
) columnLength --如果是數字,則顯示 (precision,scale)
from sysobjects o
inner join syscolumns c on c.id = o.id
inner join systypes t on t.usertype = c.usertype
where o.type = 'U' and o.name in ('表格1名稱', '表格2名稱' );

=======================================

參考網址:
From a Sybase Database, how I can get table description ( field names and types)?

2014年12月22日 星期一

CentOS筆記-CentOS 6.5 安裝MySQL 5.5

CentOS內定的yum server連結的是mysql 5.1,若要改用mysql 5.5則要先取得新的server。安裝步驟如下:

Cent OS 6.X使用底下指令
rpm -Uvh https://mirror.webtatic.com/yum/el6/latest.rpm

Cent OS 5.X使用底下指令
rpm -Uvh https://mirror.webtatic.com/yum/el5/latest.rpm

若是安裝過舊mysql server
yum remove mysql mysql-*
#rpm -q mysql mysql-server //這個也行

----------------------------------
執行底下指令,會先安裝舊版再用5.5取代掉
yum install mysql.`uname -i` yum-plugin-replace
yum replace mysql --replace-with mysql55w
----------------------------------

再來執行安裝及設定安全性
yum install mysql55w mysql55w-server
service mysqld start
/usr/bin/mysql_secure_installation

完成!

參考網址:
MySQL 5.5 on CentOS/RHEL 6.5 and 5.10 via Yum
新安裝 CentOS 6.5 筆記

2014年12月14日 星期日

PHPUnit學習筆記-Broken PHP Configuration on Mavericks or later

之前裝好的PHPUnit後一直沒用,這陣子重新用時,卻發現一直出現底下的訊息
Warning: require(/usr/lib/php/pear/PHPUnit/Autoload.php): failed to open stream: No such file or directory in /usr/bin/phpunit on line 43

Fatal error: require(): Failed opening required '/usr/lib/php/pear/PHPUnit/Autoload.php' (include_path='.:') in /usr/bin/phpunit on line 43

想不透原因是什麼,查到了Broken PHP Configuration on OSX Mavericks這篇,試了下底下的command
$sudo pear -V
//也可以用pear config-get php_dir

output底下的訊息
Could not open input file: /usr/lib/php/pear/pearcmd.php

看來PEAR真的消失了,參考自己之前寫的PHPUnit學習筆記-PHPUnit安裝(使用PEAR),重新安裝一次
$sudo cp /private/etc/php.ini.default /private/etc/php.ini
$sudo php /usr/lib/php/install-pear-nozlib.phar 
$sudo pear config-set php_ini /private/etc/php.ini
$sudo pear channel-update pear.php.net
$sudo pecl channel-update pecl.php.net
$sudo pecl config-set php_ini /private/etc/php.ini
$sudo pear upgrade-all
$sudo vim /private/etc/php.ini

再執行pear -V,發現PEAR ok了,但是執行底下的command,依然同樣的error
$phpunit -v

重新安裝PHPUnit
$sudo pear config-set auto_discover 1
//sudo pear channel-discover
$sudo pear channel-discover pear.phpunit.de
$sudo pear channel-discover components.ez.no
$sudo pear channel-discover pear.symfony-project.com 
$sudo pear channel-discover pear.symfony.com 
$sudo pear install --alldeps phpunit/PHPUnit

再次執行
$phpunit -v

但這次卻一直出現以下的訊息
You have installed PHPUnit via PEAR. This installation method is no longer
supported and http://pear.phpunit.de/ will be shut down no later than
December, 31 2014.

Please read http://phpunit.de/manual/current/en/installation.html and
learn how to use PHPUnit from a PHAR or install it via Composer.

執行底下command,看看裝了什麼
$pear list -c pear.phpunit.de
Installed packages, channel pear.phpunit.de:
============================================
Package Version State
PHPUnit 4.0.18  stable

看起來很正常,但就是不能用,只好咬牙改用Composer的方式安裝了,找到這篇寫的完整的Installing PHPUnit(官網:Composer安裝Composer

先Uninstall
$sudo pear uninstall phpunit/PHPUnit
$cd /tmp (先換到tmp目錄)
$curl -sS https://getcomposer.org/installer | php

得到以下訊息
#!/usr/bin/env php
All settings correct for using Composer
Downloading...

Composer successfully installed to: /private/tmp/composer.phar
Use it: php composer.phar

copy composer.phar去/usr/local/bin
$cp composer.phar /usr/local/bin
$php /usr/local/bin/composer.phar global require "phpunit/phpunit=4.3.*"


安裝完後,到~/.composer/vendor下,要能看到phpunit安裝在這裡
autoload.php composer phpunit  symfony
bin  doctrine sebastian

再來做個link
$cd /usr/bin
$sudo ln -s sudo ln -s /Users/alvin/.composer/vendor/phpunit/phpunit/phpunit phpunit
$phpunit --version

得到底下訊息,可以執行了
PHPUnit 4.3.5 by Sebastian Bergmann.

參考網址:
Broken PHP Configuration on OSX Mavericks
PHPUnit on OSX10.8
Installing PHPUnit
How to remove a package from composer?
composer document
Composer

2014年12月13日 星期六

MSSQL server學習筆記-取得中文檔名的欄位名

需求:
  • DB Server: MSSQL
  • 欄位使用中文
  • 要能正確讀取
一般來講Table欄位都是英文,但國內用MSSQL的公家機關大多用中文,為了省麻煩,乾脆想想怎弄,可以猜出是chart set問題而已,Windows系統在國內大多Big 5編碼,而MSSQL通常安裝時都是直接用預設,所以朝這方向處理

程式碼如下,做個編碼轉換即可用這個當key去取值了
function get_mssql_chinese_col_name($ch_name)
{
    return mb_convert_encoding($ch_name, 'big5', 'utf8');
}

蠻特別的是,Table name不會有這問題,我想是SQL上的使用是沒差的,但取值出來放在array中時,欄位名放在php array中的key在使用,就會有差別

Mac學習筆記-開啟HTTPS


[1] 產生 host key
$sudo mkdir /private/etc/apache2/ssl
$cd /private/etc/apache2/ssl
$sudo ssh-keygen -f host.key

[2] 產生certificate request file
sudo openssl req -new -key host.key -out request.csr

[3] 產生certificate request file
$sudo openssl req -new -key host.key -out request.csr

[4] 加入產生的SSL certificate及‘nopass’ key
$sudo openssl x509 -req -days 365 -in request.csr -signkey host.key -out server.crt
openssl rsa -in host.key -out host.nopass.key

找到SSLCertificateFile及SSLCertificateKeyFile,更改file位置
#SSLCertificateFile "/private/etc/apache2/server.crt"
SSLCertificateFile "/private/etc/apache2/ssl/server.crt"
#SSLCertificateKeyFile "/private/etc/apache2/server.key"
SSLCertificateKeyFile "/private/etc/apache2/ssl/host.nopass.key"

[5] 確認Apache有加入ssl_module
$sudo vim httpd.conf

找出底下兩個,將"#"刪除或加入
LoadModule ssl_module libexec/apache2/mod_ssl.so
Include /private/etc/apache2/extra/httpd-ssl.conf

[OS X 10.10的Apache 2.4] 預設不載入shmcb,要找出底下的將"#"刪除
LoadModule socache_shmcb_module libexec/apache2/mod_socache_shmcb.so

[6] 做測試
apachectl configtest

[OS X 10.10的Apache 2.4] 會出現底下訊息
AH00526: Syntax error on line 62 of /private/etc/apache2/extra/httpd-ssl.conf:
SSLSessionCache: 'shmcb' session cache not supported (known names: ). Maybe you need to load the appropriate socache module (mod_socache_shmcb?).
$sudo vim /private/etc/apache2/extra/httpd-ssl.conf

將找到SSLMutex將其註解並加上Mutex
#SSLMutex  "file:/private/var/run/ssl_mutex"
Mutex sysvsem default

[7] 確認回傳Syntax OK,啟動Server
apachectl restart


參考網址:
APACHE SSL ON MAC OSX LION 10.7
CREATING A SELF-SIGNED SSL CERT FOR MAC OSX MOUNTAIN LION & APACHE
How To: Set up SSL with OSX Mountain Lion’s built-in Apache
How to create self signed ssl certificate for apache2 web server
SSH 免密碼登入

For OS X 10.10 Yosemite
Apache 2.4 configuration for ssl not working

2014年12月12日 星期五

Wordpress開發筆記-PHP Warning: Cannot modify header information for wp-session.php on line 124

wp session這個plugin真的是蠻好用的,不過使用時一直有類似log出現

[Fri Nov 21 11:55:06 2014] [error] [client ::1] PHP Warning:  Cannot modify header information - headers already sent by (output started at /work/demo/wp-admin/menu-header.php:118) in /work/demo/inc/lib/wp-session-manager/class-wp-session.php on line 124, referer: https://localhost/~alvin/demo/wp-admin/edit.php?post_type=exercise

這個warning猜測是因為正常處理下,最後會執行到wp_session_commit(),但若是用ajax的方式時,或直接用die()結束時,並沒有執行wp_session_commit(),以至於有這個warning,最後實驗結果,果然如我所料,哈哈


參考網址:

2014年11月5日 星期三

PHP學習筆記-取代文字中的斷行

遇到個問題,部分程式碼如下:

FB.ui(
{
    method: 'feed',
    name: "Sit-Ups Won’t",
    link: "https://xxx/",
    picture: "http://xxx/shutterstock_119588920-2xpj7nxhm0d9jws3k56zuo.jpg",
    description: "I am often asked if doing situps or crunches 
will get people the toned six-pack abs they're looki..."
},
function....

其中description在php中是直接echo文章的內容,但這字串含斷行符號,一直出現“Unexpected token ILLEGAL”,使用str_replace去取代\r或\n都無效,查了這篇PHP: 移除換行符號,最後使用

echo preg_replace('/\v/', '', $description);

就解決了

參考網址:
PHP: 移除換行符號

2014年10月27日 星期一

Google+學習筆記-Share info

在FB上分享內容有很多的方式也蠻完整的,但google+針對未登入並分享的機制就沒那麼完整,只知道不使用google+提供的button下,能用link的方式只有

https://plus.google.com/share?url=(urlencode link)

仔細查找,是看到文件https://developers.google.com/+/web/snippet/有提到,部分原文如下:
========================================
使用摘要程式碼時,請務必遵守《Google 服務條款》的規定。
1. Schema.org 微型資料 (建議使用)
如果網頁有加上 schema.org 微型資料的註解,則 +Snippet 會採用任何 schema.org 類型 所顯示的 nameimage 和 description 屬性。
<body itemscope itemtype="http://schema.org/Product">
  <h1 itemprop="name">Shiny Trinket</h1>
  <img itemprop="image" src="{image-url}" />
  <p itemprop="description">Shiny trinkets are shiny.</p>
</body>
2. Open Graph 通訊協定
如果網頁包含適用於標題、圖片和說明的 Open Graph 屬性,系統就會予以採用,以產生 +Snippet。
<meta property="og:title" content="..." />
<meta property="og:image" content="..." />
<meta property="og:description" content="..." />
3. 標題與「description」中繼標記
如果網頁的 <head> 元素包含 <title> 和 <meta name="description" ... /> 標記,則 +Snippet 會採用標題,並使用 Description 中繼標記的 content 屬性產生摘要說明。至於縮圖圖片,則系統會透過分享框嘗試尋找網頁上的適用圖片。
<title>...</title>
<meta name="description" content="..." />
4. 網頁內容的最佳猜測 (不建議使用)
如果網頁都沒有提供前述資料,則 Google 會剖析網頁,嘗試找出最適用的標題、說明和圖片。
========================================

雖然文件https://developers.google.com/+/web/snippet/中寫會用Schema.org,但實際使用發現要是有找到<title>還是會優先使用,所以最快的方式是在head中提供title,縮圖可以使用itemprop="image"加在img tag中。另外,description不管怎加似乎都不放不進去,這是目前覺得奇怪之處。

參考網址:
https://developers.google.com/+/web/snippet/
Getting started with schema.org

2014年10月25日 星期六

jqplot學習筆記-x軸文字超出範圍

jqplot是很好用的chart library,但最近遇到個問題,如下圖:

可以看到x軸的日期超出了黃線外,看半天它的說明文件都不了解,解法異常簡單,他在init時會如下使用:
$.jqplot('xxx',....

將id為xxx的div(或其他你自己設定的目標)的style設定max-width即可,像我的範圍是1196,就設定最大1160。如下:
<div id="xxx" style="max-width: 1160px;"></div>

這樣就行了。

參考網址:
jqPlot Options
jqPlot-core
jqplot, remove outside border

2014年8月25日 星期一

Wordpress開發筆記-使用 $wpdb->update 如何知道是沒有更新任何東西還是更新Fail

使用wordpress的wpdb function時,總有個小疑慮,我怎知道目前的update是成功還失敗,因為若因為值相同,所以沒有update,會傳回false,失敗一樣傳回false,查半天才知道,前者是傳回0,後者是真的傳回false,但一般比較都認為相同,所以要用底下這樣寫法比對,才會比對型態的不同,使用$wpdb->update時可以像以下的方式,這邊用pseudo code表示

$suc = $wpdb->update(table_name,
    array( 
 'column1' => 'value1', // string
 'column2' => 'value2'  // integer (number) 
    ), 
    array( 'ID' => 1 ), 
        array( 
     '%s',  // value1
     '%d'   // value2
    ), 
    array( '%d' )
);
if( $suc === false ) {
    //Do something about fail
}
else if( $suc === 0 ){
    //Do something about nothing update
}


參考網址:
$wpdb - what does it return on fail?

Mac學習筆記-2010中Mac Mini換上新SSD硬碟

最近試著要將舊的Mac mini重生,目前mini都是用4500轉的硬碟,實在有夠慢,就心生去買了一顆SSD硬碟想換掉,查了半天資料,買了這顆美光Micron Crucial MX100 512GB SATAⅢ固態硬碟7mm

大致步驟如下
  1. 找出Mac mini的年代及可支援的ssd再去買
  2. 確認安裝的作業系統,OS X Mavericks是免費的了,我個人是用這個
  3. 備份原本資料(我個人是沒有 )
  4. 換硬碟
  5. 安裝OS X Mavericks
  6. 開啟trim功能
前三個步驟就看個人選擇,我直接記錄第4步驟

Step 4: 換硬碟的影片



這步驟需要的工具,我是用《JACKLY》36合1螺絲起子組(維修3C商品必備),該有的都有了,但是在推出機板的動作,因為要兩根細長的鐵棍來推出機板,如下圖,記得要準備好,不然推不出來無法取出硬碟,它又卡很緊非得這樣用力頂出來。


Step 5: 安裝OS X Mavericks

我在這步驟卡很久,一直在思考是要怎樣能Clone整顆硬碟,包含Recovery HD那部分,看了不少文章,在準備的過程中,本以為是用OS X:關於「回復磁碟輔助程式」來做出一個可重安裝重開機的硬碟,看了How to speed up an aging MacBook with a solid state drive才發現,又以為應該是要用Carbon Copy Cloner將整顆硬碟備份才是,其實如Mac Lion 系統修復的幾種方式所述都不用的,不過還是有些小步驟要注意,底下是我的做法:

將卸下來的硬碟裝用外接盒,當開機用的硬碟,接上Mac mini,沒有外接盒的,就要記得做開機usb,可以用官方的OS X:關於「回復磁碟輔助程式」下載來做一個Lion版的,或參考吉米教你製作OS X Mavericks 10.9的開機安裝碟可以做一個Mavericks的開機碟。

開機時按住OPTION鍵,選擇10.9.4進入Recovery工具,或按COMMAND鍵+R,直接進入Recovery工具。

先開磁碟工具程式,選擇新硬碟,這邊要注意的是,我之前都只選清除那個tab,再按清除按鈕直接格式化,安裝到一半都會失敗,所以要做的是,選分額那個tab,再分額區分額成一個後,按“選項”確認是否為“GUID磁碟分割區表格”,格式為“Mac OS擴充格式(日誌式)”,再按套用即可。

格式化完後,關閉磁碟工具程式,選擇“重新安裝Mavericks”,再來就等安裝好了。會自動安裝開機及Recovery HD區,有安裝成功一次後,就不再需要原硬碟或USB開機碟了。

PS. 過程中有幾次下載到一半就停止了,看起來Apple那邊Server下載的狀況不是太好,失敗了就再試就好。

Step 6: 開啟Trim功能

換上後本以為不用再注意這功能,但卻發現似乎有兩種狀況:
  1. 使用程式時會突然一頓一頓的
  2. 機器非常燙
第二種狀況在猜測是重裝及環境的關係,放著一小段時間就好了,第一種狀況是蠻介意的,照著How to Check and Enable TRIM on a Mac SSD所提,看看Trim功能有沒有打開,打開“系統資訊”->“SATA/SATA Express”,如下圖,果然是沒有

看半天主要是Apple只會將自身認可的SSD硬碟預設開啟,我這顆自然不在其認可範圍,所以要動點手腳。或用第三方軟體,但我個人比較不喜歡。

查了看來用return1 / trim_enabler.sh所提供的sh即可,看了How to Check and Enable TRIM on a Mac SSDSSD Upgrade on a Mid 2010 iMac的shell script中的perl這段都不符合Mavericks的使用,都無法成功。這邊將script記錄一下

1. 備份
sudo cp /System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext/Contents/MacOS/IOAHCIBlockStorage /System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext/Contents/MacOS/IOAHCIBlockStorage.original

2. 取代掉Apple辨識碼(From return1 / trim_enabler.sh
# for Yosemite, Mavericks 10.9.5 and 10.9.4 (thanks Tobi)
sudo perl -pi -e 's|(^\x00{1,20})[^\x00]{9}(\x00{1,20}\x54)|$1\x00\x00\x00\x00\x00\x00\x00\x00\x00$2|sg' /System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext/Contents/MacOS/IOAHCIBlockStorage

# for Mavericks 10.9.4 (thanks Tobi)
sudo perl -pi -e 's|(^\x00{1,20})[^\x00]{9}(\x00{1,20}\x54)|$1\x00\x00\x00\x00\x00\x00\x00\x00\x00$2|sg' /System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext/Contents/MacOS/IOAHCIBlockStorage

======Upadte for Yosemite Only======
sudo cp /System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext/Contents/MacOS/IOAHCIBlockStorage /System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext/Contents/MacOS/IOAHCIBlockStorage.original
# For Yosemite you have to disable driver signature check 
sudo nvram boot-args="kext-dev-mode=1"
sudo shutdown -r now
# for Yosemite, Mavericks 10.9.5 and 10.9.4 (thanks Tobi)
sudo perl -pi -e 's|(^\x00{1,20})[^\x00]{9}(\x00{1,20}\x54)|$1\x00\x00\x00\x00\x00\x00\x00\x00\x00$2|sg' /System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext/Contents/MacOS/IOAHCIBlockStorage
# !for Yosemite only! rebuild kext cache manually (could take a while)
sudo kextcache -m /System/Library/Caches/com.apple.kext.caches/Startup/Extensions.mkext /System/Library/Extensions
sudo touch /System/Library/Extensions/
sudo shutdown -r now

======Upadte for Yosemite 10.10.3 ======
資料截自:return1 / trim_enabler.txt
# backup patched file
sudo cp /System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext/Contents/MacOS/IOAHCIBlockStorage /System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext/Contents/MacOS/IOAHCIBlockStorage.original
 
# Important: Add "kext-dev-mode=1" as Kernel Arguments or the computer won't boot.
sudo nvram boot-args="kext-dev-mode=1"
sudo shutdown -r now
 
# looks for "Apple" string in HD kext, changes it to a wildcard match for anything
sudo perl -pi -e 's|\x00\x41\x50\x50\x4c\x45\x20\x53\x53\x44\x00|\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00|' /System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext/Contents/MacOS/IOAHCIBlockStorage
 
# rebuild kext cache manually (could take a while)
sudo touch /System/Library/Extensions
sudo kextcache -m /System/Library/Caches/com.apple.kext.caches/Startup/Extensions.mkext /System/Library/Extensions
 
# now reboot!
sudo shutdown -r now


3. Refresh
sudo touch /System/Library/Extensions/

4. 重開機,重開完後再打開“系統資訊”->“SATA/SATA Express”,看看是否如下圖

5. 若沒有成功,記得將備份再copy回去,重新找其他解決。若成功了也要記得重灌作業系統時要重新再做一次

看了How to Check and Enable TRIM on a Mac SSDSSD Upgrade on a Mid 2010 iMac的動作中有講“Clear the ktext caches”,我個人試了後反而有Warning出現,不知道是否動作有錯,但第三次重裝時,我沒做這動作依然可以正常,就再看看後續使用了,這邊做一下記錄
sudo kextcache -system-prelinked-kernel
sudo kextcache -system-caches

這樣就大功告成啦!!!哈哈哈

參考網址:
(Mac Mini mid-2011) Installing an SSD drive as a replacement of the HDD
Mac Lion 系統修復的幾種方式
Carbon Copy Cloner site
How to speed up an aging MacBook with a solid state drive
Carbon Copy Cloner整顆備份你的硬碟
OS X:關於「回復磁碟輔助程式」
OS X 恢復磁碟輔助程式-下載

How to Check and Enable TRIM on a Mac SSD
SSD Upgrade on a Mid 2010 iMac
return1 / trim_enabler.sh
CAUTION! SSD Drives and Yosemite

直接安裝:[MAC]自製《Mac OSX 10.9 Mavericks》專屬USB系統安裝碟
吉米教你製作OS X Mavericks 10.9的開機安裝碟

2014年8月1日 星期五

PHP學習筆記-世上所有國家列表

最近正好寫php案子時有需要,查了一下,發現不少熱心的網友做好了,可以直接用,這邊將他們列出做點記錄,免得要用時,網頁蒸發了


$country_list = array(
  "Afghanistan",
  "Albania",
  "Algeria",
  "Andorra",
  "Angola",
  "Antigua and Barbuda",
  "Argentina",
  "Armenia",
  "Australia",
  "Austria",
  "Azerbaijan",
  "Bahamas",
  "Bahrain",
  "Bangladesh",
  "Barbados",
  "Belarus",
  "Belgium",
  "Belize",
  "Benin",
  "Bhutan",
  "Bolivia",
  "Bosnia and Herzegovina",
  "Botswana",
  "Brazil",
  "Brunei",
  "Bulgaria",
  "Burkina Faso",
  "Burundi",
  "Cambodia",
  "Cameroon",
  "Canada",
  "Cape Verde",
  "Central African Republic",
  "Chad",
  "Chile",
  "China",
  "Colombi",
  "Comoros",
  "Congo (Brazzaville)",
  "Congo",
  "Costa Rica",
  "Cote d'Ivoire",
  "Croatia",
  "Cuba",
  "Cyprus",
  "Czech Republic",
  "Denmark",
  "Djibouti",
  "Dominica",
  "Dominican Republic",
  "East Timor (Timor Timur)",
  "Ecuador",
  "Egypt",
  "El Salvador",
  "Equatorial Guinea",
  "Eritrea",
  "Estonia",
  "Ethiopia",
  "Fiji",
  "Finland",
  "France",
  "Gabon",
  "Gambia, The",
  "Georgia",
  "Germany",
  "Ghana",
  "Greece",
  "Grenada",
  "Guatemala",
  "Guinea",
  "Guinea-Bissau",
  "Guyana",
  "Haiti",
  "Honduras",
  "Hungary",
  "Iceland",
  "India",
  "Indonesia",
  "Iran",
  "Iraq",
  "Ireland",
  "Israel",
  "Italy",
  "Jamaica",
  "Japan",
  "Jordan",
  "Kazakhstan",
  "Kenya",
  "Kiribati",
  "Korea, North",
  "Korea, South",
  "Kuwait",
  "Kyrgyzstan",
  "Laos",
  "Latvia",
  "Lebanon",
  "Lesotho",
  "Liberia",
  "Libya",
  "Liechtenstein",
  "Lithuania",
  "Luxembourg",
  "Macedonia",
  "Madagascar",
  "Malawi",
  "Malaysia",
  "Maldives",
  "Mali",
  "Malta",
  "Marshall Islands",
  "Mauritania",
  "Mauritius",
  "Mexico",
  "Micronesia",
  "Moldova",
  "Monaco",
  "Mongolia",
  "Morocco",
  "Mozambique",
  "Myanmar",
  "Namibia",
  "Nauru",
  "Nepal",
  "Netherlands",
  "New Zealand",
  "Nicaragua",
  "Niger",
  "Nigeria",
  "Norway",
  "Oman",
  "Pakistan",
  "Palau",
  "Panama",
  "Papua New Guinea",
  "Paraguay",
  "Peru",
  "Philippines",
  "Poland",
  "Portugal",
  "Qatar",
  "Romania",
  "Russia",
  "Rwanda",
  "Saint Kitts and Nevis",
  "Saint Lucia",
  "Saint Vincent",
  "Samoa",
  "San Marino",
  "Sao Tome and Principe",
  "Saudi Arabia",
  "Senegal",
  "Serbia and Montenegro",
  "Seychelles",
  "Sierra Leone",
  "Singapore",
  "Slovakia",
  "Slovenia",
  "Solomon Islands",
  "Somalia",
  "South Africa",
  "Spain",
  "Sri Lanka",
  "Sudan",
  "Suriname",
  "Swaziland",
  "Sweden",
  "Switzerland",
  "Syria",
  "Taiwan",
  "Tajikistan",
  "Tanzania",
  "Thailand",
  "Togo",
  "Tonga",
  "Trinidad and Tobago",
  "Tunisia",
  "Turkey",
  "Turkmenistan",
  "Tuvalu",
  "Uganda",
  "Ukraine",
  "United Arab Emirates",
  "United Kingdom",
  "United States",
  "Uruguay",
  "Uzbekistan",
  "Vanuatu",
  "Vatican City",
  "Venezuela",
  "Vietnam",
  "Yemen",
  "Zambia",
  "Zimbabwe"
 );
With Country Code
$country_list = array(
    'Afghanistan' => '93',
    'Albania' => '355',
    'Algeria' => '213',
    'American Samoa' => '1 684',
    'Andorra' => '376',
    'Angola' => '244',
    'Anguilla' => '1264',
    'Antarctica' => '672',
    'Antigua and Barbuda' => '1268',
    'Antilles, Netherlands' => '599',
    'Argentina' => '54',
    'Armenia' => '374',
    'Aruba' => '297',
    'Australia' => '61',
    'Austria' => '43',
    'Azerbaijan' => '994',
    'Bahamas' => '1242',
    'Bahrain' => '973',
    'Bangladesh' => '880',
    'Barbados' => '1246',
    'Belarus' => '375',
    'Belgium' => '375',
    'Belize' => '501',
    'Benin' => '229',
    'Bermuda' => '1 441',
    'Bhutan' => '975',
    'Bolivia' => '591',
    'Bosnia and Herzegovina' => '387',
    'Botswana' => '267',
    'Brazil' => '55',
    'British Indian Ocean Territory' => '246',
    'British Virgin Islands' => '1 284',
    'Brunei Darussalam' => '673',
    'Bulgaria' => '359',
    'Burkina Faso' => '226',
    'Burundi' => '257',
    'Cambodia' => '855',
    'Cameroon' => '237',
    'Canada' => '1',
    'Cape Verde' => '238',
    'Cayman Islands' => '1 345',
    'Central African Republic' => '236',
    'Chad' => '235',
    'Chile' => '56',
    'China' => '86',
    'Christmas Island' => '64',
    'Cocos (Keeling) Islands' => '61',
    'Colombia' => '57',
    'Comoros' => '269',
    'Congo' => '242',
    'Cook Islands' => '682',
    'Costa Rica' => '506',
    'Cote D\'Ivoire' => '225',
    'Croatia' => '385',
    'Cuba' => '53',
    'Cyprus' => '357',
    'Czech Republic' => '420',
    'Denmark' => '45',
    'Djibouti' => '253',
    'Dominica' => '1 767',
    'Dominican Republic' => '1 809',
    'East Timor (Timor-Leste)' => '670',
    'Ecuador' => '593',
    'Egypt' => '20',
    'El Salvador' => '503',
    'Equatorial Guinea' => '240',
    'Eritrea' => '291',
    'Estonia' => '372',
    'Ethiopia' => '251',
    'Falkland Islands (Malvinas)' => '500',
    'Faroe Islands' => '298',
    'Fiji' => '679',
    'Finland' => '358',
    'France' => '33',
    'French Guiana' => '594',
    'French Polynesia' => '689',
    'Gabon' => '241',
    'Gambia, the' => '220',
    'Georgia' => '995',
    'Germany' => '49',
    'Ghana' => '233',
    'Gibraltar' => '350',
    'Greece' => '30',
    'Greenland' => '299',
    'Grenada' => '1 473',
    'Guadeloupe' => '590',
    'Guam' => '1 671',
    'Guatemala' => '502',
    'Guernsey and Alderney' => '5399',
    'Guinea' => '224',
    'Guinea-Bissau' => '245',
    'Guinea, Equatorial' => '240',
    'Guiana, French' => '594',
    'Guyana' => '592',
    'Haiti' => '509',
    'Holy See (Vatican City State)' => '379',
    'Holland' => '31',
    'Honduras' => '504',
    'Hong Kong, (China)' => '852',
    'Hungary' => '36',
    'Iceland' => '354',
    'India' => '91',
    'Indonesia' => '62',
    'Iran' => '98',
    'Iraq' => '964',
    'Ireland' => '353',
    'Isle of Man' => '44',
    'Israel' => '972',
    'Italy' => '39',
    'Jamaica' => '1 876',
    'Japan' => '81',
    'Jersey' => '44',
    'Jordan' => '962',
    'Kazakhstan' => '7',
    'Kenya' => '254',
    'Kiribati' => '686',
    'Korea(North)' => '850',
    'Korea(South)' => '82',
    'Kosovo' => '381',
    'Kuwait' => '965',
    'Kyrgyzstan' => '996',
    'Lao People\'s Democratic Republic' => '856',
    'Latvia' => '371',
    'Lebanon' => '961',
    'Lesotho' => '266',
    'Liberia' => '231',
    'Libyan Arab Jamahiriya' => '218',
    'Liechtenstein' => '423',
    'Lithuania' => '370',
    'Luxembourg' => '352',
    'Macao, (China)' => '853',
    'Macedonia, TFYR' => '389',
    'Madagascar' => '261',
    'Malawi' => '265',
    'Malaysia' => '60',
    'Maldives' => '960',
    'Mali' => '223',
    'Malta' => '356',
    'Marshall Islands' => '692',
    'Martinique' => '596',
    'Mauritania' => '222',
    'Mauritius' => '230',
    'Mayotte' => '262',
    'Mexico' => '52',
    'Micronesia' => '691',
    'Moldova' => '373',
    'Monaco' => '377',
    'Mongolia' => '976',
    'Montenegro' => '382',
    'Montserrat' => '1 664',
    'Morocco' => '212',
    'Mozambique' => '258',
    'Myanmar' => '95',
    'Namibia' => '264',
    'Nauru' => '674',
    'Nepal' => '977',
    'Netherlands' => '31',
    'Netherlands Antilles' => '599',
    'New Caledonia' => '687',
    'New Zealand' => '64',
    'Nicaragua' => '505',
    'Niger' => '227',
    'Nigeria' => '234',
    'Niue' => '683',
    'Norfolk Island' => '672',
    'Northern Mariana Islands' => '1 670',
    'Norway' => '47',
    'Oman' => '968',
    'Pakistan' => '92',
    'Palau' => '680',
    'Palestinian Territory' => '970',
    'Panama' => '507',
    'Papua New Guinea' => '675',
    'Paraguay' => '595',
    'Peru' => '51',
    'Philippines' => '63',
    'Pitcairn Island' => '872',
    'Poland' => '48',
    'Portugal' => '351',
    'Puerto Rico' => '1787',
    'Qatar' => '974',
    'Reunion' => '262',
    'Romania' => '40',
    'Russia' => '7',
    'Rwanda' => '250',
    'Sahara' => '212',
    'Saint Helena' => '290',
    'Saint Kitts and Nevis' => '1869',
    'Saint Lucia' => '1758',
    'Saint Pierre and Miquelon' => '508',
    'Saint Vincent and the Grenadines' => '1784',
    'Samoa' => '685',
    'San Marino' => '374',
    'Sao Tome and Principe' => '239',
    'Saudi Arabia' => '966',
    'Senegal' => '221',
    'Serbia' => '381',
    'Seychelles' => '248',
    'Sierra Leone' => '232',
    'Singapore' => '65',
    'Slovakia' => '421',
    'Slovenia' => '386',
    'Solomon Islands' => '677',
    'Somalia' => '252',
    'South Africa' => '27',
    'S. Georgia and S. Sandwich Is.' => '500',
    'Spain' => '34',
    'Sri Lanka (ex-Ceilan)' => '94',
    'Sudan' => '249',
    'Suriname' => '597',
    'Svalbard and Jan Mayen Islands' => '79',
    'Swaziland' => '41',
    'Sweden' => '46',
    'Switzerland' => '41',
    'Syrian Arab Republic' => '963',
    'Taiwan' => '886',
    'Tajikistan' => '992',
    'Tanzania' => '255',
    'Thailand' => '66',
    'Timor-Leste (East Timor)' => '670',
    'Togo' => '228',
    'Tokelau' => '690',
    'Tonga' => '676',
    'Trinidad and Tobago' => '1 868',
    'Tunisia' => '216',
    'Turkey' => '90',
    'Turkmenistan' => '993',
    'Turks and Caicos Islands' => '1 649',
    'Tuvalu' => '688',
    'Uganda' => '256',
    'Ukraine' => '380',
    'United Arab Emirates' => '971',
    'United Kingdom' => '44',
    'United States' => '1',
    'US Minor Outlying Islands' => '808',
    'Uruguay' => '598',
    'Uzbekistan' => '998',
    'Vanuatu' => '678',
    'Vatican City State (Holy See)' => '379',
    'Venezuela' => '58',
    'Viet Nam' => '84',
    'Virgin Islands, British' => '1284',
    'Virgin Islands, U.S.' => '1340',
    'Wallis and Futuna' => '681',
    'Western Sahara' => '212',
    'Yemen' => '967',
    'Zambia' => '260',
    'Zimbabwe' => '263',
);
有人基於上頭的資訊,做成底下的function:
/**
     * Returns array with country codes
     * @param stirng $zone Country zone, optional
     * @param stirng $code Specific country code (to get country name), optional
     * @return array Where keys are country codes and values are country names in english
     */
    function countryCodeArray($zone = 'all', $code = '')
    {
        $returnData = false;
        $country_array = array(
            'Africa' => array('DZ' => 'Algeria', 'AO' => 'Angola', 'BJ' => 'Benin', 'BW' => 'Botswana', 'BF' => 'Burkina Faso', 'BI' => 'Burundi', 'CM' => 'Cameroon', 'CV' => 'Cape Verde', 'CF' => 'Central African Republic', 'TD' => 'Chad', 'KM' => 'Comoros', 'CD' => 'Congo [DRC]', 'CG' => 'Congo [Republic]', 'DJ' => 'Djibouti', 'EG' => 'Egypt', 'GQ' => 'Equatorial Guinea', 'ER' => 'Eritrea', 'ET' => 'Ethiopia', 'GA' => 'Gabon', 'GM' => 'Gambia', 'GH' => 'Ghana', 'GN' => 'Guinea', 'GW' => 'Guinea-Bissau', 'CI' => 'Ivory Coast', 'KE' => 'Kenya', 'LS' => 'Lesotho', 'LR' => 'Liberia', 'LY' => 'Libya', 'MG' => 'Madagascar', 'MW' => 'Malawi', 'ML' => 'Mali', 'MR' => 'Mauritania', 'MU' => 'Mauritius', 'YT' => 'Mayotte', 'MA' => 'Morocco', 'MZ' => 'Mozambique', 'NA' => 'Namibia', 'NE' => 'Niger', 'NG' => 'Nigeria', 'RW' => 'Rwanda', 'RE' => 'Réunion', 'SH' => 'Saint Helena', 'SN' => 'Senegal', 'SC' => 'Seychelles', 'SL' => 'Sierra Leone', 'SO' => 'Somalia', 'ZA' => 'South Africa', 'SD' => 'Sudan', 'SZ' => 'Swaziland', 'ST' => 'São Tomé and Pr?ncipe', 'TZ' => 'Tanzania', 'TG' => 'Togo', 'TN' => 'Tunisia', 'UG' => 'Uganda', 'EH' => 'Western Sahara', 'ZM' => 'Zambia', 'ZW' => 'Zimbabwe'),
            'Antarctica' => array('AQ' => 'Antarctica', 'BV' => 'Bouvet Island', 'TF' => 'French Southern Territories', 'HM' => 'Heard Island and McDonald Island', 'GS' => 'South Georgia and the South Sandwich Islands'),
            'Asia' => array('AF' => 'Afghanistan', 'AM' => 'Armenia', 'AZ' => 'Azerbaijan', 'BH' => 'Bahrain', 'BD' => 'Bangladesh', 'BT' => 'Bhutan', 'IO' => 'British Indian Ocean Territory', 'BN' => 'Brunei', 'KH' => 'Cambodia', 'CN' => 'China', 'CX' => 'Christmas Island', 'CC' => 'Cocos [Keeling] Islands', 'GE' => 'Georgia', 'HK' => 'Hong Kong', 'IN' => 'India', 'ID' => 'Indonesia', 'IR' => 'Iran', 'IQ' => 'Iraq', 'IL' => 'Israel', 'JP' => 'Japan', 'JO' => 'Jordan', 'KZ' => 'Kazakhstan', 'KW' => 'Kuwait', 'KG' => 'Kyrgyzstan', 'LA' => 'Laos', 'LB' => 'Lebanon', 'MO' => 'Macau', 'MY' => 'Malaysia', 'MV' => 'Maldives', 'MN' => 'Mongolia', 'MM' => 'Myanmar [Burma]', 'NP' => 'Nepal', 'KP' => 'North Korea', 'OM' => 'Oman', 'PK' => 'Pakistan', 'PS' => 'Palestinian Territories', 'PH' => 'Philippines', 'QA' => 'Qatar', 'SA' => 'Saudi Arabia', 'SG' => 'Singapore', 'KR' => 'South Korea', 'LK' => 'Sri Lanka', 'SY' => 'Syria', 'TW' => 'Taiwan', 'TJ' => 'Tajikistan', 'TH' => 'Thailand', 'TR' => 'Turkey', 'TM' => 'Turkmenistan', 'AE' => 'United Arab Emirates', 'UZ' => 'Uzbekistan', 'VN' => 'Vietnam', 'YE' => 'Yemen'),
            'Europe' => array('AL' => 'Albania', 'AD' => 'Andorra', 'AT' => 'Austria', 'BY' => 'Belarus', 'BE' => 'Belgium', 'BA' => 'Bosnia and Herzegovina', 'BG' => 'Bulgaria', 'HR' => 'Croatia', 'CY' => 'Cyprus', 'CZ' => 'Czech Republic', 'DK' => 'Denmark', 'EE' => 'Estonia', 'FO' => 'Faroe Islands', 'FI' => 'Finland', 'FR' => 'France', 'DE' => 'Germany', 'GI' => 'Gibraltar', 'GR' => 'Greece', 'GG' => 'Guernsey', 'HU' => 'Hungary', 'IS' => 'Iceland', 'IE' => 'Ireland', 'IM' => 'Isle of Man', 'IT' => 'Italy', 'JE' => 'Jersey', 'XK' => 'Kosovo', 'LV' => 'Latvia', 'LI' => 'Liechtenstein', 'LT' => 'Lithuania', 'LU' => 'Luxembourg', 'MK' => 'Macedonia', 'MT' => 'Malta', 'MD' => 'Moldova', 'MC' => 'Monaco', 'ME' => 'Montenegro', 'NL' => 'Netherlands', 'NO' => 'Norway', 'PL' => 'Poland', 'PT' => 'Portugal', 'RO' => 'Romania', 'RU' => 'Russia', 'SM' => 'San Marino', 'RS' => 'Serbia', 'CS' => 'Serbia and Montenegro', 'SK' => 'Slovakia', 'SI' => 'Slovenia', 'ES' => 'Spain', 'SJ' => 'Svalbard and Jan Mayen', 'SE' => 'Sweden', 'CH' => 'Switzerland', 'UA' => 'Ukraine', 'GB' => 'United Kingdom', 'VA' => 'Vatican City', 'AX' => 'Åland Islands'),
            'North America' => array('AI' => 'Anguilla', 'AG' => 'Antigua and Barbuda', 'AW' => 'Aruba', 'BS' => 'Bahamas', 'BB' => 'Barbados', 'BZ' => 'Belize', 'BM' => 'Bermuda', 'BQ' => 'Bonaire, Saint Eustatius and Saba', 'VG' => 'British Virgin Islands', 'CA' => 'Canada', 'KY' => 'Cayman Islands', 'CR' => 'Costa Rica', 'CU' => 'Cuba', 'CW' => 'Curacao', 'DM' => 'Dominica', 'DO' => 'Dominican Republic', 'SV' => 'El Salvador', 'GL' => 'Greenland', 'GD' => 'Grenada', 'GP' => 'Guadeloupe', 'GT' => 'Guatemala', 'HT' => 'Haiti', 'HN' => 'Honduras', 'JM' => 'Jamaica', 'MQ' => 'Martinique', 'MX' => 'Mexico', 'MS' => 'Montserrat', 'AN' => 'Netherlands Antilles', 'NI' => 'Nicaragua', 'PA' => 'Panama', 'PR' => 'Puerto Rico', 'BL' => 'Saint Barthélemy', 'KN' => 'Saint Kitts and Nevis', 'LC' => 'Saint Lucia', 'MF' => 'Saint Martin', 'PM' => 'Saint Pierre and Miquelon', 'VC' => 'Saint Vincent and the Grenadines', 'SX' => 'Sint Maarten', 'TT' => 'Trinidad and Tobago', 'TC' => 'Turks and Caicos Islands', 'VI' => 'U.S. Virgin Islands', 'US' => 'United States'),
            'South America' => array('AR' => 'Argentina', 'BO' => 'Bolivia', 'BR' => 'Brazil', 'CL' => 'Chile', 'CO' => 'Colombia', 'EC' => 'Ecuador', 'FK' => 'Falkland Islands', 'GF' => 'French Guiana', 'GY' => 'Guyana', 'PY' => 'Paraguay', 'PE' => 'Peru', 'SR' => 'Suriname', 'UY' => 'Uruguay', 'VE' => 'Venezuela'),
            'Oceania' => array('AS' => 'American Samoa', 'AU' => 'Australia', 'CK' => 'Cook Islands', 'TL' => 'East Timor', 'FJ' => 'Fiji', 'PF' => 'French Polynesia', 'GU' => 'Guam', 'KI' => 'Kiribati', 'MH' => 'Marshall Islands', 'FM' => 'Micronesia', 'NR' => 'Nauru', 'NC' => 'New Caledonia', 'NZ' => 'New Zealand', 'NU' => 'Niue', 'NF' => 'Norfolk Island', 'MP' => 'Northern Mariana Islands', 'PW' => 'Palau', 'PG' => 'Papua New Guinea', 'PN' => 'Pitcairn Islands', 'WS' => 'Samoa', 'SB' => 'Solomon Islands', 'TK' => 'Tokelau', 'TO' => 'Tonga', 'TV' => 'Tuvalu', 'UM' => 'U.S. Minor Outlying Islands', 'VU' => 'Vanuatu', 'WF' => 'Wallis and Futuna')
        );
        if ($zone == 'all') {
            $returnData = $country_array;
        } elseif (isset($country_array[$zone])) {
            if ($code != '') {
                if (isset($country_array[$zone][$code])) {
                    $returnData = $country_array[$zone][$code];
                }
            } else {
                $returnData = $country_array[$zone];
            }
        }
        return $returnData;
    }


也許寫個像Create your own countries list的工具倒是。

參考網址:
List of world countries in php array
Text List of All Countries in the World
Javascript - All countries of the world in an array
Create your own countries list

2014年7月11日 星期五

Wordpress開發筆記-check If page is something then do something

正好有個需求是在某個叫test的page中時,若是在這個page時沒有login,那麼要導向首頁

function check_not_login() {

    if( is_page('test') && is_user_login() ) {
        wp_redirect(home_url());
        die();
    }
}
add_action('init','check_not_login');

查到這篇Wordpress is_page() always returned with false,再仔細看看Plugin API/Action Referenc的說明,看起來不能用hook在init中,要改用wp

function check_not_login() {

    if( is_page('test') && is_user_login() ) {
        wp_redirect(home_url());
        die();
    }
}
add_action('wp','check_not_login');

像這樣就ok了,對wordpress的action還真的不是太懂

參考網址:
Wordpress is_page() always returned with false
Plugin API/Action Referenc

CentOS筆記-grep指令

grep指令在查log時非常好用,以前不知道時都寫程式去parse,不過用grep後就不再寫了,它可以和cut或wc等一塊用,不過這邊就只紀錄一下grep簡單用法,免得我老是要查找

grep 'test' xxx.log //找單一檔案中的test那幾行
grep -v 'test' xxx.log //找單一檔案中的沒有test的那幾行
grep --color=auto 'test' xxx.log //找單一檔案,且字用顏色標起來
grep -n --color=auto 'test' xxx.log //找單一檔案,標出行號
grep -n -A3 -B2 --color=auto 'test' xxx.log //找單一檔案,有這個字的前2行後3行
grep -n -A3 -B2 --color=auto 'test' xxx.log.2014-07* //找多個檔案
grep -n -A3 -B2 --color=auto 't[a-z]st' xxx.log.2014-07* //找多個檔案中,t(任意小寫)st的字,使用正規表示式

暫時記到這裡!

參考網址:
鳥哥的 Linux 私房菜-第十一章、認識與學習 BASH -grep
鳥哥的 Linux 私房菜-第十二章、正規表示法與文件格式化處理-grep

2014年7月5日 星期六

MySQL學習筆記-更動Root密碼

重新安裝Mysql後,發現所有設定都消失了,就要重新再設定,開啟後第一步就是想說先設定root密碼,command如下,但enter按太快,就這樣設下去了

mysqladmin -u root password newpass

之後想重設定如下,都只會有

$ mysqladmin -u root -p 'newpass' password
Enter password: 
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: NO)'

不論我怎設定都不行,查了半天,看來這問題很多人都有,這邊做個記錄好了。我的系統是Mac,所以在停止Mysql server的command會有不同

$ sudo /usr/local/mysql/support-files/mysql.server stop
$ sudo mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
$ mysql -u root mysql
mysql> UPDATE user SET Password=PASSWORD('你的密碼') where USER='root';
mysql> quit;
$ sudo /usr/local/mysql/support-files/mysql.server start
$ mysql -u root -p
Enter password: 

能進入mysql>就表示ok囉

參考網址:
MySQL Change root Password
解決 MYSQL 登入時,ERROR 1045 (28000) using password: NO 的方法
mysql Access denied for user root@localhost错误解决方法总结

2014年6月28日 星期六

WordPress開發筆記-更動Theme後發生post的feature image無法顯示預設上傳功能

今天遇到個問題,就是在後台要設定某篇post的feature image時,卻發現沒有跳出預設的上傳視窗,如下圖:



正常是要看到下圖:


查半天查不出來,不知道是不是只有我有這問題,後來比對其他預設的theme,才發現在要加上底下這行:
add_theme_support( 'post-thumbnails' );

我是加在function.php,且定義個my_setup這function,放在它之中,再學其他的theme,寫成
add_action( 'after_setup_theme', 'my_setup' );

這樣就可以正常取得第二張圖的功能囉~

PS. 不知道這是不是3.X之後才有的,之前從來不用加這個的啊~ 還是因為我都用plugin或post_register就用掉了~(抓頭)

2014年6月22日 星期日

Sybase學習筆記-使用HenPlus下command方式使用Sybase

由於Sybase資料庫保密及系統只能使用文字模式的問題,造成我只能使用command line的方式來查詢資料庫,找半天資料都大多都是要安裝Sybase ASE或iSQL,但又似乎無法只安裝iSQL,雖然有免費的開發版本,但連下載Sybase的軟體的link都要找半天,找到了還要註冊一堆資料,雖然弄好了,但安裝設定一堆有的沒的,我放棄了,最後到Open Source SQL Clients in Java找到了HenPlus

HenPlus下載連結: http://sourceforge.net/projects/henplus/files/

下載完後,要先用ANT做compiler,要先安裝JDK,下載ANT(Download),ANT下載後直接在家目錄下解壓縮。設定PATH參數

$export PATH=$PATH:~/(剛才解壓縮的ant目錄)/bin

再來將下載的HenPlus解壓縮
$tar jxvf henplus-0.9.8.tar.gz
$cd henplus-0.9.8
$ant jar

在這邊要注意的是,若是用root安裝,可以用ant install,但若是個人使用而已,則用ant jar。

最後compiler好後,就可以用底下的執令來
$cd bin
$./henplus

再來是加入Driver,先列出目前預設的。
Hen*Plus> list-drivers
------------+---------------------------------+---------+-----------------------------------------+
    for     |          driver class           | Version |               sample url                |
------------+---------------------------------+---------+-----------------------------------------+
   Adabas   | de.sag.jdbc.adabasd.ADriver     | [NULL]  | jdbc:adabasd://localhost:7200/work      |
   DB2      | COM.ibm.db2.jdbc.net.DB2Driver  | [NULL]  | jdbc:db2://localhost:6789/foobar        |
 * MySQL    | org.gjt.mm.mysql.Driver         | 5.0     | jdbc:mysql://localhost/foobar           |
   Oracle   | oracle.jdbc.driver.OracleDriver | [NULL]  | jdbc:oracle:thin:@localhost:1521:ORCL   |
   SAP-DB   | com.sap.dbtech.jdbc.DriverSapDB | [NULL]  | jdbc:sapdb://localhost/foobar           |
------------+---------------------------------+---------+-----------------------------------------+

我主要是要sybase,要先找到jconn3(jconn3.jar下載),我是放在henplus-0.9.8/lib底下,然後打底下的命令
Hen*Plus> register sybase com.sybase.jdbc3.jdbc.SybDriver jdbc:sybase:Tds:localhost:xxxx/db
Hen*Plus> list-drivers
loaded drivers are marked with '*' (otherwise not found in CLASSPATH)
------------+---------------------------------+---------+-----------------------------------------+
    for     |          driver class           | Version |               sample url                |
------------+---------------------------------+---------+-----------------------------------------+
   Adabas   | de.sag.jdbc.adabasd.ADriver     | [NULL]  | jdbc:adabasd://localhost:7200/work      |
   DB2      | COM.ibm.db2.jdbc.net.DB2Driver  | [NULL]  | jdbc:db2://localhost:6789/foobar        |
 * MySQL    | org.gjt.mm.mysql.Driver         | 5.0     | jdbc:mysql://localhost/foobar           |
   Oracle   | oracle.jdbc.driver.OracleDriver | [NULL]  | jdbc:oracle:thin:@localhost:1521:ORCL   |
   SAP-DB   | com.sap.dbtech.jdbc.DriverSapDB | [NULL]  | jdbc:sapdb://localhost/foobar           |
 * sybase   | com.sybase.jdbc3.jdbc.SybDriver | 6.0     | jdbc:sybase:Tds:localhost:xxxx/db       |
------------+---------------------------------+---------+-----------------------------------------+

再來連結資料庫,用connect指令
Hen*Plus> connect jdbc:sybase:Tds:localhost:xxxx/db
HenPlus II connecting 
 url 'jdbc:sybase:Tds:localhost:xxxx/db
 driver version 6.0
JZ004: DriverManager.getConnection(..., 属性) 中缺少用户名属性。
============ authorization required ===
Username: xxxx
Password: 

出現底下訊息就表示連結上囉
 Adaptive Server Enterprise - ............
 read uncommitted 
 read committed *
 serializable 
xxxx@sybase:db> 

再來就是用SQL查詢啦~~

參考網址:
HenPlus - JDBC SQL-shell
Open Source SQL Clients in Java

2014年6月19日 星期四

WordPress開發筆記-將textarea改用WordPress內建的編輯器

現在要在wordpress中加入內建的編輯器真簡單,只要使用wp_editor就行了

Function Reference/wp editor中,如下
wp_editor( $content, $editor_id, $settings = array() );

例如,要創建一個id及name是description的編輯框,就如下:
$args = array("textarea_rows" => 5, "textarea_name" => "description");
wp_editor($notes, "description", $args);

這樣就可以用了,不過這function只有3.3之後才有囉

參考網址
Add rich text editor(s)… the right way
Function Reference/wp editor

2014年5月27日 星期二

SQL學習筆記-資料中單引號問題

一個蠻白痴的問題,若要做insert,而你的資料中正好又有單引號('),如:'123'456這時要怎寫?

insert into table_name (val1,val2) values ('''123''456','789');

還真是有夠怪的寫法,要記錄一下

2014年5月14日 星期三

WordPress開發筆記-如何得知目前在Admin的哪個頁面

有時在自訂post type的管理時,需要知道目前正在哪個管理頁面,針對那個頁面做特別處理,之前都是去看post type的值,後來發現其實用get_current_screen比較準確,他的id值就可以知道目前在管理列表或在編輯post,如:

post type為abc
在管理列表頁時,get_current_screenid=edit-abc
在編輯post時,get_current_screenid=abc

若是編輯使用者時,則為
在管理列表頁時,get_current_screenid=users
在編輯post時,get_current_screenid=user-edit

另外,也可以用$pagenow,不過在post type上就不像screen那麼明確,同上例的結果如下
post type為abc
在管理列表頁時,$pagenow=edit.php
在編輯post時,$pagenow=post.php

若是編輯使用者時,則為
在管理列表頁時,$pagenow=users.php
在編輯post時,$pagenow=user-edit.php

2014年5月9日 星期五

CentOS筆記-Postfix fail: connect to aspmx2.googlemail.com[2607:f8b0:4001:c03::1b]:25: Network is unreachable

出現底下的問題:
connect to aspmx2.googlemail.com[2607:f8b0:4001:c03::1b]:25: Network is unreachable

Google了一下看到這篇[SOLVED] Postfix Connection failure (outbound)?,看起來是CentOS 6才有問題,修改了vim /etc/postfix/main.cf裡頭的資訊

vim /etc/postfix/main.cf
inet_interfaces = all //原設定為localhost
inet_protocols = ipv4 //原設定為all

重新啟動即可
/sbin/service postfix restart

參考網址:
[SOLVED] Postfix Connection failure (outbound)?

2014年4月24日 星期四

Mac學習筆記-MySQL on Mac啟動Log file

在Mac中裝好MySQL後,想說寫程式時能看到正在執行的sql,不過我用的是mac的dmg檔直接安裝,看起來跟一般Linux設定不同,做法如下:

Step1. 新增~/.my.cnf

touch ~/.my.cnf
vim ~/.my.cnf

Step2. 加入底下的內容

general_log_file = /tmp/mysql.log
general_log = 1

Step3. 重啟MySQL

/usr/local/mysql/support-files/mysql.server restart

參考網址:
How and When To Enable MySQL Logs
MySQL 設定 記錄執行過的 SQL 語法

一份不錯的socket program的文件

一份不錯的socket program的文件“Beej's Guide to Network Programming”,在這邊做個紀錄

中文網站:Beej's Guide to Network Programming 正體中文版
PDF: https://drive.google.com/file/d/0Bw4lG0-ie2P6UGw4OXdlOE9scTg/edit?usp=sharing

2014年3月26日 星期三

PHP開發筆記-中英文字取子字串長度問題

之前在Wordpress開發筆記-資料內容有html special code又要呈現縮減字數問題有記錄到如何解決字數過長,將之截成同樣長字串的問題,不過那時主要的大問題在

  1. 包含一堆的html tag
  2. 中英交雜

不過說實在,那時算是隨便解解,也沒什麼好方法,這次的問題單純多,只是中英文交雜,在畫面上長過一定長度要截成相同的長度,PHP的API其實提供不錯的function了,那就是mb_strimwidth,他會將中文字(multibyte)辨識成寬度2,英文或符號為寬度1,這樣就可以解決直接辨識中英文字數,在畫面上截出的長度不同的問題。用法如下:
echo mb_strimwidth("abc 笨笨的 123", 0, 13, '...', 'UTF-8');

output:
abc 笨笨的...

不過,若是真的要截字數,就用mb_substr吧~

參考網址:
function: mb-strimwidth
Ref: mbstring
PHP 切中文字串怎麼切

2014年3月21日 星期五

PHP開發筆記-php中移除array元素要記得re-index

有時有些bug總來自於不了解使用的API或語言特性,這邊做個紀錄。

以下程式碼來自PHP Manual Language Reference Types > Arrays
<?php
// Create a simple array.
$array = array(1, 2, 3, 4, 5);
print_r($array);

// Now delete every item, but leave the array itself intact:
foreach ($array as $i => $value) {
    unset($array[$i]);
}
print_r($array);

// Append an item (note that the new key is 5, instead of 0).
$array[] = 6;
print_r($array);

// Re-index:
$array = array_values($array);
$array[] = 7;
print_r($array);
?>

結果如下:
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
Array
(
)
Array
(
    [5] => 6
)
Array
(
    [0] => 6
    [1] => 7
)

光查這種bug,就花掉一個小時,真蠢

參考網址:
PHP Manual Language Reference Types > Arrays

2014年3月16日 星期日

jQuery開發筆記-jQuery plugin - Select2 如何秀出clear標示

使用好用的WordPress plugin - Meta boxes時,看到他裡頭用了一個很不錯的jQuery Plugin - Select2。他可以讓select或input都做成像是dropdown list或div中有多重的選單,可以動態search,有興趣的人可以自行去jQuery plugin - Select2看。

最近太忙,這篇依然是記錄用,主要需求是,當使用Meta boxes時,又要自己加入dropdown list,總希望能長的一樣,但偏偏就是少個"x"(就是clear或close)的圖示,後來查了下資料,發現有clear的選項,所以加入底下的code

$('#test').select({allowClear:true});

想說這樣就ok了,但發現並沒有,要再多個placeholder這個參數才會如自己所想要

$('#test').select({placeholder:'',allowClear:true});

使用別人寫好的library很方便,不過要依著別人的想法才行。

參考網址:
jQuery plugin - Select2

WordPress開發筆記-取得theme的絕對路徑

這邊記錄一下,要取得WordPress中目前正在使用的theme的實際目錄的路徑,要使用get_template_directory,然後再加上自己要用的file的路徑,例如:要取的theme下的inc的demo.php要用

echo get_template_directory().'/inc/demo.php';

參考網址:
Function Reference/get template directory

2014年3月6日 星期四

PHP開發筆記-刪除cookie

我個人很少使用Cookie,也許是我對他的安全性有疑慮,不過不管如何,還是有它方便的時候,不過在在PHP上使用時(也許其他語言也是,但我太少用),用PHP所述的unset方式,卻發現使用後,依舊存在在browser中,令我覺得奇怪,Google了一下,發現這問題還真是很多人都遇到,依w3schools上所述,是用設定過期的方式,如下例(截自PHP Cookies-w3schools):

setcookie("user", "", time()-3600);

這樣就可以了,是有些網頁講要在之前用unset,但我覺得後面又set,完全多餘的。

參考網址:
Remove a cookie
PHP Cookies-w3schools
PHP: Delete or Expire a Browser Cookie

PHP開發筆記-隨機做出亂數字串

需求:做出一個10個字元的亂數字串,不能重覆

要完成這需求我能想到的只有MD5或sha1這類的東西,不過MD5產生出來的太長,而且試過的結果,我加入time()或datetime,產生出來的結果都相同,找半天才發現,原來還有個unique的函數可用,大概用法如下

$temp = md5(uniqid(rand(), true));
substr( $temp,-10); //我個人是取最後10個字

這樣就可以達到這要求了。uniqid的第二個參數不見得要有,不過uniqid提到在cywin底下是有需要加入的。

參考網址:
PHP: How to generate a random, unique, alphanumeric string?
uniqid

2014年2月15日 星期六

Mac學習筆記-將Sites中網頁資料放至dropbox

接續這篇Mac學習筆記-Mac OS X 10.8.x 配置Apache PHP MySQL,今天想將project放在Dropbox,但又希望http的設定仍然存取使用者家目錄下的Sites,這時可以使用soft link的方式,步驟如下:

Step1. 先做連結

ln -s {路徑}/Dropbox/target target

Step2. 所有目錄要開755的權限,底下只做Dropbox及target這兩個目錄的更動記得Dropbox上的所有目錄也要相同權限

chmod 777 Dropbox
chmod 777 Dropbox/target

Step3. 更動apache user conf。

這部分我使用user底下的目錄,所以是在/private/etc/apache2/users底下更動,我個人照Mac學習筆記-Mac OS X 10.8.x 配置Apache PHP MySQL所寫處理,所以設定是在alvin.conf。

vim /private/etc/apache2/users/alvin.conf

在Options最後加上FollowSymLinks,再重開啟apache server即可。


參考網址:
Getting Apache to host Dropbox files on a mac

2014年1月26日 星期日

iOS開發筆記 - iOS6及iOS7的Status bar的Interface問題

在iOS6及iOS7上,最上面的Status bar最大不同就是一個是佔了20px,一個是變成可以使用且讓電池或訊號的影示浮貼在上面。但這就造成一個很大的問題,在iOS6上面看來好好的,到了iOS7上就往上移了20px,在Interface builder上往下調20px,在iOS6上也真的往下調了20px,這真完全不是我要的,而偏偏我沒有用autolayout(說實在是不知道怎使用,跟storyboard一樣難用),找半天發現Interface builder多了底下的調整選項


這部分就是調整兩者間的差距,例如我這邊選的UIImageView的大小是548*320,在iPhone5的iOS6的是正常的,但在iOS7就會整個往上移了20px,所以要像底下這樣設定
Y設20,但iOS 6/7 Deltas的△Y部分要設-20,這樣這UIImageView在這兩個系統中都是在你想要的位置上了。


參考網址:
Supporting iOS 6
iOS 7 is out, but we can't completely forget about iOS 6 just yet.
點我參考Apple的iOS 7 UI轉換指南PDF

2014年1月23日 星期四

iOS開發筆記 -放入舊的SDKs

弄完了PHP的單元測試,回過頭來看看iOS的,下載了最新的xCode 5.0.2(安裝網址)並安裝後,發現整個介面變了,完全是iOS 7的介面。這這這!!!第一個想到的是那SDK 6.0會不會也不在了,開Terminator
$ cd /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs
$ ls -l

果然剩iOS 7.0了,其實是沒太大的差別,以前也一直都這樣,不過這次改變實在太大,加上舊專案都還在用,還是最好有舊的SDK比較放心,查了下資料,可以去下載以前舊的xcode版本或找出舊版xcode,好在我都有備份,將dmg打開,開Terminator,基本上都是放在/Volumes,目錄名稱叫Xcode
$ cd /Volumes/Xcode/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/
$ ls

我這邊是看到iPhoneOS6.1.sdk,所以就
$ mkdir /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk
$ cp -ri iPhoneOS6.1.sdk/ /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk

其他版本也一樣的做法。

How To Support Old iOS SDK’s in Xcode 5文中提到
Update: @rekle points out that it’s possible to get older versions of Xcode from the Downloads area of the Apple Dev Center. Even once Xcode 5 hits the App Store, it should still be possible to get an old SDK version.
有申請開發帳號的人,可以點擊get older versions of Xcode,登入後找以前的版本下載

PS. 建議是在安裝前,先將這目錄下的原SDK都先備份起來,若有同名的,當然是以新的為主

參考網址:
iOS 5 SDK is gone after upgrade to Xcode 4.5
How To Support Old iOS SDK’s in Xcode 5

測試開發學習筆記-PHP篇

從上星期一弄到昨天,將整個從Mac的PHP開發環境安裝一直到WordPress的TDD及BDD的安裝整理完畢,照著整個過程列出,方便自己及大家看。

  1. Mac學習筆記-Mac OS X 10.8.x 配置Apache PHP MySQL
  2. PHPUnit學習筆記-PHPUnit安裝(使用PEAR)
  3. PHPUnit學習筆記-測試的第一步
  4. Wordpress開發筆記-執行WordPress的Unit test的第一步
  5. WordPress開發筆記-一個簡單的WordPress Plugin Unit Test
  6. WordPress開發筆記-使用Selenium測試的第一步
看起來還有很多要再加上去,後續再更新

2014年1月22日 星期三

WordPress開發筆記-使用Selenium測試的第一步

WordPress開發筆記-一個簡單的WordPress Plugin Unit Test這篇寫的是TDD(Test Deriven Develpment),另有一種是BDD(Behavior Deriven Develpment),這次來寫寫BDD的測試。

查了不少資料,看起來都是指向Selenium,另外有Behavior,但看來是給Python用,不知道還有沒有其他的,不過也不想再花時間找了,這邊一樣以WordPress當例子,但只會介紹到環境建好,剩下的就像是在開發WordPress的流程,就由看倌再自己找囉~

開始之前可以先看看這影片How to Use Selenium 2 With PHPUnit

Step1 安裝Selenium2 Server及phpunit/PHPUnit_Selenium套件

http://phpunit.de/manual/3.7/en/selenium.html可以找到下載點,如下:

點選"Selenium Server"後,再點選紅框部分,就可以取得jar檔。

執行底下指令將jar檔移到/usr/local及啟動(PS. 在影片中是移到/usr/local/bin,但在mac中,那邊是只有root可以執行的,有點麻煩)
$ sudo mv selenium-server-standalone-2.39.0.jar /usr/local
$ java -jar /usr/local/selenium-server-standalone-2.39.0.jar

接下安裝phpunit/PHPUnit_Selenium,若沒有裝過PHPUnit可以參考PHPUnit學習筆記-PHPUnit安裝(使用PEAR),另外文中提及安裝PHPUnit_Selenium,指令如下
$sudo pear install phpunit/PHPUnit_Selenium

若沒有成功,可能是沒有找到,可以先下底下指令,再執行剛才的指令
$sudo pear channel-discover pear.symfony-project.com 
$sudo pear channel-discover pear.symfony.com

Step2. 安裝好一個WordPress
安裝WordPress的方式,這邊就不多說了,大概流程如下:
下載WordPress繁中版(這裡
-> 解開後直接放在自己目錄下的Sites,命名為wordpress
-> 建立Database
-> 設定wp-config.php
-> 開啟http://localhost/~alvin/wordpress,做初始設定,按下一步即可。
我這邊網誌是命為Hello WordPress,使用者admin,密碼1234。

Step3. 寫第一個測試
cd ~/Sites/
vim WpSeleniumTest.php
加入底下的程式
<?php
class WpSeleniumTest extends PHPUnit_Extensions_Selenium2TestCase
{
    protected function setUp()
    {
        $this->setHost('localhost');
        $this->setPort(4444);
        $this->setBrowser('firefox');
        $this->setBrowserUrl('http://localhost/wordpress/');
    }

    public function testTitle()
    {
        $this->url('http://localhost/~alvin/longda/');
        $this->assertRegExp('/Hello WordPress/i', $this->title());
    }
}
?>

執行
phpunit WpSeleniumTest.php

得到這結果

這邊就可以看到ok了,另外再測測登入的功能加入底下的程式
public function testLogin()
{
    $this->url('http://localhost/~alvin/wordpress/wp-login.php');
    $form = $this->byCssSelector('form');

    $action_page = $form->attribute('action');
    $this->assertStringEndsWith('wp-login.php',$action_page);
    $this->byId('user_login')->value('admin');
    $this->byId('user_pass')->value('1234');
    $form->submit();

    $this->url('http://localhost/~alvin/wordpress/wp-admin');
    $this->assertRegExp('/Hello WordPress/i',$this->title());
}

再度ok


當然也可以用別的帳密測測,會得到Fail,因為沒登入成功,我這邊是硬要它轉用wp-admin,因為title不是期望的,所以Fail。

PS. 命名php檔要注意,不能用"_"分隔,否則會被取代成"/",而造成找不到檔案。

Selenium預設是只支援FireFox,所以要支援其他browser要另外再安裝其他的WebDriver,可以仔細看看http://docs.seleniumhq.org/download/

感覺起來,用先用BDD建起一個期望的行為,再建立WordPress的function時,再用TDD的概念將每個function建起它的unit test,就可以建構完整的測試,之後修改就不怕會有side effect,當然超出預期的動作或結果也是會有的,但也可以藉此一個個補強囉

PS. 看到一篇文章不要盲目的 BDD / TDD,我對寫測試的看法,嗯~就看看囉!

參考網址:
Chapter 17. PHPUnit and Selenium
Selenium Downloads
https://github.com/stuartherbert/sublime-phpunit