User:Purodha/Downloading Extensions from the MediaWiki Wiki

Several small MediaWiki extensions are distributed as source code within this wiki. If you need a lot of these, you may want to automatize copying them. This depends on your operating system.

Linux, MacOs X, OpenBSD, Solaris, Unix, and similar edit

Here is a shell script that downloads an article from here to a temporary file. It then extracts code sections between <source…> and </source> one by one, using an awk script, placing them into individual files, the names of which are script parameters. Finally, it deletes the temporary file.

#! /bin/bash
# Author: Purodha Blissenbach, http://www.mediawiki.org/wiki/User:Purodha
# Source: http://www.mediawiki.org/wiki/Manual:Downloading_Extensions_from_the_MediaWiki_Wiki
nam="${0##*/}"  # basename $0
if [ $# -lt 2 ]; then
        echo    "$nam - Get Mediawiki Extension Source from MediaWiki Web Site."
        echo    " usage: $nam NameOfExtensionPage targetfile-1 [targetfile-2 [ ... ] ]"
        exit 2
fi
extension="${1}"
tempfile=/tmp/$$.`basename $0`
wget -O "$tempfile" "http://www.mediawiki.org/w/index.php?title=Extension:$extension&action=raw"
if [ -r "$tempfile" ]; then
        shift
        ((i=1))
        while [ $1 ]; do
                targetfile="${1}"
                echo $i "->" $targetfile
                awk -f - -- "$tempfile" >"$targetfile" <<EOF
( p == 0 ) && /<syntaxhighlight[^>]*>/ {
                i++
                if ( i == $i )
                {
                        p=1
                }
                next
        }
/<\/source>/ {
                p=0
        }
p       { print }
EOF
                ((i=$i+1))
                shift
        done
fi
rm $tempfile

This script takes two or more parameters:

  1. The page name where the extension is located in this wiki, without the "Extension:" name space prefix.
  2. A file where to store the 1st source segment. It the file existst, it is overwritten without warning. The directory, if any, of the file must already exist. Use /dev/null so as to skip this source segment.
  3. (optional) file where 2nd source segment is to be stored. Everything else resembles previous parameter.
  4. (optional) file where to store the 3rd source segment of the article.
  5. etc.

Feel free to enhance the code above.

Examples of use edit

Assuming, you have saved the above file to your local host as ~/bin/gtmwex and made it executable. If you wanted to download the Extensions PSINoTocNum, MagicNumberedHeadings, and Pdf_Export, you would use something like these commands:

mkdir PSINoTocNum
~/bin/gtmwex PSINoTocNum PSINoTocNum/PSINoTocNum.php
mkdir MagicNumberedHeadings
~/bin/gtmwex MagicNumberedHeadings /dev/null MagicNumberedHeadings/MagicNumberedHeadings.php
mkdir PdfExport
~/bin/gtmwex Pdf_Export /dev/null PdfExport/PdfExport.php PdfExport/PdfExport.i18.php