Mediawiki 修改

出自六年制學程
在2020年10月24日 (六) 22:24由丁志仁對話 | 貢獻所做的修訂版本

(差異) ←上個修訂 | 最新修訂 (差異) | 下一修訂→ (差異)
跳轉到: 導覽搜尋

開發參考頁

  1. 官方文件庫(英文)
  2. 中文程式修改參考文件
  3. 不錯學群限讀功能測試頁

更新快取

  • 在頁面網址列加上「?action=purge」
  • 資料夾沒有更名:編輯再儲存
  • 當修改 mediawiki 所在資料夾名稱,須同步改 LocalSettings.php 中的 $wgScriptPath 。但此時 mediawiki 仍會從快取中取得包含舊資料夾名稱的許多快取頁,導致網站完全無法運作。請依畫面指示,在相關的檔案中加一行「echo __FILE__;」,使網頁內容發生變化,導致產生新的快取,問題即可逐步排除。例如在 includes/Defines.php 中加「echo __FILE__;」。

限制某些頁面只有某些群組可讀取

wiki_user_groups表

對目標帳號加群組,如 jj

LocalSettings.php

定義 $canRead['group']

  1. 為一陣列
  2. 各元素索引為頁名
  3. 各元素值為可讀此頁之群組陣列
  4. 中文頁名須以 URL 格式寫,再以 urldecode 函式解碼

includes/Title.php

1.17版

在 Title 類別 userCanRead 方法中增加
global $wgUser, $wgGroupPermissions, $canRead;	// by jj

// 依 $canRead 判斷是否可讀本頁 by jj
if(isset($canRead['group']) && count($canRead['group'])){
	$name = $this->getPrefixedText();	// 取回本頁帶名字空間的頁名
	if(isset($canRead['group'][$name]) && is_array($canRead['group'][$name])){
		if(isset($wgUser->mGroups) && is_array($wgUser->mGroups)){
			$revoke=0;
			foreach($wgUser->mGroups as $v){if(in_array($v,$canRead['group'][$name])){$revoke+=1;}}
			if($revoke==0){
				return false;
			}
		}
	}
}

1.19~1.23.10版

userCanRead()交給了userCan('read'); userCan(動作,帳號物件,是否全套檢查)又交給了getUserPermissionsErrorsInternal(動作,帳號,是否全套,遇錯就短路);再交給checkReadPermissions(動作,帳號,錯陣列,是否全套,遇錯就短路)。

但上述這些函式只能增加「錯誤訊息」陣列,並不能阻擋共筆頁內容的顯示。所有要進行共筆頁讀取權限控管,必須從 /includes/SkinTemplate.php 中 SkinTemplate 類別的 outputPage 方法,在

$tpl->data['bodytext'] .= $tpl->data['debughtml'];
之後加上:
// 依 $canRead 判斷是否可讀本頁,by jj
global $wgUser, $canRead, $wgTitle, $wgServer, $wgScriptPath;
if(isset($canRead['group']) && count($canRead['group'])){
	$name = $wgTitle->getPrefixedText();	// 取回本頁帶名字空間的頁名
	if(isset($canRead['group'][$name]) && is_array($canRead['group'][$name])){
		if(isset($wgUser->mGroups) && is_array($wgUser->mGroups)){
			$revoke=0;
			foreach($wgUser->mGroups as $v){if(in_array($v,$canRead['group'][$name])){$revoke+=1;}}
			if($revoke==0){$tpl->data['bodycontent']='您無權限讀此頁。[<a href='.$wgServer.$wgScriptPath.'>回首頁</a>]';}
		}
	}
}

1.29~1.34.2

從 /includes/skins/SkinTemplate.php 中 SkinTemplate 類別的 prepareQuickTemplate 方法,在

$tpl->data['bodytext'] .= $tpl->data['debughtml'];
之後加上:
// 依 $canRead 判斷是否可讀本頁,by jj
global $wgUser, $canRead, $wgTitle, $wgServer, $wgScriptPath;
if(isset($canRead['group']) && count($canRead['group'])){
	$name = $wgTitle->getPrefixedText();	// 取回本頁帶名字空間的頁名
	if(isset($canRead['group'][$name]) && is_array($canRead['group'][$name])){
		$mGroups=array_keys($wgUser->getGroupMemberships());
		$revoke=0;
		foreach($mGroups as $v){if(in_array($v,$canRead['group'][$name])){$revoke+=1;}}
		if($revoke==0){$tpl->data['bodycontent']='您無權限讀此頁。[<a href='.$wgServer.$wgScriptPath.'>回首頁</a>]';}
	}
}

取群組

在 includes/user/User.php 中 User 類別的 getGroupMemberships() 方法中執行 loadGroups(),

會叫用 includes/user/UserGroupMembership.php 中 UserGroupMembership::getMembershipsForUser($this->mId,$db) ,

並將結果存入 $this->mGroupMemberships (沒呼叫getGroupMemberships()方法就不會有mGroupMemberships屬性),

其為陣列:key為該使用者存於 user_groups 表中的群組名,值為UserGroupMembership物件(有方法而無屬性)。

如果未註冊或 user_groups 表中的沒有該使用者,均傳回空陣列。

除錯

skins/Vector.php

VectorTemplate 類別中的 execute 方法中,找到 <!-- bodytext -->(1.19版為<!-- bodycontent -->)

在之下插入除錯資訊 全域變數 $jjj,然後在要除錯的程式段落中對 $jjj 派值。

{ {…} }內層嵌入

用{ {…} }嵌入之title,其運行中之變數傳不到上一層,所以無法在skin(最外層)查看變數(如上段的方法),除錯,請用:

$flog=fopen('./jjj',"a+");fwrite($flog,變數."\n");fclose($flog); // by jj

然後查看 mediawiki/jjj 檔之新寫入內容。

調整中文化訊息

languages/messages/MessagesZh_tw.php

$messages 陣列中以下元素

'badaccess-groups' => '您剛才的請求只有{{PLURAL:$2|這個|這些}}使用者群組的使用者才能使用: $1',

將「群」改成「群組」

資料表

wiki_interwiki

跨 wiki 連結增加中文維基百科 zhwikipedia:http://zh.wikipedia.org/wiki/$1

嵌入本站及跨wiki段落

詳見 includes/parser/Parser.php 中 Parser類別中的braceSubstitution方法之修改。

擴充可以用HTML標籤

  • 參考 英文維基百科說明
  • 調整 include/Sanitizer.php 中 Sanitizer.php,$htmlsingle,$htmlsingleonly,$htmlnest,$tabletags,$htmllist,$listtags 諸陣列的內容。
    1. a 標籤是有效的。
    2. iframe不但無效,而且基於安全考量,wikimedia 連 Extension:Website in iFrame 都加以移除。

支援 SVG