1*13efc523SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*13efc523SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*13efc523SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*13efc523SAndrew Rist  * distributed with this work for additional information
6*13efc523SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*13efc523SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*13efc523SAndrew Rist  * "License"); you may not use this file except in compliance
9*13efc523SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*13efc523SAndrew Rist  *
11*13efc523SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*13efc523SAndrew Rist  *
13*13efc523SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*13efc523SAndrew Rist  * software distributed under the License is distributed on an
15*13efc523SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*13efc523SAndrew Rist  * KIND, either express or implied.  See the License for the
17*13efc523SAndrew Rist  * specific language governing permissions and limitations
18*13efc523SAndrew Rist  * under the License.
19*13efc523SAndrew Rist  *
20*13efc523SAndrew Rist  *************************************************************/
21*13efc523SAndrew Rist 
22*13efc523SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package com.sun.star.wiki;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import com.sun.star.task.UrlRecord;
27cdf0e10cSrcweir import java.io.*;
28cdf0e10cSrcweir import java.util.Hashtable;
29cdf0e10cSrcweir import javax.swing.text.html.*;
30cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
31cdf0e10cSrcweir 
32cdf0e10cSrcweir import org.apache.commons.httpclient.*;
33cdf0e10cSrcweir import org.apache.commons.httpclient.methods.*;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir 
36cdf0e10cSrcweir public class WikiArticle
37cdf0e10cSrcweir {
38cdf0e10cSrcweir     private XComponentContext m_xContext;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir     private String m_sEditTime = "";
41cdf0e10cSrcweir     private String m_sEditToken = "";
42cdf0e10cSrcweir 
43cdf0e10cSrcweir     protected String m_sHTMLCode;
44cdf0e10cSrcweir     private boolean m_bNoArticle = true;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir     protected String m_sWikiUser;
47cdf0e10cSrcweir     protected String m_sWikiPass;
48cdf0e10cSrcweir 
49cdf0e10cSrcweir     protected String m_sTitle = "";
50cdf0e10cSrcweir 
51cdf0e10cSrcweir     private URI m_aMainURI;
52cdf0e10cSrcweir     private HostConfiguration m_aHostConfig;
53cdf0e10cSrcweir 
54cdf0e10cSrcweir 
55cdf0e10cSrcweir     /** Creates a new instance of WikiArticle */
WikiArticle( XComponentContext xContext, String sTitle, Hashtable wikiSettings, boolean bLogin, WikiPropDialog aPropDialog )56cdf0e10cSrcweir     public WikiArticle( XComponentContext xContext, String sTitle, Hashtable wikiSettings, boolean bLogin, WikiPropDialog aPropDialog )
57cdf0e10cSrcweir         throws java.net.MalformedURLException, com.sun.star.uno.Exception, java.io.IOException, WikiCancelException
58cdf0e10cSrcweir     {
59cdf0e10cSrcweir         m_xContext = xContext;
60cdf0e10cSrcweir 
61cdf0e10cSrcweir         String sMainUrl = (String) wikiSettings.get("Url");
62cdf0e10cSrcweir         m_sWikiUser = (String) wikiSettings.get("Username");
63cdf0e10cSrcweir         m_sWikiPass = (String) wikiSettings.get("Password");
64cdf0e10cSrcweir         m_sTitle = sTitle;
65cdf0e10cSrcweir 
66cdf0e10cSrcweir         m_aMainURI = new URI( sMainUrl );
67cdf0e10cSrcweir 
68cdf0e10cSrcweir //         viewURL = sMainUrl + "index.php?title=" + m_sTitle;
69cdf0e10cSrcweir //         editURL = sMainUrl + "index.php?title=" + m_sTitle + "&action=edit";
70cdf0e10cSrcweir //         submitURL = sMainUrl + "index.php?title=" + m_sTitle + "&action=submit";
71cdf0e10cSrcweir //         loginURL = sMainUrl + "index.php?title=Special:Userlogin";
72cdf0e10cSrcweir //         loginSubmitURL = sMainUrl + "index.php?title=Special:Userlogin&action=submitlogin";
73cdf0e10cSrcweir 
74cdf0e10cSrcweir         if ( bLogin )
75cdf0e10cSrcweir         {
76cdf0e10cSrcweir             WikiEditSettingDialog aDialog = new WikiEditSettingDialog(m_xContext, "vnd.sun.star.script:WikiEditor.EditSetting?location=application", wikiSettings, false );
77cdf0e10cSrcweir             try
78cdf0e10cSrcweir             {
79cdf0e10cSrcweir                 while( !Login() )
80cdf0e10cSrcweir                 {
81cdf0e10cSrcweir                     if ( aPropDialog != null )
82cdf0e10cSrcweir                         aPropDialog.SetThrobberActive( false );
83cdf0e10cSrcweir 
84cdf0e10cSrcweir                     if ( MainThreadDialogExecutor.Show( xContext, aDialog ) )
85cdf0e10cSrcweir                     {
86cdf0e10cSrcweir                         m_sWikiUser = (String) wikiSettings.get("Username");
87cdf0e10cSrcweir                         m_sWikiPass = (String) wikiSettings.get("Password");
88cdf0e10cSrcweir                     }
89cdf0e10cSrcweir                     else
90cdf0e10cSrcweir                         throw new WikiCancelException();
91cdf0e10cSrcweir 
92cdf0e10cSrcweir                     if ( aPropDialog != null )
93cdf0e10cSrcweir                     {
94cdf0e10cSrcweir                         aPropDialog.SetThrobberActive( true );
95cdf0e10cSrcweir                         Thread.yield();
96cdf0e10cSrcweir                     }
97cdf0e10cSrcweir                 }
98cdf0e10cSrcweir             }
99cdf0e10cSrcweir             finally
100cdf0e10cSrcweir             {
101cdf0e10cSrcweir                 aDialog.DisposeDialog();
102cdf0e10cSrcweir             }
103cdf0e10cSrcweir         }
104cdf0e10cSrcweir 
105cdf0e10cSrcweir         // in case of loading the html contents are used
106cdf0e10cSrcweir         // in case of saving the contents should be checked whether they are empty
107cdf0e10cSrcweir         InitArticleHTML();
108cdf0e10cSrcweir 
109cdf0e10cSrcweir         // getArticleWiki();
110cdf0e10cSrcweir     }
111cdf0e10cSrcweir 
GetMainURL()112cdf0e10cSrcweir     public String GetMainURL()
113cdf0e10cSrcweir     {
114cdf0e10cSrcweir         return m_aMainURI.toString();
115cdf0e10cSrcweir     }
116cdf0e10cSrcweir 
GetTitle()117cdf0e10cSrcweir     public String GetTitle()
118cdf0e10cSrcweir     {
119cdf0e10cSrcweir         return m_sTitle;
120cdf0e10cSrcweir     }
121cdf0e10cSrcweir 
GetViewURL()122cdf0e10cSrcweir     public String GetViewURL()
123cdf0e10cSrcweir     {
124cdf0e10cSrcweir         return m_aMainURI.toString() + "index.php?title=" + m_sTitle;
125cdf0e10cSrcweir     }
126cdf0e10cSrcweir 
getArticleWiki()127cdf0e10cSrcweir     private String getArticleWiki()
128cdf0e10cSrcweir         throws com.sun.star.uno.Exception, java.io.IOException, WikiCancelException
129cdf0e10cSrcweir     {
130cdf0e10cSrcweir         String sWikiCode = null;
131cdf0e10cSrcweir 
132cdf0e10cSrcweir         if ( m_aHostConfig != null )
133cdf0e10cSrcweir         {
134cdf0e10cSrcweir             URI aURI = new URI( m_aMainURI.toString() + "index.php?title=" + m_sTitle + "&action=edit" );
135cdf0e10cSrcweir             GetMethod aRequest = new GetMethod( aURI.getEscapedPathQuery() );
136cdf0e10cSrcweir 
137cdf0e10cSrcweir             Helper.ExecuteMethod( aRequest, m_aHostConfig, aURI, m_xContext, false );
138cdf0e10cSrcweir 
139cdf0e10cSrcweir             int nResultCode = aRequest.getStatusCode();
140cdf0e10cSrcweir             String sWebPage = null;
141cdf0e10cSrcweir             if ( nResultCode == 200 )
142cdf0e10cSrcweir                 sWebPage = aRequest.getResponseBodyAsString();
143cdf0e10cSrcweir 
144cdf0e10cSrcweir             aRequest.releaseConnection();
145cdf0e10cSrcweir 
146cdf0e10cSrcweir             if ( sWebPage != null )
147cdf0e10cSrcweir             {
148cdf0e10cSrcweir                 StringReader r = new StringReader(sWebPage);
149cdf0e10cSrcweir                 HTMLEditorKit.Parser parse = Helper.GetHTMLParser();
150cdf0e10cSrcweir                 EditPageParser callback = new EditPageParser();
151cdf0e10cSrcweir 
152cdf0e10cSrcweir                 parse.parse(r,callback,true);
153cdf0e10cSrcweir                 m_sEditTime = callback.m_sEditTime;
154cdf0e10cSrcweir                 m_sEditToken = callback.m_sEditToken;
155cdf0e10cSrcweir 
156cdf0e10cSrcweir                 int iPosStart = callback.m_nWikiArticleStart;
157cdf0e10cSrcweir                 int iPosEnd = callback.m_nWikiArticleEnd;
158cdf0e10cSrcweir 
159cdf0e10cSrcweir                 if ( iPosStart >= 0 && iPosEnd > 0 )
160cdf0e10cSrcweir                 {
161cdf0e10cSrcweir                     String sArticle = sWebPage.substring(iPosStart, iPosEnd);
162cdf0e10cSrcweir                     iPosStart = sArticle.indexOf(">") + 1;
163cdf0e10cSrcweir                     sWikiCode = sArticle.substring( iPosStart, sArticle.length() );
164cdf0e10cSrcweir                 }
165cdf0e10cSrcweir             }
166cdf0e10cSrcweir         }
167cdf0e10cSrcweir 
168cdf0e10cSrcweir         return sWikiCode;
169cdf0e10cSrcweir     }
170cdf0e10cSrcweir 
InitArticleHTML()171cdf0e10cSrcweir     private void InitArticleHTML()
172cdf0e10cSrcweir         throws com.sun.star.uno.Exception, java.io.IOException, WikiCancelException
173cdf0e10cSrcweir     {
174cdf0e10cSrcweir         if ( m_aHostConfig != null )
175cdf0e10cSrcweir         {
176cdf0e10cSrcweir             URI aURI = new URI( m_aMainURI.toString() + "index.php?title=" + m_sTitle );
177cdf0e10cSrcweir             GetMethod aRequest = new GetMethod( aURI.getEscapedPathQuery() );
178cdf0e10cSrcweir 
179cdf0e10cSrcweir             Helper.ExecuteMethod( aRequest, m_aHostConfig, aURI, m_xContext, false );
180cdf0e10cSrcweir 
181cdf0e10cSrcweir             int nResultCode = aRequest.getStatusCode();
182cdf0e10cSrcweir             String sWebPage = null;
183cdf0e10cSrcweir             if ( nResultCode == 200 )
184cdf0e10cSrcweir                 sWebPage = aRequest.getResponseBodyAsString();
185cdf0e10cSrcweir 
186cdf0e10cSrcweir             if ( sWebPage != null )
187cdf0e10cSrcweir             {
188cdf0e10cSrcweir                 StringReader r = new StringReader(sWebPage);
189cdf0e10cSrcweir                 HTMLEditorKit.Parser parse = Helper.GetHTMLParser();
190cdf0e10cSrcweir                 EditPageParser callback = new EditPageParser();
191cdf0e10cSrcweir 
192cdf0e10cSrcweir                 parse.parse(r,callback,true);
193cdf0e10cSrcweir 
194cdf0e10cSrcweir                 int iPosStart = callback.m_nHTMLArticleStart;
195cdf0e10cSrcweir                 int iPosEnd = callback.m_nHTMLArticleEnd;
196cdf0e10cSrcweir                 int nPosNoArt = callback.m_nNoArticleInd;
197cdf0e10cSrcweir 
198cdf0e10cSrcweir                 if ( iPosStart >= 0 && iPosEnd > 0 )
199cdf0e10cSrcweir                 {
200cdf0e10cSrcweir                     m_sHTMLCode = sWebPage.substring(iPosStart, iPosEnd);
201cdf0e10cSrcweir                     m_bNoArticle = ( nPosNoArt >= 0 && nPosNoArt >= iPosStart && nPosNoArt <= iPosEnd );
202cdf0e10cSrcweir                 }
203cdf0e10cSrcweir             }
204cdf0e10cSrcweir         }
205cdf0e10cSrcweir     }
206cdf0e10cSrcweir 
setArticle( String sWikiCode, String sWikiComment, boolean bMinorEdit )207cdf0e10cSrcweir     protected boolean setArticle( String sWikiCode, String sWikiComment, boolean bMinorEdit )
208cdf0e10cSrcweir         throws com.sun.star.uno.Exception, java.io.IOException, WikiCancelException
209cdf0e10cSrcweir     {
210cdf0e10cSrcweir         boolean bResult = false;
211cdf0e10cSrcweir 
212cdf0e10cSrcweir         if ( m_aHostConfig != null && sWikiCode != null && sWikiComment != null )
213cdf0e10cSrcweir         {
214cdf0e10cSrcweir             // get the edit time and token
215cdf0e10cSrcweir             getArticleWiki();
216cdf0e10cSrcweir 
217cdf0e10cSrcweir             URI aURI = new URI( m_aMainURI.getPath() + "index.php?title=" + m_sTitle + "&action=submit" );
218cdf0e10cSrcweir             PostMethod aPost = new PostMethod();
219cdf0e10cSrcweir             aPost.setPath( aURI.getEscapedPathQuery() );
220cdf0e10cSrcweir 
221cdf0e10cSrcweir             // aPost.addParameter( "title", m_sTitle );
222cdf0e10cSrcweir             // aPost.addParameter( "action", "submit" );
223cdf0e10cSrcweir             aPost.addParameter( "wpTextbox1", sWikiCode );
224cdf0e10cSrcweir             aPost.addParameter( "wpSummary", sWikiComment );
225cdf0e10cSrcweir             aPost.addParameter( "wpSection", "" );
226cdf0e10cSrcweir             aPost.addParameter( "wpEdittime", m_sEditTime );
227cdf0e10cSrcweir             aPost.addParameter( "wpSave", "Save page" );
228cdf0e10cSrcweir             aPost.addParameter( "wpEditToken", m_sEditToken );
229cdf0e10cSrcweir 
230cdf0e10cSrcweir             if ( bMinorEdit )
231cdf0e10cSrcweir                 aPost.addParameter( "wpMinoredit", "1" );
232cdf0e10cSrcweir 
233cdf0e10cSrcweir             Helper.ExecuteMethod( aPost, m_aHostConfig, aURI, m_xContext, false );
234cdf0e10cSrcweir 
235cdf0e10cSrcweir             int nResultCode = aPost.getStatusCode();
236cdf0e10cSrcweir             if ( nResultCode < 400 )
237cdf0e10cSrcweir                 bResult = true;
238cdf0e10cSrcweir 
239cdf0e10cSrcweir             String aResult = aPost.getResponseBodyAsString();
240cdf0e10cSrcweir 
241cdf0e10cSrcweir             // TODO: remove the debug printing, try to detect the error
242cdf0e10cSrcweir             System.out.print( "nSubmitCode = " + nResultCode + "\n===\n" + aResult );
243cdf0e10cSrcweir         }
244cdf0e10cSrcweir 
245cdf0e10cSrcweir         return bResult;
246cdf0e10cSrcweir     }
247cdf0e10cSrcweir 
Login()248cdf0e10cSrcweir     protected boolean Login()
249cdf0e10cSrcweir         throws com.sun.star.uno.Exception, java.io.IOException, WikiCancelException
250cdf0e10cSrcweir     {
251cdf0e10cSrcweir         m_aHostConfig = Helper.Login( m_aMainURI, m_sWikiUser, m_sWikiPass, m_xContext );
252cdf0e10cSrcweir         return ( m_aHostConfig != null );
253cdf0e10cSrcweir     }
254cdf0e10cSrcweir 
cleanHTML()255cdf0e10cSrcweir     protected void cleanHTML()
256cdf0e10cSrcweir     {
257cdf0e10cSrcweir         if ( m_sHTMLCode != null )
258cdf0e10cSrcweir         {
259cdf0e10cSrcweir             //Welcome to regex hell ;)
260cdf0e10cSrcweir 
261cdf0e10cSrcweir             //strip comments
262cdf0e10cSrcweir             m_sHTMLCode = m_sHTMLCode.replaceAll("\\<![ \\r\\n\\t]*(--([^\\-]|[\\r\\n]|-[^\\-])*--[ \\r\\n\\t]*)\\>","");
263cdf0e10cSrcweir 
264cdf0e10cSrcweir             //strip edit section links
265cdf0e10cSrcweir             m_sHTMLCode = m_sHTMLCode.replaceAll("\\<div class=\"editsection\".*?\\</div\\>","");
266cdf0e10cSrcweir 
267cdf0e10cSrcweir             //strip huge spaces
268cdf0e10cSrcweir             m_sHTMLCode = m_sHTMLCode.replaceAll("\\<p\\>\\<br /\\>[ \r\n\t]*?\\</p\\>","");
269cdf0e10cSrcweir 
270cdf0e10cSrcweir             //strip toc
271cdf0e10cSrcweir             m_sHTMLCode = m_sHTMLCode.replaceAll("\\<table.*id=\"toc\"(.|[\r\n])*?\\</table\\>","");
272cdf0e10cSrcweir 
273cdf0e10cSrcweir             //strip jump-to-nav
274cdf0e10cSrcweir             m_sHTMLCode = m_sHTMLCode.replaceAll("\\<div id=\"jump-to-nav\".*?\\</div\\>","");
275cdf0e10cSrcweir 
276cdf0e10cSrcweir             //strip Javascript
277cdf0e10cSrcweir             m_sHTMLCode = m_sHTMLCode.replaceAll("\\<script(.|[\r\n])*?\\</script\\>","");
278cdf0e10cSrcweir         }
279cdf0e10cSrcweir     }
280cdf0e10cSrcweir 
281cdf0e10cSrcweir 
NotExist()282cdf0e10cSrcweir     protected boolean NotExist()
283cdf0e10cSrcweir     {
284cdf0e10cSrcweir         boolean bResult = true;
285cdf0e10cSrcweir         if ( m_sHTMLCode != null )
286cdf0e10cSrcweir             bResult = m_bNoArticle;
287cdf0e10cSrcweir 
288cdf0e10cSrcweir         return bResult;
289cdf0e10cSrcweir     }
290cdf0e10cSrcweir 
291cdf0e10cSrcweir }
292