1 /*
2 * Copyright (c) 2002-2023, City of Paris
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice
10 * and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice
13 * and the following disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 *
16 * 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *
32 * License 1.0
33 */
34 package fr.paris.lutece.plugins.wiki.service;
35
36 import fr.paris.lutece.plugins.wiki.business.TopicVersion;
37 import fr.paris.lutece.plugins.wiki.service.parser.LuteceWikiParser;
38 import fr.paris.lutece.portal.service.cache.AbstractCacheableService;
39 import java.util.Locale;
40 import java.util.regex.Pattern;
41
42 /**
43 * Wiki Service
44 */
45 public final class WikiService extends AbstractCacheableService
46 {
47 private static final String SERVICE_CACHE_NAME = "Wiki Cache Service";
48
49 private static WikiService _singleton;
50
51 /** Private Constructor */
52 private WikiService( )
53 {
54 initCache( );
55 }
56
57 /**
58 * {@inheritDoc }
59 */
60 @Override
61 public String getName( )
62 {
63 return SERVICE_CACHE_NAME;
64 }
65
66 /**
67 * Returns the unique instance
68 *
69 * @return The instance
70 */
71 public static WikiService instance( )
72 {
73 synchronized( WikiService.class )
74 {
75 if ( _singleton == null )
76 {
77 _singleton = new WikiService( );
78 }
79 }
80 return _singleton;
81 }
82
83 /**
84 * Get the Wiki page in HTML format
85 *
86 * @param strPageName
87 * The page name
88 * @param version
89 * The content version
90 * @param strPageUrl
91 * The page URL
92 * @param strLanguage
93 * The language
94 * @return The HTML code
95 */
96 public String getWikiPage( String strPageName, TopicVersion version, String strPageUrl, String strLanguage )
97 {
98 StringBuilder sbKey = new StringBuilder( );
99 sbKey.append( strPageName ).append( '[' ).append( version.getIdTopicVersion( ) ).append( ']' )
100 .append( ( ( strPageUrl != null ) ? "[Url]" : "[noUrl]" ) ).append( ':' ).append( strLanguage );
101 String strPageContent = (String) getFromCache( sbKey.toString( ) );
102 synchronized( WikiService.class )
103 {
104 if ( strPageContent == null )
105 {
106 String strContent = version.getWikiContent( strLanguage ).getWikiContent( );
107 strPageContent = new LuteceWikiParser( strContent, strPageName, strLanguage ).toString( );
108 putInCache( sbKey.toString( ), strPageContent );
109 }
110 }
111 return strPageContent;
112 }
113
114 /**
115 * Get the Wiki page in text format
116 *
117 * @param strPageName
118 * The page name
119 * @param version
120 * The content version
121 * @param strLanguage
122 * The language
123 * @return The HTML code
124 */
125 public String getPageSource( String strPageName, TopicVersion version, String strLanguage )
126 {
127 String strContent = version.getWikiContent( strLanguage ).getWikiContent( );
128 strContent = renderSource( strContent );
129 Pattern pattern = Pattern.compile( "^\\s*$", Pattern.MULTILINE );
130 strContent = pattern.matcher( strContent ).replaceAll( "<br>" );
131 pattern = Pattern.compile( "^\\s*(.*)\\s*$", Pattern.MULTILINE );
132 strContent = pattern.matcher( strContent ).replaceAll( "<div>$1</div>" );
133
134 return strContent;
135 }
136
137 /**
138 * Get the Wiki page in HTML format
139 *
140 * @param strPageName
141 * The page name
142 * @param version
143 * The topic version
144 * @param strLanguage
145 * The language
146 * @return The HTML code
147 */
148 public String getWikiPage( String strPageName, TopicVersion version, String strLanguage )
149 {
150 return getWikiPage( strPageName, version, null, strLanguage );
151 }
152
153 /**
154 * Render the wiki content for the editor (convert special characters)
155 *
156 * @param version
157 * The version
158 * @param strLanguage
159 * The Language
160 * @return The content
161 */
162 public static String renderEditor( TopicVersion version, String strLanguage )
163 {
164 return LuteceWikiParser.renderWiki( version.getWikiContent( strLanguage ).getWikiContent( ) );
165 }
166
167 /**
168 * Render the wiki source content
169 *
170 * @param strContent
171 * The wiki page source
172 * @return The content
173 */
174 public static String renderSource( String strContent )
175 {
176 return LuteceWikiParser.renderSource( strContent );
177 }
178 }