001/* 
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.  
018 */
019package org.apache.wiki.pages;
020
021import org.apache.wiki.api.core.Context;
022import org.apache.wiki.api.core.Page;
023import org.apache.wiki.api.exceptions.ProviderException;
024import org.apache.wiki.api.exceptions.WikiException;
025import org.apache.wiki.api.providers.PageProvider;
026import org.apache.wiki.event.WikiEventListener;
027
028import java.util.Collection;
029import java.util.List;
030import java.util.Set;
031
032
033public interface PageManager extends WikiEventListener {
034
035    /** The property value for setting the current page provider.  Value is {@value}. */
036    String PROP_PAGEPROVIDER = "jspwiki.pageProvider";
037    /** The property value for setting the cache on/off.  Value is {@value}. */
038    String PROP_USECACHE = "jspwiki.usePageCache";
039    /** The property value for setting the amount of time before the page locks expire. Value is {@value}. */
040    String PROP_LOCKEXPIRY = "jspwiki.lockExpiryTime";
041
042    /**
043     * Returns the page provider currently in use.
044     *
045     * @return A WikiPageProvider instance.
046     */
047    PageProvider getProvider();
048
049    /**
050     * Returns all pages in some random order.  If you need just the page names,
051     * please see {@link org.apache.wiki.references.ReferenceManager#findCreated() ReferenceManager#findCreated()}, which is probably a lot
052     * faster.  This method may cause repository access.
053     *
054     * @return A Collection of WikiPage objects.
055     * @throws ProviderException If the backend has problems.
056     */
057    Collection< Page > getAllPages() throws ProviderException;
058
059    /**
060     * Fetches the page text from the repository.  This method also does some sanity checks, like checking for the pageName validity, etc.
061     * Also, if the page repository has been modified externally, it is smart enough to handle such occurrences.
062     *
063     * @param pageName The name of the page to fetch.
064     * @param version  The version to find
065     * @return The page content as a raw string
066     * @throws ProviderException If the backend has issues.
067     */
068    String getPageText( String pageName, int version ) throws ProviderException;
069
070    /**
071     *  Returns the pure text of a page, no conversions.  Use this if you are writing something that depends on the parsing
072     *  of the page. Note that you should always check for page existence through pageExists() before attempting to fetch
073     *  the page contents.
074     *
075     *  This method is pretty similar to {@link #getPageText(String, int)}, except that it doesn't throw {@link ProviderException},
076     *  it logs and swallows them.
077     *
078     *  @param page The name of the page to fetch.
079     *  @param version If WikiPageProvider.LATEST_VERSION, then uses the latest version.
080     *  @return The page contents.  If the page does not exist, returns an empty string.
081     */
082    String getPureText( String page, int version );
083
084    /**
085     *  Returns the pure text of a page, no conversions.  Use this if you are writing something that depends on the parsing
086     *  the page. Note that you should always check for page existence through pageExists() before attempting to fetch
087     *  the page contents.
088     *
089     *  This method is pretty similar to {@link #getPageText(String, int)}, except that it doesn't throw {@link ProviderException},
090     *  it logs and swallows them.
091     *
092     *  @param page A handle to the WikiPage
093     *  @return String of WikiText.
094     *  @since 2.1.13, moved to PageManager on 2.11.0.
095     */
096    default String getPureText( final Page page ) {
097        return getPureText( page.getName(), page.getVersion() );
098    }
099
100    /**
101     *  Returns the un-HTMLized text of the given version of a page. This method also replaces the &lt; and &amp; -characters with
102     *  their respective HTML entities, thus making it suitable for inclusion on an HTML page.  If you want to have the page text
103     *  without any conversions, use {@link #getPureText(String, int)}.
104     *
105     * @param page WikiName of the page to fetch
106     * @param version  Version of the page to fetch
107     * @return WikiText.
108     */
109    String getText( String page, int version );
110
111    /**
112     *  Returns the un-HTMLized text of the latest version of a page. This method also replaces the &lt; and &amp; -characters with
113     *  their respective HTML entities, thus making it suitable for inclusion on an HTML page.  If you want to have the page text
114     *  without any conversions, use {@link #getPureText(String, int)}.
115     *
116     *  @param page WikiName of the page to fetch.
117     *  @return WikiText.
118     */
119    default String getText( final String page ) {
120        return getText( page, PageProvider.LATEST_VERSION );
121    }
122
123    /**
124     *  Returns the un-HTMLized text of the given version of a page in the given context.  USE THIS METHOD if you don't know what doing.
125     *  <p>
126     *  This method also replaces the &lt; and &amp; -characters with their respective HTML entities, thus making it suitable
127     *  for inclusion on an HTML page.  If you want to have the page text without any conversions, use {@link #getPureText(Page)}.
128     *
129     *  @since 1.9.15.
130     *  @param page A page reference (not an attachment)
131     *  @return The page content as HTMLized String.
132     *  @see PageManager#getPureText(Page)
133     */
134    default String getText( final Page page ) {
135        return getText( page.getName(), page.getVersion() );
136    }
137
138    /**
139     *  Writes the WikiText of a page into the page repository. If the <code>jspwiki.properties</code> file contains
140     *  the property <code>jspwiki.approver.workflow.saveWikiPage</code> and its value resolves to a valid user,
141     *  {@link org.apache.wiki.auth.authorize.Group} or {@link org.apache.wiki.auth.authorize.Role}, this method will
142     *  place a {@link org.apache.wiki.workflow.Decision} in the approver's workflow inbox and throw a
143     *  {@link org.apache.wiki.workflow.DecisionRequiredException}. If the submitting user is authenticated and the
144     *  page save is rejected, a notification will be placed in the user's decision queue.
145     *
146     *  @since 2.1.28, moved to PageManager on 2.11.0
147     *  @param context The current WikiContext
148     *  @param text    The Wiki markup for the page.
149     *  @throws WikiException if the save operation encounters an error during the save operation. If the page-save
150     *  operation requires approval, the exception will be of type {@link org.apache.wiki.workflow.DecisionRequiredException}.
151     *  Individual PageFilters, such as the {@link org.apache.wiki.filters.SpamFilter} may also throw a
152     *  {@link org.apache.wiki.api.exceptions.RedirectException}.
153     */
154    void saveText( Context context, String text ) throws WikiException;
155
156    /**
157     * Puts the page text into the repository.  Note that this method does NOT update
158     * JSPWiki internal data structures, and therefore you should always use saveText()
159     *
160     * @param page    Page to save
161     * @param content Wikimarkup to save
162     * @throws ProviderException If something goes wrong in the saving phase
163     */
164    void putPageText( Page page, String content ) throws ProviderException;
165
166    /**
167     * Locks page for editing.  Note, however, that the PageManager will in no way prevent you from actually editing this page;
168     * the lock is just for information.
169     *
170     * @param page WikiPage to lock
171     * @param user Username to use for locking
172     * @return null, if page could not be locked.
173     */
174    PageLock lockPage( Page page, String user );
175
176    /**
177     * Marks a page free to be written again.  If there has not been a lock, will fail quietly.
178     *
179     * @param lock A lock acquired in lockPage().  Safe to be null.
180     */
181    void unlockPage( PageLock lock );
182
183    /**
184     * Returns the current lock owner of a page.  If the page is not locked, will return null.
185     *
186     * @param page The page to check the lock for
187     * @return Current lock, or null, if there is no lock
188     */
189    PageLock getCurrentLock( Page page );
190
191    /**
192     * Returns a list of currently applicable locks.  Note that by the time you get the list,
193     * the locks may have already expired, so use this only for informational purposes.
194     *
195     * @return List of PageLock objects, detailing the locks.  If no locks exist, returns an empty list.
196     * @since 2.0.22.
197     */
198    List< PageLock > getActiveLocks();
199
200    /**
201     *  Finds the corresponding WikiPage object based on the page name.  It always finds
202     *  the latest version of a page.
203     *
204     *  @param pagereq The name of the page to look for.
205     *  @return A WikiPage object, or null, if the page by the name could not be found.
206     */
207    Page getPage( String pagereq );
208
209    /**
210     *  Finds the corresponding WikiPage object base on the page name and version.
211     *
212     *  @param pagereq The name of the page to look for.
213     *  @param version The version number to look for.  May be WikiProvider.LATEST_VERSION,
214     *  in which case it will look for the latest version (and this method then becomes
215     *  the equivalent of getPage(String).
216     *
217     *  @return A WikiPage object, or null, if the page could not be found; or if there
218     *  is no such version of the page.
219     *  @since 1.6.7 (moved to PageManager on 2.11.0).
220     */
221    Page getPage( String pagereq, int version );
222
223    /**
224     * Finds a WikiPage object describing a particular page and version.
225     *
226     * @param pageName The name of the page
227     * @param version  A version number
228     * @return A WikiPage object, or null, if the page does not exist
229     * @throws ProviderException If there is something wrong with the page name or the repository
230     */
231    Page getPageInfo( String pageName, int version ) throws ProviderException;
232
233    /**
234     * Gets a version history of page.  Each element in the returned List is a WikiPage.
235     *
236     * @param pageName The name of the page or attachment to fetch history for
237     * @return If the page does not exist or there's some problem retrieving the version history, returns null,
238     *         otherwise a List of WikiPages / Attachments, each corresponding to a different revision of the page / attachment.
239     */
240    < T extends Page > List< T > getVersionHistory( String pageName );
241
242    /**
243     *  Returns the provider name.
244     *
245     *  @return The full class name of the current page provider.
246     */
247    String getCurrentProvider();
248
249    /**
250     * Returns a human-readable description of the current provider.
251     *
252     * @return A human-readable description.
253     */
254    String getProviderDescription();
255
256    /**
257     * Returns the total count of all pages in the repository. This
258     * method is equivalent of calling getAllPages().size(), but
259     * it swallows the ProviderException and returns -1 instead of
260     * any problems.
261     *
262     * @return The number of pages, or -1, if there is an error.
263     */
264    int getTotalPageCount();
265
266    /**
267     *  Returns a Collection of WikiPages, sorted in time order of last change (i.e. first object is the most recently changed).
268     *  This method also includes attachments.
269     *
270     *  @return Set of WikiPage objects.
271     */
272    Set< Page > getRecentChanges();
273
274    /**
275     * Returns true, if the page exists (any version) on the underlying WikiPageProvider.
276     *
277     * @param pageName Name of the page.
278     * @return A boolean value describing the existence of a page
279     * @throws ProviderException If the backend fails or the name is illegal.
280     */
281    boolean pageExists( String pageName ) throws ProviderException;
282
283    /**
284     * Checks for existence of a specific page and version on the underlying WikiPageProvider.
285     *
286     * @param pageName Name of the page
287     * @param version  The version to check
288     * @return <code>true</code> if the page exists, <code>false</code> otherwise
289     * @throws ProviderException If backend fails or name is illegal
290     * @since 2.3.29
291     */
292    boolean pageExists( String pageName, int version ) throws ProviderException;
293
294    /**
295     *  Checks for existence of a specific page and version denoted by a WikiPage on the underlying WikiPageProvider.
296     *
297     *  @param page A WikiPage object describing the name and version.
298     *  @return true, if the page (or alias, or attachment) exists.
299     *  @throws ProviderException If something goes badly wrong.
300     *  @since 2.0
301     */
302    default boolean pageExists( final Page page ) throws ProviderException {
303        if( page != null ) {
304            return pageExists( page.getName(), page.getVersion() );
305        }
306        return false;
307    }
308
309    /**
310     *  Returns true, if the requested page (or an alias) exists.  Will consider any version as existing. Will check for all types of
311     *  WikiPages: wiki pages themselves, attachments and special pages (non-existant references to other pages).
312     *
313     *  @param page WikiName of the page.
314     *  @return true, if page (or attachment) exists.
315     */
316    boolean wikiPageExists( String page );
317
318    /**
319     *  Returns true, if the requested page (or an alias) exists with the requested version. Will check for all types of
320     *  WikiPages: wiki pages themselves, attachments and special pages (non-existant references to other pages).
321     *
322     *  @param page Page name
323     *  @param version Page version
324     *  @return True, if page (or alias, or attachment) exists
325     *  @throws ProviderException If the provider fails.
326     */
327    boolean wikiPageExists( String page, int version ) throws ProviderException;
328
329    /**
330     *  Returns true, if the requested page (or an alias) exists, with the specified version in the WikiPage. Will check for all types of
331     *  WikiPages: wiki pages themselves, attachments and special pages (non-existant references to other pages).
332     *
333     *  @param page A WikiPage object describing the name and version.
334     *  @return true, if the page (or alias, or attachment) exists.
335     *  @throws ProviderException If something goes badly wrong.
336     *  @since 2.0
337     */
338    default boolean wikiPageExists( final Page page ) throws ProviderException {
339        if( page != null ) {
340            return wikiPageExists( page.getName(), page.getVersion() );
341        }
342        return false;
343    }
344
345    /**
346     * Deletes only a specific version of a WikiPage.
347     *
348     * @param page The page to delete.
349     * @throws ProviderException if the page fails
350     */
351    void deleteVersion( Page page ) throws ProviderException;
352
353    /**
354     *  Deletes a page or an attachment completely, including all versions.  If the page does not exist, does nothing.
355     *
356     * @param pageName The name of the page.
357     * @throws ProviderException If something goes wrong.
358     */
359    void deletePage( String pageName ) throws ProviderException;
360
361    /**
362     * Deletes an entire page, all versions, all traces.
363     *
364     * @param page The WikiPage to delete
365     * @throws ProviderException If the repository operation fails
366     */
367    void deletePage( Page page ) throws ProviderException;
368
369    /**
370     * Returns the configured {@link PageSorter}.
371     * 
372     * @return the configured {@link PageSorter}.
373     */
374    PageSorter getPageSorter();
375
376}