It is quite easy to integrate Google AdSense banners or any other adverts source in MoinMoinWikis. Not everyone loves Google, but for a debate on principles you certainly find a better place, e.g. GoogleSucks. Furthermore, not everyone accepts adverts in wikis. But as this is not a place to discuss such meta-things.

1. Google AdSense

First you have to register at https://www.google.com/adsense. It takes 1-2 days to get your account.

Then you have to chose the banner you want (see examples). You can customize the colors too.

You get a piece of Java Script to integrate in your wiki. It looks similar to this:

<script type="text/javascript"><!--
google_ad_client = "pub-1702912899203427";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel ="3636989479";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

If you want to put the Google adds before the logo, just put the script in the configuration option named 'page_header1'. It will be included by the line self.emit_custom_html(self.cfg.page_header1),

For other positions it is necessary to integrate it in your default theme (HelpOnThemes). For example you can copy MoinMoin/theme/modern.py to modern_adsense.py (in wich directory?). Then you can put the piece of Java Script (see above) in a variable called adsense or something like that. This can be inserted in the html variable where it should appear. Finally you have to set theme_default = modern_adsense in your configuration file.

This is a way a python rookie has find out. Suggestions for improvement are welcome.

2. Toggle option

2.1. Simple JavaScript

Some users might not like to see this instead of real content. To let them hide the google ad, we can add a toggle link. Add this to page_header1 config option:

   1 toggle = '''
   2 <script type="text/javascript">
   3 function toggle(id) {
   4     var element = document.getElementById(id);
   5     if (element == null) return;
   6     element.style.display = element.style.display ? '' : 'none';
   7 }
   8 </script>
   9 <noscript></noscript>
  10 '''
  11 goooogle = # put here the google code
  12 
  13 page_header1 = '''
  14 %(toggle)s
  15 <p><a href="javascript:toggle('goooogle')">Toggle Goooogle ads</a></p>
  16 <div id="goooogle">
  17 %(goooogle)s
  18 </div>
  19 ''' % {'toggle': toggle, 'goooogle': goooogle}

I hope it works - not sure if it will hide html code generated by javascript.

2.2. User preferences

The toggle option is easy to add but suck. If you don't like ads, you have to go and close the ad on each page view. The best option would be a user preference:

Here is a patch to add this to moin. The patch adds a show_adverts option to the user preferences, an advert() method to ThemeBase, and include this in Modern theme. You can easily include it in any other theme by adding a call to the method where you want the advert to appear. I think that the bottom of the page make sense for horizontal adverts. Finally, the advert is designed by #adverts rule in screen.css.

   1 * looking for nirs@freeshell.org--2005/moin--fix--1.3--patch-27 to compare with
   2 * comparing to nirs@freeshell.org--2005/moin--fix--1.3--patch-27
   3 M  MoinMoin/theme/modern.py
   4 M  wiki/htdocs/modern/css/screen.css
   5 M  MoinMoin/theme/__init__.py
   6 M  MoinMoin/user.py
   7 
   8 * modified files
   9 
  10 --- orig/MoinMoin/theme/__init__.py
  11 +++ mod/MoinMoin/theme/__init__.py
  12 @@ -1001,6 +1001,12 @@
  13      def endPage(self):
  14          """ End page div """
  15          return '</div> <!-- end page -->\n'        
  16 +
  17 +    def adverts(self):
  18 +        """ Return adverts defined in the wiki config """
  19 +        if self.request.user.show_adverts:
  20 +            return '<div id="adverts">%s</div>' % self.request.cfg.adverts
  21 +
  22      
  23      # Public functions #####################################################
  24  
  25 
  26 
  27 --- orig/MoinMoin/theme/modern.py
  28 +++ mod/MoinMoin/theme/modern.py
  29 @@ -68,6 +68,7 @@
  30              # Footer
  31              u'<div id="footer">',
  32              self.editbar(d),
  33 +            self.adverts(),
  34              self.credits(d),
  35              self.showversion(d, **keywords),
  36              u'</div>',
  37 
  38 
  39 --- orig/MoinMoin/user.py
  40 +++ mod/MoinMoin/user.py
  41 @@ -203,6 +203,7 @@
  42           ('remember_me', lambda _: _('Remember login information')),
  43           ('want_trivial', lambda _: _('Subscribe to trivial changes')),
  44           ('disabled', lambda _: _('Disable this account forever')),
  45 +         ('show_adverts', lambda _: _('Show adverts')),
  46      ]
  47      _transient_fields =  ['id', 'valid', 'may', 'auth_username', 'trusted']
  48  
  49 
  50 
  51 --- orig/wiki/htdocs/modern/css/screen.css
  52 +++ mod/wiki/htdocs/modern/css/screen.css
  53 @@ -312,6 +312,8 @@
  54  	vertical-align: middle;
  55  }
  56  
  57 +#adverts {text-align: center; margin-top: 10px;}
  58 +
  59  .diff {
  60  	width:99%;
  61  }
advert.patch

Add the adverts code to your wikiconfig or farmconfig with the config option named adverts. Here is an example farmconfig:

   1 from MoinMoin.multiconfig import DefaultConfig
   2 
   3 class FarmConfig(DefaultConfig):
   4 
   5     adverts = '''
   6 <script type="text/javascript"><!--
   7 google_ad_client = "";
   8 google_ad_width = 728;
   9 google_ad_height = 90;
  10 google_ad_format = "728x90_as";
  11 google_ad_type = "text_image";
  12 google_ad_channel ="";
  13 //--></script>
  14 <script type="text/javascript"
  15   src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
  16 </script>
  17 '''

The code is not related to Google, you can use any source. What you put in your config file will be in the rendered pages. It can be your own ads or googles or whatever.

As only registered users can turn of advertisement with the solution above, there is another good reason to register. ;-) To realise this for unregistered users too, it might be necessary to set a cookie. Something like this: near the advertisement is a link "toggle advertisement off". If you klick on it, it sets the cookie no_moin_ads. The advertisement is not shown if this cookie is set. Therefor changes the link to "toogle advertising on" to remove the cookie.

Here some code for the config-file:

    page_header1 = '''
<div id="adwords" align="center">
    <a href="http://www.jurawiki.de/JuraWikiTestetGoogleWerbung">JuraWikiTestetGoogleWerbung</a>
    <script type="text/javascript">
        function addeactivate() {
            var now = new Date();
            var end = new Date(now.getTime() + 1000 * 60 * 60 * 24 * 14); // 14 days
            document.cookie = "no_moin_ad=true; expires=" + end.toGMTString();
            location.reload();
        }
        function adactivate() {
            document.cookie = "no_moin_ad=false; expires=" + new Date(0).toGMTString();
            location.reload();
        }
        function checkcookie () {
            return document.cookie.match("no_moin_ad=true")
        }
        
        if (navigator.cookieEnabled == true) {
            if (checkcookie()) {
                document.write('(<a href="javascript:adactivate();">Werbung aktivieren</a>)');
            } else {
                document.write('(<a href="javascript:addeactivate();">Werbung deaktivieren</a>)<br>');
                document.write('Google-Werbung');
            }
        }
    </script>
</div>
    '''

You can check that this works here: http://zosel.dyndns.org/testwiki/

Now the Google-Code from above has to be inserted. I am still looking how to do this with the line

<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

This can not be insertet with document.write, because then the code has to run twice.

The easiest way seems to read out the cookie in Python and write the Google-Java Script or not. Like this:

    import cgi, os, Cookie
    
    Keks = Cookie.SimpleCookie()
    try:
        Keks.load (os.environ["HTTP_COOKIE"])
        Wert = Keks["no_moin_ad"].value
    except:
        page_header1 = page_header1 + '''
<script type="text/javascript"><!--
google_ad_client = "pub-9833124038263688";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel ="";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
        '''

It works, look at http://zosel.dyndns.org/testwiki/ - but it's propably not very elegant. B-)

ToDo: using request instead of os.environ

/!\ This probably does not work with twisted, because the config-file is not read out every time.

Perhaps there is a way to set and get the cookie using exclusively JavaScript. Then this could work with twisted (see above) too. See http://techpatterns.com/downloads/javascript_cookies.php for an example.

3. Further ideas

Flag #advert no to prevent/allow advertisement on certain pages.

4. Wikis using adverts

4.1. MoinMoinWikis

4.2. Other Wikis

MoinMoin: AdvertsIntegration (last edited 2007-10-29 19:20:19 by localhost)