Search in sources :

Example 1 with XarInstalledExtension

use of org.xwiki.extension.xar.internal.repository.XarInstalledExtension in project xwiki-platform by xwiki.

the class XarExtensionHandler method uninstall.

@Override
public void uninstall(InstalledExtension installedExtension, String namespace, Request request) throws UninstallException {
    try {
        initializePagesIndex(request);
        initJobPackageConfiguration(request, false);
    } catch (Exception e) {
        throw new UninstallException("Failed to initialize extension plan index", e);
    }
    // probably not be in an expected state)
    if (!request.isRemote()) {
        Job currentJob;
        try {
            currentJob = this.componentManager.<JobContext>getInstance(JobContext.class).getCurrentJob();
        } catch (ComponentLookupException e) {
            currentJob = null;
        }
        if (currentJob == null) {
            String wiki;
            try {
                wiki = XarHandlerUtils.getWikiFromNamespace(namespace);
            } catch (UnsupportedNamespaceException e) {
                throw new UninstallException("Failed to extract wiki id from namespace", e);
            }
            PackageConfiguration configuration = createPackageConfiguration(null, request, wiki);
            try {
                XarInstalledExtension xarLocalExtension = (XarInstalledExtension) this.xarRepository.resolve(installedExtension.getId());
                Collection<XarEntry> pages = xarLocalExtension.getXarPackage().getEntries();
                this.packager.unimportPages(pages, configuration);
            } catch (Exception e) {
                // Not supposed to be possible
                throw new UninstallException("Failed to get xar extension [" + installedExtension.getId() + "] from xar repository", e);
            }
        } else {
        // The actual delete of pages is done in XarExtensionJobFinishedListener
        }
    }
}
Also used : XarInstalledExtension(org.xwiki.extension.xar.internal.repository.XarInstalledExtension) XarEntry(org.xwiki.xar.XarEntry) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) JobContext(org.xwiki.job.JobContext) Job(org.xwiki.job.Job) AbstractExtensionJob(org.xwiki.extension.job.internal.AbstractExtensionJob) PackageConfiguration(org.xwiki.extension.xar.internal.handler.packager.PackageConfiguration) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) ExtensionException(org.xwiki.extension.ExtensionException) IOException(java.io.IOException) UninstallException(org.xwiki.extension.UninstallException) InstallException(org.xwiki.extension.InstallException) XarException(org.xwiki.xar.XarException) UninstallException(org.xwiki.extension.UninstallException)

Example 2 with XarInstalledExtension

use of org.xwiki.extension.xar.internal.repository.XarInstalledExtension in project xwiki-platform by xwiki.

the class DocumentsDeletingListener method checkIfPageBelongToExtensions.

private void checkIfPageBelongToExtensions(EntitySelection entitySelection, ExtensionBreakingQuestion question) {
    XarInstalledExtensionRepository repository = (XarInstalledExtensionRepository) installedExtensionRepository;
    DocumentReference documentReference = (DocumentReference) entitySelection.getEntityReference();
    Collection<XarInstalledExtension> extensions = repository.getXarInstalledExtensions(documentReference);
    if (extensions.isEmpty()) {
        question.markAsFreePage(entitySelection);
        return;
    }
    for (XarInstalledExtension extension : extensions) {
        question.pageBelongsToExtension(entitySelection, extension);
    }
}
Also used : XarInstalledExtension(org.xwiki.extension.xar.internal.repository.XarInstalledExtension) XarInstalledExtensionRepository(org.xwiki.extension.xar.internal.repository.XarInstalledExtensionRepository) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 3 with XarInstalledExtension

use of org.xwiki.extension.xar.internal.repository.XarInstalledExtension in project xwiki-platform by xwiki.

the class Packager method reset.

/**
 * @since 9.3RC1
 */
public void reset(DocumentReference reference, DocumentReference authorReference) throws IOException, XarException, XWikiException, XarExtensionExtension {
    Collection<XarInstalledExtension> installedExtensions = getXarInstalledExtensionRepository().getXarInstalledExtensions(reference);
    if (!installedExtensions.isEmpty()) {
        XarInstalledExtension extension = installedExtensions.iterator().next();
        // Remove the version if any since it does not make sense in a XAR
        DocumentReference documentReference = cleanDocumentReference(reference);
        XWikiDocument document = getXWikiDocument(documentReference, extension);
        if (document != null) {
            XWikiContext xcontext = this.xcontextProvider.get();
            // Get database document
            XWikiDocument databaseDocument = xcontext.getWiki().getDocument(documentReference, xcontext);
            // Override data of database document with extension document
            databaseDocument.apply(document, true);
            // Make sure new version will have the right author
            databaseDocument.setAuthorReference(authorReference);
            databaseDocument.setContentAuthorReference(authorReference);
            // Force generating new version
            databaseDocument.setMetaDataDirty(true);
            databaseDocument.setContentDirty(true);
            // Save
            xcontext.getWiki().saveDocument(databaseDocument, "Reset document from extension [" + extension + "]", xcontext);
        } else {
            throw new XarExtensionExtension("Can't find any document with reference [" + documentReference + "] in extension [" + extension.getId() + "]");
        }
    } else {
        throw new XarExtensionExtension("Can't find any installed extension associated with the document reference [" + reference + "]");
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XarInstalledExtension(org.xwiki.extension.xar.internal.repository.XarInstalledExtension) XarExtensionExtension(org.xwiki.extension.xar.XarExtensionExtension) XWikiContext(com.xpn.xwiki.XWikiContext) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 4 with XarInstalledExtension

use of org.xwiki.extension.xar.internal.repository.XarInstalledExtension in project xwiki-platform by xwiki.

the class DocumentsDeletingListenerTest method testCancel.

@Test
public void testCancel() throws Exception {
    Request request = mock(Request.class);
    Job job = mock(Job.class);
    JobStatus status = mock(JobStatus.class);
    when(job.getRequest()).thenReturn(request);
    when(request.isInteractive()).thenReturn(true);
    when(job.getStatus()).thenReturn(status);
    Map<EntityReference, EntitySelection> concernedEntities = new HashMap<>();
    DocumentReference doc1 = new DocumentReference("a", "b", "c1");
    concernedEntities.put(doc1, new EntitySelection(doc1));
    XarInstalledExtension ext1 = mock(XarInstalledExtension.class);
    when(ext1.getId()).thenReturn(new ExtensionId("ext1"));
    when(repository.getXarInstalledExtensions(doc1)).thenReturn(Arrays.asList(ext1));
    InterruptedException e = new InterruptedException();
    doThrow(e).when(status).ask(any(), anyLong(), any());
    // Test
    DocumentsDeletingEvent event = mock(DocumentsDeletingEvent.class);
    mocker.getComponentUnderTest().onEvent(event, job, concernedEntities);
    // Check
    verify(status, times(1)).ask(any(), eq(5L), eq(TimeUnit.MINUTES));
    verify(event).cancel(eq("Question has been interrupted."));
    verify(mocker.getMockedLogger()).warn("Confirm question has been interrupted.");
}
Also used : JobStatus(org.xwiki.job.event.status.JobStatus) HashMap(java.util.HashMap) XarInstalledExtension(org.xwiki.extension.xar.internal.repository.XarInstalledExtension) EntitySelection(org.xwiki.refactoring.job.question.EntitySelection) Request(org.xwiki.job.Request) EntityReference(org.xwiki.model.reference.EntityReference) ExtensionId(org.xwiki.extension.ExtensionId) Job(org.xwiki.job.Job) DocumentReference(org.xwiki.model.reference.DocumentReference) DocumentsDeletingEvent(org.xwiki.bridge.event.DocumentsDeletingEvent) Test(org.junit.Test)

Example 5 with XarInstalledExtension

use of org.xwiki.extension.xar.internal.repository.XarInstalledExtension in project xwiki-platform by xwiki.

the class XarExtensionHandlerTest method testInstallOnWiki.

// Tests
@Test
public void testInstallOnWiki() throws Throwable {
    XWikiDocument existingDocument = new XWikiDocument(new DocumentReference("wiki", "space", "page"));
    BaseObject object = new BaseObject();
    object.setXClassReference(new DocumentReference("wiki", "space", "class"));
    existingDocument.addXObject(object);
    existingDocument.setCreatorReference(new DocumentReference("wiki", "space", "existingcreator"));
    this.oldcore.getSpyXWiki().saveDocument(existingDocument, "", true, getXWikiContext());
    MandatoryDocumentInitializer mandatoryInitializer = this.componentManager.registerMockComponent(MandatoryDocumentInitializer.class, "space.mandatory");
    when(mandatoryInitializer.updateDocument(any(XWikiDocument.class))).thenReturn(true);
    XWikiDocument mandatoryDocument = new XWikiDocument(new DocumentReference("wiki", "space", "mandatory"));
    mandatoryDocument.setCreatorReference(new DocumentReference("wiki", "space", "existingcreator"));
    mandatoryDocument.setSyntax(Syntax.PLAIN_1_0);
    mandatoryDocument.setContent("modified content");
    this.oldcore.getSpyXWiki().saveDocument(mandatoryDocument, "", true, getXWikiContext());
    // install
    XarInstalledExtension xarInstalledExtension = install(this.localXarExtensiontId1, "wiki", this.contextUser);
    verifyHasAdminRight(2);
    // validate
    // space.page
    XWikiDocument page = this.oldcore.getSpyXWiki().getDocument(existingDocument.getDocumentReference(), getXWikiContext());
    Assert.assertFalse("Document wiki:space.page has not been saved in the database", page.isNew());
    Assert.assertNull(page.getXObject(object.getXClassReference()));
    Assert.assertEquals("Wrong content", "content", page.getContent());
    Assert.assertEquals("Wrong creator", new DocumentReference("wiki", "space", "existingcreator"), page.getCreatorReference());
    Assert.assertEquals("Wrong author", this.contextUser, page.getAuthorReference());
    Assert.assertEquals("Wrong content author", this.contextUser, page.getContentAuthorReference());
    Assert.assertEquals("Wrong version", "2.1", page.getVersion());
    Assert.assertEquals("Wrong version", Locale.ROOT, page.getLocale());
    Assert.assertFalse("Document is hidden", page.isHidden());
    Assert.assertFalse("Document is minor edit", page.isMinorEdit());
    BaseClass baseClass = page.getXClass();
    Assert.assertNotNull(baseClass.getField("property"));
    Assert.assertEquals("property", baseClass.getField("property").getName());
    Assert.assertSame(NumberClass.class, baseClass.getField("property").getClass());
    // space.pagewithattachment
    XWikiDocument pagewithattachment = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "pagewithattachment"), getXWikiContext());
    Assert.assertFalse(pagewithattachment.isNew());
    Assert.assertEquals("Wrong version", "1.1", pagewithattachment.getVersion());
    Assert.assertEquals("Wrong creator", this.contextUser, pagewithattachment.getCreatorReference());
    Assert.assertEquals("Wrong author", this.contextUser, pagewithattachment.getAuthorReference());
    Assert.assertEquals("Wrong content author", this.contextUser, pagewithattachment.getContentAuthorReference());
    XWikiAttachment attachment = pagewithattachment.getAttachment("attachment.txt");
    Assert.assertNotNull(attachment);
    Assert.assertEquals("attachment.txt", attachment.getFilename());
    Assert.assertEquals(18, attachment.getContentLongSize(getXWikiContext()));
    Assert.assertEquals("attachment content", IOUtils.toString(attachment.getContentInputStream(getXWikiContext()), StandardCharsets.UTF_8));
    Assert.assertEquals(this.contextUser, attachment.getAuthorReference());
    // space1.page1
    XWikiDocument page1 = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space1", "page1"), getXWikiContext());
    Assert.assertFalse("Document wiki:space1.page1 has not been saved in the database", page1.isNew());
    assertEquals(Arrays.asList(xarInstalledExtension), this.installedExtensionRepository.getXarInstalledExtensions(page1.getDocumentReference()));
    assertEquals(Arrays.asList(xarInstalledExtension), this.installedExtensionRepository.getXarInstalledExtensions(page1.getDocumentReferenceWithLocale()));
    assertEquals(0, this.installedExtensionRepository.getXarInstalledExtensions(new DocumentReference("wiki", "space1", "page1", Locale.ENGLISH)).size());
    assertEquals(0, this.installedExtensionRepository.getXarInstalledExtensions(new DocumentReference("otherwiki", "space1", "page1")).size());
    // translated.translated
    DocumentReference translatedReference = new DocumentReference("wiki", "translated", "translated");
    XWikiDocument defaultTranslated = this.oldcore.getSpyXWiki().getDocument(translatedReference, getXWikiContext());
    Assert.assertNotNull("Document wiki:translated.translated has not been saved in the database", defaultTranslated);
    Assert.assertFalse("Document wiki:translated.translated has not been saved in the database", defaultTranslated.isNew());
    Assert.assertEquals("Wrong content", "default content", defaultTranslated.getContent());
    Assert.assertEquals("Wrong creator", this.contextUser, defaultTranslated.getCreatorReference());
    Assert.assertEquals("Wrong author", this.contextUser, defaultTranslated.getAuthorReference());
    Assert.assertEquals("Wrong content author", this.contextUser, defaultTranslated.getContentAuthorReference());
    Assert.assertEquals("Wrong version", "1.1", defaultTranslated.getVersion());
    assertEquals(Arrays.asList(xarInstalledExtension), this.installedExtensionRepository.getXarInstalledExtensions(defaultTranslated.getDocumentReferenceWithLocale()));
    // translated.translated.tr
    XWikiDocument translated = this.oldcore.getDocuments().get(new DocumentReference(translatedReference, new Locale("tr")));
    Assert.assertNotNull("Document wiki:translated.translated in langauge tr has not been saved in the database", translated);
    Assert.assertFalse("Document wiki:translated.translated in langauge tr has not been saved in the database", translated.isNew());
    Assert.assertEquals("Wrong content", "tr content", translated.getContent());
    Assert.assertEquals("Wrong creator", this.contextUser, translated.getCreatorReference());
    Assert.assertEquals("Wrong author", this.contextUser, translated.getAuthorReference());
    Assert.assertEquals("Wrong content author", this.contextUser, translated.getContentAuthorReference());
    Assert.assertEquals("Wrong version", "1.1", translated.getVersion());
    assertEquals(Arrays.asList(xarInstalledExtension), this.installedExtensionRepository.getXarInstalledExtensions(translated.getDocumentReferenceWithLocale()));
    // translated.translated.fr
    XWikiDocument translated2 = this.oldcore.getDocuments().get(new DocumentReference(translatedReference, new Locale("fr")));
    Assert.assertNotNull("Document wiki:translated.translated in language fr has not been saved in the database", translated2);
    Assert.assertFalse("Document wiki:translated.translated in langauge fr has not been saved in the database", translated2.isNew());
    Assert.assertEquals("Wrong content", "fr content", translated2.getContent());
    Assert.assertEquals("Wrong creator", this.contextUser, translated2.getCreatorReference());
    Assert.assertEquals("Wrong author", this.contextUser, translated2.getAuthorReference());
    Assert.assertEquals("Wrong content author", this.contextUser, translated2.getContentAuthorReference());
    Assert.assertEquals("Wrong version", "1.1", translated2.getVersion());
    assertEquals(Arrays.asList(xarInstalledExtension), this.installedExtensionRepository.getXarInstalledExtensions(translated2.getDocumentReferenceWithLocale()));
    // space.hiddenpage
    XWikiDocument hiddenpage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "hiddenpage"), getXWikiContext());
    Assert.assertFalse("Document wiki:space.hiddenpage has not been saved in the database", hiddenpage.isNew());
    Assert.assertTrue("Document is not hidden", hiddenpage.isHidden());
    // space.mandatory
    XWikiDocument mandatorypage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "mandatory"), getXWikiContext());
    Assert.assertEquals("Document wiki:space.mandatory has been overwritten", "1.1", mandatorypage.getVersion());
}
Also used : Locale(java.util.Locale) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) MandatoryDocumentInitializer(com.xpn.xwiki.doc.MandatoryDocumentInitializer) XarInstalledExtension(org.xwiki.extension.xar.internal.repository.XarInstalledExtension) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Aggregations

XarInstalledExtension (org.xwiki.extension.xar.internal.repository.XarInstalledExtension)7 DocumentReference (org.xwiki.model.reference.DocumentReference)5 Job (org.xwiki.job.Job)4 Test (org.junit.Test)3 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 HashMap (java.util.HashMap)2 DocumentsDeletingEvent (org.xwiki.bridge.event.DocumentsDeletingEvent)2 ExtensionId (org.xwiki.extension.ExtensionId)2 InstallException (org.xwiki.extension.InstallException)2 UninstallException (org.xwiki.extension.UninstallException)2 Request (org.xwiki.job.Request)2 JobStatus (org.xwiki.job.event.status.JobStatus)2 EntityReference (org.xwiki.model.reference.EntityReference)2 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)2 EntitySelection (org.xwiki.refactoring.job.question.EntitySelection)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 MandatoryDocumentInitializer (com.xpn.xwiki.doc.MandatoryDocumentInitializer)1 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)1