User:Simon Moon/AdSense Extension

From MediaWiki.org

Jump to: navigation, search
Manual on MediaWiki Extensions
List of MediaWiki Extensions
AdSense Extension

Release status: unstable

Implementation Tag
Description Shows google ads (ad unit, link unit, referals) inside the wiki in a safe way
Author(s) Christian Riesen (Simon Moon Talk)
MediaWiki 1.10.1
Download See this section
Parameters $wgAdSenseAdClient

Contents

[edit] About

This extension provides a secure way of adding Google AdSense Ad Units (Text, Image and Video), Link Units and the new 2.0 referal units into your wiki. A good way to do so is put it inside a template and then include the template. Protect the template from edits, and make sure nobody removes the template calls from the other pages. Others advocate for putting them into the templates directly, which is also working nicely, but sometimes you just want them inside the content in a good way, which is what this extension is for.


The AdSense Extension by Naoise Golden creates in essence one huge JavaScript attacking platform, so I strongly recommend not using that one. Mike Dillon came up with a more secure version but it does not work, at all. The bug here is MediaWiki does not allow argument names to have _ or - characters in them. If there are, the wiki simply ignores them, and they are not given to the extension. This extension here

Most notable additions:

  • Now also takes "cpa_choice", "alternate_color" and "alternate_ad_url" as arguments, for referrals and alternative ads. Also adds now the option for rounded corners.
  • Optional: Parameter $wgAdSenseAdClient in LocalSettings, to lock the ad client to your value and not let anyone ever change it without access to this file. If not set, any ad id can be placed. With it put you can even "forget" the client id, as it will be filled in always from this variable.

[edit] Installation

  1. Copy the source below, put it in a file called AdSense.php
  2. Upload the file to your extension directory, inside a folder you name "AdSense"
  3. Load it in your LocalSettings.php with the following code. Replace the pub-XXXXXXXXXXXXXXXX with your ad client id, if you want locking of the adclient, otherwise just dont add that line.
$wgAdSenseAdClient = "pub-XXXXXXXXXXXXXXXX";
require_once("$IP/extensions/AdSense/AdSense.php");

If you do not wish to use the locking of the ad client, simply delete or comment the line starting with $wgAdSenseAdClient

[edit] Usage

When you look at the code you get from google, you can clearly see that there is a lot in common between that code and the one below. In essence, a google line looks like this:

google_ad_width = 728;

The translation for the extension is by removing the "google_" in front, and any _ sign on the left side of the = sign. Then remove the ; sign at the end and put the rest inside " signs. The result looks like this:

adwidth="728"

Here a complete code of how it could look like in your wiki. New lines and spaces are added for easier reading, but are not needed.

<adsense
 adclient="pub-XXXXXXXXXXXXXXXX"
 adwidth="728"
 adheight="90"
 adformat="728x90_as"
 adtype="text_image"
 adchannel=""
 colorborder="FFFFFF"
 colorbg="FFFFFF"
 colorlink="0000FF"
 colortext="000000"
 colorurl="008000"
/>

If nothing shows up, please ensure you have only one < sign and only one > sign. Also make sure the end is exactly "/>", otherwise the wiki wont know where the ad code stops.

[edit] AdSense.php

<?php
 
/**
 * Google AdSense MediaWiki extension
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 *
 * 
 * Installation:
 * 
 * Copy all of this into a new file. Call it "AdSense.php" and upload it to your
 * exentions directory, in a Folder called "AdSense".
 * Optional: You can lock the ad client id to a predefined one in the settings
 * to prevent others from placing their client code into your wiki. To do this
 * simply add the following line ABOVE the require_once statement for inclusion
 * of the extension, in your LocalSettings.php and replace the pub-XXX with your
 * own client id:
 * $wgAdSenseAdClient = "pub-XXXXXXXXXXXXXXXX";
 * Edit your LocalSettings.php and place at the end of it this line:
 * require_once("$IP/extensions/AdSense/AdSense.php");
 * The extension is now installed and can be used.
 *
 * 
 * Usage:
 * Simply add the following code to your wiki, where you want the ad to show up.
 * To You can see the syntax is very simple. The field names are the same as
 * google gives you on their codes, but you have to remove the "google_" in
 * front of it and remove all _ characters too. So "google_ad_client" becomes
 * "adclient" as an example. This extension was made to work with the alternate
 * ad feature (including the solid color) and the new referal ads as well. It is
 * not meant to be used for searches.
 *
 * <adsense
 *  adclient="pub-XXXXXXXXXXXXXXXX"
 *  adwidth="728"
 *  adheight="90"
 *  adformat="728x90_as"
 *  adtype="text_image"
 *  adchannel=""
 *  colorborder="FFFFFF"
 *  colorbg="FFFFFF"
 *  colorlink="0000FF"
 *  colortext="000000"
 *  colorurl="008000"
 * />
 *
 *
 * Special Thanks:
 * Naoise Golden Santos for a very first rough version.
 * Mike Dillon for an updated version, which laied the ground work.
 *
 * @category   Wiki
 * @package    wiki
 * @author     Christian Riesen
 * @copyright  2007 Christian Riesen
 * @link       http://www.mediawiki.org/wiki/User:Simon_Moon/AdSense_Extension
 * 
 */
 
// Check to make sure we're actually in MediaWiki.
if (!defined('MEDIAWIKI'))
{
        echo 'This file is part of MediaWiki. It is not a valid entry point.';
        exit(1);
}
 
/*
 * Include parser function and parser credits
 */
$wgExtensionFunctions[] = 'wfAdsense';
 
$wgExtensionCredits['parserhook'][] = array(
        'name' => 'Google AdSense',
        'description' => 'Shows google ads (ad unit, link unit, referals) inside the wiki in a safe way',
        'author' => 'Christian Riesen',
        'url' => 'http://www.mediawiki.org/wiki/User:Simon_Moon/AdSense_Extension'
);
 
/**
 * Loading parser function
 */
function wfAdsense()
{
    global $wgParser;
 
    // registers the <adsense> extension with the WikiText parser
    $wgParser->setHook('adsense', 'renderAdsense');
}
 
/**
 * Does all the magic. See the comments above for details on how to use it.
 */
function renderAdsense($input, $args, &$parser)
{
    global $wgAdSenseAdClient;
    $params = array(
        "adclient" => "ad_client",
        "adwidth" => "ad_width",
        "adheight" => "ad_height",
        "adformat" => "ad_format",
        "adtype" => "ad_type",
        "adchannel" => "ad_channel",
        "colorborder" => "color_border",
        "colorbg" => "color_bg",
        "colorlink" => "color_link",
        "colortext" => "color_text",
        "colorurl" => "color_url",
        "cpachoice" => "cpa_choice",
        "alternatecolor" => "alternate_color",
        "alternateadurl" => "alternate_ad_url",
    );
 
                if (count($args) > 1)
                {
            $output  = "<script type=\"text/javascript\">";
 
            // Render parameters
            foreach ($params as $key => $param) {
                if (!isset($args[$key])) {
                    // Error on missing param?
                    continue;
                }
 
                // if this variable is set, it will always replace whatever the input might be before this
                if ($param == "ad_client" AND $wgAdSenseAdClient)
                {
                        $args[$key] = $wgAdSenseAdClient;
                }
 
                $output .= "google_$param = \"";
                $output .= preg_replace('/([\\\\"])/', '\\\\$1', $args[$key]);
                $output .= "\"; ";
            }
 
            $output .= "</script>";
            $output .= "<script type=\"text/javascript\"";
            $output .= " src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\">";
            $output .= "</script>";
                }
 
                else
                {
                        $output = "Google AdSense Extension: Fatal error, not enough parameters. <pre>".var_export($args,true)."</pre>";
                }
    return $output;
}
Personal tools