Extension:AWCmod

From Organic Design wiki
Revision as of 02:27, 15 September 2008 by Nad (talk | contribs) (current state)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

<?php /**

* AWC extension
*Template:Php
Info.svg These are the MediaWiki extensions we're using and/or developing. Please refer to the information on the mediawiki.org wiki for installation and usage details. Extensions here which have no corresponding mediawiki article are either not ready for use or have been superseded. You can also browse our extension code in our local Subversion repository or our GitHub mirror.
* @package MediaWiki
* @subpackage Extensions
* @author Aran Dunkley User:Nad
* @licence GNU General Public Licence 2.0 or later
*/

if (!defined('MEDIAWIKI')) die('Not an entry point.');

define('AWCMOD_VERSION', '0.0.0, 2008-09-13');

$wgExtensionFunctions[] = 'wfSetupAWCmod'; $wgExtensionCredits['other'][] = array( 'name' => 'AWCmod', 'author' => 'User:Nad', 'description' => 'Custom extension for Adeft', 'url' => 'http://www.organicdesign.co.nz/Extension:AWCmod', 'version' => AWCMOD_VERSION );

AWCmod::fixSearchType();

$awcDBHook = true; if (class_exists('Database')) wfAWCmodDBHook();

class AWCmod {

function __construct() { global $wgHooks, $wgMessageCache, $wgParser; $wgHooks['RenderPreferencesForm'][] = $this; $wgHooks['UserCreateForm'][] = $this;

# Override the existing AWC tag $this->oldtag = $wgParser->mTagHooks['forum_info']; $wgParser->mTagHooks['forum_info'] = array($this, 'forum_info');

# Modify login form messages to say email and name compulsory

$wgMessageCache->addMessages(array('prefs-help-email' => '

Required

')); $wgMessageCache->addMessages(array('prefs-help-realname' => '

Required

'));

}

/** * Add the new prefs to a new tab in the preferences form */ function onRenderPreferencesForm(&$form, &$out) { $out->addHTML('<fieldset><legend>Contact details</legend>'.$this->renderAWCprefs().'</fieldset>'); return true; }

/** * Add the new prefs to the "create new account" form */ function onUserCreateForm(&$template) { $template->data['header'] = $this->renderAWCprefs(); return true; }

/** * Return the HTML for the AWC preference inputs */ function renderAWCprefs() {

$html = '

' .

$this->addRow( wfLabel('Phone', 'wpPhone'), wfInput('wpPhone', 20, , array('id' => 'wpPhone')), 'Required' ) . $this->addRow( wfLabel('Mobile', 'wpMobile'), wfInput('wpMobile', 20, , array('id' => 'wpMobile')) ) . $this->addRow( wfLabel('Business', 'wpBusiness'), wfInput('wpBusiness', 20, , array('id' => 'wpBusiness')), 'Can be "none"' ) . $this->addRow( wfLabel('Website', 'wpWebsite'), wfInput('wpWebsite', 20, , array('id' => 'wpWebsite')) ) . $this->addRow( wfLabel('Zip Code', 'wpZipCode'), wfInput('wpZipCode', 10, , array('id' => 'wpZipCode')), 'Required'

) . '

';

return $html; }

function addRow($td1, $td2, $td3 = ) {

return " $td1: $td2

$td3

"; } /** * AWC forum_info tag is overridden with this one to catch the tag parameters */ function forum_info($input, $argv) { $this->tagargs = $input; return call_user_func($this->oldtag, $input, $argv); } /** * Patch the SQL used by the forum_info tag to filter by user properties */ function patchSQL(&$sql) { print_r($this->tagargs); } /** * Updates passed LoadBalancer's DB servers to secure class */ static function updateLB(&$lb) { $lb->closeAll(); foreach ($lb->mServers as $i => $server) $lb->mServers[$i]['type'] = 'SimpleSecurity'; } /** * Hack to ensure proper search class is used * - $wgDBtype determines search class unless already defined in $wgSearchType * - just copied method from SearchEngine::create() */ static function fixSearchType() { global $wgDBtype, $wgSearchType; if ($wgSearchType) return; elseif ($wgDBtype == 'mysql') $wgSearchType = 'SearchMySQL4'; elseif ($wgDBtype == 'postgres') $wgSearchType = 'SearchPostgres'; elseif ($wgDBtype == 'oracle') $wgSearchType = 'SearchOracle'; else $wgSearchType = 'SearchEngineDummy'; } } /** * Hook in to DB class to allow overriding the AWC query (based on SimpleSecurity method) */ function wfAWCmodDBHook() { global $wgDBtype, $awcDBHook, $wgOldDBtype; $wgOldDBtype = $wgDBtype; $oldClass = ucfirst($wgDBtype); $wgDBtype = 'AWC'; eval("class Database{$wgDBtype} extends Database{$oldClass}".' { public function query($sql, $fname = "", $tempIgnore = false) { global $wgAWCmod; if (ereg("^SELECT",$sql) && ereg("awc_f_threads", $sql) && ereg("awc_f_forums",$sql) && ereg("awc_f_cats",$sql) && ereg("awc_f_posts",$sql) ) { $wgAWCmod->patchSQL($sql); } return parent::query($sql, $fname, $tempIgnore); } }'); $awcDBHook = false; } function wfSetupAWCmod() { global $wgAWCmod, $awcDBHook, $wgLanguageCode, $wgMessageCache, $wgLoadBalancer, $wgDBtype, $wgOldDBtype; # Instantiate the AWC singleton now that the environment is prepared $wgAWCmod = new AWCmod(); # If the DB hook couldn't be set up early, do it now # - but now the LoadBalancer exists and must have its DB types changed if ($awcDBHook) { wfAWCmodDBHook(); if (function_exists('wfGetLBFactory')) wfGetLBFactory()->forEachLB(array('AWCmod', 'updateLB')); elseif (is_object($wgLoadBalancer)) AWCmod::updateLB($wgLoadBalancer); else die("Can't hook in to Database class!"); } # Request a DB connection to ensure the LoadBalancer is initialised, # then change back to old DBtype since it won't be used for making connections again but can affect other operations # such as $wgContLang->stripForSearch which is called by SearchMySQL::parseQuery wfGetDB(); $wgDBtype = $wgOldDBtype; # Add messages if ($wgLanguageCode == 'en') { $wgMessageCache->addMessages(array( 'awc-prefhead' => "AWC" )); } }