<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 org.xwiki.wikistream.instance.internal.output;

import java.io.IOException;
import java.util.Locale;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;

import org.xwiki.component.annotation.Component;
import org.xwiki.component.annotation.InstantiationStrategy;
import org.xwiki.component.descriptor.ComponentInstantiationStrategy;
import org.xwiki.component.manager.ComponentManager;
import org.xwiki.filter.FilterDescriptorManager;
import org.xwiki.filter.FilterEventParameters;
import org.xwiki.model.reference.DocumentReferenceResolver;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.model.reference.EntityReferenceResolver;
import org.xwiki.wikistream.WikiStreamException;
import org.xwiki.wikistream.instance.output.DocumentInstanceOutputProperties;
import org.xwiki.wikistream.internal.output.AbstractBeanOutputWikiStream;
import org.xwiki.wikistream.model.filter.WikiDocumentFilter;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.doc.XWikiDocument;

/**
 * @version $Id$
 * @since 5.2
 */
@Component
@Named(DocumentInstanceOutputWikiStreamFactory.ROLEHINT)
@InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP)
public class DocumentInstanceOutputWikiStream extends AbstractBeanOutputWikiStream<DocumentInstanceOutputProperties>
    implements WikiDocumentFilter
{
    @Inject
    private FilterDescriptorManager filterManager;

    @Inject
    @Named("current")
    private DocumentReferenceResolver<EntityReference> entityResolver;

    @Inject
    @Named("relative")
    private EntityReferenceResolver<String> relativeResolver;

    @Inject
    private Provider<XWikiContext> xcontextProvider;

    @Inject
    @Named("context")
    private Provider<ComponentManager> componentManagerProvider;

    @Inject
    private XWikiDocumentOutputWikiStream documentListener;

    @Override
    protected Object createFilter() throws WikiStreamException
    {
        return this.filterManager.createCompositeFilter(this.documentListener.getFilter(), this);
    }

    @Override
    public void close() throws IOException
    {
        // Nothing to close
    }

    @Override
    public void setProperties(DocumentInstanceOutputProperties properties) throws WikiStreamException
    {
        super.setProperties(properties);

        this.documentListener.setProperties(properties);
    }
    
    // Events

    @Override
    public void beginWikiDocument(String arg0, FilterEventParameters arg1) throws WikiStreamException
    {
        // Nothing to do
    }

    @Override
    public void endWikiDocument(String arg0, FilterEventParameters arg1) throws WikiStreamException
    {
        // Nothing to do
    }

    @Override
    public void beginWikiDocumentLocale(Locale arg0, FilterEventParameters arg1) throws WikiStreamException
    {
        // Nothing to do
    }

    @Override
    public void endWikiDocumentLocale(Locale arg0, FilterEventParameters arg1) throws WikiStreamException
    {
        // Nothing to do
    }

    @Override
    public void beginWikiDocumentRevision(String arg0, FilterEventParameters arg1) throws WikiStreamException
    {
        // Nothing to do
    }

    @Override
    public void endWikiDocumentRevision(String version, FilterEventParameters parameters) throws WikiStreamException
    {
        XWikiDocument inputDocument = this.documentListener.getDocument();

        XWikiContext xcontext = this.xcontextProvider.get();

        try {
            XWikiDocument document =
                xcontext.getWiki().getDocument(inputDocument.getDocumentReferenceWithLocale(), xcontext);

            if (document.isNew()) {
                document = inputDocument;
            } else {
                if (this.properties.isPreviousDeleted()) {
                    xcontext.getWiki().deleteDocument(document, xcontext);
                    document = inputDocument;
                } else {
                    document.loadAttachmentsContent(xcontext);
                    document.apply(inputDocument);
                }
            }

            document.setMinorEdit(inputDocument.isMinorEdit());

            // Author

            if (this.properties.isAuthorPreserved()) {
                document.setAuthorReference(inputDocument.getAuthorReference());
                document.setContentAuthorReference(inputDocument.getContentAuthorReference());
            } else {
                if (document.isNew()) {
                    document.setCreatorReference(xcontext.getUserReference());
                }
                document.setAuthorReference(xcontext.getUserReference());
                document.setContentAuthorReference(xcontext.getUserReference());
            }
<commit_msg>XWIKI-10020: Only the last version is kept in wikistream instance output module if delete previous option is enabled<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 org.xwiki.wikistream.instance.internal.output;

import java.io.IOException;
import java.util.Locale;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;

import org.xwiki.component.annotation.Component;
import org.xwiki.component.annotation.InstantiationStrategy;
import org.xwiki.component.descriptor.ComponentInstantiationStrategy;
import org.xwiki.component.manager.ComponentManager;
import org.xwiki.filter.FilterDescriptorManager;
import org.xwiki.filter.FilterEventParameters;
import org.xwiki.model.reference.DocumentReferenceResolver;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.model.reference.EntityReferenceResolver;
import org.xwiki.wikistream.WikiStreamException;
import org.xwiki.wikistream.instance.output.DocumentInstanceOutputProperties;
import org.xwiki.wikistream.internal.output.AbstractBeanOutputWikiStream;
import org.xwiki.wikistream.model.filter.WikiDocumentFilter;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.doc.XWikiDocument;

/**
 * @version $Id$
 * @since 5.2
 */
@Component
@Named(DocumentInstanceOutputWikiStreamFactory.ROLEHINT)
@InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP)
public class DocumentInstanceOutputWikiStream extends AbstractBeanOutputWikiStream<DocumentInstanceOutputProperties>
    implements WikiDocumentFilter
{
    @Inject
    private FilterDescriptorManager filterManager;

    @Inject
    @Named("current")
    private DocumentReferenceResolver<EntityReference> entityResolver;

    @Inject
    @Named("relative")
    private EntityReferenceResolver<String> relativeResolver;

    @Inject
    private Provider<XWikiContext> xcontextProvider;

    @Inject
    @Named("context")
    private Provider<ComponentManager> componentManagerProvider;

    @Inject
    private XWikiDocumentOutputWikiStream documentListener;

    private boolean documentDeleted;

    @Override
    protected Object createFilter() throws WikiStreamException
    {
        return this.filterManager.createCompositeFilter(this.documentListener.getFilter(), this);
    }

    @Override
    public void close() throws IOException
    {
        // Nothing to close
    }

    @Override
    public void setProperties(DocumentInstanceOutputProperties properties) throws WikiStreamException
    {
        super.setProperties(properties);

        this.documentListener.setProperties(properties);
    }
    
    // Events

    @Override
    public void beginWikiDocument(String arg0, FilterEventParameters arg1) throws WikiStreamException
    {
        this.documentDeleted = false;
    }

    @Override
    public void endWikiDocument(String arg0, FilterEventParameters arg1) throws WikiStreamException
    {
        // Nothing to do
    }

    @Override
    public void beginWikiDocumentLocale(Locale arg0, FilterEventParameters arg1) throws WikiStreamException
    {
        // Nothing to do
    }

    @Override
    public void endWikiDocumentLocale(Locale arg0, FilterEventParameters arg1) throws WikiStreamException
    {
        // Nothing to do
    }

    @Override
    public void beginWikiDocumentRevision(String arg0, FilterEventParameters arg1) throws WikiStreamException
    {
        // Nothing to do
    }

    @Override
    public void endWikiDocumentRevision(String version, FilterEventParameters parameters) throws WikiStreamException
    {
        XWikiDocument inputDocument = this.documentListener.getDocument();

        XWikiContext xcontext = this.xcontextProvider.get();

        try {
            XWikiDocument document =
                xcontext.getWiki().getDocument(inputDocument.getDocumentReferenceWithLocale(), xcontext);

            if (document.isNew()) {
                document = inputDocument;
            } else {
                if (this.properties.isPreviousDeleted() && !this.documentDeleted) {
                    xcontext.getWiki().deleteDocument(document, xcontext);
                    this.documentDeleted = true;
                    document = inputDocument;
                } else {
                    document.loadAttachmentsContent(xcontext);
                    document.apply(inputDocument);
                }
            }

            document.setMinorEdit(inputDocument.isMinorEdit());

            // Author

            if (this.properties.isAuthorPreserved()) {
                document.setAuthorReference(inputDocument.getAuthorReference());
                document.setContentAuthorReference(inputDocument.getContentAuthorReference());
            } else {
                if (document.isNew()) {
                    document.setCreatorReference(xcontext.getUserReference());
                }
                document.setAuthorReference(xcontext.getUserReference());
                document.setContentAuthorReference(xcontext.getUserReference());
            }

{'fraction_non_alphanumeric': 0.07331709862584797, 'fraction_numerical': 0.004783440598364933, 'mean_word_length': 3.313203300825206, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '686692', 'n_tokens_mistral': 2923, 'n_tokens_neox': 2817, 'n_words': 728}