<commit_before>package com.xpn.xwiki.plugin.wikimanager.doc;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xwiki.bridge.event.WikiDeletedEvent;
import org.xwiki.observation.ObservationManager;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.api.Document;
import com.xpn.xwiki.api.Object;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.objects.BaseObject;
import com.xpn.xwiki.plugin.wikimanager.WikiManagerException;
import com.xpn.xwiki.plugin.wikimanager.WikiManagerMessageTool;
import com.xpn.xwiki.web.Utils;

/**
 * This class manage wiki document descriptor.
 * 
 * @version $Id$
 */
// TODO: Future: XA2: create a Wiki interface to implement here and rename.
public class Wiki extends Document
{
    /**
     * The logging tool.
     */
    protected static final Log LOG = LogFactory.getLog(Wiki.class);

    /**
     * Create instance of wiki descriptor.
     * 
     * @param xdoc the encapsulated XWikiDocument.
     * @param context the XWiki context.
     * @throws XWikiException error when creating {@link Document}.
     */
    public Wiki(XWikiDocument xdoc, XWikiContext context) throws XWikiException
    {
        super(xdoc, context);
    }

    /**
     * @return the name of the wiki.
     * @throws XWikiException error when getting {@link XWikiServerClass} instance.
     */
    public String getWikiName() throws XWikiException
    {
        return XWikiServerClass.getInstance(this.context).getItemDefaultName(getFullName());
    }

    /**
     * Delete the wiki.
     * 
     * @param deleteDatabase if true wiki's database is also removed.
     * @throws XWikiException error deleting the wiki.
     * @since 1.1
     */
    public void delete(boolean deleteDatabase) throws XWikiException
    {
        String wikiName = getWikiName();

        if (wikiName.equals(this.context.getMainXWiki())) {
            throw new WikiManagerException(XWikiException.ERROR_XWIKI_ACCESS_DENIED, WikiManagerMessageTool.getDefault(
                this.context).get(WikiManagerMessageTool.ERROR_DELETEMAINWIKI, wikiName));
        }

        if (hasAdminRights()) {
            try {
                this.context.getWiki().getStore().deleteWiki(wikiName, this.context);
                Utils.getComponent(ObservationManager.class).notify(new WikiDeletedEvent(wikiName), wikiName,
                    this.context);
            } catch (XWikiException e) {
                LOG.error("Failed to delete wiki from database", e);
            }
            this.context.getWiki().getVirtualWikiList().remove(wikiName);
        } else {
            throw new WikiManagerException(XWikiException.ERROR_XWIKI_ACCESS_DENIED, WikiManagerMessageTool.getDefault(
                this.context).get(WikiManagerMessageTool.ERROR_RIGHTTODELETEWIKI, wikiName));
        }

        super.delete();
    }

    /**
     * @return the number of wiki aliases in this wiki.
     * @throws XWikiException when getting the number of wiki aliases.<commit_msg>XWIKI-6734: WikiManager's delete wiki without deleting database method not working.<commit_after>package com.xpn.xwiki.plugin.wikimanager.doc;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xwiki.bridge.event.WikiDeletedEvent;
import org.xwiki.observation.ObservationManager;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.api.Document;
import com.xpn.xwiki.api.Object;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.objects.BaseObject;
import com.xpn.xwiki.plugin.wikimanager.WikiManagerException;
import com.xpn.xwiki.plugin.wikimanager.WikiManagerMessageTool;
import com.xpn.xwiki.web.Utils;

/**
 * This class manage wiki document descriptor.
 * 
 * @version $Id$
 */
// TODO: Future: XA2: create a Wiki interface to implement here and rename.
public class Wiki extends Document
{
    /**
     * The logging tool.
     */
    protected static final Log LOG = LogFactory.getLog(Wiki.class);

    /**
     * Create instance of wiki descriptor.
     * 
     * @param xdoc the encapsulated XWikiDocument.
     * @param context the XWiki context.
     * @throws XWikiException error when creating {@link Document}.
     */
    public Wiki(XWikiDocument xdoc, XWikiContext context) throws XWikiException
    {
        super(xdoc, context);
    }

    /**
     * @return the name of the wiki.
     * @throws XWikiException error when getting {@link XWikiServerClass} instance.
     */
    public String getWikiName() throws XWikiException
    {
        return XWikiServerClass.getInstance(this.context).getItemDefaultName(getFullName());
    }

    /**
     * Delete the wiki.
     * 
     * @param deleteDatabase if true wiki's database is also removed.
     * @throws XWikiException error deleting the wiki.
     * @since 1.1
     */
    public void delete(boolean deleteDatabase) throws XWikiException
    {
        String wikiName = getWikiName();

        if (wikiName.equals(this.context.getMainXWiki())) {
            throw new WikiManagerException(XWikiException.ERROR_XWIKI_ACCESS_DENIED, WikiManagerMessageTool.getDefault(
                this.context).get(WikiManagerMessageTool.ERROR_DELETEMAINWIKI, wikiName));
        }

        if (deleteDatabase) {
            if (hasAdminRights()) {
                try {
                    this.context.getWiki().getStore().deleteWiki(wikiName, this.context);
                } catch (XWikiException e) {
                    LOG.error("Failed to delete wiki from database", e);
                }
            } else {
                throw new WikiManagerException(XWikiException.ERROR_XWIKI_ACCESS_DENIED, WikiManagerMessageTool
                    .getDefault(this.context).get(WikiManagerMessageTool.ERROR_RIGHTTODELETEWIKI, wikiName));
            }
        }

        super.delete();

        Utils.getComponent(ObservationManager.class).notify(new WikiDeletedEvent(wikiName), wikiName, this.context);
    }

    /**
     * @return the number of wiki aliases in this wiki.
     * @throws XWikiException when getting the number of wiki aliases.
{'fraction_non_alphanumeric': 0.0965702544649913, 'fraction_numerical': 0.00158052789631737, 'mean_word_length': 2.9304347826086956, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5838025', 'n_tokens_mistral': 1937, 'n_tokens_neox': 1847, 'n_words': 415}