首頁‎ > ‎電子期刊‎ > ‎Semantic MediaWiki‎ > ‎

MediaWiki網站架設進階設定

作者:張騉翔

前言

本文將以FAQ的方式與讀者分享MediaWiki網站架設的進階設定,以幫助讀者更深入地掌握MediaWiki。

介面

要如何擴充編輯工具列?

大標題文字

新建好的MediaWiki在編輯文章時只有少數的編輯按鈕,不過中文的Wikipedia有許多的編輯按鈕,這是如何做到的?若要新增編輯文章時的按鈕,藉由新增Java Script語法至MediaWiki:common.js頁面來達成,底下為加入一個表格的編輯按鈕。

 
if (mwCustomEditButtons) {

mwCustomEditButtons[mwCustomEditButtons.length] = {
"imageFile":"http://upload.wikimedia.org/wikipedia/en/6/60/Button_insert_table.png",
"speedTip": "Insert a table",
"tagOpen": '{| class="wikitable"\n|-\n',
"tagClose": "\n|}",
"sampleText": "! header 1\n! header 2\n! header 3\n|-\n| row 1, cell 1\n| row 1, cell 2\n| row 1, cell 3\n|-\n| row 2, cell 1\n| row 2, cell 2\n| row 2, cell 3"};
}

下面為關鍵程式碼的說明:

變數 說明
imageFile 編輯按鈕的圖片URL
speedTip 編輯按鈕提示
tagOpen 開始樣板內容
tagClose 結束樣板內容
sampleText 加入tagOpen及tagClose中間的範例內容

加入完Java Script並將MediaWiki:Common.js頁面存檔即加入了新的編輯按鈕;要注意的事,在修改完後需重新啟動Web Server以重新載入MediaWiki才能將反映至編輯頁面。底下再練習加入一個將編輯內容變成註解的按鈕。

 
if (mwCustomEditButtons) {
mwCustomEditButtons[mwCustomEditButtons.length] = {
"imageFile": "http://upload.wikimedia.org/wikipedia/en/3/34/Button_hide_comment.png",
"speedTip": "Insert hidden Comment",
"tagOpen": "",
"sampleText": "Comment"};

mwCustomEditButtons[mwCustomEditButtons.length] = {
"imageFile":"http://upload.wikimedia.org/wikipedia/en/6/60/Button_insert_table.png",
"speedTip": "Insert a table",
"tagOpen": '{| class="wikitable"\n|-\n',
"tagClose": "\n|}",
"sampleText": "! header 1\n! header 2\n! header 3\n|-\n| row 1, cell 1\n| row 1, cell 2\n| row 1, cell 3\n|-\n| row 2, cell 1\n| row 2, cell 2\n| row 2, cell 3"};
}

更多的編輯按鈕可以參考中文Wikipedia的Common.js英文Wikipedia的Common.js其中的內容。

要如何更改網站外觀

MediaWiki有樣版的設計,可以選擇不同的樣版,在每位使用者的my perferences頁面中的Skin頁面可以選擇不同的樣設,而預設的樣板為Monobook;若要更改網站的外觀可藉由安裝新的樣版或是修改現有的 樣版。若要小幅度的修改樣板,例如顏色,可以藉由樣版名稱的CSS檔案來達成,例如預設的樣版為Monobook則其CSS檔案為MediaWiki:Monobook.css,而MediaWiki:Common.css這個CSS檔案為MediaWiki會預先載入的樣式表,適用於所有樣版。

在上節中,我們加入了新的表格範例編輯按鈕,在產生的表格範例其中的class屬性為wikitable,而這個樣式在預設的MediaWiki:Common.css檔案中並沒有,我們藉由加入wikitable這個CSS樣式來練習修改MediaWiki:Common.css檔案。

 
table.wikitable,
table.prettytable {
margin: 1em 1em 1em 0;
background: #f9f9f9;
border: 1px #aaaaaa solid;
border-collapse: collapse;
}
table.wikitable th, table.wikitable td,
table.prettytable th, table.prettytable td {
border: 1px #aaaaaa solid;
padding: 0.2em;
}
table.wikitable th,
table.prettytable th {
background: #f2f2f2;
text-align: center;
}
table.wikitable caption,
table.prettytable caption {
margin-left: inherit;
margin-right: inherit;
}

在加入存檔後即可看到表格能正常的顯示樣式,同樣的在修改完MediaWiki:Common.css檔案後需要重新啟動Web Server再重新整理頁面才能正確的反映。

接下看到一個對於網站外觀簡單的修改,加入以下的CSS語法至MediaWiki:Common.css當中,存檔後重新整理即可看到網站外觀邊邊原本皆是直角型,而現在則變成圓角型,看起來有親和力多了。

 
.pBody {
padding: 0.3em 0.1em;
-moz-border-radius-topright: 0.5em;
}

 
.portlet h5 {
text-transform:none;
font-variant:normal;
background-color: #e0e3e6;
border: thin solid silver;
-moz-border-radius-topright: 0.5em;
}
#p-cactions ul li, #p-cactions ul li a {
-moz-border-radius-topright: 0.5em;
-moz-border-radius-topleft: 0.5em;
}

同樣的,讀者可以參考英文Wikipedia的Common.css以及中文Wikipedia的Common.css檔案來做更進一步的客製化。

外部連結如何開新視窗瀏覽?

MediaWiki開啟外部連結會在目前的視窗載入,然而較適合的方式是在新的視窗開啟,要達成這樣的方式,可以藉由加入JavaScript在MediaWiki:Common.js或是MediaWiki:Monobook.js中達成,以下為程式碼。其原理是在點選連結時攔載事件,將原本要在目前視窗載入改成開新視窗載入。

 
 externalLinks = function() {
if (!document.getElementsByTagName) {
return;
}
var anchors = document.getElementsByTagName("a");
for (var i = 0; i < anchors.length; i++) {
var anchor = anchors[i];
if (anchor.getAttribute("href") &&
anchor.getAttribute("rel") != null &&
(anchor.getAttribute("rel").indexOf("external") >= 0 ||
anchor.getAttribute("rel").indexOf("nofollow") >= 0)
) {
anchor.target = "_blank";
}
}
}

if (window.addEventListener) {
window.addEventListener("load", externalLinks, false);
}
else if (window.attachEvent) {
window.attachEvent("onload", externalLinks);
}

如何更改首頁連結

預設的MediaWiki首面頁面為Main Page,通常會將之移動更名為適合的首面名稱,然而首面仍是連結到Main Page再導向到新的頁面,而在首面名稱下方會出現重新導向的名字如下。

 
Redirected from Main Page

要實際的將首面設為新頁面,可以藉由編輯以下的頁面來達成;將Main Page改為新的頁面名稱即可。

MediaWiki:Mainpage

還有哪些訊息可以自定?

MediaWiki還有許多的系統訊息可以自定,這些可以在Special:Allmessages頁面中看到;對應系統程式碼儲存在languages/messages/目錄下,裡面包含100多個國家的區域設定,每個國家一個檔案,例如臺灣的檔案是MessageZH_tw.php;每個檔案包含了1575個訊息可以自定。要修改這些訊息可透過Special:Allmessages這面頁中的連結來達成,所有自訂訊息都藉由修改MediaWiki這個命名空間下的頁面來達成。

例如MediaWiki:Toolbox可以用來修改左方Toolbox的名稱、MediaWiki:Search可以用來修改左方Search的名稱;藉由系統訊息也用Wiki頁面的方式來修改,使得系統介面相當容易客製化。

如何更改提示訊息?

在MediaWiki軟體有些連結會出現提示訊息,稱之為Tooltip,這些提示訊息也可藉由增加到MediaWiki:Monobook.jsMediaWiki:Common.js來達成,例如登出的訊息為:

 
ta['pt-logout'] = new Array(,'Logout');

可將之改成:

 
ta['pt-logout'] = new Array(,'登出');

當滑鼠移到登出連結時,即會出現登出的提示訊息;以下為所有的訊息提示列表,在Special:Allmessages中的PHP頁面中也可以找到,不過因為其中包含了許多程式碼,在此列出以方便讀者查閱。

 
/* tooltips and access keys */
var ta = new Object();
ta['pt-userpage'] = new Array('.','My user page');
ta['pt-anonuserpage'] = new Array('.','The user page for the ip you\'re editing as');
ta['pt-mytalk'] = new Array('n','My talk page');
ta['pt-anontalk'] = new Array('n','Discussion about edits from this ip address');
ta['pt-preferences'] = new Array(,'My preferences');
ta['pt-watchlist'] = new Array('l','The list of pages you\'re monitoring for changes.');
ta['pt-mycontris'] = new Array('y','List of my contributions');
ta['pt-login'] = new Array('o','You are encouraged to log in, it is not mandatory however.');
ta['pt-anonlogin'] = new Array('o','You are encouraged to log in, it is not mandatory however.');
ta['pt-logout'] = new Array(,'登出');
ta['ca-talk'] = new Array('t','Discussion about the content page');
ta['ca-edit'] = new Array('e','You can edit this page. Please use the preview button before saving.');
ta['ca-addsection'] = new Array('+','Add a comment to this discussion.');
ta['ca-viewsource'] = new Array('e','This page is protected. You can view its source.');
ta['ca-history'] = new Array('h','Past versions of this page.');
ta['ca-protect'] = new Array('=','Protect this page');
ta['ca-delete'] = new Array('d','Delete this page');
ta['ca-undelete'] = new Array('d','Restore the edits done to this page before it was deleted');
ta['ca-move'] = new Array('m','Move this page');
ta['ca-watch'] = new Array('w','Add this page to your watchlist');
ta['ca-unwatch'] = new Array('w','Remove this page from your watchlist');
ta['search'] = new Array('f','Search this wiki');
ta['p-logo'] = new Array(,'Main Page');
ta['n-mainpage'] = new Array('z','Visit the Main Page');
ta['n-portal'] = new Array(,'About the project, what you can do, where to find things');
ta['n-currentevents'] = new Array(,'Find background information on current events');
ta['n-recentchanges'] = new Array('r','The list of recent changes in the wiki.');
ta['n-randompage'] = new Array('x','Load a random page');
ta['n-help'] = new Array(,'The place to find out.');
ta['n-sitesupport'] = new Array(,'Support us');
ta['t-whatlinkshere'] = new Array('j','List of all wiki pages that link here');
ta['t-recentchangeslinked'] = new Array('k','Recent changes in pages linked from this page');
ta['feed-rss'] = new Array(,'RSS feed for this page');
ta['feed-atom'] = new Array(,'Atom feed for this page');
ta['t-contributions'] = new Array(,'View the list of contributions of this user');
ta['t-emailuser'] = new Array(,'Send a mail to this user');
ta['t-upload'] = new Array('u','Upload images or media files');
ta['t-specialpages'] = new Array('q','List of all special pages');
ta['ca-nstab-main'] = new Array('c','View the content page');
ta['ca-nstab-user'] = new Array('c','View the user page');
ta['ca-nstab-media'] = new Array('c','View the media page');
ta['ca-nstab-special'] = new Array(,'This is a special page, you can\'t edit the page itself.');
ta['ca-nstab-project'] = new Array('a','View the project page');
ta['ca-nstab-image'] = new Array('c','View the image page');
ta['ca-nstab-mediawiki'] = new Array('c','View the system message');
ta['ca-nstab-template'] = new Array('c','View the template');
ta['ca-nstab-help'] = new Array('c','View the help page');
ta['ca-nstab-category'] = new Array('c','View the category page');

編輯

如何加入Category頁面的連結?

若要將某個頁面加入某個分類,可以採用如下的語法:

 
[[Category:MediaWiki]]

然而,若要實際的連到某個分類頁面,需在Category前面加上冒號:

 
[[:Category:MediaWiki]]

則可以顯示分類頁面的連結,而非將頁面進行分類。

表格如何排序?

若要能將表格根據各個欄位來進行排序,可以在表格中加入排序的Class語法,其名稱為sortable,以下為加入後的語法及結果:

語法 結果
 
 
{| class="sortable wikitable"
|-
! 字母
! 數字
|-
| a
| 1
|-
| b
| 2
|}

字母   ↓ 數字   ↓
a 1
b 2

要如何做到編輯表格的功能?

在Wikipedia的首頁,若將原始碼複製到自己的Wiki頁面來編輯,會發覺每個區塊有個Edit的連結,可以點選進去編輯,這是如何做到的呢?

底下為精簡後的表格語法,呈現的結果加入了Edit的連結,其中的關鍵在於<h2>標題</h2>標籤,若加入h1至h6的標籤,那麼在表格呈現的結果就會自動加上編輯的超連結以方便使用者編輯。hx那列會成為表格標題,而其後的表格會為其內容。


擴充

MediaWiki支援外掛程式的撰寫,外掛程式碼放置於wikipath/extensions/目錄下;在MediaWiki.org網站可以查到許多的外掛(Category:Extensions);底下我們以一個PropertyTable外掛(Extension:PropertyTable)來示範安裝方式。

ProperTable主要的目的是在於簡化Wiki Table語法,其使用方式為等號前面是欄位名稱,後面是欄位值,範例如下:


此外掛的安裝步驟如下:

下載程式碼至Extensions目錄

下載程式碼,將PropertyTable.php另存至wiki下的extensions目錄下。

於LocalSettings.php引用外掛

將下面的程式碼加入到LocalSettings.php檔案中即可使用PropertyTable的語法。

// Activate PropertyTable extension
include_once('extensions/PropertyTable.php');

在本小節以一個簡單的外掛安裝來讓讀者瞭解MediaWiki的功能,在往後的文章中會介紹更多的外掛,以及外掛的撰寫方式。

升級

軟體是否升級容易,相關的套件能否在升級後能夠使用,以及資料能否在新版中完整;是每個軟體使用者及管理者都相當關心的事。

根據筆者升級MediaWiki軟體的經驗,MediaWiki軟體在升級上相當方便以及穩定,這點可大大地的放心。尤其一般的軟體是採取 先測試後再發佈給使用者,而MediaWiki軟體剛好相反,是在Wikipedia系列網站先使用過Alpha及Beta版本穩定過後才發佈 MediaWiki軟體。這樣的過程在軟體工程上稱之為Continuous_Integration

有關MediaWiki的升級方式,可以參考Manual:Upgrading_to_1.9網頁,底下以升級至1.9為例簡述其步驟。

備份現有資料及檔案

在升級MediaWiki 1.9前需先將資料庫備份,以及主要的檔案包括:

  • LocalSettings.php設定檔。
  • 上傳檔案,一般在images目錄下。
  • 外掛及佈景檔案,外掛一般在extensions目錄下,佈景檔案則視安裝狀況而定。

取得新的程式碼並覆蓋之

在MediaWiki網站可以下載最新的程式碼(http://www.mediawiki.org/wiki/Download),將之下載並將所有檔案覆蓋取代舊的檔案,如此即完成程式碼的更新。

執行升級程式

首先要確定在Wiki的根目錄下AdminSettings.php的檔案內容,其中的wgDBadminuser及 wgDBadminpassword為資料庫root的帳號及密碼;若AdminSettings.php檔案不存在,可以用 AdminSettings.sample另存成AdminSettings.php再更新之。

接下來執行update.php程式,主要用來更新資料庫網要,在Linux下執行指令如下:

php update.php

如此即完成升級。

檢查外掛相容性及版本檢查

在升級完後需檢查每個外掛的相容性(Category:Extensions),此部分是由每個外掛的撰寫者來維護,而非MediaWiki開發團隊的責任;最後可以檢查Special:Version裡的版本編號是否為最新版本。

結語

在本文中我們討論了許多MediaWiki的議題,例如如何自定介面、如何更有效率地進行編輯,以及外掛與升級的議題;希望這些對於讀者們在使用及維護MediaWiki上有所幫助。




Comments