Jump to content

User:Jiangxin/Patch extra template namespace

From Meta, a Wikimedia project coordination wiki


This patch for MediaWiki 1.5.x only!

MediaWiki version above 1.6.x, a new table mw_templatelinks will handle this case, and is more versatile.


Extra Template namespace

for MediaWiki 1.5.x only. MediaWiki 1.6.x already support similar feature.

Any page can be used as transclusion, but pages in template namespace is different:

  • they are listed on the edit page of pages that call the template;
  • when a page in the template namespace is changed, all the other pages that link to it automatically have their server caches (as opposed to browser cache) purged. With other namespaces, you need to purge the server cache manually.

I have exported wiki helps from http://meta.wikimedia.org, but after import into my own wiki, I find it will be better moving pages in orignal meta templates into other namespace. This patch I wrote will add "template like feature" support to other namespaces user defined.

It is very simple, administrators just add wanted namespaces into an array, named $wgExtraTemplateNS.

只对 MediaWiki 1.5.x 有效。MediaWiki 1.6.x 通过增加 mw_templatelinks 数据表支持类似功能。

虽然除了 Template 命名空间之外的页面也可以被当做模板被页面包含,但是 Template 名字空间具有其它命名空间不具有的特性(详见 Help:Template):

  • 在页面编辑时,引用的模板显示在编辑框下方;
  • 当处于 Template 名字空间的模板被修改后,其它引用该模板的文件被自动更新(touched)。而其它名字空间的页面如果被当做模板来使用,如果该文件修改,引用它的文件必须重新打开并编辑后才能更新。

下面这个补丁,增加了一个数组 $wgExtraTemplateNS。该数组中定义的名字空间将被赋予和 Template 名字空间相同的特性。

Patch


Index: includes/Article.php
===================================================================
RCS file: /user/jiangxin/project/wiki/mediawiki/src/includes/Article.php,v
retrieving revision 1.2
diff -u -r1.2 Article.php
--- includes/Article.php	2 Nov 2005 08:04:27 -0000	1.2
+++ includes/Article.php	27 Nov 2005 17:38:57 -0000
@@ -1194,6 +1194,7 @@
 		global $wgOut, $wgUser;
 		global $wgDBtransactions, $wgMwRedir;
 		global $wgUseSquid, $wgInternalServer, $wgPostCommitUpdateList, $wgUseFileCache;
+		global $wgExtraTemplateNS;
 
 		$fname = 'Article::updateArticle';
 		wfProfileIn( $fname );
@@ -1287,7 +1288,9 @@
 			$urls = array();
 			# Template namespace
 			# Purge all articles linking here
-			if ( $this->mTitle->getNamespace() == NS_TEMPLATE) {
+			## OpenSourceXpress: ns in $wgExtraTemplateNS, works like template namespace.
+			$myns = $this->mTitle->getNamespace();
+			if ( $myns == NS_TEMPLATE || (is_array($wgExtraTemplateNS) && in_array($myns, $wgExtraTemplateNS)) ) {
 				$titles = $this->mTitle->getLinksTo();
 				Title::touchArray( $titles );
 				if ( $wgUseSquid ) {
@@ -2471,23 +2474,32 @@
 	 * @return array
 	 */
 	function getUsedTemplates() {
+		global $wgExtraTemplateNS;
+		
 		$result = array();
 		$id = $this->mTitle->getArticleID();
 
 		$db =& wfGetDB( DB_SLAVE );
-		$res = $db->select( array( 'pagelinks' ),
-			array( 'pl_title' ),
-			array(
-				'pl_from' => $id,
-				'pl_namespace' => NS_TEMPLATE ),
-			'Article:getUsedTemplates' );
-		if ( false !== $res ) {
-			if ( $db->numRows( $res ) ) {
-				while ( $row = $db->fetchObject( $res ) ) {
-					$result[] = $row->pl_title;
+		
+		## OpenSourceXpress: ns in $wgExtraTemplateNS, works like template namespace.
+		$mynsitem = NS_TEMPLATE;
+		do {
+			$res = $db->select( array( 'pagelinks' ),
+				array( 'pl_title' ),
+				array(
+					'pl_from' => $id,
+					'pl_namespace' => $mynsitem ),
+				'Article:getUsedTemplates' );
+
+			if ( false !== $res ) {
+				if ( $db->numRows( $res ) ) {
+					while ( $row = $db->fetchObject( $res ) ) {
+						$result[] = "$mynsitem" . ":" . $row->pl_title;
+					}
 				}
 			}
-		}
+		} while ( is_array($wgExtraTemplateNS) && $mynsitem = array_shift($wgExtraTemplateNS) );
+
 		$db->freeResult( $res );
 		return $result;
 	}
Index: includes/DefaultSettings.php
===================================================================
RCS file: /user/jiangxin/project/wiki/mediawiki/src/includes/DefaultSettings.php,v
retrieving revision 1.9
diff -u -r1.9 DefaultSettings.php
--- includes/DefaultSettings.php	25 Nov 2005 02:13:01 -0000	1.9
+++ includes/DefaultSettings.php	27 Nov 2005 17:38:57 -0000
@@ -1067,8 +1067,17 @@
  	NS_MEDIAWIKI_TALK => true,
  	NS_TEMPLATE_TALK  => true,
  	NS_HELP_TALK      => true,
- 	NS_CATEGORY_TALK  => true
+ 	NS_CATEGORY_TALK  => true,
+	NS_OSXMETA        => true,
+	NS_OSXMETA_TALK   => true,
+	NS_OSXHELP        => true,
+	NS_OSXHELP_TALK   => true,
  );
+
+## OpenSourceXpress: ns in $wgExtraTemplateNS, works like template namespace.
+$wgExtraTemplateNS = array(
+	NS_OSXMETA,
+);
 
 $wgNamespacesToBeSearchedDefault = array(
 	NS_MAIN           => true,
Index: includes/Defines.php
===================================================================
RCS file: /user/jiangxin/project/wiki/mediawiki/src/includes/Defines.php,v
retrieving revision 1.2
diff -u -r1.2 Defines.php
--- includes/Defines.php	25 Nov 2005 02:13:01 -0000	1.2
+++ includes/Defines.php	27 Nov 2005 17:38:57 -0000
@@ -46,6 +46,10 @@
 define('NS_HELP_TALK', 13);
 define('NS_CATEGORY', 14);
 define('NS_CATEGORY_TALK', 15);
+define('NS_OSXMETA', 80);
+define('NS_OSXMETA_TALK', 81);
+define('NS_OSXHELP', 82);
+define('NS_OSXHELP_TALK', 83);
 /**#@-*/
 
 /**
Index: includes/EditPage.php
===================================================================
RCS file: /user/jiangxin/project/wiki/mediawiki/src/includes/EditPage.php,v
retrieving revision 1.5
diff -u -r1.5 EditPage.php
--- includes/EditPage.php	13 Nov 2005 15:57:20 -0000	1.5
+++ includes/EditPage.php	27 Nov 2005 17:38:57 -0000
@@ -660,7 +660,18 @@
 		if ( count( $articleTemplates ) > 0 ) {
 			$templates = '<br />'. wfMsg( 'templatesused' ) . '<ul>';
 			foreach ( $articleTemplates as $tpl ) {
-				if ( $titleObj = Title::makeTitle( NS_TEMPLATE, $tpl ) ) {
+				$mypos = strpos($tpl, ":");
+				if ($mypos !== false)
+				{
+					$myns = substr($tpl, 0, $mypos);
+					$mytitle = substr($tpl, $mypos+1);
+				}
+				else
+				{
+					$myns = NS_TEMPLATE;
+					$mytitle = $tpl;
+				}
+				if ( $titleObj = Title::makeTitle( $myns, $mytitle ) ) {
 					$templates .= '<li>' . $sk->makeLinkObj( $titleObj ) . '</li>';
 				}
 			}
Index: languages/Language.php
===================================================================
RCS file: /user/jiangxin/project/wiki/mediawiki/src/languages/Language.php,v
retrieving revision 1.6
diff -u -r1.6 Language.php
--- languages/Language.php	23 Nov 2005 19:33:15 -0000	1.6
+++ languages/Language.php	27 Nov 2005 17:38:58 -0000
@@ -44,6 +44,10 @@
 	NS_HELP_TALK        => 'Help_talk',
 	NS_CATEGORY         => 'Category',
 	NS_CATEGORY_TALK    => 'Category_talk',
+	NS_OSXMETA			=> 'Osxmeta',
+	NS_OSXMETA_TALK		=> 'Osxmeta_talk',
+	NS_OSXHELP			=> '帮助',
+	NS_OSXHELP_TALK		=> '帮助_讨论',
 );
 
 if(isset($wgExtraNamespaces)) {
Index: languages/LanguageZh_cn.php
===================================================================
RCS file: /user/jiangxin/project/wiki/mediawiki/src/languages/LanguageZh_cn.php,v
retrieving revision 1.20
diff -u -r1.20 LanguageZh_cn.php
--- languages/LanguageZh_cn.php	26 Nov 2005 06:10:05 -0000	1.20
+++ languages/LanguageZh_cn.php	27 Nov 2005 17:38:58 -0000
@@ -6,8 +6,6 @@
 require_once( "LanguageUtf8.php" );
 
 /* private */ $wgNamespaceNamesZh_cn = array(
-//  126                 => '帮助',
-//  127                 => '帮助 对话',
 ) + $wgNamespaceNamesEn;
 
 /* private */ $wgQuickbarSettingsZh_cn = array(

--Jiangxin 14:26, 29 November 2005 (UTC)