Search in sources :

Example 6 with XarEntry

use of org.xwiki.xar.XarEntry in project xwiki-platform by xwiki.

the class AbstractValidationTest method readXarContents.

protected static List<DocumentReference> readXarContents(String fileName, String patternFilter) throws Exception {
    Collection<XarEntry> entries = XarPackage.getEntries(new File(fileName));
    List<DocumentReference> result = new ArrayList<DocumentReference>(entries.size());
    WikiReference wikiReference = new WikiReference("xwiki");
    Pattern pattern = patternFilter == null ? null : Pattern.compile(patternFilter);
    for (XarEntry entry : entries) {
        if (pattern == null || pattern.matcher(SERIALIZER.serialize(entry)).matches()) {
            result.add(new DocumentReference(entry, wikiReference));
        }
    }
    return result;
}
Also used : Pattern(java.util.regex.Pattern) XarEntry(org.xwiki.xar.XarEntry) ArrayList(java.util.ArrayList) WikiReference(org.xwiki.model.reference.WikiReference) File(java.io.File) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 7 with XarEntry

use of org.xwiki.xar.XarEntry in project xwiki-platform by xwiki.

the class DiffXarJob method diff.

private void diff(XarFile xarFile, WikiReference wikiReference, ExtensionId extensionId, Set<LocalDocumentReference> alreadydone) {
    Collection<XarEntry> xarEntries = xarFile.getEntries();
    this.progressManager.pushLevelProgress(xarEntries.size(), this);
    try {
        for (XarEntry xarEntry : xarEntries) {
            this.progressManager.startStep(this);
            if (!alreadydone.contains(xarEntry)) {
                try {
                    diff(this.packager.getXWikiDocument(xarFile.getInputStream(xarEntry), wikiReference), extensionId);
                } catch (Exception e) {
                    // Skip this document and continue.
                    this.logger.error("Failed to parse document [{}] from XAR.", xarEntry.getDocumentName(), e);
                }
                alreadydone.add(xarEntry);
            }
        }
    } finally {
        try {
            xarFile.close();
        } catch (IOException e) {
        // Ignore.
        }
        this.progressManager.popLevelProgress(this);
    }
}
Also used : XarEntry(org.xwiki.xar.XarEntry) IOException(java.io.IOException) XWikiException(com.xpn.xwiki.XWikiException) IOException(java.io.IOException) UnsupportedNamespaceException(org.xwiki.extension.xar.internal.handler.UnsupportedNamespaceException) XarException(org.xwiki.xar.XarException)

Example 8 with XarEntry

use of org.xwiki.xar.XarEntry in project xwiki-platform by xwiki.

the class XarExtensionHandler method createPackageConfiguration.

private PackageConfiguration createPackageConfiguration(LocalExtension extension, Request request, String wiki) {
    PackageConfiguration configuration;
    // Search job configuration in the context
    ExecutionContext context = this.execution.getContext();
    if (context != null) {
        configuration = (PackageConfiguration) context.getProperty(CONTEXTKEY_PACKAGECONFIGURATION);
    } else {
        configuration = null;
    }
    // Create a configuration for this extension
    if (configuration != null) {
        configuration = configuration.clone();
    } else {
        configuration = new PackageConfiguration();
        DocumentReference userReference = getRequestUserReference(AbstractExtensionValidator.PROPERTY_USERREFERENCE, request);
        configuration.setInteractive(request.isInteractive());
        configuration.setUser(userReference);
        configuration.setVerbose(request.isVerbose());
        configuration.setSkipMandatorytDocuments(true);
    }
    configuration.setWiki(wiki);
    // Filter entries to import if there is a plan
    if (extension != null && configuration.getXarExtensionPlan() != null) {
        Map<String, Map<XarEntry, LocalExtension>> nextXAREntries = configuration.getXarExtensionPlan().nextXAREntries;
        Set<String> entriesToImport = new HashSet<>();
        Map<XarEntry, LocalExtension> nextXAREntriesOnRoot = nextXAREntries.get(null);
        if (nextXAREntriesOnRoot != null) {
            for (Map.Entry<XarEntry, LocalExtension> entry : nextXAREntriesOnRoot.entrySet()) {
                if (entry.getValue() == extension) {
                    entriesToImport.add(entry.getKey().getEntryName());
                }
            }
        }
        Map<XarEntry, LocalExtension> nextXAREntriesOnWiki = nextXAREntries.get(wiki);
        if (nextXAREntriesOnWiki != null) {
            for (Map.Entry<XarEntry, LocalExtension> entry : nextXAREntriesOnWiki.entrySet()) {
                if (entry.getValue() == extension) {
                    entriesToImport.add(entry.getKey().getEntryName());
                }
            }
        }
        configuration.setEntriesToImport(entriesToImport);
    }
    return configuration;
}
Also used : ExecutionContext(org.xwiki.context.ExecutionContext) XarEntry(org.xwiki.xar.XarEntry) LocalExtension(org.xwiki.extension.LocalExtension) PackageConfiguration(org.xwiki.extension.xar.internal.handler.packager.PackageConfiguration) Map(java.util.Map) DocumentReference(org.xwiki.model.reference.DocumentReference) HashSet(java.util.HashSet)

Example 9 with XarEntry

use of org.xwiki.xar.XarEntry in project xwiki-platform by xwiki.

the class XarExtensionJobFinishedListener method onEvent.

@Override
public void onEvent(Event event, Object source, Object data) {
    JobFinishingEvent jobFinishingEvent = (JobFinishingEvent) event;
    if (!jobFinishingEvent.getRequest().isRemote()) {
        ExecutionContext context = this.execution.getContext();
        if (context != null) {
            XarExtensionPlan xarExtensionPlan = (XarExtensionPlan) context.getProperty(XarExtensionPlan.CONTEXTKEY_XARINSTALLPLAN);
            if (xarExtensionPlan != null) {
                try {
                    Map<String, Map<XarEntry, XarExtensionPlanEntry>> previousXAREntries = xarExtensionPlan.previousXAREntries;
                    Map<String, Map<XarEntry, LocalExtension>> nextXAREntries = xarExtensionPlan.nextXAREntries;
                    Map<XarEntry, LocalExtension> rootNextPages = nextXAREntries.get(null);
                    if (rootNextPages == null) {
                        rootNextPages = Collections.emptyMap();
                    }
                    XWikiContext xcontext = this.xcontextProvider.get();
                    Packager packager = this.packagerProvider.get();
                    // Get pages to delete
                    Set<DocumentReference> pagesToDelete = new HashSet<DocumentReference>();
                    for (Map.Entry<String, Map<XarEntry, XarExtensionPlanEntry>> previousWikiEntry : previousXAREntries.entrySet()) {
                        if (!previousWikiEntry.getValue().isEmpty()) {
                            try {
                                List<DocumentReference> references = packager.getDocumentReferences(previousWikiEntry.getValue().keySet(), createPackageConfiguration(jobFinishingEvent.getRequest(), previousWikiEntry.getKey()));
                                for (DocumentReference reference : references) {
                                    // propose to enable them)
                                    if (((XarInstalledExtensionRepository) this.xarRepository).getXarInstalledExtensions(reference).isEmpty()) {
                                        pagesToDelete.add(reference);
                                    }
                                }
                            } catch (Exception e) {
                                this.logger.warn("Exception when cleaning pages removed since previous xar extension version", e);
                            }
                        }
                    }
                    // Create cleanup question
                    CleanPagesQuestion question = new CleanPagesQuestion(pagesToDelete);
                    Map<DocumentReference, Boolean> pages = question.getPages();
                    // Remove pages which are in the next XAR packages
                    for (DocumentReference previousReference : pagesToDelete) {
                        if (xarExtensionPlan.containsNewPage(previousReference)) {
                            pages.remove(previousReference);
                        }
                    }
                    for (Map.Entry<DocumentReference, Boolean> entry : pages.entrySet()) {
                        DocumentReference reference = entry.getKey();
                        // Get current
                        XWikiDocument currentDocument;
                        try {
                            currentDocument = xcontext.getWiki().getDocument(reference, xcontext);
                        } catch (Exception e) {
                            this.logger.error("Failed to get document [{}]", reference, e);
                            // Lets be safe and skip removing that page
                            pages.put(reference, false);
                            continue;
                        }
                        if (currentDocument.isNew()) {
                            // Current already removed
                            pages.put(reference, false);
                            continue;
                        }
                        // Get previous
                        XWikiDocument previousDocument;
                        try {
                            previousDocument = xarExtensionPlan.getPreviousXWikiDocument(reference, packager);
                        } catch (Exception e) {
                            this.logger.error("Failed to get previous version of document [{}]", reference, e);
                            // Lets be safe and skip removing that page
                            pages.put(reference, false);
                            continue;
                        }
                        // Compare previous and current
                        try {
                            currentDocument.loadAttachmentsContentSafe(xcontext);
                            if (!currentDocument.equalsData(previousDocument)) {
                                // conflict between current and new
                                pages.put(reference, false);
                            }
                        } catch (Exception e) {
                            this.logger.error("Failed to load attachments", e);
                            // Lets be safe and skip removing that page
                            pages.put(reference, false);
                            continue;
                        }
                    }
                    // Ask confirmation
                    if (!pages.isEmpty() && jobFinishingEvent.getRequest().isInteractive()) {
                        try {
                            ((Job) source).getStatus().ask(question);
                        } catch (InterruptedException e) {
                            this.logger.warn("The thread has been interrupted", e);
                            // The thread has been interrupted, do nothing
                            return;
                        }
                    }
                    // Delete pages
                    PackageConfiguration configuration = createPackageConfiguration(jobFinishingEvent.getRequest());
                    for (Map.Entry<DocumentReference, Boolean> entry : pages.entrySet()) {
                        if (entry.getValue()) {
                            packager.deleteDocument(entry.getKey(), configuration);
                        }
                    }
                } finally {
                    // Cleanup extension plan
                    try {
                        xarExtensionPlan.close();
                    } catch (IOException e) {
                        this.logger.error("Failed to close XAR extension plan", e);
                    }
                    context.setProperty(XarExtensionPlan.CONTEXTKEY_XARINSTALLPLAN, null);
                }
            }
        }
    }
}
Also used : XarEntry(org.xwiki.xar.XarEntry) CleanPagesQuestion(org.xwiki.extension.xar.question.CleanPagesQuestion) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Packager(org.xwiki.extension.xar.internal.handler.packager.Packager) LocalExtension(org.xwiki.extension.LocalExtension) PackageConfiguration(org.xwiki.extension.xar.internal.handler.packager.PackageConfiguration) DocumentReference(org.xwiki.model.reference.DocumentReference) JobFinishingEvent(org.xwiki.job.event.JobFinishingEvent) HashSet(java.util.HashSet) XWikiContext(com.xpn.xwiki.XWikiContext) IOException(java.io.IOException) IOException(java.io.IOException) ExecutionContext(org.xwiki.context.ExecutionContext) Map(java.util.Map)

Example 10 with XarEntry

use of org.xwiki.xar.XarEntry in project xwiki-platform by xwiki.

the class XarExtensionPlan method getNextXarExtension.

public LocalExtension getNextXarExtension(String wiki, LocalDocumentReference localDocumentReference) {
    XarEntry xarEntry = new XarEntry(localDocumentReference);
    LocalExtension nextExtension = null;
    Map<XarEntry, LocalExtension> wikiEntry = this.nextXAREntries.get(wiki);
    if (wikiEntry != null) {
        nextExtension = wikiEntry.get(xarEntry);
    }
    if (nextExtension == null) {
        wikiEntry = this.nextXAREntries.get(null);
        if (wikiEntry != null) {
            nextExtension = wikiEntry.get(xarEntry);
        }
    }
    return nextExtension;
}
Also used : XarEntry(org.xwiki.xar.XarEntry) LocalExtension(org.xwiki.extension.LocalExtension)

Aggregations

XarEntry (org.xwiki.xar.XarEntry)11 DocumentReference (org.xwiki.model.reference.DocumentReference)5 File (java.io.File)3 IOException (java.io.IOException)3 LocalExtension (org.xwiki.extension.LocalExtension)3 PackageConfiguration (org.xwiki.extension.xar.internal.handler.packager.PackageConfiguration)3 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)3 WikiReference (org.xwiki.model.reference.WikiReference)3 XWikiContext (com.xpn.xwiki.XWikiContext)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Pattern (java.util.regex.Pattern)2 ExecutionContext (org.xwiki.context.ExecutionContext)2 XarException (org.xwiki.xar.XarException)2 XWikiException (com.xpn.xwiki.XWikiException)1 MergeConfiguration (com.xpn.xwiki.doc.merge.MergeConfiguration)1 MergeResult (com.xpn.xwiki.doc.merge.MergeResult)1 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)1