Class Context

  • Direct Known Subclasses:
    DeprecatedContext

    public class Context
    extends Api
    Provides a secure proxy for the internal XWikiContext class, that can be used safely in scripts. All dangerous methods are protected by requiring Programming Rights.
    Version:
    $Id: 674723936f93789fe42c4282a63c71c68bf77309 $
    • Constructor Detail

    • Method Detail

      • getRequest

        public XWikiRequest getRequest()
        Returns the current request object. If the request has been made to a servlet container using the HTTP protocol then the returned object wraps a HttpServletRequest instance.
        Returns:
        an object wrapping the current request object
      • getResponse

        public XWikiResponse getResponse()
        Returns the current response object. If the request has been made to a servlet container using the HTTP protocol then the returned object wraps a HttpServletResponse instance.
        Returns:
        an object wrapping the current response object
      • getMode

        public int getMode()
        Specifies the container or environment in which XWiki is currently running. See the following table for possible values it can return:
        Return values
        Return Meaning
        0 Servlet Container
        1 Portlet Container
        2 XML RPC
        3 Atom
        4 PDF
        5 GWT
        6 GWT Debugging
        Returns:
        an integer constant identifying the container or environment in which XWiki is currently running
      • getDatabase

        public String getDatabase()
        Returns the current database name. If isMainWiki() returns true then the current database name is the same as getMainWikiName(). Otherwise, each virtual wiki has it's own database. In this case the returned string identifies the current virtual wiki we operate on and prefixes document names like in databaseName:spaceName.pageName.
        Returns:
        The current database name.
        See Also:
        isMainWiki(), getMainWikiName()
      • getMainWikiName

        public String getMainWikiName()
        Returns the name of the main wiki. In non-virtual mode there is only one wiki called xwiki. In virtual mode, the main wiki stores information about all the other virtual wikis.
        Returns:
        The name of the main wiki.
      • getOriginalDatabase

        public String getOriginalDatabase()
        Returns the name of the original database. Here, original means the database corresponding to the requested URL, which can be changed when including a document from a different database, by using, for example, #includeTopic("virtualwiki:Some.Document").
        Returns:
        The original database name
      • setDatabase

        @Programming
        public void setDatabase​(String database)
        Sets the current database. You need programming rights to be able to call this method.
        Parameters:
        database - a database name
        See Also:
        getDatabase()
      • getURLFactory

        public XWikiURLFactory getURLFactory()
        Returns the URL factory matching both the protocol used to make the current request and the container or environment in which XWiki is running. The most used concrete implementation of the XWikiURLFactory is XWikiServletURLFactory. XWikiURLFactory offers a generic way of creating XWiki specific URLs that should be chosen instead of the basic string concatenation. Hard-coding the protocol used, like HTTP, inside wiki pages should be avoided.
        Returns:
        The URL factory.
      • isMainWiki

        public boolean isMainWiki()

        Specifies if the current request was made to a virtual (non-main) wiki, or to the main wiki.

        In virtual mode the server can host more than one wiki, each having it's own database and its own URL. We refer to them as virtual wikis. One of them stores information about the others and it is called the main wiki. You can switch to virtual mode by changing the xwiki.virtual parameter from 0 to 1 in the configuration file.

        Returns:
        true if XWiki is in the main wiki, or if virtual mode is disabled.
        See Also:
        getDatabase(), getMainWikiName()
      • getAction

        public String getAction()
        Returns the current action. XWiki is built on top of the Struts framework, so here "action" means a StrutsAction. Some predefined actions are: view, edit, delete, download and export. Custom actions can be defined. The action and it's target are extracted from the request URL. For instance, in http://platform.xwiki.org/xwiki/bin/view/DevGuide/API the action is view and it's target is the DevGuide.API document.
        Returns:
        The action corresponding to the request URL.
      • getLocale

        public Locale getLocale()
        Returns the locale of the current request. If multilingual is turned off then the locale used is given by the default_language preference. Otherwise, the locale is taken from either the request object, the cookie, user preferences or from the navigator locale settings, the last having the lower priority.
        Returns:
        The locale of the current request.
        Since:
        6.0M1
        See Also:
        getInterfaceLocale()
      • getInterfaceLocale

        public Locale getInterfaceLocale()
        Returns the interface locale preference of the current user. If multilingual is turned off then the locale used is given by the default_language preference. Otherwise, the locale is taken from either the request object, the context, the cookie, user preferences or from the navigator locale settings, the last having the lower priority.
        Returns:
        The interface locale preference of the current user.
        Since:
        6.0M1
        See Also:
        getLocale()
      • getXWiki

        @Programming
        public XWiki getXWiki()
        Returns the XWiki object. Programming rights are needed in order to call this method. If programming rights are not available in the current document, the XWiki object can still be accessed through a secure API available as a predefined variable for scripting inside wiki pages; use $xwiki in Velocity.
        Returns:
        The internal XWiki object, if the document has programming rights, or null otherwise.
      • getDoc

        @Programming
        public XWikiDocument getDoc()
        Returns the current requested document. Programming rights are needed in order to call this method. If programming rights are not available in the current document, the current can can still be accessed document through a secure API available as a predefined variable for scripting inside wiki pages; use $doc in Velocity.
        Returns:
        The current requested document, if the document has programming rights, or null otherwise.
      • getUser

        public String getUser()
        Returns the current user which made the request. If there's no currently logged in user in XWiki then the returned string is XWiki.XWikiGuest which represents any anonymous user. The name of the user is returned relative to the current wiki so if the user is in the current wiki or in non-virtual mode the name will be of the form XWiki.UserLogin. If the user comes from another wiki the full prefixed name will be returned as in wikiid:XWiki.UserLogin. At the same time this method returns the name of the document containing the current user's profile so in Velocity you can do, for instance, $xwiki.getDocument($xcontext.user) to find out more about the current user, like his/hers real name or e-mail address.
        Returns:
        The current user which made the request.
        See Also:
        getLocalUser(), getDatabase(), getUserReference()
      • getUserReference

        public DocumentReference getUserReference()
        Returns the document reference for the profile page of the current user which made the request. The returned reference can always be considered an absolute document reference, meaning that getUserReference().getWikiReference().getName() will always return the name of the user's wiki.
        Returns:
        The document reference for the current logged in user which made the request or null if there is no currently logged in user (anonymous/guest user).
        Since:
        3.2M3
      • getLocalUser

        public String getLocalUser()
        Returns the current user which made the request. The difference from getUser() is that the returned string is never prefixed with the database name, not even in virtual mode.
        Returns:
        The current user which made the request.
        See Also:
        getUser(), getDatabase()
      • setDoc

        @Programming
        public void setDoc​(XWikiDocument doc)
        Sets the current document. Programming rights are needed in order to call this method.
        Parameters:
        doc - XWiki document to set as the context document.
      • getContext

        @Programming
        public XWikiContext getContext()
        Returns the XWiki context. Programming rights are needed in order to call this method. The XWiki context represents the execution environment for all the wiki pages. Accessing it directly in wiki pages may lead to security issues.
        Returns:
        The unwrapped version of the context if you have programming rights, or null otherwise.
      • get

        @Programming
        public Object get​(String key)
        Returns the value associated with the given key in the XWiki context. Programming rights are needed in order to call this method. The context can be seen as a map of (paramName, paramValue) pairs. This mechanism is useful for passing parameters between pages or from Java to Velocity. For instance an exception caught in Java code can be put on the context and handled in a user-friendly way in Velocity. This method is protected because sensitive information may be placed in the internal context, which shouldn't be publicly accessible.
        Parameters:
        key - The key to look for in the context.
        Returns:
        The value associated with the given key in the XWiki context, if you have programming rights, or null otherwise.
        See Also:
        put(String, java.lang.Object)
      • getEditorWysiwyg

        @Deprecated
        public String getEditorWysiwyg()
        Deprecated.
        since 8.2RC1 when we started using the Edit Module to load the configured WYSIWYG editor
        Returns the list of TextArea fields that use the WYSIWYG editor. This list is automatically built when displaying TextArea properties.
        Returns:
        a string containing a comma-separated list of TextArea field names for which the WYSIWYG editor should be enabled
      • put

        @Programming
        public void put​(String key,
                        Object value)
        Puts an object on the context using the given key. The context can be seen as a map of (paramName, paramValue) pairs. Requires programming rights.
        Parameters:
        key - The parameter name.
        value - The parameter value.
        See Also:
        get(String)
      • setFinished

        public void setFinished​(boolean finished)
        Specifies if the current page should be sent to the client or not. When the context is finished, the client response contains only the (HTTP) headers and no body (as in the case of a response to a HTTP HEAD request). This is useful for instance when exporting the entire wiki as a .xar archive.
        Parameters:
        finished - true to avoid rendering of the current page
      • getCacheDuration

        public int getCacheDuration()
        Returns the amount of time this document should be cached.
        Returns:
        The cache duration, in seconds.
        See Also:
        setCacheDuration(int)
      • setCacheDuration

        public void setCacheDuration​(int duration)
        Sets the cache duration in seconds. Setting this to a non-zero, positive value will cause the rendered document to be stored in a cache, so next time a client requests this document, if it is still in the cache, and the document content did not change, then it will be taken from the cache and will not be parsed/rendered again. While it is a good idea to cache pages containing only static content (no scripting), it should be used with care for documents that gather information from the wiki using queries.
        Parameters:
        duration - The cache duration specified in seconds.
        See Also:
        getCacheDuration()
      • setLinksAction

        public void setLinksAction​(String action)
        Sets the action to be used instead of the view action inside URLs. The XWiki URL factories will replace the view action with the given action when creating URLs.
        Parameters:
        action - view action replacement
        See Also:
        unsetLinksAction(), getLinksAction(), getURLFactory()
      • getLinksAction

        public String getLinksAction()
        Returns the action used by XWiki URL factories to replace the view action when creating URLs. If no action replacement has been specified, it returns null.
        Returns:
        The view action replacement, or null.
        See Also:
        setLinksAction(String), unsetLinksAction(), getURLFactory()
      • setLinksQueryString

        public void setLinksQueryString​(String value)
        Sets an extra query string to be added to all the URLs created by XWiki URL factories.
        Parameters:
        value - The additional query string to be added to all the URLs created by XWiki URL factories.
        See Also:
        unsetLinksQueryString(), getLinksQueryString(), getURLFactory()
      • getLinksQueryString

        public String getLinksQueryString()
        Returns the extra query string that is added to all the URLs created by XWiki URL factories. If no such string has been specified it returns null.
        Returns:
        The additional query string that is added to all XWiki URLs, or null.
        See Also:
        setLinksQueryString(String), unsetLinksQueryString(), getURLFactory()
      • getValidationStatus

        public XWikiValidationStatus getValidationStatus()
        Returns the form field validation status, which contains the exceptions or errors that may have occured during the validation process performed during a save.
        Returns:
        The validation status.
      • getDisplayedFields

        public List<String> getDisplayedFields()
        Returns the list with the currently displayed fields. Each time we call display on a document for a specific field that field is added to the list returned by this method.
        Returns:
        The list with the currently displayed fields.
        See Also:
        Document.display(String)
      • setDisplayMode

        public void setDisplayMode​(String mode)
        Sets the default field display mode, when using Document.display(String) or Document.display(String, Object). It is automatically set to "edit" when the action is "edit" or "inline", and to "view" in all other cases.
        Parameters:
        mode - the display mode, one of "view", "edit", "hidden", "search", "rendered".
      • getMacro

        public Object getMacro()
        Retrieves the information about the currently executing macro. This method is only useful inside wiki macros.
        Returns:
        macro information, normally a Map containing the macro content, the params, and the macro execution context
      • getMessage

        public String getMessage()
        Get the registered (generally error) message for the previous action.
        Returns:
        the registered message
        Since:
        5.2RC1