Search in sources :

Example 6 with Document

use of com.xpn.xwiki.api.Document in project xwiki-platform by xwiki.

the class XWikiScriptContextInitializer method setDocument.

private Document setDocument(ScriptContext scriptContext, String key, XWikiDocument document, XWikiContext xcontext) {
    // Change the Document instance only if it's not already wrapping the same XWikiDocument (otherwise we might
    // loose modifications made in a previous script and not yet saved)
    Document apiDocument = (Document) scriptContext.getAttribute(key);
    if (apiDocument == null || !apiDocument.same(document)) {
        apiDocument = document.newDocument(xcontext);
        scriptContext.setAttribute(key, apiDocument, ScriptContext.ENGINE_SCOPE);
    }
    return apiDocument;
}
Also used : Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument)

Example 7 with Document

use of com.xpn.xwiki.api.Document in project xwiki-platform by xwiki.

the class XWikiScriptContextInitializer method initialize.

@Override
public void initialize(ScriptContext scriptContext) {
    XWikiContext xcontext = this.xcontextProvider.get();
    if (scriptContext.getAttribute("util") == null) {
        // Put the Util API in the Script context.
        scriptContext.setAttribute("util", new com.xpn.xwiki.api.Util(xcontext.getWiki(), xcontext), ScriptContext.ENGINE_SCOPE);
        // We put the com.xpn.xwiki.api.XWiki object into the context and not the com.xpn.xwiki.XWiki one which is
        // for internal use only. In this manner we control what the user can access.
        scriptContext.setAttribute("xwiki", new XWiki(xcontext.getWiki(), xcontext), ScriptContext.ENGINE_SCOPE);
        scriptContext.setAttribute("request", xcontext.getRequest(), ScriptContext.ENGINE_SCOPE);
        scriptContext.setAttribute("response", xcontext.getResponse(), ScriptContext.ENGINE_SCOPE);
        // We put the com.xpn.xwiki.api.Context object into the context and not the com.xpn.xwiki.XWikiContext one
        // which is for internal use only. In this manner we control what the user can access.
        // We use "xcontext" because "context" is a reserved binding in JSR-223 specifications
        scriptContext.setAttribute("xcontext", new Context(xcontext), ScriptContext.ENGINE_SCOPE);
    }
    // Current document
    Document docAPI = null;
    XWikiDocument doc = xcontext.getDoc();
    if (doc != null) {
        docAPI = setDocument(scriptContext, "doc", doc, xcontext);
        XWikiDocument tdoc = (XWikiDocument) xcontext.get("tdoc");
        if (tdoc == null) {
            try {
                tdoc = doc.getTranslatedDocument(xcontext);
            } catch (XWikiException e) {
                this.logger.warn("Failed to retrieve the translated document for [{}]. " + "Continue using the default translation.", doc.getDocumentReference(), e);
                tdoc = doc;
            }
        }
        Document tdocAPI = setDocument(scriptContext, "tdoc", tdoc, xcontext);
        XWikiDocument cdoc = (XWikiDocument) xcontext.get("cdoc");
        if (cdoc == null) {
            Document cdocAPI = tdocAPI;
            if (cdocAPI == null) {
                cdocAPI = docAPI;
            }
            scriptContext.setAttribute("cdoc", cdocAPI, ScriptContext.ENGINE_SCOPE);
        } else {
            setDocument(scriptContext, "cdoc", cdoc, xcontext);
        }
    }
    // Current secure document
    XWikiDocument sdoc = (XWikiDocument) xcontext.get("sdoc");
    if (sdoc == null) {
        scriptContext.setAttribute("sdoc", docAPI, ScriptContext.ENGINE_SCOPE);
    } else {
        setDocument(scriptContext, "sdoc", sdoc, xcontext);
    }
    // Miscellaneous
    scriptContext.setAttribute("locale", xcontext.getLocale(), ScriptContext.ENGINE_SCOPE);
}
Also used : Context(com.xpn.xwiki.api.Context) ScriptContext(javax.script.ScriptContext) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.api.XWiki) Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiException(com.xpn.xwiki.XWikiException)

Example 8 with Document

use of com.xpn.xwiki.api.Document in project xwiki-platform by xwiki.

the class RightsManagerRightsApi method getParentPreference.

// Inheritance
/**
 * Get the document containing inherited rights of provided document.
 *
 * @param spaceOrPage the space of page where to get XWikiRights. If null get wiki rights.
 * @return the document containing inherited rights of provided document.
 * @throws XWikiException error when browsing rights preferences.
 */
public Document getParentPreference(String spaceOrPage) throws XWikiException {
    Document parent = null;
    try {
        XWikiDocument xdoc = RightsManager.getInstance().getParentPreference(spaceOrPage, this.context);
        parent = convert(xdoc);
    } catch (RightsManagerException e) {
        logError(MessageFormat.format("Try to get parent rights preference for [{0}]", new Object[] { spaceOrPage }), e);
    }
    return parent;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Document(com.xpn.xwiki.api.Document)

Example 9 with Document

use of com.xpn.xwiki.api.Document in project xwiki-platform by xwiki.

the class BaseSearchResult method searchPages.

/**
 * Search for keyword in the given scopes. Limit the search only to Pages. Search for keyword
 *
 * @param keywords the string that will be used in a "like" XWQL clause.
 * @param number number of results to be returned.
 * @param start 0-based start offset.
 * @param orderField the field to be used to order the results.
 * @param order "asc" or "desc"
 * @return the results.
 */
protected List<SearchResult> searchPages(List<SearchScope> searchScopes, String keywords, String wikiName, String space, boolean hasProgrammingRights, int number, int start, String orderField, String order, Boolean withPrettyNames) throws QueryException, IllegalArgumentException, UriBuilderException, XWikiException {
    XWiki xwikiApi = Utils.getXWikiApi(componentManager);
    String database = Utils.getXWikiContext(componentManager).getWikiId();
    /* This try is just needed for executing the finally clause. */
    try {
        List<SearchResult> result = new ArrayList<SearchResult>();
        if (keywords == null) {
            return result;
        }
        Formatter f = new Formatter();
        /*
             * If the order field is already one of the field hard coded in the base query, then do not add it to the
             * select clause.
             */
        String addColumn = "";
        if (!StringUtils.isBlank(orderField)) {
            addColumn = (orderField.equals("") || orderField.equals("fullName") || orderField.equals("name") || orderField.equals("space")) ? "" : ", doc." + orderField;
        }
        if (space != null) {
            f.format("select distinct doc.fullName, doc.space, doc.name, doc.language");
            f.format(addColumn);
            f.format(" from XWikiDocument as doc where doc.space = :space and ( ");
        } else {
            f.format("select distinct doc.fullName, doc.space, doc.name, doc.language");
            f.format(addColumn);
            f.format(" from XWikiDocument as doc where ( ");
        }
        /* Look for scopes related to pages */
        int acceptedScopes = 0;
        for (int i = 0; i < searchScopes.size(); i++) {
            SearchScope scope = searchScopes.get(i);
            switch(scope) {
                case CONTENT:
                    f.format("upper(doc.content) like :keywords ");
                    acceptedScopes++;
                    break;
                case NAME:
                    f.format("upper(doc.fullName) like :keywords ");
                    acceptedScopes++;
                    break;
                case TITLE:
                    f.format("upper(doc.title) like :keywords ");
                    acceptedScopes++;
                    break;
            }
            if (i != searchScopes.size() - 1) {
                f.format(" or ");
            }
        }
        /* If we don't find any scope related to pages then return empty results */
        if (acceptedScopes == 0) {
            return result;
        }
        /* Build the order clause. */
        String orderClause = null;
        if (StringUtils.isBlank(orderField)) {
            orderClause = "doc.fullName asc";
        } else {
            /* Check if the order parameter is a valid "asc" or "desc" string, otherwise use "asc" */
            if ("asc".equals(order) || "desc".equals(order)) {
                orderClause = String.format("doc.%s %s", orderField, order);
            } else {
                orderClause = String.format("doc.%s asc", orderField);
            }
        }
        // Add ordering
        f.format(") order by %s", orderClause);
        String query = f.toString();
        List<Object> queryResult = null;
        /* This is needed because if the :space placeholder is not in the query, setting it would cause an exception */
        if (space != null) {
            queryResult = this.queryManager.createQuery(query, Query.XWQL).bindValue("keywords", String.format("%%%s%%", keywords.toUpperCase())).bindValue("space", space).addFilter(Utils.getHiddenQueryFilter(this.componentManager)).setOffset(start).setLimit(number).execute();
        } else {
            queryResult = this.queryManager.createQuery(query, Query.XWQL).bindValue("keywords", String.format("%%%s%%", keywords.toUpperCase())).addFilter(Utils.getHiddenQueryFilter(this.componentManager)).setOffset(start).setLimit(number).execute();
        }
        for (Object object : queryResult) {
            Object[] fields = (Object[]) object;
            String spaceId = (String) fields[1];
            List<String> spaces = Utils.getSpacesFromSpaceId(spaceId);
            String pageName = (String) fields[2];
            String language = (String) fields[3];
            String pageId = Utils.getPageId(wikiName, spaces, pageName);
            String pageFullName = Utils.getPageFullName(wikiName, spaces, pageName);
            /* Check if the user has the right to see the found document */
            if (xwikiApi.hasAccessLevel("view", pageId)) {
                Document doc = xwikiApi.getDocument(pageFullName);
                String title = doc.getDisplayTitle();
                SearchResult searchResult = objectFactory.createSearchResult();
                searchResult.setType("page");
                searchResult.setId(pageId);
                searchResult.setPageFullName(pageFullName);
                searchResult.setTitle(title);
                searchResult.setWiki(wikiName);
                searchResult.setSpace(spaceId);
                searchResult.setPageName(pageName);
                searchResult.setVersion(doc.getVersion());
                searchResult.setAuthor(doc.getAuthor());
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(doc.getDate());
                searchResult.setModified(calendar);
                if (withPrettyNames) {
                    searchResult.setAuthorName(Utils.getAuthorName(doc.getAuthorReference(), componentManager));
                }
                String pageUri = null;
                if (StringUtils.isBlank(language)) {
                    pageUri = Utils.createURI(this.uriInfo.getBaseUri(), PageResource.class, wikiName, spaces, pageName).toString();
                } else {
                    searchResult.setLanguage(language);
                    pageUri = Utils.createURI(this.uriInfo.getBaseUri(), PageTranslationResource.class, wikiName, spaces, pageName, language).toString();
                }
                Link pageLink = new Link();
                pageLink.setHref(pageUri);
                pageLink.setRel(Relations.PAGE);
                searchResult.getLinks().add(pageLink);
                result.add(searchResult);
            }
        }
        return result;
    } finally {
        Utils.getXWikiContext(componentManager).setWikiId(database);
    }
}
Also used : Formatter(java.util.Formatter) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) XWiki(com.xpn.xwiki.api.XWiki) SearchResult(org.xwiki.rest.model.jaxb.SearchResult) Document(com.xpn.xwiki.api.Document) Link(org.xwiki.rest.model.jaxb.Link)

Example 10 with Document

use of com.xpn.xwiki.api.Document in project xwiki-platform by xwiki.

the class BaseSearchResult method searchSpaces.

/**
 * Search for keyword in the given scopes. Limit the search only to spaces.
 *
 * @param keywords the string that will be used in a "like" XWQL clause
 * @param number number of results to be returned
 * @param start 0-based start offset
 * @return the results.
 */
protected List<SearchResult> searchSpaces(String keywords, String wikiName, int number, int start) throws QueryException, IllegalArgumentException, UriBuilderException, XWikiException {
    List<SearchResult> result = new ArrayList<SearchResult>();
    if (StringUtils.isEmpty(keywords)) {
        return result;
    }
    String escapedKeywords = keywords.replaceAll("([%_!])", "!$1");
    String query = "select space.reference from XWikiSpace as space" + " where lower(space.name) like lower(:keywords) escape '!'" + " or lower(space.reference) like lower(:prefix) escape '!'" + " order by lower(space.reference), space.reference";
    List<Object> queryResult = queryManager.createQuery(query, Query.HQL).bindValue("keywords", String.format("%%%s%%", escapedKeywords)).bindValue("prefix", String.format("%s%%", escapedKeywords)).setWiki(wikiName).setLimit(number).setOffset(start).addFilter(this.hiddenSpaceFilter).execute();
    XWiki xwikiApi = Utils.getXWikiApi(componentManager);
    for (Object object : queryResult) {
        String spaceId = (String) object;
        List<String> spaces = Utils.getSpacesFromSpaceId(spaceId);
        SpaceReference spaceReference = new SpaceReference(wikiName, spaces);
        if (this.authorizationManager.hasAccess(Right.VIEW, spaceReference)) {
            Document spaceDoc = xwikiApi.getDocument(spaceReference);
            SearchResult searchResult = objectFactory.createSearchResult();
            searchResult.setType("space");
            searchResult.setId(spaceId);
            searchResult.setWiki(wikiName);
            searchResult.setSpace(spaceId);
            searchResult.setTitle(spaceDoc != null ? spaceDoc.getPlainTitle() : spaceReference.getName());
            // Add a link to the space information.
            Link spaceLink = new Link();
            spaceLink.setRel(Relations.SPACE);
            spaceLink.setHref(Utils.createURI(uriInfo.getBaseUri(), SpaceResource.class, wikiName, spaces).toString());
            searchResult.getLinks().add(spaceLink);
            // Add a link to the home page if it exists and it is viewable.
            if (spaceDoc != null && !spaceDoc.isNew()) {
                Link pageLink = new Link();
                pageLink.setHref(Utils.createURI(uriInfo.getBaseUri(), PageResource.class, wikiName, spaces, spaceDoc.getName()).toString());
                pageLink.setRel(Relations.HOME);
                searchResult.getLinks().add(pageLink);
            }
            result.add(searchResult);
        }
    }
    return result;
}
Also used : SpaceReference(org.xwiki.model.reference.SpaceReference) ArrayList(java.util.ArrayList) XWiki(com.xpn.xwiki.api.XWiki) SearchResult(org.xwiki.rest.model.jaxb.SearchResult) Document(com.xpn.xwiki.api.Document) Link(org.xwiki.rest.model.jaxb.Link)

Aggregations

Document (com.xpn.xwiki.api.Document)97 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)60 XWikiException (com.xpn.xwiki.XWikiException)41 XWikiRestException (org.xwiki.rest.XWikiRestException)40 DocumentReference (org.xwiki.model.reference.DocumentReference)37 BaseObject (com.xpn.xwiki.objects.BaseObject)24 Test (org.junit.Test)23 WebApplicationException (javax.ws.rs.WebApplicationException)22 ArrayList (java.util.ArrayList)16 Attachment (com.xpn.xwiki.api.Attachment)14 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)14 Vector (java.util.Vector)12 Link (org.xwiki.rest.model.jaxb.Link)10 XWikiContext (com.xpn.xwiki.XWikiContext)9 AbstractComponentTest (com.celements.common.test.AbstractComponentTest)7 Date (java.util.Date)7 XWiki (com.xpn.xwiki.api.XWiki)6 RangeIterable (org.xwiki.rest.internal.RangeIterable)6 Object (org.xwiki.rest.model.jaxb.Object)6 XWiki (com.xpn.xwiki.XWiki)5