<commit_before>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package com.celements.web.plugin;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.VelocityContext;

import com.celements.mandatory.CheckMandatoryDocuments;
import com.celements.navigation.cmd.GetMappedMenuItemsForParentCommand;
import com.celements.pagetype.IPageType;
import com.celements.rendering.RenderCommand;
import com.celements.web.plugin.api.CelementsWebPluginApi;
import com.celements.web.plugin.cmd.AddTranslationCommand;
import com.celements.web.plugin.cmd.CelSendMail;
import com.celements.web.plugin.cmd.CheckClassesCommand;
import com.celements.web.plugin.cmd.PasswordRecoveryAndEmailValidationCommand;
import com.celements.web.plugin.cmd.PossibleLoginsCommand;
import com.celements.web.plugin.cmd.SkinConfigObjCommand;
import com.celements.web.plugin.cmd.TokenBasedUploadCommand;
import com.celements.web.plugin.cmd.UserNameForUserDataCommand;
import com.celements.web.service.IPrepareVelocityContext;
import com.celements.web.service.IWebUtilsService;
import com.celements.web.token.NewCelementsTokenForUserCommand;
import com.celements.web.utils.IWebUtils;
import com.celements.web.utils.WebUtils;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.api.Api;
import com.xpn.xwiki.api.Attachment;
import com.xpn.xwiki.api.Document;
import com.xpn.xwiki.doc.XWikiAttachment;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.objects.BaseObject;
import com.xpn.xwiki.objects.classes.PasswordClass;
import com.xpn.xwiki.plugin.XWikiDefaultPlugin;
import com.xpn.xwiki.plugin.XWikiPluginInterface;
import com.xpn.xwiki.store.XWikiStoreInterface;
import com.xpn.xwiki.user.api.XWikiUser;
import com.xpn.xwiki.util.Util;
import com.xpn.xwiki.web.Utils;
import com.xpn.xwiki.web.XWikiResponse;

public class CelementsWebPlugin extends XWikiDefaultPlugin {

  private static Log LOGGER = LogFactory.getFactory().getInstance(
      CelementsWebPlugin.class);

  private final static IWebUtils util = WebUtils.getInstance();

  final String PARAM_XPAGE = "xpage";
  final String PARAM_CONF = "conf";
  final String PARAM_AJAX_MODE = "ajax_mode";
  final String PARAM_SKIN = "skin";
  final String PARAM_LANGUAGE = "language";
  final String PARAM_XREDIRECT = "xredirect";

  private List<String> supportedAdminLangList;

  private CelSendMail injectedCelSendMail;

  public CelementsWebPlugin(
      String name, String className, XWikiContext context) {
    super(name, className, context);
  }

  public Api getPluginApi(XWikiPluginInterface plugin, XWikiContext context) {
    return new CelementsWebPluginApi((CelementsWebPlugin) plugin, context);
  }

  public String getName() {
    return getPrepareVelocityContextService().getVelocityName();
  }

  public void flushCache() {
    //TODO: check if flushCache is called for changing a page MenuItem.
    LOGGER.debug("Entered method flushCache");
  }

  public void flushCache(XWikiContext context) {
    util.flushMenuItemCache(context);
  }

  public void init(XWikiContext context) {
    LOGGER.trace("init called database [" + context.getDatabase() + "]");
    new CheckClassesCommand().checkClasses();
    super.init(context);
  }

  public void virtualInit(XWikiContext context) {
    LOGGER.trace("virtualInit called database [" + context.getDatabase() + "]");
    new CheckClassesCommand().checkClasses();
    new CheckMandatoryDocuments().checkMandatoryDocuments();
    super.virtualInit(context);
  }

  public int queryCount() {
    return util.queryCount();
  }
  
  /**
   * getSubMenuItemsForParent
   * get all submenu items of given parent document (by fullname).
   * 
   * @param parent
   * @param menuSpace (default: $doc.space)
   * @param menuPart 
   * @return (array of menuitems)
   */
  public List<com.xpn.xwiki.api.Object> getSubMenuItemsForParent(
      String parent, String menuSpace, String menuPart, XWikiContext context) {
    return util.getSubMenuItemsForParent(parent, menuSpace, menuPart, context);
  }

  public String getVersionMode(XWikiContext context) {
    String versionMode = context.getWiki().getSpacePreference("celements_version",
        context);
    if ("---".equals(versionMode)) {
      versionMode = context.getWiki().getXWikiPreference("celements_version",
          "celements2", context);
      if ("---".equals(versionMode)) {
        versionMode = "celements2";
      }<commit_msg>make checking classCollections on init and virtualInit configurable make checking mandatory documents on init and virtualInit configurable<commit_after>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package com.celements.web.plugin;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.VelocityContext;

import com.celements.mandatory.CheckMandatoryDocuments;
import com.celements.navigation.cmd.GetMappedMenuItemsForParentCommand;
import com.celements.pagetype.IPageType;
import com.celements.rendering.RenderCommand;
import com.celements.web.plugin.api.CelementsWebPluginApi;
import com.celements.web.plugin.cmd.AddTranslationCommand;
import com.celements.web.plugin.cmd.CelSendMail;
import com.celements.web.plugin.cmd.CheckClassesCommand;
import com.celements.web.plugin.cmd.PasswordRecoveryAndEmailValidationCommand;
import com.celements.web.plugin.cmd.PossibleLoginsCommand;
import com.celements.web.plugin.cmd.SkinConfigObjCommand;
import com.celements.web.plugin.cmd.TokenBasedUploadCommand;
import com.celements.web.plugin.cmd.UserNameForUserDataCommand;
import com.celements.web.service.IPrepareVelocityContext;
import com.celements.web.service.IWebUtilsService;
import com.celements.web.token.NewCelementsTokenForUserCommand;
import com.celements.web.utils.IWebUtils;
import com.celements.web.utils.WebUtils;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.api.Api;
import com.xpn.xwiki.api.Attachment;
import com.xpn.xwiki.api.Document;
import com.xpn.xwiki.doc.XWikiAttachment;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.objects.BaseObject;
import com.xpn.xwiki.objects.classes.PasswordClass;
import com.xpn.xwiki.plugin.XWikiDefaultPlugin;
import com.xpn.xwiki.plugin.XWikiPluginInterface;
import com.xpn.xwiki.store.XWikiStoreInterface;
import com.xpn.xwiki.user.api.XWikiUser;
import com.xpn.xwiki.util.Util;
import com.xpn.xwiki.web.Utils;
import com.xpn.xwiki.web.XWikiResponse;

public class CelementsWebPlugin extends XWikiDefaultPlugin {

  private static Log LOGGER = LogFactory.getFactory().getInstance(
      CelementsWebPlugin.class);

  private final static IWebUtils util = WebUtils.getInstance();

  final String PARAM_XPAGE = "xpage";
  final String PARAM_CONF = "conf";
  final String PARAM_AJAX_MODE = "ajax_mode";
  final String PARAM_SKIN = "skin";
  final String PARAM_LANGUAGE = "language";
  final String PARAM_XREDIRECT = "xredirect";

  private List<String> supportedAdminLangList;

  private CelSendMail injectedCelSendMail;

  public CelementsWebPlugin(
      String name, String className, XWikiContext context) {
    super(name, className, context);
  }

  public Api getPluginApi(XWikiPluginInterface plugin, XWikiContext context) {
    return new CelementsWebPluginApi((CelementsWebPlugin) plugin, context);
  }

  public String getName() {
    return getPrepareVelocityContextService().getVelocityName();
  }

  public void flushCache() {
    //TODO: check if flushCache is called for changing a page MenuItem.
    LOGGER.debug("Entered method flushCache");
  }

  public void flushCache(XWikiContext context) {
    util.flushMenuItemCache(context);
  }

  public void init(XWikiContext context) {
    LOGGER.trace("init called database [" + context.getDatabase() + "]");
    if ("1".equals(context.getWiki().Param("celements.classCollections.checkOnStart",
        "1"))) {
      new CheckClassesCommand().checkClasses();
    }
    super.init(context);
  }

  public void virtualInit(XWikiContext context) {
    LOGGER.trace("virtualInit called database [" + context.getDatabase() + "]");
    if ("1".equals(context.getWiki().Param("celements.classCollections.checkOnStart",
        "1"))) {
      new CheckClassesCommand().checkClasses();
    }
    if ("1".equals(context.getWiki().Param("celements.mandatory.checkOnStart",
        "1"))) {
      new CheckMandatoryDocuments().checkMandatoryDocuments();
    }
    super.virtualInit(context);
  }

  public int queryCount() {
    return util.queryCount();
  }
  
  /**
   * getSubMenuItemsForParent
   * get all submenu items of given parent document (by fullname).
   * 
   * @param parent
   * @param menuSpace (default: $doc.space)
   * @param menuPart 
   * @return (array of menuitems)
   */
  public List<com.xpn.xwiki.api.Object> getSubMenuItemsForParent(
      String parent, String menuSpace, String menuPart, XWikiContext context) {
    return util.getSubMenuItemsForParent(parent, menuSpace, menuPart, context);
  }

  public String getVersionMode(XWikiContext context) {
    String versionMode = context.getWiki().getSpacePreference("celements_version",
        context);
    if ("---".equals(versionMode)) {
      versionMode = context.getWiki().getXWikiPreference("celements_version",
          "celements2", context);
      if ("---".equals(versionMode)) {
        versionMode = "celements2";
      }
{'fraction_non_alphanumeric': 0.09976818064737701, 'fraction_numerical': 0.0030909247016399073, 'mean_word_length': 4.712604217753801, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '827485', 'n_tokens_mistral': 3571, 'n_tokens_neox': 3476, 'n_words': 853}