I'm not sure just how useful this MediaWiki is to you because I'm a n00b but it looks like some of your script duplicates it. Instead of reinventing the wheel maybe you could use some of it?