November 24

MediaWiki Word Count Tab

Posted by seditiousbroom

My National Novel Writing Month project just passed the 40k mark, and I find that I'm in need of a little structure. I know basically what I want for my final two 5k chapters. I have to line a few ducks first, though.

I slowly started putting everything into a wiki last week. Then, yesterday, when I crossed the 40k word barrier, I sped up the process so I could finish. At first I went with Wikka Wiki. It has a cute, handy, and easy to implement word count action. Copy the code. Paste it into a new document. Name the document and upload it to the right directory. Done. Wikka Wikki has ease of plugins down to a science.

However, my first love is MediaWiki. So I did a fair bit of digging and came across a reasonable little word count hack for MediaWiki page content. Thanks to Kishore Asokan.

MediaWiki Article Word Count

// Getting the total words count
global $wgUser, $wgArticle, $db;
//check if its an article
if($wgArticle && !$_GET['action']) {
$stripped= strip_tags($c);
$exploded=explode(" ", $stripped);
//you can print this variable anywhere you want on the page


I played with the code a bit, but eventually left it as it was. The code is not perfect. It doesn't give an exact word count, but neither do most word processors. What I get is a very reasonable estimate.

I stuck it on a tab in the MonoBook skin and love it.

Here's how:

  1. Download and install MediaWiki, if you haven't already.

  2. Assuming you use the MonoBook skin for MediaWiki, open: skins/MonoBook.php

  3. Around line 124, find:

    echo htmlspecialchars($tab['text']) ?></a></li>
        <?php         } ?>

  4. Insert two line breaks immediately after <?php } ?> but before <ul>.

  5. At the first new line break, copy and paste the MediaWiki Article Word Count code above.

  6. At the second new line break, add a little formatting and a label to fit the word count into a cute, little tab:

    <li><font color="blue">&nbsp; <?php echo $totalwords ;?> words &nbsp;</font></li>

  7. Save the skins/MonoBook.php file and upload it to your server.

When you finish, there should be a word count tab at the top of every article page. Some special pages will show the word count tab as well.