groovy publication tools System Administrator Jahia 7.3 Jahia 8

How to force a publication

Question

You have a message "Nothing to publish" but you know that something has been modified. 

Answer

From Jahia 8.1.6.1 you can use the forcePublishAll module on the store: 

https://store.jahia.com/contents/modules-repository/org/jahia/support/modules/forcePublishAll.html

When you install this module, you can easily force a publication with a GraphQL Mutation:

The main functionality provided by this module is the forcePublish mutation. This mutation can be used to force the publication of a whole sub-tree.

Example Mutation (you would have to modify the path in the graphql mutation):


mutation {
    jcr {
        mutateNode(pathOrId:"/sites/digitall/home/about") {
            forcePublish
        }
    }
}

 

Before 8.1.6.1 the solution could be a script solution:

To see if a node needs to be published or not, Jahia compare the last modification date with the publication date. So a good way to force a publication is to update the last publication node of all nodes of a sub-tree.
Here is a script you could use to force a publication. Please set the startPath with the starting node path. You can run this script from the Groovy Console of the tools

import org.jahia.api.Constants
import javax.jcr.NodeIterator
import javax.jcr.RepositoryException
import javax.jcr.query.Query

String startPath = "/sites/mySite/home";

JCRTemplate.getInstance().doExecuteWithSystemSession(null, Constants.EDIT_WORKSPACE, null, new JCRCallback() {
    @Override
    Object doInJCR(JCRSessionWrapper session) throws RepositoryException {
        try {
            // touch the starting node
            JCRNodeWrapper startNode = session.getNode(startPath);
            touch(startNode);

            // touch all sub nodes
            String query = "select * from [nt:base] as p where isdescendantnode('" +  startPath + "')";
            NodeIterator iterator = session.getWorkspace().getQueryManager().createQuery(query, Query.JCR_SQL2).execute().getNodes();
            while (iterator.hasNext()) {
                JCRNodeWrapper node = (JCRNodeWrapper) iterator.nextNode();
                touch(node);
            }
        } catch (javax.jcr.PathNotFoundException e) {
            log.info("Could not find node with path " + startPath);
        }
        session.save();
        return null;
    }
});


private void touch(JCRNodeWrapper node) {
    if (node != null) {
        try {
            node.setProperty("jcr:lastModified", new GregorianCalendar());
            log.info(node.getPath())
        } catch (javax.jcr.nodetype.ConstraintViolationException e) {
        }
    }
}