<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.xpn.xwiki.store.migration;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiDocument;
import org.apache.commons.collections.set.ListOrderedSet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.*;

/**
 * Template for {@link XWikiMigrationManagerInterface}.
 * @version $Id: $
 */
public abstract class AbstractXWikiMigrationManager implements XWikiMigrationManagerInterface
{
    /** logger. */
    private static final Log LOG = LogFactory.getLog(AbstractXWikiMigrationManager.class);
    /**
     * Unified constructor for all subclasses.
     * @param context - used everywhere
     */
    public AbstractXWikiMigrationManager(XWikiContext context) { }
    /**
     * read data version from xwiki.cfg.
     * @param context used for read config
     * @return data version if set, or null.
     */
    protected XWikiDBVersion getDBVersionFromConfig(XWikiContext context)
    {
        String ver = context.getWiki().getConfig().getProperty("xwiki.store.migration.version");
        return ver == null ? null : new XWikiDBVersion(Integer.parseInt(ver));
    }
    /**
     * {@inheritDoc}
     */
    public XWikiDBVersion getDBVersion(XWikiContext context) throws XWikiException
    {
        XWikiDBVersion result = getDBVersionFromConfig(context);
        return result == null ? new XWikiDBVersion(0) : result;
    }
    /**
     * @param version to set
     * @param context used everywhere
     * @throws XWikiException if any error
     */
    protected abstract void setDBVersion(XWikiDBVersion version, XWikiContext context)
        throws XWikiException;

    /**
     * {@inheritDoc}
     */
    public void startMigrations(XWikiContext context) throws XWikiException
    {
        if (context.getWiki().isVirtual()) {
            // Save context values so that we can restore them as they were before the migration.
            String currentWikiOwner = context.getWikiOwner();
            XWikiDocument currentWikiServer = context.getWikiServer();
            boolean currentIsVirtual = context.isVirtual();
            String currentDatabase = context.getDatabase();
            String currentOriginalDatabase = context.getOriginalDatabase();

            try {
                for (Iterator it = getDatabasesToMigrate(context).iterator(); it.hasNext();) {
                    String database = (String) it.next();
                    LOG.info("Starting migration for database [" + database + "]...");
                    // Set up the context so that it points to the virtual wiki corresponding to the database.
                    setContextForDatabase(database, context);
                    startMigrationsForDatabase(context);
                }
            } finally {
                context.setWikiOwner(currentWikiOwner);
                context.setWikiServer(currentWikiServer);
                context.setVirtual(currentIsVirtual);
                context.setDatabase(currentDatabase);
                context.setOriginalDatabase(currentOriginalDatabase);
            }
        } else {
            // Just migrate the main wiki
            startMigrationsForDatabase(context);
        }
    }

    /**
     * Sets the XWiki Context so that it points to the passed databasse name
     *
     * @param databaseName the name of the database to point to. Example: "xwiki"
     */
    protected void setContextForDatabase(String databaseName, XWikiContext context) throws XWikiException
    {
        context.setVirtual(true);
        context.setDatabase(databaseName);
        context.setOriginalDatabase(databaseName);
    }

<commit_msg>XWIKI-1900: Allow database migrations also for multiwikis<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.xpn.xwiki.store.migration;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiDocument;
import org.apache.commons.collections.set.ListOrderedSet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.*;

/**
 * Template for {@link XWikiMigrationManagerInterface}.
 * @version $Id: $
 */
public abstract class AbstractXWikiMigrationManager implements XWikiMigrationManagerInterface
{
    /** logger. */
    private static final Log LOG = LogFactory.getLog(AbstractXWikiMigrationManager.class);
    /**
     * Unified constructor for all subclasses.
     * @param context - used everywhere
     */
    public AbstractXWikiMigrationManager(XWikiContext context) { }
    /**
     * read data version from xwiki.cfg.
     * @param context used for read config
     * @return data version if set, or null.
     */
    protected XWikiDBVersion getDBVersionFromConfig(XWikiContext context)
    {
        String ver = context.getWiki().getConfig().getProperty("xwiki.store.migration.version");
        return ver == null ? null : new XWikiDBVersion(Integer.parseInt(ver));
    }
    /**
     * {@inheritDoc}
     */
    public XWikiDBVersion getDBVersion(XWikiContext context) throws XWikiException
    {
        XWikiDBVersion result = getDBVersionFromConfig(context);
        return result == null ? new XWikiDBVersion(0) : result;
    }
    /**
     * @param version to set
     * @param context used everywhere
     * @throws XWikiException if any error
     */
    protected abstract void setDBVersion(XWikiDBVersion version, XWikiContext context)
        throws XWikiException;

    /**
     * {@inheritDoc}
     */
    public void startMigrations(XWikiContext context) throws XWikiException
    {
        if (context.getWiki().isVirtual()) {
            // Save context values so that we can restore them as they were before the migration.
            boolean currentIsVirtual = context.isVirtual();
            String currentDatabase = context.getDatabase();
            String currentOriginalDatabase = context.getOriginalDatabase();

            try {
                for (Iterator it = getDatabasesToMigrate(context).iterator(); it.hasNext();) {
                    String database = (String) it.next();
                    LOG.info("Starting migration for database [" + database + "]...");
                    // Set up the context so that it points to the virtual wiki corresponding to the database.
                    context.setVirtual(true);
                    context.setDatabase(database);
                    context.setOriginalDatabase(database);
                    startMigrationsForDatabase(context);
                }
            } finally {
                context.setVirtual(currentIsVirtual);
                context.setDatabase(currentDatabase);
                context.setOriginalDatabase(currentOriginalDatabase);
            }
        } else {
            // Just migrate the main wiki
            startMigrationsForDatabase(context);
        }
    }

{'fraction_non_alphanumeric': 0.0825910931174089, 'fraction_numerical': 0.0037015615962984385, 'mean_word_length': 2.7444781290601994, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5111440', 'n_tokens_mistral': 2237, 'n_tokens_neox': 2191, 'n_words': 711}