<commit_before>package org.curriki.xwiki.plugin.curriki;

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.Vector;
import java.lang.reflect.Method;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.curriki.plugin.spacemanager.impl.CurrikiSpaceManager;
import org.curriki.plugin.spacemanager.plugin.CurrikiSpaceManagerPluginApi;
import org.curriki.xwiki.plugin.asset.*;
import org.curriki.xwiki.plugin.asset.composite.RootCollectionCompositeAsset;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.XWiki;
import com.xpn.xwiki.notify.XWikiNotificationRule;
import com.xpn.xwiki.notify.XWikiDocChangeNotificationInterface;
import com.xpn.xwiki.notify.DocChangeRule;
import com.xpn.xwiki.api.Api;
import com.xpn.xwiki.api.Property;
import com.xpn.xwiki.api.Document;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.objects.BaseObject;
import com.xpn.xwiki.objects.PropertyInterface;
import com.xpn.xwiki.objects.classes.BaseClass;
import com.xpn.xwiki.objects.classes.DBTreeListClass;
import com.xpn.xwiki.objects.classes.StaticListClass;
import com.xpn.xwiki.objects.classes.BooleanClass;
import com.xpn.xwiki.plugin.XWikiDefaultPlugin;
import com.xpn.xwiki.plugin.XWikiPluginInterface;
import com.xpn.xwiki.plugin.spacemanager.api.Space;
import com.xpn.xwiki.web.XWikiRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 */
public class CurrikiPlugin extends XWikiDefaultPlugin implements XWikiPluginInterface, XWikiDocChangeNotificationInterface {
    public static final String PLUGIN_NAME = "curriki";

    private static final Logger LOG = LoggerFactory.getLogger(CurrikiPlugin.class);
    private static ThreadLocal<SimpleDateFormat> durationDf = new ThreadLocal<SimpleDateFormat>() {
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("mm'm'ss's'SSS'ms'");
        }};

    public CurrikiPlugin(String name, String className, XWikiContext context) {
        super(name, className, context);
        init(context);
        LOG.debug("Curriki plugin constructed");
    }

    @Override public void init(XWikiContext context) {
        super.init(context);

        // Creating classes
        try {
            // we need to create the asset classes if they don't exist
            initAssetClass(context);
            initAssetLicenseClass(context);
            initAssetTextClass(context);
            initAssetDocumentClass(context);
            initAssetImageClass(context);
            initAssetVideoClass(context);
            initAssetArchiveClass(context);
            initAssetExternalClass(context);
            initSubAssetClass(context);
            initReorderAssetClass(context);
            initCompositeAssetClass(context);
        } catch (XWikiException e) {
            if (LOG.isErrorEnabled())
                LOG.error("Error generating asset classes", e);
        }

        try {
            // init the asset type managers
            DefaultAssetManager.initAssetSubTypes(context);
        } catch (Exception e) {
            if (LOG.isErrorEnabled())
                LOG.error("Error initing sub asset type classes", e);
        }

        // Insert a notification so that we can handle rollback and convert assets
        // if we are reading an asset in the old format
        context.getWiki().getNotificationManager(). addGeneralRule(new DocChangeRule(this, true, false));

        String startupURLs = context.getWiki().Param("curriki.startupURLs");
        final Pattern ptrn = Pattern.compile("http://(.*):(.*)@([^/]+)(:([0-9]+))?/.*");
        if(startupURLs!=null) for(final String startupURL: startupURLs.split("[,\t\n ]+")) {
            new Thread("Startup URL fetch " + startupURL) {
                public void run() {
                    try {
                        LOG.warn("Loading startup URL " + startupURL + ".");
                        HttpClient client = new HttpClient();
                        Matcher matcher = ptrn.matcher(startupURL);
                        if(matcher.matches()) {
                            client.getParams().setAuthenticationPreemptive(true);
                            Credentials defaultcreds = new UsernamePasswordCredentials(matcher.group(1), matcher.group(2));
                            String host = matcher.group(3);
                            int port = 80;
                            if(matcher.group(5)!=null && matcher.group(5).length()>0) port = Integer.parseInt(matcher.group(5));
                            client.getState().setCredentials(new AuthScope(host, port, AuthScope.ANY_REALM), defaultcreds);
                        }
                        GetMethod method = new GetMethod(startupURL);
                        int status = client.executeMethod(method);
                        if(status!=200) throw new IllegalStateException("URL " + startupURL + " responded " + method.getStatusLine());
                        LOG.warn("Startup URL " + startupURL + " successfully loaded.");
                    } catch (Exception e) {
                        LOG.warn("Notification page " + startupURL + " failed to load.",e);
                    }
                }
            }.start();
        }

    }

    @Override public String getName() {
        return PLUGIN_NAME;
    }

    @Override public Api getPluginApi(XWikiPluginInterface plugin, XWikiContext context) {
        return new CurrikiPluginApi((CurrikiPlugin) plugin, context);
    }

    @Override public void flushCache() {
        super.flushCache();
    }



    public Asset createAsset(String parent, String publishSpace, XWikiContext context) throws XWikiException {
        return Asset.createTempAsset(parent, publishSpace, context);
    }

    public Asset createAsset(String parent, XWikiContext context) throws XWikiException {
        return Asset.createTempAsset(parent, context);
    }

<commit_msg>prevent double load-at-startup in case the plugin is double loaded. paul<commit_after>package org.curriki.xwiki.plugin.curriki;

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.Vector;
import java.lang.reflect.Method;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.curriki.plugin.spacemanager.impl.CurrikiSpaceManager;
import org.curriki.plugin.spacemanager.plugin.CurrikiSpaceManagerPluginApi;
import org.curriki.xwiki.plugin.asset.*;
import org.curriki.xwiki.plugin.asset.composite.RootCollectionCompositeAsset;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.XWiki;
import com.xpn.xwiki.notify.XWikiNotificationRule;
import com.xpn.xwiki.notify.XWikiDocChangeNotificationInterface;
import com.xpn.xwiki.notify.DocChangeRule;
import com.xpn.xwiki.api.Api;
import com.xpn.xwiki.api.Property;
import com.xpn.xwiki.api.Document;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.objects.BaseObject;
import com.xpn.xwiki.objects.PropertyInterface;
import com.xpn.xwiki.objects.classes.BaseClass;
import com.xpn.xwiki.objects.classes.DBTreeListClass;
import com.xpn.xwiki.objects.classes.StaticListClass;
import com.xpn.xwiki.objects.classes.BooleanClass;
import com.xpn.xwiki.plugin.XWikiDefaultPlugin;
import com.xpn.xwiki.plugin.XWikiPluginInterface;
import com.xpn.xwiki.plugin.spacemanager.api.Space;
import com.xpn.xwiki.web.XWikiRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 */
public class CurrikiPlugin extends XWikiDefaultPlugin implements XWikiPluginInterface, XWikiDocChangeNotificationInterface {
    public static final String PLUGIN_NAME = "curriki";

    private static final Logger LOG = LoggerFactory.getLogger(CurrikiPlugin.class);
    private static ThreadLocal<SimpleDateFormat> durationDf = new ThreadLocal<SimpleDateFormat>() {
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("mm'm'ss's'SSS'ms'");
        }};
    private static boolean startupURLLaunched = false;

    public CurrikiPlugin(String name, String className, XWikiContext context) {
        super(name, className, context);
        init(context);
        LOG.debug("Curriki plugin constructed");
    }

    @Override public void init(XWikiContext context) {
        super.init(context);

        // Creating classes
        try {
            // we need to create the asset classes if they don't exist
            initAssetClass(context);
            initAssetLicenseClass(context);
            initAssetTextClass(context);
            initAssetDocumentClass(context);
            initAssetImageClass(context);
            initAssetVideoClass(context);
            initAssetArchiveClass(context);
            initAssetExternalClass(context);
            initSubAssetClass(context);
            initReorderAssetClass(context);
            initCompositeAssetClass(context);
        } catch (XWikiException e) {
            if (LOG.isErrorEnabled())
                LOG.error("Error generating asset classes", e);
        }

        try {
            // init the asset type managers
            DefaultAssetManager.initAssetSubTypes(context);
        } catch (Exception e) {
            if (LOG.isErrorEnabled())
                LOG.error("Error initing sub asset type classes", e);
        }

        // Insert a notification so that we can handle rollback and convert assets
        // if we are reading an asset in the old format
        context.getWiki().getNotificationManager(). addGeneralRule(new DocChangeRule(this, true, false));

        if(!startupURLLaunched) {
            String startupURLs = context.getWiki().Param("curriki.startupURLs");
            final Pattern ptrn = Pattern.compile("http://(.*):(.*)@([^/]+)(:([0-9]+))?/.*");
            if(startupURLs!=null) for(final String startupURL: startupURLs.split("[,\t\n ]+")) {
                new Thread("Startup URL fetch " + startupURL) {
                    public void run() {
                        try {
                            LOG.warn("Loading startup URL " + startupURL + ".");
                            HttpClient client = new HttpClient();
                            Matcher matcher = ptrn.matcher(startupURL);
                            if(matcher.matches()) {
                                client.getParams().setAuthenticationPreemptive(true);
                                Credentials defaultcreds = new UsernamePasswordCredentials(matcher.group(1), matcher.group(2));
                                String host = matcher.group(3);
                                int port = 80;
                                if(matcher.group(5)!=null && matcher.group(5).length()>0) port = Integer.parseInt(matcher.group(5));
                                client.getState().setCredentials(new AuthScope(host, port, AuthScope.ANY_REALM), defaultcreds);
                            }
                            GetMethod method = new GetMethod(startupURL);
                            int status = client.executeMethod(method);
                            if(status!=200) throw new IllegalStateException("URL " + startupURL + " responded " + method.getStatusLine());
                            LOG.warn("Startup URL " + startupURL + " successfully loaded.");
                        } catch (Exception e) {
                            LOG.warn("Notification page " + startupURL + " failed to load.",e);
                        }
                    }
                }.start();
            }
            startupURLLaunched = true;
        }


    }

    @Override public String getName() {
        return PLUGIN_NAME;
    }

    @Override public Api getPluginApi(XWikiPluginInterface plugin, XWikiContext context) {
        return new CurrikiPluginApi((CurrikiPlugin) plugin, context);
    }

    @Override public void flushCache() {
        super.flushCache();
    }



    public Asset createAsset(String parent, String publishSpace, XWikiContext context) throws XWikiException {
        return Asset.createTempAsset(parent, publishSpace, context);
    }

    public Asset createAsset(String parent, XWikiContext context) throws XWikiException {
        return Asset.createTempAsset(parent, context);
    }


{'fraction_non_alphanumeric': 0.10139075818752805, 'fraction_numerical': 0.002392702258112756, 'mean_word_length': 2.6937310135321733, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6747146', 'n_tokens_mistral': 3719, 'n_tokens_neox': 3639, 'n_words': 738}