Integrating MediaWiki With CakePHP

Hi Folks. As you might have guessed from the title, I was up trying to integrate MediaWiki with a CakePHP application of mine. Yeah, all hell did break loose while I was at it. As naive as it may sound, I was dreaming something like an API bridge between the MediaWiki and CakePHP. But like I said, it was a dream, and as the notion suggests, "so you wanna build an API bridge between MediaWiki and CakePHP in one night ?" - "DREAM ON SUCKER !" because you'll never find the time and the nerve to do it ... doh !

May be some day, if no one beats me to it, I'd get that bridge up and running. But till then let me explain a nifty way of doing this.

Your best chance to include a MediaWiki with your CakePHP application is via some RewriteRule magic in your .htaccess file. You simply need to specify which URL you want to be pointed to your wiki application. In my case, I simply wanted all http://mysite.com/wiki/ requests to map to /doc-root/wiki instead of /doc-root/app.

I usually don't keep the App folder type of structure in my CakePHP apps but this particular application was an exception. The concept is to:

"read the URL you want pointing to the MediaWiki and change your document root to the folder where you are keeping the MediaWiki"

Now, here is the file structure of my the application I was working on:

  • / < app_name >
    • / branches
      • / sohaib
        • / app
        • .htaccess
      • / wiki
        • index.php
        • .htaccess
        • ....

Say, like me, you want to get all URLs with "/wiki" to be directed to the MediaWiki app, then a small change you'll make int the .htaccess file highlighted in red above would be:


RewriteEngine on
RewriteRule ^(.*)/wiki/(.*) wiki/ [NC]
RewriteRule ^(.*)/wiki/(.*) wiki/$1 [NC]
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]

This tells Apache to first look for URLs containing /wiki - and redirect these to to the /sohaib/wiki folder where another .htaccess file awaits the request (highlighted in green above). This file is identical to the one you can find in /webroot folder of your app. You can copy paste this file to your wiki folder.


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

This connects your /wiki requests to the index.php file of your media wiki. And thats it as non-technical as I could be in trying to explain what you can do. I am not going into the exaplaination of the code in the .htaccess files. You'd be able to find good resources via google. But if you're like me, you'll settle for the documentation on mod_rewrite on Apache.org

But let me tell you, this is a very simple demonstration of what you can achieve with mod_rewrite ... this thing is COMPLEX and way too much voodoo, especially when regular expressions come into play. So be careful, it's addictive :P - Hope this helps anyone out there trying to integrate a MediaWiki with their App. Over and OUT !

4 Responses to “Integrating MediaWiki With CakePHP”

  1. ax0rz  on June 21st, 2009

    What a painful process! Why not just extract MediaWiki into “/app/webroot/wiki/” and run it from there? You still get your “www.example.com/wiki” URL.

    Greetings from India,
    Akshay

  2. admin  on June 21st, 2009

    Akshay, for someone who knows some regex + url rewriting it’s actually quite simple :) the idea is to keep both applications separate, avoid any namespace / global function conflicts. I had Drupal running like this, but I ran into problems later on.
    The way you describe surely is the simplest, but it might have unprecendented consequences (and I’ve been there).

    BTW - Felix from debuggable tried this quite a while ago on Cake 1.1

  3. php  on July 22nd, 2009

    i think this way is bit difficult but describe is good

  4. Pakistan Basic Accounting Software BizCore-i  on September 16th, 2009

    thanks this will help me in my project.


Leave a Reply