解決 WordPress 中 Google Web Font 被 GFW 屏蔽問題

自從本 blog 的 wordpress 升級並更換 theme 後,偶爾發現 page load 得很慢,用 chrome 自帶的 developer tool 查了一下才發現原來 theme 用了 google web font,因爲 google service 被牆了所以在大陸瀏覽就像卡住了一樣。由於我絕大部份時間都是科學上網,所以一開始沒有發現問題。

Google font is blocked in China

既然 google web font 不能用,最簡單就是乾脆不用,把相關代碼拿掉就行,不過這樣做的後果是網站會極其難看。上策還是把 google web font 放到 wordpress server,再將 CSS 配置好。Google 一下發現有個很棒的 shell script 能幫個大忙:https://neverpanic.de/blog/2014/03/19/downloading-google-web-fonts-for-local-hosting/,它能幫你把需要的字體下載下來並產生一個立馬能用的 CSS 文檔。

WordPress 最少有兩個地方會加載 google web font,第一是 wordpress 本身用到的 Open Sans,另外是個別 theme 可能會用到的字體。視乎你的 theme 有沒有用 google font,有可能兩個地方都要處理。

目前我用的 theme 基於 twentyfourteen(我把原來左邊的 navigation 拿掉,header 加了 facebook, twitter 等幾個 icon),best practice 是在 wordpress 的 themes 目錄開一個叫 twentyfourteen-child 的新目錄,裏面只放用來 override parent theme 的東西。

解決問題的具體步驟如下:

  1. 下載剛才介紹過的網站的 shell script,我把它命名爲 downloadgooglefont.sh
  2. 先下載 wordpress 會下載的所有 Open Sans 字體(包括不同粗幼和斜體),在 downloadgooglefont.sh 裏要指定它們:
    families+=('Open Sans:300')
    families+=('Open Sans:400')
    families+=('Open Sans:600')
    families+=('Open Sans:300italic')
    families+=('Open Sans:400italic')
    families+=('Open Sans:600italic')
    
  3. 針對你使用的 theme,需要把 theme 用到的 google 字體寫進去。你要查一下你的 theme 會用到什麼字體,例如 twentyfourteen 會用到的字體在 themes/twentyfourteen/functions.php 的 215 行有提到:
                            'family' => urlencode( 'Lato:300,400,700,900,300italic,400italic,700italic' ),
    

    總共有7個字體,所以 script 需要加上:

    families+=('Lato:300')
    families+=('Lato:400')
    families+=('Lato:700')
    families+=('Lato:900')
    families+=('Lato:300italic')
    families+=('Lato:400italic')
    families+=('Lato:700italic')
    
  4. 執行該 script:
    $ bash ./downloadgooglefont.sh
    Downloading Open Sans:300... eot woff ttf svg 
    Downloading Open Sans:400... eot woff ttf svg 
    Downloading Open Sans:600... eot woff ttf svg 
    Downloading Open Sans:300italic... eot woff ttf svg 
    Downloading Open Sans:400italic... eot woff ttf svg 
    Downloading Open Sans:600italic... eot woff ttf svg 
    Downloading Lato:300... eot woff ttf svg 
    Downloading Lato:400... eot woff ttf svg 
    Downloading Lato:700... eot woff ttf svg 
    Downloading Lato:900... eot woff ttf svg 
    Downloading Lato:300italic... eot woff ttf svg 
    Downloading Lato:400italic... eot woff ttf svg 
    Downloading Lato:700italic... eot woff ttf svg 
    

    這些就是下載完的所有文檔,包括適用於 chrome/chromium, firefox, safari 會用到的字體,還有一個 CSS file:

    Lato_300italic.svg
    Lato_300italic.ttf
    Lato_300italic.woff
    Lato_300.svg
    Lato_300.woff
    Lato_400italic.svg
    Lato_400italic.ttf
    Lato_400italic.woff
    Lato_400.svg
    Lato_400.ttf
    Lato_400.woff
    Lato_700italic.svg
    Lato_700italic.ttf
    Lato_700italic.woff
    Lato_700.svg
    Lato_700.ttf
    Lato_700.woff
    Lato_900.svg
    Lato_900.ttf
    Lato_900.woff
    LNM3t2QINm
    Open_Sans_300italic.svg
    Open_Sans_300italic.ttf
    Open_Sans_300italic.woff
    Open_Sans_300.svg
    Open_Sans_300.ttf
    Open_Sans_300.woff
    Open_Sans_400italic.svg
    Open_Sans_400italic.ttf
    Open_Sans_400italic.woff
    Open_Sans_400.svg
    Open_Sans_400.ttf
    Open_Sans_400.woff
    Open_Sans_600italic.svg
    Open_Sans_600italic.ttf
    Open_Sans_600italic.woff
    Open_Sans_600.svg
    Open_Sans_600.ttf
    Open_Sans_600.woff
    font.css
    
  5. 把上面所有的字體上傳到 wordpress server 的 wp-content/themes/twentyfourteen-child/ 目錄。
  6. 把 font.css 複製到 wp-content/themes/twentyfourteen-chile/style.css。
  7. 由於 twentyfourteen 在 functions.php 通過 wp_enqueue_style 加上 fonts.googleapis.com 的 link 來加載 Lato 字體,要想辦法把它拿掉:
            // Add Lato font, used in the main stylesheet.
            wp_enqueue_style( 'twentyfourteen-lato', twentyfourteen_font_url(), array(), null );
    

    不過我不喜歡直接修改 parent theme,所以我選擇在 twentyfourteen-child 把它 dequeue 掉。注意 add_action 的 priority 必須低於 10,dequeue 才會成功。theme_enqueue_styles 的前兩行是用來禁止 Open Sans 字體從 google server 加載,第3行才是禁止 Lato。
    twentyfourteen-child/functions.php:

    <?php
    add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles', 11 );
    function theme_enqueue_styles() {
        wp_deregister_style('open-sans');
        wp_register_style('open-sans', false);
        wp_dequeue_style('twentyfourteen-lato');
        ...
    }
    ?>
    

Osaka, Kyoto and Nara Travel Tips

After more than 10 years I traveled to Japan again. This time I went with my wife to Kyoto, Nara and Osaka. Here are some tips about travelling to these areas I want to share and also serve as notes to myself if I will ever go there again in future, which I will definitely do as Kansai is so beautiful and relaxing.

Keep connected

Getting mobile internet connection was not easy in the past since you need Japan residency to get a SIM from telco, but nowadays the market is more open and there are now many options. In this travel, I used a SIM card called Fuji card (富士卡), which I bought from Taobao at 76 CNY (about 1450 JPY or 12 USD). This card has 1.8GB 3G data, valid for 7 days, but does not have any voice call and cannot be recharged after you have used it up. This card is backed by China Unicom and Softbank, in my experience the data connection and coverage is very good. If you live in Hong Kong, you can find this card in Sham Shui Po as well.

If this card does not suit your purpose, you can find other alternatives:

  1. Fuji card has a cheaper sibling which has 1GB data only, sold at 66 CNY, and a more expensive sibling which has double the data to 3.2GB, sold at 155 CNY.
  2. Before the existence of Fuji card, there was Sakura card (櫻花卡). Sakura card still exists and offers 3GB of data with 7-day validity. From what I read it is slower than Fuji card’s 7.2Mbps and lesser coverage.
  3. There is also a nameless SIM which offers unlimited data. The first 100MB data each day uses 3G speed but afterwards it falls back to 2G speed. It is cheaper but I bet no one want to suffer from slow speed especially during traveling.
  4. If your phone does not support WCDMA or you are not good at fiddling mobile phone settings, you can rent a WIFI router. More expensive, and you have to return it after the trip which I find inconvenient.
  5. So-net also sells pre-paid SIM cards at the airport, at a higher price than the Fuji card.

About Hotel

Valentine’s day is not a good day to travel to Japan not (just) because restaurants are fully booked but room rates could be more expensive than normal days 2 to 3 times, even for budget hotels like Toyoko Inn. I was very lucky that I found a really nice ryokan called Kitanoya (北野家) where the rates remained the same on the special day. Only available at Jalan, so knowing some basic Japanese is a pre-requisite. In general, check Jalan and Airbnb and you might find hidden gems.

Hotel near Kansai Airport

My flight landed almost at midnight so I chose to stay in a hotel close to the airport and began our journey the next day. There are not many options, the hotel at the airport is too expensive so I chose to stay at First Hotel. It is located in Izumisano (泉佐野) and very close to the Izumisano railway station, so you can take a train there, or if it is too late like my case, you can take the late night airport bus, the last stop is exactly the Izumisano railyway station.

The bus station is just outside the exit of the departure hall. After exiting the airport, turn right, look for the stand numbered ’12’. No need to buy ticket beforehand, pay when you take off, so better make sure you have 1000 yen notes or coins, which I did not have. Luckily the bus driver was able to find a lady who had ten 1000 yen notes to change for my 10000 yen note, otherwise I don’t know what would end up.

Travel to/from Kansai Airport/Kyoto

If your destination is either Osaka or Kyoto and your flight will land at the Kansai International airport, you will likely buy the “ICOCA and Haruka” ticket set to take advantage of the Haruka train (non-reserved seat) from airport to Osaka or Kyoto.

In my journey I made a mistake of buying the one-way instead of the round-trip, thinking that I would take the Nankai railway back as many people suggest. Later I found out my hotel actually is closer to Tennoji (天王寺) where there is JR station, so at the end I took the JR Hanwa line rapid train to the airport, that costs 1060 JPY, and in total it is more expensive than the round-trip “ICOCA and Haruka” ticket set.

The ticket set is sold on the 2nd floor of the JR office at Kansai airport, show them the passport when buying.

The railway system in Japan is complicated and could be astounding to new individual travelers, so
do your homework carefully in advance.

Travel in Kyoto

Kyoto is well connected with buses, they are very frequent and can reach all the popular attractions in the city. I took the bus almost exclusively in Kyoto, so I highly recommend buying the one-day bus pass. The pass costs 500 JPY and each bus ride costs 220 JPY, so the pass makes sense if you take more than 3 bus rides, not to mention you free your mind from worrying coins.

There are other passes that let you take subway as well, but since there are only two subway lines in Kyoto, I find bus to be more convenient and so subway passes may not be worth buying.

Travel in Nara

As Nara is not big and most attractions are close to each other, most people go to Nara as a day trip and stay in Osaka or Kyoto at night. For me, I stayed in Osaka and went to Nara in the morning by taking Kintetsu train to Nara Kintetsu station. Kintetsu has discount tickets called Nara-Ikaruga one-day pass (奈良・斑鳩1dayチケット) that offers unlimited bus rides in Nara and round-trip Kintetsu trains from/to Nara. Like Kyoto, you can go to many places just by bus so this ticket is highly recommended.

Depending on which city you buy the ticket, there are additional benefits. As I bought in Osaka, I could also enjoy unlimited rides on bus, subway and new tram in Osaka.

In Osaka, the ticket can be bought at the subway station supervisor office (駅長室), which is surprisingly located after passing the gate, which could be hard to find in maze-like Osaka subway stations.

Travel in Osaka

Osaka has a good subway system that covers most key destinations so getting a one-day pass is a smart choice.

The most basic form of the one-day pass is the Enjoy Eco card, which costs 800 JPY on week days and 600 JPY on weekends and national holidays. It can be bought directly from ticket machines. This card can be used on New Tram and bus as well, and offers discounts to some attractions like Osaka Castle and Floating Garden Observatory.

If you ever come across the Osaka Visitors’ Ticket and wonder what it is, forget it if you do not live in Hong Kong, Taiwan or Korea. It has the same benefits as the Enjoy Eco card, but with a lower price and is only sold in these 3 regions.

There are many other passes, such as Osaka Amazing Pass and Osaka Kaiyu Ticket, see which one suits you best here.

Money Matters

If you buy the “ICOCA and Haruka” ticket set, you will then have the ICOCA e-money card with 1500 JPY in it, which can be used to take trains and subways. But it is not too useful as a means in buying stuffs as many shops do not accept it, except convenient stores like 7-11. In Osaka, I found vending machines accepts PiTaPa rather than ICOCA as well.

Before leaving Japan, ICOCA can be returned at JR office and you can get 500 JPY deposit back. 220 JPY will be charged as handling fee from the remaining amount in your card, so if you have spent all the money in the card then it means no handling fee. If you think you will visit Kansai again in 10 years, you can keep the card and recharge it next time when you come back to Japan.

That’s it! Hope you enjoy Japan and find my tips useful.

Compile kernel module on Linode Debian VPS

Connection to linode hosts from within China can be poor from time to time. Choosing a suitable TCP congestion algorithm may alleviate that from insignificant to a great deal, your mileage may vary. Trying it out is always the best way to find out. Here I will show you how to do that step by step.

  1. Login to your VPS and find out the version of the running kernel

    # uname -a
    Linux jhelom 3.12.6-x86_64-linode36 #2 SMP Mon Jan 13 18:54:10 EST 2014 x86_64 GNU/Linux
    

    That is 3.12.6.

  2. Go get the 3.12.6 from kernel.org at https://www.kernel.org/pub/linux/kernel/v3.x/
  3. Extract it and copy the kernel config over from the current one.

    # tar xf linux-3.12.6.tar.xz
    # zcat /proc/config.gz > .config
    # make oldconfig
    
  4. Now, edit .config with an editor such as vi, look for the line CONFIG_TCP_CONG_ADVANCED, remove the line, and add the following lines there:

    CONFIG_TCP_CONG_ADVANCED=y
    CONFIG_TCP_CONG_BIC=m
    CONFIG_TCP_CONG_WESTWOOD=m
    CONFIG_TCP_CONG_HTCP=m
    CONFIG_TCP_CONG_HSTCP=m
    CONFIG_TCP_CONG_HYBLA=m
    CONFIG_TCP_CONG_VEGAS=m
    CONFIG_TCP_CONG_SCALABLE=m
    CONFIG_TCP_CONG_LP=m
    CONFIG_TCP_CONG_VENO=m
    CONFIG_TCP_CONG_YEAH=m
    CONFIG_TCP_CONG_ILLINOIS=m
    CONFIG_DEFAULT_CUBIC=y
    # CONFIG_DEFAULT_RENO is not set
    
  5. Now compile the kernel and the modules.

    # make -j4
    # make modules SUBDIRS=net/ipv4
    
  6. If everything goes well, you will be able to load the module.

    # sudo insmod net/ipv4/tcp_hybla.ko
    

    Check with lsmod and make sure the kernel module is loaded successfully. If not, check dmesg for any errors.

That’s it!

How to include local packages for pbuilder

The ibus-cangjie suite consists of 3 source packages: libcangjie, pycangjie and ibus-cangjie, pycangjie depends on libcangjie and ibus-cangjie depends on the other two. When you use pbuilder or its wrappers (I mainly use pbuilder-dist) to build pycangjie or ibus-cangjie, you have to make sure the depended packages are in the pbuilder chroot somehow otherwise the build will fail.

I used to build the package in the lowest level first, in this case libcangjie, then login to the pbuilder chroot with the --save-after-login argument and manually copy the built packages to where the chroot is mounted, run dpkg to install the packages, then exit the chroot. Now libcangjie is installed the chroot and so the build dependencies of pycangjie can be satisfied. This is simple, but requires quite a lot of typing.

There is a simpler way. As pbuilder puts all its built packages in a single directory, we can make the chroot use it as an apt source.

Assume packages built by your pbuilder is located in /home/ubuntu/pbuilder/sid_result, and pbuilder hooks are stored in /var/cache/pbuilder/hook.d. Now, update your .pbuilderrc like this:

# cat ~/.pbuilderrc 
HOOKDIR="/var/cache/pbuilder/hook.d"
BINDMOUNTS="/home/ubuntu/pbuilder/sid_result"

Then put a new hook script to generate a Packages file:

# cat /var/cache/pbuilder/hook.d/D70results 
cd /home/ubuntu/pbuilder/sid_result
/usr/bin/dpkg-scanpackages . /dev/null > /home/ubuntu/pbuilder/sid_result/Packages
/usr/bin/apt-get update

To verify it is set up correctly, login to the pbuilder chroot with the --override-config and --othermirror arguments and check if /etc/apt/sources.list is updated, OTHERMIRROR parameter in .pbuilderrc does not work for me so I can only use --othermirror, not nice as you need to supply it every time you run pbuilder:

# pbuilder-dist testing login --override-config --othermirror "deb [trusted=yes] file:///home/ubuntu/pbuilder/sid_result ./"

# grep -r home /etc/apt
/etc/apt/sources.list:deb [trusted=yes] file:///home/ubuntu/pbuilder/sid_result ./

If everything goes well, build your package with the --override-config and --othermirror arguments like what you just did for the login operation:

# pbuilder-dist testing build --override-config --othermirror "deb [trusted=yes] file:///home/ubuntu/pbuilder/sid_result ./" <.dsc-file>
References

Hack a fan – 自製空氣淨化器

上週日在北京 Bookworm 書店+咖啡廳參加了一個很有趣的 DIY 土炮空氣淨化器活動。北京經常被陰霾籠罩, AQI (空氣質素指數) 大部份時間處於不健康水平,在北京生活,口罩和空氣淨化器必不可少。其實空氣淨化器的構造不複雜,簡單說就是由風機抽取室內空氣,通過濾網把空氣中的顆粒隔除 。濾網包括 HEPA,活性炭等。很多市面上賣的淨化器都用 HEPA,HEPA 效能高,相對廉宜,技術成熟,能過濾 99.7% 以上 0.3 微米的粒子,所以對付 PM 2.5 很有效。當然,淨化器廠商不會只滿足於此,爲了提高利潤,都會加些有的沒的功能,譬如除甲醛、除臭、殺菌等。不過,這些 fancy 的功能往往只是錦上添花,至少在帝都這些功能遠遠比不上過濾 PM 2.5 來得重要 (沒聽說北京的細菌特別猖狂)。

說回這個 workshop,它是由一位在北京做研究的美國心理學博士生 Thomas Talhelm 舉辦的。話說他對北京的空氣污染很擔心,但又發現市面上的空氣淨化器貴的離譜,所以就開始着手研究自製淨化器,然後把結果發表到 http://particlecounting.tumblr.com/。目前他做了兩款,一款售價 200 RMB(我自己算了一下,成本大概 160 RMB),另一款過濾效能更高名叫「大炮」的賣 450 RMB。

在他的「發明」面世前,就已經有人嘗試過空氣淨化器,把過濾網放在風扇的前面(或後面),這件事誰都會幹,你想問這有什麼值得講的吧?答案就是 open data。Thomas 爲了證明他的淨化器有用,他做了非常多的實驗,在特定的環境下,採集了大量的數據,並進行對照實驗,跟量產的淨化器對比,證明他的 poor man’s air purifier 效果不比那些賣幾千塊錢的過濾器差。最重要是他把實驗方法和數據公佈在 smart air 網站particlecounting 博客,他對數據的嚴謹和認真態度都是長年在學術界鍛鍊出來的。他的朋友取笑他不願意付錢買淨化器,卻願意花 260 USD 買個粒子測量器,求真就是這個態度。
跟量產的過濾器對比

更買了各種不同的風扇,找出最好的一個用在下一代的「大炮」當中:

瘋狂的對不同風扇做實驗

他還在不斷的進行各種實驗,目前正在測試 HEPA 在長期使用下效能的變化

剛才說到 AQI,要注意 AQI 在各國的計算方法都不同。雖然美國中國所用的算式一樣,但等級分類卻有點差別。分別在於 AQI 200 以下的時候,美國所用標準要求更高,AQI 200 以上則幾乎一樣,所以會出現下面的情況,左圖按照中國標準,右圖按照美國標準,以後 quote AQI 要小心囉:

    
    
 

最後順便推薦幾款監察中國 PM 2.5 的 Android app: