use of org.xwiki.extension.xar.question.CleanPagesQuestion in project xwiki-platform by xwiki.
the class CleanPagesQuestionRecorderTest method recordAndReplay.
@Test
public void recordAndReplay() {
DocumentReference aliceReference = new DocumentReference("dev", "Users", "Alice");
DocumentReference bobReference = new DocumentReference("dev", "Users", "Bob");
DocumentReference carolReference = new DocumentReference("dev", "Users", "Carol");
CleanPagesQuestion firstQuestion = new CleanPagesQuestion(Arrays.asList(aliceReference, bobReference));
firstQuestion.getPages().put(aliceReference, false);
CleanPagesQuestion secondQuestion = new CleanPagesQuestion(Arrays.asList(carolReference));
secondQuestion.getPages().put(carolReference, false);
CleanPagesQuestionRecorder recorder = new CleanPagesQuestionRecorder();
recorder.record(firstQuestion);
recorder.record(secondQuestion);
DocumentReference johnReference = new DocumentReference("drafts", "Users", "John");
DocumentReference aliceDraftsRef = aliceReference.replaceParent(aliceReference.getWikiReference(), johnReference.getWikiReference());
CleanPagesQuestion thirdQuestion = new CleanPagesQuestion(Arrays.asList(aliceDraftsRef, johnReference));
assertFalse(recorder.replay(thirdQuestion));
assertFalse(thirdQuestion.getPages().get(aliceDraftsRef));
assertTrue(thirdQuestion.getPages().get(johnReference));
DocumentReference bobDraftsRef = bobReference.replaceParent(bobReference.getWikiReference(), johnReference.getWikiReference());
DocumentReference carolDraftsRef = carolReference.replaceParent(carolReference.getWikiReference(), johnReference.getWikiReference());
CleanPagesQuestion fourthQuestion = new CleanPagesQuestion(Arrays.asList(bobDraftsRef, carolDraftsRef));
assertTrue(recorder.replay(fourthQuestion));
assertTrue(fourthQuestion.getPages().get(bobDraftsRef));
assertFalse(fourthQuestion.getPages().get(carolDraftsRef));
}
use of org.xwiki.extension.xar.question.CleanPagesQuestion 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);
}
}
}
}
}
Aggregations