View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de 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.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   * Document Alias Generator
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       * {@inheritDoc }
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              // get the page title of the last version
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                 /* Explicit URLs properties
108                 * Templates parameters :
109                 * - {0} : wiki path, ex : "wiki"
110                 * - {1} : page name, ex : "Lutece-plugins" (converted with FriendlyUrlUtils)
111                 * - {2} : page title, ex : "Presentation-du-wiki" (converted with FriendlyUrlUtils)
112                 * - {3} : language, ex : "fr"
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                 // generate Explict Urls based on topic page name
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                 // generate Explict Urls based on topic page title, by language
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      * {@inheritDoc }
180      */
181     @Override
182     public String getName(  )
183     {
184         return GENERATOR_NAME;
185     }
186 
187     /**
188      * Initialiaze generator with options stored with Datastore
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 }