Search in sources :

Example 76 with XWikiDocument

use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.

the class R910100XWIKI14871DataMigration method storeDeletedAttachment.

private void storeDeletedAttachment(File directory, long id, Session session) throws ParserConfigurationException, SAXException, IOException {
    this.logger.info("Storing attachment metadata [{}] in the database", directory);
    // Find document reference
    File documentDirectory = directory.getParentFile().getParentFile().getParentFile();
    DocumentReference documentReference = getDocumentReference(documentDirectory);
    if (getXWikiContext().getWikiReference().equals(documentReference.getWikiReference())) {
        // Parse ~DELETED_ATTACH_METADATA.xml
        File file = new File(directory, "~DELETED_ATTACH_METADATA.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(file);
        String filename = getElementText(doc, "filename", null);
        String deleter = getElementText(doc, "deleter", null);
        Date deleteDate = new Date(Long.valueOf(getElementText(doc, "datedeleted", null)));
        long docId = new XWikiDocument(documentReference).getId();
        // We need to make sure the deleted attachment is not already in the database with a different id (left
        // there by the attachment porter script for example)
        Query selectQuery = session.createQuery("SELECT id FROM DeletedAttachment WHERE docId=? AND filename=? AND date=?");
        selectQuery.setLong(0, docId);
        selectQuery.setString(1, filename);
        selectQuery.setTimestamp(2, new java.sql.Timestamp(deleteDate.getTime()));
        Long databaseId = (Long) selectQuery.uniqueResult();
        if (databaseId == null) {
            // Try without the milliseconds since most versions of MySQL don't support them
            selectQuery.setTimestamp(2, new java.sql.Timestamp(deleteDate.toInstant().getEpochSecond() * 1000));
            databaseId = (Long) selectQuery.uniqueResult();
        }
        DeletedAttachment dbAttachment;
        if (databaseId != null) {
            // Update the database metadata (probably left there by the attachment porter script)
            dbAttachment = new DeletedAttachment(docId, this.serializer.serialize(documentReference), filename, FileSystemStoreUtils.HINT, deleter, deleteDate, null, databaseId);
            session.update(dbAttachment);
        } else {
            // Insert new deleted attachment metadata in the DB
            dbAttachment = new DeletedAttachment(docId, this.serializer.serialize(documentReference), filename, FileSystemStoreUtils.HINT, deleter, deleteDate, null);
            databaseId = (Long) session.save(dbAttachment);
        }
        // Refactor file storage to be based on database id instead of date
        File newDirectory = new File(directory.getParentFile(), encode(dbAttachment.getFilename() + "-id" + databaseId));
        FileUtils.moveDirectory(directory, newDirectory);
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Query(org.hibernate.Query) Document(org.w3c.dom.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DeletedAttachment(com.xpn.xwiki.doc.DeletedAttachment) Date(java.util.Date) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentBuilder(javax.xml.parsers.DocumentBuilder) File(java.io.File) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 77 with XWikiDocument

use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.

the class AbstractSxExportURLFactoryActionHandler method createURL.

@Override
public URL createURL(String spaces, String name, String queryString, String anchor, String wikiId, XWikiContext context, FilesystemExportContext exportContext) throws Exception {
    // Check if the current user has the right to view the SX file. We do this since this is what would happen
    // in XE when a SX action is called (check done in XWikiAction).
    // Note that we cannot just open an HTTP connection to the SX action here since we wouldn't be authenticated...
    // Thus we have to simulate the same behavior as the SX action...
    List<String> spaceNames = this.legacySpaceResolve.resolve(spaces);
    DocumentReference sxDocumentReference = new DocumentReference(wikiId, spaceNames, name);
    this.authorizationManager.checkAccess(Right.VIEW, sxDocumentReference);
    // Set the SX document as the current document in the XWiki Context since unfortunately the SxSource code
    // uses the current document in the context instead of accepting it as a parameter...
    XWikiDocument sxDocument = context.getWiki().getDocument(sxDocumentReference, context);
    Map<String, Object> backup = new HashMap<>();
    XWikiDocument.backupContext(backup, context);
    try {
        sxDocument.setAsContextDoc(context);
        return processSx(spaceNames, name, queryString, context, exportContext);
    } finally {
        XWikiDocument.restoreContext(backup, context);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) HashMap(java.util.HashMap) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 78 with XWikiDocument

use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.

the class DefaultWikiUserManager method removeMembers.

@Override
public void removeMembers(Collection<String> userIds, String wikiId) throws WikiUserManagerException {
    // Get the group document
    XWikiDocument groupDoc = getMembersGroupDocument(wikiId);
    // Get the member objects
    List<BaseObject> objects = groupDoc.getXObjects(GROUPCLASS_REFERENCE);
    if (objects != null) {
        // Get the member objects to remove
        List<BaseObject> objectsToRemove = new ArrayList<>();
        for (String userId : userIds) {
            for (BaseObject object : objects) {
                if (object == null) {
                    continue;
                }
                String member = object.getStringValue(GROUP_CLASS_MEMBER_FIELD);
                if (userId.equals(member)) {
                    objectsToRemove.add(object);
                }
            }
        }
        // Remove them
        for (BaseObject object : objectsToRemove) {
            groupDoc.removeXObject(object);
        }
        // Save the document
        saveGroupDocument(groupDoc, "Remove some users from the group.");
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ArrayList(java.util.ArrayList) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 79 with XWikiDocument

use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.

the class DefaultWikiUserManager method addMembers.

@Override
public void addMembers(Collection<String> userIds, String wikiId) throws WikiUserManagerException {
    Collection<String> members = getMembers(wikiId);
    // Get the group document
    XWikiDocument groupDoc = getMembersGroupDocument(wikiId);
    // If the group does not contain any user yet, add an empty member (cf: XWIKI-6275).
    List<BaseObject> memberObjects = groupDoc.getXObjects(GROUPCLASS_REFERENCE);
    if (memberObjects == null || memberObjects.isEmpty()) {
        addMemberObject(groupDoc, "");
    }
    // Add members
    for (String userId : userIds) {
        if (!members.contains(userId)) {
            // Add a member object
            addMemberObject(groupDoc, userId);
        }
    }
    // Save the document
    saveGroupDocument(groupDoc, "Add members to the group.");
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 80 with XWikiDocument

use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.

the class DefaultWikiUserManager method getMembers.

@Override
public Collection<String> getMembers(String wikiId) throws WikiUserManagerException {
    List<String> members = new ArrayList<>();
    try {
        // Get the descriptor
        WikiDescriptor descriptor = wikiDescriptorManager.getById(wikiId);
        // Add the wiki owner
        members.add(descriptor.getOwnerId());
    } catch (WikiManagerException e) {
        throw new WikiUserManagerException(String.format("Failed to get the descriptor for [%s]", wikiId), e);
    }
    // Get the other members from the wiki AllGroup
    XWikiDocument groupDoc = getMembersGroupDocument(wikiId);
    List<BaseObject> memberObjects = groupDoc.getXObjects(GROUPCLASS_REFERENCE);
    if (memberObjects != null) {
        for (BaseObject object : memberObjects) {
            if (object == null) {
                continue;
            }
            String member = object.getStringValue(GROUP_CLASS_MEMBER_FIELD);
            if (!member.isEmpty() && !members.contains(member)) {
                members.add(member);
            }
        }
    }
    return members;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) WikiUserManagerException(org.xwiki.wiki.user.WikiUserManagerException) ArrayList(java.util.ArrayList) WikiDescriptor(org.xwiki.wiki.descriptor.WikiDescriptor) BaseObject(com.xpn.xwiki.objects.BaseObject)

Aggregations

XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)869 DocumentReference (org.xwiki.model.reference.DocumentReference)469 BaseObject (com.xpn.xwiki.objects.BaseObject)318 Test (org.junit.Test)284 XWikiContext (com.xpn.xwiki.XWikiContext)232 XWikiException (com.xpn.xwiki.XWikiException)178 ArrayList (java.util.ArrayList)99 XWiki (com.xpn.xwiki.XWiki)97 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)86 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)71 Document (com.xpn.xwiki.api.Document)48 EntityReference (org.xwiki.model.reference.EntityReference)48 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)41 Date (java.util.Date)41 IOException (java.io.IOException)40 HashMap (java.util.HashMap)33 QueryException (org.xwiki.query.QueryException)27 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)25 ParseGroovyFromString (com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString)23 IncludeServletAsString (com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString)23