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