User:Mike Dillon/AdSense

From Meta, a Wikimedia project coordination wiki
Jump to: navigation, search
<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * Adsense WikiMedia extension
 *
 * Firstly create a file called adsense.php and place this in the extensions
 * directory - copy all this code to that file.
 * To activate the extension, include it from your LocalSettings.php
 * with: require_once 'extensions/YourExtensionName.php';
 *
 * PHP versions 5
 *
 * LICENSE: This source file is subject to version 3.0 of the PHP license
 * that is available through the world-wide-web at the following URI:
 * http://www.php.net/license/3_0.txt. If you did not receive a copy of
 * the PHP License and are unable to obtain it through the web, please
 * send a note to license@php.net so we can mail you a copy immediately.
 *
 * @category   Wiki
 * @package    wiki
 * @author     Naoise Golden Santos
 * @copyright  2006 Naoise Golden Santos
 * @license    http://www.php.net/license/3_0.txt PHP License 3.0
 * @version    SVN: $Id: $
 * @link       http://www.goldensantos.com/blog/?p=7
 * 
 * <code>
 * <adsense
 *     ad_client    = "pub-XXXXXXXXXXXXXXXX"
 *     ad_width     = "728"
 *     ad_height    = "90"
 *     ad_format    = "728x90_as"
 *     ad_type      = "text_image"
 *     ad_channel   = "3267063621"
 *     color_border = "FFFFFF"
 *     color_bg     = "FFFFFF"
 *     color_link   = "3D81EE"
 *     color_text   = "000000"
 *     color_url    = "3D81EE"
 * />
 * </code>
 */

/*
 * @todo Document 
 */
$wgExtensionFunctions[] = 'wfAdsense';

/**
 * @todo Document
 */
function wfAdsense()
{
    global $wgParser;

    // registers the <adsense> extension with the WikiText parser
    $wgParser->setHook('adsense', 'renderAdsense');
}

/**
 * @todo Document
 */
function renderAdsense($input, $argv)
{
    $params = array(
        "ad_client", "ad_width", "ad_height", "ad_format", "ad_type", "ad_channel",
        "color_border", "color_bg", "color_link", "color_text", "color_url"
    );

    $output  = "<script type=\"text/javascript\">/* <![CDATA[ */\n";

    // Render parameters
    foreach ($params as $param) {
        if (!isset($argv[$param])) {
            // Error on missing param?
            continue;
        }

        $output .= "google_$param = \"";
        $output .= preg_replace('/([\\\\"])/', '\\\\$1', $argv[$param]);
        $output .= "\";\n";
    }

    $output .= "/* ]]> */</script>\n";
    $output .= "<script type=\"text/javascript\"";
    $output .= " src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\">";
    $output .= "</script>";

    return $output;
}
?>