/**
 * Do the actual initialisation of the extension. This is just a delayed init that
 * makes sure MediaWiki is set up properly before we add our stuff.
 *
 * The main things this function does are: register all hooks, set up extension
 * credits, and init some globals that are not for configuration settings.
 */
function haclfSetupExtension()
{
    wfProfileIn(__FUNCTION__);
    global $haclgIP, $wgHooks, $wgParser, $wgExtensionCredits, $wgLanguageCode, $wgRequest, $wgContLang;
    global $haclgUnprotectableNamespaces, $haclgUnprotectableNamespaceIds;
    /* Title patch is disabled until full initialization of extension.
     * This was formerly done with haclfDisableTitlePatch() in the beginning
     * of this file and haclfRestoreTitlePatch() here.
     * But this does not allow changing $haclgEnableTitlePatch after enabling IntraACL.
     */
    if (!empty($_SERVER['SERVER_NAME'])) {
        define('HACL_HALOACL_VERSION', '1.0');
        // UI hooks - useless in console mode
        $wgHooks['EditPage::showEditForm:initial'][] = 'IACLToolbar::warnNonReadableCreate';
        $wgHooks['UploadForm:initial'][] = 'IACLToolbar::warnNonReadableUpload';
        $wgHooks['SpecialUploadCheckWarnings'][] = 'IACLToolbar::attemptNonReadableUpload';
        $wgHooks['EditPage::attemptSave'][] = 'IACLToolbar::attemptNonReadableCreate';
        $wgHooks['EditPage::showEditForm:fields'][] = 'haclfAddToolbarForEditPage';
        $wgHooks['SkinTemplateContentActions'][] = 'IACLToolbar::SkinTemplateContentActions';
        $wgHooks['SkinTemplateNavigation'][] = 'IACLToolbar::SkinTemplateNavigation';
        // UI hooks used to update permissions along with article modification
        // ArticleSaveComplete_SaveSD hook must run before articleSaveComplete_SaveEmbedded
        $wgHooks['ArticleSaveComplete'][] = 'IACLToolbar::articleSaveComplete_SaveSD';
        $wgHooks['ArticleSaveComplete'][] = 'IACLToolbar::articleSaveComplete_SaveEmbedded';
        // Permission and cache checks - intentionally disabled in console mode
        $wgHooks['userCan'][] = 'IACLEvaluator::userCan';
        $wgHooks['IsFileCacheable'][] = 'haclfIsFileCacheable';
        $wgHooks['ParserOutputRenderKey'][] = 'IACLEvaluator::ParserOutputRenderKey';
        $wgHooks['FilterPageQuery'][] = 'IACLEvaluator::FilterPageQuery';
    } else {
        // Also disable security checks in console mode
        // Also issue a warning as an insurance to not run Wiki in some bad setup
        file_put_contents('php://stderr', '** WARNING: IntraACL security checks are disabled because
** $_SERVER[SERVER_NAME] is empty, which probably means we are in console
');
    }
    //--- Transform config (unprotectable namespace names to ids) ---
    $haclgUnprotectableNamespaceIds = array();
    foreach ($haclgUnprotectableNamespaces as $ns) {
        if ($ns === 'Main') {
            $ns = '';
        }
        $ns = $wgContLang->getNsIndex($ns);
        if ($ns !== false) {
            $haclgUnprotectableNamespaceIds[$ns] = true;
        }
    }
    $wgHooks['GetPreferences'][] = 'IACLToolbar::GetPreferences';
    //-- includes for Ajax calls --
    global $wgUseAjax, $wgRequest;
    if ($wgUseAjax && $wgRequest->getVal('action') == 'ajax') {
        $funcName = isset($_POST["rs"]) ? $_POST["rs"] : (isset($_GET["rs"]) ? $_GET["rs"] : NULL);
        if (strpos($funcName, 'hacl') === 0) {
            require_once "{$haclgIP}/includes/Toolbar.php";
            require_once "{$haclgIP}/includes/AjaxConnector.php";
        }
    }
    //--- credits (see "Special:Version") ---
    $wgExtensionCredits['other'][] = array('name' => 'IntraACL', 'version' => '2.1.7', 'author' => "Vitaliy Filippov, Stas Fomin, Thomas Schweitzer", 'url' => 'http://wiki.4intra.net/IntraACL', 'description' => 'The best MediaWiki rights extension, loosely based on HaloACL');
    // IACLParserFunctions callbacks
    $wgParser->setFunctionHook('haclaccess', 'IACLParserFunctionHooks::access');
    $wgParser->setFunctionHook('haclpredefinedright', 'IACLParserFunctionHooks::predefinedRight');
    $wgParser->setFunctionHook('haclmanagerights', 'IACLParserFunctionHooks::manageRights');
    $wgParser->setFunctionHook('haclmember', 'IACLParserFunctionHooks::addMember');
    $wgParser->setFunctionHook('haclmanagegroup', 'IACLParserFunctionHooks::manageRights');
    haclCheckScriptPath();
    wfProfileOut(__FUNCTION__);
    return true;
}
 /**
  * "Real" group list, loaded using AJAX
  */
 static function haclGrouplist($n, $not_n = NULL)
 {
     global $wgScript, $haclgHaloScriptPath, $haclgContLang;
     haclCheckScriptPath();
     /* Load data */
     $total = 0;
     $titles = IACLStorage::get('SD')->getSDPages('group', $n, $not_n, NULL, NULL, $total);
     $groups = array();
     $l = strlen($haclgContLang->getPetPrefix(IACL::PE_GROUP)) + 1;
     foreach ($titles as $t) {
         $groups[] = array('name' => $t->getText(), 'real' => substr($t->getText(), $l), 'editlink' => $wgScript . '?title=Special:IntraACL&action=group&group=' . urlencode($t->getText()), 'viewlink' => $t->getLocalUrl());
     }
     $max = false;
     /* Run template */
     ob_start();
     require dirname(__FILE__) . '/../templates/HACL_GroupListContents.tpl.php';
     $html = ob_get_contents();
     ob_end_clean();
     return $html;
 }