1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.seo.modules.wiki.service;
35
36 import fr.paris.lutece.plugins.seo.business.FriendlyUrl;
37 import fr.paris.lutece.plugins.seo.service.FriendlyUrlUtils;
38 import fr.paris.lutece.plugins.seo.service.SEODataKeys;
39 import fr.paris.lutece.plugins.seo.service.generator.FriendlyUrlGenerator;
40 import fr.paris.lutece.plugins.seo.service.generator.GeneratorOptions;
41 import fr.paris.lutece.plugins.seo.service.sitemap.SitemapUtils;
42 import fr.paris.lutece.plugins.wiki.business.Topic;
43 import fr.paris.lutece.plugins.wiki.business.TopicHome;
44 import fr.paris.lutece.plugins.wiki.business.TopicVersion;
45 import fr.paris.lutece.plugins.wiki.business.TopicVersionHome;
46 import fr.paris.lutece.plugins.wiki.business.WikiContent;
47 import fr.paris.lutece.plugins.wiki.service.WikiLocaleService;
48 import fr.paris.lutece.portal.service.datastore.DatastoreService;
49 import fr.paris.lutece.portal.service.util.AppLogService;
50 import fr.paris.lutece.portal.service.util.AppPropertiesService;
51 import java.text.MessageFormat;
52 import java.util.ArrayList;
53
54 import java.util.Collection;
55 import java.util.List;
56 import org.apache.commons.lang.StringUtils;
57
58
59
60
61
62 public class WikiFriendlyUrlGenerator implements FriendlyUrlGenerator
63 {
64 private static final String GENERATOR_NAME = "Wiki Friendly URL Generator";
65 private static final String TECHNICAL_URL = "/jsp/site/Portal.jsp?page=wiki&view=page&page_name=";
66 private static final String LANGUAGE_ARG = "&language=" ;
67 private static final String SLASH = "/";
68 private static final String PATH_WIKI = "wiki/";
69
70 private static final String PROPERTY_PAGE_NAME_BASED_URL_ACTIVATE = "seo-wiki.pageNameBasedExplicitUrl.activate";
71 private static final String PROPERTY_PAGE_NAME_BASED_URL_TEMPLATE = "seo-wiki.pageNameBasedExplicitUrl.template";
72 private static final String PROPERTY_PAGE_TITLE_BASED_URLS_ACTIVATE = "seo-wiki.pageTitleBasedExplicitUrls.activate";
73 private static final String PROPERTY_PAGE_TITLE_BASED_URLS_TEMPLATE = "seo-wiki.pageTitleBasedExplicitUrls.template";
74
75 private static final String DEFAULT_CHANGE_FREQ = SitemapUtils.CHANGE_FREQ_VALUES[3];
76 private static final String DEFAULT_PRIORITY = SitemapUtils.PRIORITY_VALUES[3];
77 private boolean _bCanonical;
78 private boolean _bSitemap;
79 private String _strChangeFreq;
80 private String _strPriority;
81
82
83
84
85 @Override
86 public String generate( List<FriendlyUrl> list, GeneratorOptions options )
87 {
88 Collection<Topic> listTopics = TopicHome.getTopicsList( );
89
90 List<String> listLanguages = WikiLocaleService.getLanguages( );
91 String defaultLanguage = WikiLocaleService.getDefaultLanguage( ) ;
92 init( );
93
94 for ( Topic t : listTopics )
95 {
96
97
98 TopicVersion lastTopicVersion = TopicVersionHome.findLastVersion(t.getIdTopic());
99
100 if( lastTopicVersion == null )
101 {
102 AppLogService.error( "SEO Wiki indexer : No data was found for topic #" + t.getIdTopic() + ". Data may be corrupted.");
103 }
104 else
105 {
106
107
108
109
110
111
112
113
114
115 Boolean generatePageNameBasedExplicitURL = "true".equals( AppPropertiesService.getProperty( PROPERTY_PAGE_NAME_BASED_URL_ACTIVATE ) ) ;
116 Boolean generatePageTitleBasedExplicitURLs = "true".equals( AppPropertiesService.getProperty( PROPERTY_PAGE_TITLE_BASED_URLS_ACTIVATE ) ) ;
117
118
119 if (generatePageNameBasedExplicitURL) {
120 String template = AppPropertiesService.getProperty( PROPERTY_PAGE_NAME_BASED_URL_TEMPLATE );
121 FriendlyUrl url = new FriendlyUrl( );
122
123 String wikiPath = ( options.isAddPath( ) ? PATH_WIKI : "" ) ;
124 String pageName = FriendlyUrlUtils.convertToFriendlyUrl( t.getPageName( ) ) ;
125 String strPath = SLASH + MessageFormat.format( template , wikiPath, pageName, "", "") ;
126
127 url.setFriendlyUrl( strPath );
128
129 String strTechnicalUrl = TECHNICAL_URL + t.getPageName( ) ;
130 url.setTechnicalUrl( strTechnicalUrl );
131
132 url.setCanonical( _bCanonical );
133 url.setSitemap( _bSitemap );
134 url.setSitemapChangeFreq( _strChangeFreq );
135
136 url.setSitemapLastmod( SitemapUtils.formatDate( lastTopicVersion.getDateEdition( ) ) );
137
138 url.setSitemapPriority( _strPriority );
139 list.add( url );
140 }
141
142
143 if (generatePageTitleBasedExplicitURLs) {
144 for ( String strLanguage : listLanguages )
145 {
146 String template = AppPropertiesService.getProperty( PROPERTY_PAGE_TITLE_BASED_URLS_TEMPLATE );
147 FriendlyUrl url = new FriendlyUrl( );
148
149 WikiContent lastContent = lastTopicVersion.getWikiContent( strLanguage ) ;
150 String pageTitle = FriendlyUrlUtils.convertToFriendlyUrl(( !StringUtils.isBlank( lastContent.getPageTitle( ) ) ? lastContent.getPageTitle( ) : t.getPageName( ) ) ) ;
151 String wikiPath = ( options.isAddPath( ) ? PATH_WIKI : "" ) ;
152 String pageName = FriendlyUrlUtils.convertToFriendlyUrl( t.getPageName( ) ) ;
153 String strPath = SLASH + MessageFormat.format( template , wikiPath, pageName, pageTitle, strLanguage ) ;
154
155 url.setFriendlyUrl( strPath );
156
157
158 String strTechnicalUrl = TECHNICAL_URL + t.getPageName( ) ;
159 if ( !defaultLanguage.equals( strLanguage ) ) strTechnicalUrl += LANGUAGE_ARG + strLanguage;
160 url.setTechnicalUrl( strTechnicalUrl );
161
162 url.setCanonical( _bCanonical );
163 url.setSitemap( _bSitemap );
164 url.setSitemapChangeFreq( _strChangeFreq );
165
166 url.setSitemapLastmod( SitemapUtils.formatDate( lastTopicVersion.getDateEdition( ) ) );
167
168 url.setSitemapPriority( _strPriority );
169 list.add( url );
170 }
171 }
172 }
173 }
174
175 return "";
176 }
177
178
179
180
181 @Override
182 public String getName( )
183 {
184 return GENERATOR_NAME;
185 }
186
187
188
189
190 private void init( )
191 {
192 String strKeyPrefix = SEODataKeys.PREFIX_GENERATOR + getClass( ).getName( );
193 _bCanonical = DatastoreService.getDataValue( strKeyPrefix + SEODataKeys.SUFFIX_CANONICAL,
194 DatastoreService.VALUE_TRUE ).equals( DatastoreService.VALUE_TRUE );
195 _bSitemap = DatastoreService.getDataValue( strKeyPrefix + SEODataKeys.SUFFIX_SITEMAP,
196 DatastoreService.VALUE_TRUE ).equals( DatastoreService.VALUE_TRUE );
197 _strChangeFreq = DatastoreService.getDataValue( strKeyPrefix + SEODataKeys.SUFFIX_CHANGE_FREQ,
198 DEFAULT_CHANGE_FREQ );
199 _strPriority = DatastoreService.getDataValue( strKeyPrefix + SEODataKeys.SUFFIX_PRIORITY, DEFAULT_PRIORITY );
200 }
201 }