1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 package com.sun.star.wiki; 25 26 import java.net.URL; 27 import java.util.Hashtable; 28 import java.util.concurrent.Callable; 29 import com.sun.star.uno.XComponentContext; 30 import org.w3c.dom.Document; 31 import org.w3c.dom.Element; 32 33 34 public class WikiArticle 35 { 36 private XComponentContext m_xContext; 37 38 private boolean m_bNoArticle = true; 39 40 protected String m_sWikiUser; 41 protected String m_sWikiPass; 42 43 protected String m_sTitle = ""; 44 45 private URL m_aMainURL; 46 private WikiSession m_aSession; 47 48 49 /** Creates a new instance of WikiArticle */ WikiArticle( XComponentContext xContext, String sTitle, Hashtable wikiSettings, boolean bLogin, WikiPropDialog aPropDialog )50 public WikiArticle( XComponentContext xContext, String sTitle, Hashtable wikiSettings, boolean bLogin, WikiPropDialog aPropDialog ) 51 throws java.net.MalformedURLException, com.sun.star.uno.Exception, java.io.IOException, WikiCancelException 52 { 53 m_xContext = xContext; 54 55 String sMainUrl = (String) wikiSettings.get("Url"); 56 m_sWikiUser = (String) wikiSettings.get("Username"); 57 m_sWikiPass = (String) wikiSettings.get("Password"); 58 m_sTitle = sTitle; 59 60 m_aMainURL = new URL( sMainUrl ); 61 62 if ( bLogin ) 63 { 64 final XComponentContext xDialogContext = xContext; 65 final Hashtable aDialogSettings = wikiSettings; 66 Callable<Boolean> aAskForLogin = new Callable<Boolean>() 67 { 68 public Boolean call() 69 { 70 WikiEditSettingDialog aDialog = new WikiEditSettingDialog( xDialogContext, "vnd.sun.star.script:WikiEditor.EditSetting?location=application", aDialogSettings, false ); 71 try 72 { 73 return Boolean.valueOf( aDialog.show() ); 74 } 75 finally 76 { 77 aDialog.DisposeDialog(); 78 } 79 } 80 }; 81 82 while( !Login() ) 83 { 84 if ( aPropDialog != null ) 85 aPropDialog.SetThrobberActive( false ); 86 87 if ( MainThreadDialogExecutor.Run( xContext, aAskForLogin ) ) 88 { 89 m_sWikiUser = (String) wikiSettings.get("Username"); 90 m_sWikiPass = (String) wikiSettings.get("Password"); 91 } 92 else 93 throw new WikiCancelException(); 94 95 if ( aPropDialog != null ) 96 { 97 aPropDialog.SetThrobberActive( true ); 98 Thread.yield(); 99 } 100 } 101 } 102 103 InitArticleExistence(); 104 } 105 GetMainURL()106 public String GetMainURL() 107 { 108 return m_aMainURL.toString(); 109 } 110 GetTitle()111 public String GetTitle() 112 { 113 return m_sTitle; 114 } 115 InitArticleExistence()116 private void InitArticleExistence() 117 throws java.io.IOException, WikiCancelException 118 { 119 if ( m_aSession != null ) 120 { 121 Document aResult = m_aSession.CallAPI( new String[][] { 122 { "action", "query" }, { "prop", "info" }, { "titles", m_sTitle } } ); 123 Element aPage = WikiSession.GetElement( aResult, "page" ); 124 m_bNoArticle = ( aPage == null || aPage.hasAttribute( "missing" ) || aPage.hasAttribute( "invalid" ) ); 125 } 126 } 127 setArticle( String sWikiCode, String sWikiComment, boolean bMinorEdit )128 protected boolean setArticle( String sWikiCode, String sWikiComment, boolean bMinorEdit ) 129 throws java.io.IOException, WikiCancelException 130 { 131 boolean bResult = false; 132 133 if ( m_aSession != null && sWikiCode != null && sWikiComment != null ) 134 { 135 String sToken = m_aSession.GetToken( "csrf" ); 136 if ( sToken == null ) 137 return false; 138 139 Document aResult = m_aSession.CallAPI( new String[][] { 140 { "action", "edit" }, 141 { "title", m_sTitle }, 142 { "text", sWikiCode }, 143 { "summary", sWikiComment }, 144 { bMinorEdit ? "minor" : "notminor", "1" }, 145 // fail instead of silently saving as an anonymous edit if the session was lost 146 { "assert", "user" }, 147 // MediaWiki recommends sending the token last 148 { "token", sToken } } ); 149 150 Element aEdit = WikiSession.GetElement( aResult, "edit" ); 151 bResult = ( aEdit != null && "Success".equals( aEdit.getAttribute( "result" ) ) ); 152 } 153 154 return bResult; 155 } 156 Login()157 protected boolean Login() 158 throws java.io.IOException, WikiCancelException 159 { 160 m_aSession = Helper.Login( m_aMainURL, m_sWikiUser, m_sWikiPass, m_xContext ); 161 return ( m_aSession != null ); 162 } 163 NotExist()164 protected boolean NotExist() 165 { 166 return m_bNoArticle; 167 } 168 } 169