MediaWiki customizations

Fra WikiWegge
Skift til: Navigation, Søgning

Indholdsfortegnelse

URL rewrite

It is possible to get some astonishing results with the mod_rewrite Apache module. Unfortunately to get there, mod_rewrite is needed. The Summary comments are perfectly fitting to my state of mind after I've spent some time hacking that thing. However, the results are often worth the pain. On this Wiki, I'm using mod_rewrite to achieve two goals:

  1. Hide index.php from the visible URL
  2. In combination with robots.txt, prevent spiders from fetching any non-article pages.

I have made the following changes to my LocalSettings.php and virtual host configuration:

LocalSettings.php:

$wgScriptPath	    = "";
$wgScript           = "$wgScriptPath/w/index.php";
$wgArticlePath      = "$wgScriptPath/$1";

These changes are using the fact that regular article URL's are prefixed with $wgArticlePath, while all other articles are prefixed with $wgScript. So with these changes to the LocalSettings.php, the link to this article will be easy on the eyes, http://wiki.wegge.dk/MediaWiki_customizations, while the edit link will be a definite eye-sore: http://wiki.wegge.dk/w/index.php?title=MediaWiki_customizations&action=edit. We will come back to this part later on. But first we'll take a look at httpd.conf (or .htaccess). And in a hurry, because right now the generated links will likely point to a "404 Not found" page. Enter mod_rewrite:

# Rewrite rules for a MW installation with normal article view
# without index.php and /w/ in front of all other links.
  
RewriteEngine on

# Don't rewrite requests for files in MediaWiki subdirectories,
# MediaWiki PHP files, HTTP error documents, favicon.ico, or robots.txt
RewriteCond %{REQUEST_URI} !^/(stylesheets|images|skins)/
RewriteCond %{REQUEST_URI} !^/(redirect|texvc|index).php
RewriteCond %{REQUEST_URI} !^/error/(40(1|3|4)|500).html
RewriteCond %{REQUEST_URI} !^/favicon.ico
RewriteCond %{REQUEST_URI} !^/robots.txt
RewriteCond %{REQUEST_URI} !^/w/

# Rewrite anything not matched by the rules above.
RewriteRule ^/(.*)$ /index.php/$1 [L,QSA]

# Remove the /w/ from the edit links
RewriteRule ^/w/(.*)$ /$1 [L,QSA]

The RewriteCond statement is important. First of all, it is, roughly speaking, only the clickable links that are affected by the changes to LocalSettings.php. Second, most browsers and spiders expect certain links to be available. Note that the RewriteCond statement is only active until the next RewriteRule statement, so it makes perfect sense in this case to first ignore links starting with /w/, and later on to rewrite them anyway. The link manipulation itself is pretty straightforward. Anything not matched by the RewriteCond statements (! means match anything but), will be appended to /index.php/. The second rule, on the other hand, removes the leading /w/. So the example links mentioned above will be rewritten to http://wiki.wegge.dk/index.php/MediaWiki_customizations and http://wiki.wegge.dk/index.php?title=MediaWik_customizationsi&action=edit respectively. And that is precisely what is intended, because index.php is the entry point to every MediaWiki page.

And now to the clever bit. In fact clever bits. The short URL form is nicer to look at (and remember), and therefore desireable for normal users. But the main thing is that we now have a way of restricting the spiders (GoogleBot, msnbot, Slurp etc.) from following the edit links. All of the edit pages contains the "noindex,nofollow" robots metatag. So there are no reason to let the spiders waste bandwidth on getting to those pages in the first place. And to that end we return to the beginning of this exercise, the robots.txt file:

# Any agent can index the article pages, but we want to limit acces to 
# those pages only. Therefore we deny access to URLs starting with /w/

User-agent: *
Disallow: /w/

Skin customizations

Quite a lot can be changed, just by hacking the skin files. In my case, I have added a google Adsense bar and some buttons for two site-ranking services. The changes below are output from the unix diff-utility. However, since my mediawiki code is heavily hacked in some places, the line numbers (the first number after @@ in the snippets) may or may not fit your source distribution, so be prepared to apply the changes by hand. After all, you will have to adapt the changes to your needs. Also remeber to make a backup copy of the files, before you start hacking. Especially the left hand column is a maze of nested divs.

Google Adsense in Mediawiki 1.5+

The Google Adsense bar is added to skkins/MonoBook.php:

--- skins/MonoBook.php	27 Oct 2005 00:11:41 -0000	1.63
+++ skins/MonoBook.php	1 Nov 2005 12:33:41 -0000
@@ -93,6 +93,24 @@
 	    <div class="visualClear"></div>
 	  </div>
 	</div>
+        <div id="column-google">
+          <script type="text/javascript"><!--
+            google_ad_client = "pub-XXXXXXXXXXXXXX";
+            google_ad_width = 120;
+            google_ad_height = 600;
+            google_ad_format = "120x600_as";
+            google_ad_channel ="";
+            google_ad_type = "text_image";
+	    google_color_border = "AAAAAA";
+	    google_color_bg = "FFFFFF";
+	    google_color_link = "000000";
+	    google_color_url = "002BB8";
+	    google_color_text = "000000";
+          //--></script>
+          <script type="text/javascript"
+            src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
+          </script>
+        </div>
       </div>
       <div id="column-one">
 	<div id="p-cactions" class="portlet">

In this case, I have added the ads on the right hand side of the page, which means that it goes into the HTML between the article body and the html for the column on the laft hand side. Cascading stylesheets are truly wonderfull to work with :-) It is also possible to place the ads in the column on the left side of the page. The changes needed will be similar to the one described below for the web-tracking buttons.

And to make sure that the body text of the artivles stay to the left of the ads, a few changes to the stylesheet is also required:

--- skins/monobook/main.css	13 Oct 2005 05:18:35 -0000	1.77
+++ skins/monobook/main.css	1 Nov 2005 12:33:41 -0000
@@ -8,6 +8,19 @@
 ** Michael Zeltner and Geir Bækholt)
 ** All you guys rock :)
 */
+#column-google {
+    width: 120px;
+    clear: left;
+    position: absolute;
+    padding: 35px 0px 6px 6px;
+    float: right;
+    right: 0px;
+    top: 10px;
+    z-index: 5;
+}
 
 #column-content {
     width: 100%;
     padding: 0;
 }
 #content {
-    margin: 2.8em 0 0 12.2em;
+    margin: 2.8em 125px 0 12.2em;
     padding: 0 1em 1.5em 1em;
     background: white;
     color: black;


Web tracking buttons

The web tracking buttons are even simpler to place, since we can simply add another div with the class portlet:

--- skins/MonoBook.php	27 Oct 2005 00:11:41 -0000	1.63
+++ skins/MonoBook.php	1 Nov 2005 12:33:41 -0000
@@ -211,6 +229,43 @@
 	  </div>
 	</div>
 	<?php } ?>
+
+	<div id="p-counters" class="portlet">
+	  <h5>web stats</h5>
+           <div id="counter-wrapper">
+<!-- Counter code here -->
...
+<!-- Counter code end -->
+         </div>
+	</div>
+
       </div><!-- end of the left (by default at least) column -->
       <div class="visualClear"></div>
       <div id="footer">

Depending on requirements, the div can be moved further up in the column. In skins/MonoBook.php, the places to make the insertion is anywhere before or af one of the other portlet-divs.

Personlige værktøjer
Andre sprog