1 16 package org.outerj.daisy.install; 17 18 import org.apache.commons.cli.CommandLine; 19 import org.apache.commons.cli.CommandLineParser; 20 import org.apache.commons.cli.HelpFormatter; 21 import org.apache.commons.cli.Option; 22 import org.apache.commons.cli.Options; 23 import org.apache.commons.cli.ParseException; 24 import org.apache.commons.cli.PosixParser; 25 import org.outerj.daisy.repository.Credentials; 26 import org.outerj.daisy.repository.DocumentCollection; 27 import org.outerj.daisy.repository.Document; 28 import org.outerj.daisy.repository.Repository; 29 import org.outerj.daisy.repository.RepositoryException; 30 import org.outerj.daisy.repository.RepositoryManager; 31 import org.outerj.daisy.repository.clientimpl.RemoteRepositoryManager; 32 import org.outerj.daisy.repository.user.Role; 33 import org.outerj.daisy.repository.variant.Language; 34 import org.outerj.daisy.repository.variant.LanguageNotFoundException; 35 import org.outerj.daisy.repository.variant.Branch; 36 import org.outerj.daisy.util.VersionHelper; 37 38 import java.io.*; 39 import java.util.Properties ; 40 41 public class DaisyWikiAddSite { 42 private Document navigationDoc; 43 private DocumentCollection collection; 44 private Document sampleDoc; 45 46 public static void main(String [] args) throws Exception { 47 new DaisyWikiAddSite().install(args); 48 } 49 50 private void install(String [] args) throws Exception { 51 Options options = new Options(); 52 options.addOption(new Option("c", "conf", true, "Configuration file for automated install")); 53 options.addOption(new Option("v", "version", false, "Print version info")); 54 55 CommandLineParser parser = new PosixParser(); 56 CommandLine cmd = null; 57 try { 58 cmd = parser.parse(options, args); 59 } catch (ParseException e) { 60 HelpFormatter help = new HelpFormatter(); 61 help.printHelp("daisy-wiki-add-site", options, true); 62 System.exit(0); 63 } 64 65 if (cmd.hasOption('v')) { 66 System.out.println(VersionHelper.getVersionString(getClass().getClassLoader(), "org/outerj/daisy/install/versioninfo.properties")); 67 System.exit(0); 68 } 69 70 InitialisationProperties initSettings; 71 if (cmd.hasOption("c")) { 72 initSettings = new InitialisationProperties(new File(cmd.getOptionValue("c"))); 73 createCollection(initSettings); 74 } else { 75 76 initSettings = new InitialisationProperties(); 77 78 if (cmd.getArgs().length < 1) { 82 System.err.println("Please specify the wikidata directory path as argument."); 83 System.exit(1); 84 } 85 initSettings.setWikiDataDir(new File(cmd.getArgs()[0])); 86 87 InstallHelper.printTitle("Daisy Wiki New Site Creation"); 91 System.out.println(); 92 System.out.println("This program will create a new Daisy Wiki site. This consists of:"); 93 System.out.println(" * creating a collection for the documents of that site"); 94 System.out.println(" * creating a navigation document for the site"); 95 System.out.println(" * creating one (home) page for the site, linked from the navigation"); 96 System.out.println(" * adding the site definition in the <wikidata dir>/sites directory"); 97 System.out.println(); 98 99 initSettings.setRepository(InstallHelper.promptRepository()); 103 System.out.println("Connecting to the repository.\n"); 104 105 System.out.println("Please specify a name for the site. This name will be used as:"); 109 System.out.println(" * name for the collection"); 110 System.out.println(" * directory name for the directory containing the site-specific stuff"); 111 initSettings.setSiteName(InstallHelper.prompt("Enter a name for the site (without spaces):")); 112 113 createCollection(initSettings); 114 115 System.out.println("\n"); 119 System.out.println("You can now select a language for the site."); 120 Language[] languages = initSettings.getRepository().getVariantManager().getAllLanguages(false).getArray(); 121 System.out.println("Currently defined languages are:\n"); 122 for (int i = 0; i < languages.length; i++) { 123 System.out.println(" " + languages[i].getName()); 124 } 125 System.out.println("\nTo define a new language, simply enter a non-existing name"); 126 System.out.println("(names must start with a letter (a-z or A-Z) and can then contain"); 127 System.out.println("letters, digits and the symbols - and _"); 128 System.out.println(); 129 System.out.println("It is recommended to use the 2-letter codes from ISO 639. A list of"); 130 System.out.println("these codes can be found at:"); 131 System.out.println("http://ftp.ics.uci.edu/pub/ietf/http/related/iso639.txt"); 132 System.out.println(""); 133 System.out.println("If unsure, or when creating a mixed language site, just press enter"); 134 System.out.println("to use the 'default' language."); 135 System.out.println(""); 136 initSettings.setSiteLanguage(InstallHelper.prompt("Enter the language [default: default]:", "default")); 137 } 138 139 createNavigation(initSettings); 140 createSampleDocument(initSettings); 141 generateSiteConf(initSettings); 142 143 System.out.println(); 144 System.out.println("Done."); 145 146 } 147 148 private void createCollection(InitialisationProperties settings) throws RepositoryException { 149 System.out.println("Creating collection " + settings.getSiteName()); 150 collection = settings.getRepository().getCollectionManager().createCollection(settings.getSiteName()); 151 collection.save(); 152 System.out.println("Collection created."); 153 } 154 155 private void createNavigation(InitialisationProperties settings) throws UnsupportedEncodingException, RepositoryException { 156 System.out.println("Creating navigation document."); 157 navigationDoc = settings.getRepository().createDocument("Navigation for " + settings.getSiteName(), "Navigation", Branch.MAIN_BRANCH_NAME, settings.getSiteLanguage()); 158 String emptyNavigation = "<d:navigationTree xmlns:d='http://outerx.org/daisy/1.0#navigationspec'/>"; 159 navigationDoc.setPart("NavigationDescription", "text/xml", emptyNavigation.getBytes("UTF-8")); 160 navigationDoc.addToCollection(collection); 161 navigationDoc.save(); 162 System.out.println("Navigation document created, id = " + navigationDoc.getId()); 163 } 164 165 private void createSampleDocument(InitialisationProperties settings) throws UnsupportedEncodingException, RepositoryException { 166 System.out.println("Creating sample document."); 167 sampleDoc = settings.getRepository().createDocument(settings.getSiteName() + " home", "SimpleDocument", Branch.MAIN_BRANCH_NAME, settings.getSiteLanguage()); 168 String content = "<html><body><h1>Welcome!</h1><p>Hello, welcome to your new site.</p></body></html>"; 169 sampleDoc.setPart("SimpleDocumentContent", "text/xml", content.getBytes("UTF-8")); 170 sampleDoc.addToCollection(collection); 171 sampleDoc.save(); 172 System.out.println("Sample document created, id = " + sampleDoc.getId()); 173 174 System.out.println("Updating navigation document."); 178 StringBuffer navigation = new StringBuffer (); 179 navigation.append("<d:navigationTree xmlns:d='http://outerx.org/daisy/1.0#navigationspec'>\n"); 180 navigation.append(" <d:collections>\n"); 181 navigation.append(" <d:collection name='").append(settings.getSiteName()).append("'/>\n"); 182 navigation.append(" </d:collections>\n"); 183 navigation.append(" <d:doc id='").append(sampleDoc.getId()).append("' label='").append(sampleDoc.getName()).append("'/>\n"); 184 navigation.append(" <d:group label='All documents A->Z'>\n"); 185 navigation.append(" <d:query q='select name where true order by name'/>\n"); 186 navigation.append(" </d:group>\n"); 187 navigation.append("</d:navigationTree>"); 188 navigationDoc.setPart("NavigationDescription", "text/xml", navigation.toString().getBytes("UTF-8")); 189 navigationDoc.save(); 190 System.out.println("Navigation document updated."); 191 } 192 193 private void generateSiteConf(InitialisationProperties settings) throws Exception { 194 StringBuffer siteConfBuffer = new StringBuffer (); 195 siteConfBuffer.append("<siteconf xmlns=\"http://outerx.org/daisy/1.0#siteconf\">"); 196 siteConfBuffer.append("\n <title>" + settings.getSiteName() + "</title>"); 197 siteConfBuffer.append("\n <description>The \"" + settings.getSiteName() + "\" site</description>"); 198 siteConfBuffer.append("\n <skin>default</skin>"); 199 siteConfBuffer.append("\n <navigationDocId>" + navigationDoc.getId() + "</navigationDocId>"); 200 siteConfBuffer.append("\n <homepageDocId>" + sampleDoc.getId() + "</homepageDocId>"); 201 siteConfBuffer.append("\n <collectionId>" + collection.getId() + "</collectionId>"); 202 siteConfBuffer.append("\n <contextualizedTree>false</contextualizedTree>"); 203 siteConfBuffer.append("\n <branch>").append(Branch.MAIN_BRANCH_NAME).append("</branch>"); 204 siteConfBuffer.append("\n <language>").append(settings.getSiteLanguage()).append("</language>"); 205 siteConfBuffer.append("\n <defaultDocumentType>SimpleDocument</defaultDocumentType>"); 206 siteConfBuffer.append("\n <newVersionStateDefault>publish</newVersionStateDefault>"); 207 siteConfBuffer.append("\n <locking>"); 208 siteConfBuffer.append("\n <automatic lockType='pessimistic' defaultTime='15' autoExtend='true'/>"); 209 siteConfBuffer.append("\n </locking>"); 210 siteConfBuffer.append("\n</siteconf>"); 211 String siteConf = siteConfBuffer.toString(); 212 213 File sitesDir = new File(settings.getWikiDataDir(), "sites"); 214 if (!sitesDir.exists() || !sitesDir.isDirectory()) { 215 System.out.println("Non-existing directory or not a directory: " + sitesDir.getAbsolutePath()); 216 sitesDir = null; 217 } 218 219 if (sitesDir != null) { 220 File newDir = new File(sitesDir, settings.getSiteName()); 221 newDir.mkdir(); 222 File siteConfFile = new File(newDir, "siteconf.xml"); 223 if (siteConfFile.exists()) { 224 InstallHelper.backupFile(siteConfFile); 225 } 226 227 OutputStream os = new FileOutputStream(siteConfFile); 228 try { 229 OutputStreamWriter writer = new OutputStreamWriter(os, "UTF-8"); 230 writer.write(siteConf); 231 writer.flush(); 232 } finally { 233 os.close(); 234 } 235 System.out.println("Written " + siteConfFile.getAbsolutePath()); 236 237 File cocoonExtDir = new File(newDir, "cocoon"); 238 cocoonExtDir.mkdir(); 239 File sitemap = new File(cocoonExtDir, "sitemap.xmap"); 240 os = new FileOutputStream(sitemap); 241 try { 242 OutputStreamWriter writer = new OutputStreamWriter(os, "UTF-8"); 243 writer.write(readResource("org/outerj/daisy/install/sample-extension-sitemap.xml")); 244 writer.flush(); 245 } finally { 246 os.close(); 247 } 248 249 } 250 } 251 252 String readResource(String name) throws Exception { 253 InputStream is = getClass().getClassLoader().getResourceAsStream(name); 254 Reader reader = new InputStreamReader(is, "UTF-8"); 255 BufferedReader bufferedReader = new BufferedReader(reader); 256 257 StringBuffer buffer = new StringBuffer (); 258 int c = bufferedReader.read(); 259 while (c != -1) { 260 buffer.append((char) c); 261 c = bufferedReader.read(); 262 } 263 264 return buffer.toString(); 265 } 266 267 private static class InitialisationProperties { 268 public static final String DAISYWIKI_DATADIR = "wikiDataDir"; 269 public static final String DAISY_URL = "daisyUrl"; 270 public static final String DAISY_LOGIN = "daisyLogin"; 271 public static final String DAISY_PASSWORD = "daisyPassword"; 272 public static final String SITE_NAME = "siteName"; 273 public static final String SITE_LANGUAGE = "siteLanguage"; 274 275 private String daisyUrl; 276 private Repository repository; 277 private Credentials credentials; 278 private String daisyLogin; 279 private String daisyPassword; 280 private File wikiDataDir; 281 private String siteName; 282 private String siteLanguage; 283 284 public InitialisationProperties() { 285 } 286 287 public InitialisationProperties(File configProperties) throws PropertyNotFoundException, Exception { 288 FileInputStream is = null; 289 try { 290 is = new FileInputStream(configProperties); 291 Properties props = new Properties (); 292 props.load(is); 293 294 Credentials credentials = new Credentials(InstallHelper.getPropertyValue(props, InitialisationProperties.DAISY_LOGIN), InstallHelper 295 .getPropertyValue(props, InitialisationProperties.DAISY_PASSWORD)); 296 System.out.println("\nConnecting to the repository."); 297 RepositoryManager repositoryManager = new RemoteRepositoryManager(InstallHelper.getPropertyValue(props, InitialisationProperties.DAISY_URL), 298 credentials); 299 repository = repositoryManager.getRepository(credentials); 300 repository.switchRole(Role.ADMINISTRATOR); 301 setWikiDataDir(new File(InstallHelper.getPropertyValue(props,InitialisationProperties.DAISYWIKI_DATADIR))); 302 siteName = InstallHelper.getPropertyValue(props,InitialisationProperties.SITE_NAME); 303 setSiteLanguage(InstallHelper.getPropertyValue(props,InitialisationProperties.SITE_LANGUAGE, "default")); 304 305 } finally { 306 if (is != null) 307 is.close(); 308 else 309 System.out.println("Could not open " + configProperties.getAbsolutePath() + " exiting ..."); 310 } 311 } 312 313 public Credentials getCredentials() { 314 return credentials; 315 } 316 317 public void setCredentials(Credentials credentials) { 318 this.credentials = credentials; 319 } 320 321 public String getDaisyLogin() { 322 return daisyLogin; 323 } 324 325 public void setDaisyLogin(String daisyLogin) { 326 this.daisyLogin = daisyLogin; 327 } 328 329 public String getDaisyPassword() { 330 return daisyPassword; 331 } 332 333 public void setDaisyPassword(String daisyPassword) { 334 this.daisyPassword = daisyPassword; 335 } 336 337 public String getDaisyUrl() { 338 return daisyUrl; 339 } 340 341 public void setDaisyUrl(String daisyUrl) { 342 this.daisyUrl = daisyUrl; 343 } 344 345 public Repository getRepository() { 346 return repository; 347 } 348 349 public void setRepository(Repository repository) { 350 this.repository = repository; 351 } 352 353 public File getWikiDataDir() { 354 return wikiDataDir; 355 } 356 357 public void setWikiDataDir(File wikiDataDir) throws Exception { 358 if (!wikiDataDir.exists()) { 359 throw new Exception ("Could not find the wikidata directory at : " + wikiDataDir.getAbsolutePath()); 360 } 361 this.wikiDataDir = wikiDataDir; 362 } 363 364 public String getSiteLanguage() { 365 return siteLanguage; 366 } 367 368 public void setSiteLanguage(String siteLanguage) throws RepositoryException { 369 try { 370 this.siteLanguage = repository.getVariantManager().getLanguageByName(siteLanguage, false).getName(); 372 } catch (LanguageNotFoundException e) { 373 Language language = repository.getVariantManager().createLanguage(siteLanguage); 374 language.save(); 375 } 376 this.siteLanguage = siteLanguage; 377 } 378 379 public String getSiteName() { 380 return siteName; 381 } 382 383 public void setSiteName(String siteName) { 384 this.siteName = siteName; 385 } 386 387 } 388 } | Popular Tags |