📄 wikiengine.java
字号:
* @since 2.0 * @param page Page name. * @return The rewritten page name, or null, if the page does not exist. */ public String getFinalPageName( String page ) { boolean isThere = simplePageExists( page ); if( !isThere && m_matchEnglishPlurals ) { if( page.endsWith("s") ) { page = page.substring( 0, page.length()-1 ); } else { page += "s"; } isThere = simplePageExists( page ); } return isThere ? page : null ; } /** * Just queries the existing pages directly from the page manager. * We also check overridden pages from jspwiki.properties */ private boolean simplePageExists( String page ) { if( getSpecialPageReference(page) != null ) return true; return m_pageManager.pageExists( page ); } /** * Turns a WikiName into something that can be * called through using an URL. * * @since 1.4.1 */ public String encodeName( String pagename ) { if( m_useUTF8 ) return TextUtil.urlEncodeUTF8( pagename ); else return java.net.URLEncoder.encode( pagename ); } public String decodeName( String pagerequest ) { if( m_useUTF8 ) return TextUtil.urlDecodeUTF8( pagerequest ); else return java.net.URLDecoder.decode( pagerequest ); } /** * Returns the IANA name of the character set encoding we're * supposed to be using right now. * * @since 1.5.3 */ public String getContentEncoding() { if( m_useUTF8 ) return "UTF-8"; return "ISO-8859-1"; } /** * Returns the unconverted text of a page. * * @param page WikiName of the page to fetch. */ public String getText( String page ) { return getText( page, WikiPageProvider.LATEST_VERSION ); } /** * Returns the unconverted text of the given version of a page, * if it exists. This method also replaces the HTML entities. * * @param page WikiName of the page to fetch * @param version Version of the page to fetch */ public String getText( String page, int version ) { String result = getPureText( page, version ); // // Replace ampersand first, or else all quotes and stuff // get replaced as well with " etc. // result = TextUtil.replaceString( result, "&", "&" ); result = TextUtil.replaceEntities( result ); return result; } /** * Returns the pure text of a page, no conversions. * * @param version If WikiPageProvider.LATEST_VERSION, then uses the * latest version. */ // FIXME: Should throw an exception on unknown page/version? public String getPureText( String page, int version ) { String result = null; try { result = m_pageManager.getPageText( page, version ); } catch( ProviderException e ) { // FIXME } finally { if( result == null ) result = ""; } return result; } /** * Returns plain text (with HTML entities replaced). This should * be the main entry point for getText(). * * @since 1.9.15. */ public String getText( WikiContext context, WikiPage page ) { return getText( page.getName(), page.getVersion() ); } /** * Returns the converted HTML of the page using a different * context than the default context. */ public String getHTML( WikiContext context, WikiPage page ) { String pagedata = null; pagedata = getPureText( page.getName(), page.getVersion() ); String res = textToHTML( context, pagedata ); return res; } /** * Returns the converted HTML of the page. * * @param page WikiName of the page to convert. */ public String getHTML( String page ) { return getHTML( page, WikiPageProvider.LATEST_VERSION ); } /** * Returns the converted HTML of the page's specific version. * The version must be a positive integer, otherwise the current * version is returned. * * @param pagename WikiName of the page to convert. * @param version Version number to fetch */ public String getHTML( String pagename, int version ) { WikiContext context = new WikiContext( this, pagename ); WikiPage page = new WikiPage( pagename ); page.setVersion( version ); String res = getHTML( context, page ); return res; } /** * Converts raw page data to HTML. * * @param pagedata Raw page data to convert to HTML */ public String textToHTML( WikiContext context, String pagedata ) { return textToHTML( context, pagedata, null, null ); } /** * Reads a WikiPageful of data from a String and returns all links * internal to this Wiki in a Collection. */ protected Collection scanWikiLinks( String pagedata ) { LinkCollector localCollector = new LinkCollector(); textToHTML( new WikiContext(this,""), pagedata, localCollector, null, localCollector ); return localCollector.getLinks(); } /** * Just convert WikiText to HTML. */ public String textToHTML( WikiContext context, String pagedata, StringTransmutator localLinkHook, StringTransmutator extLinkHook ) { return textToHTML( context, pagedata, localLinkHook, extLinkHook, null ); } /** * Helper method for combining simple textToHTML() and scanWikiLinks(). */ private String textToHTML( WikiContext context, String pagedata, StringTransmutator localLinkHook, StringTransmutator extLinkHook, StringTransmutator attLinkHook ) { String result = ""; if( pagedata == null ) { log.error("NULL pagedata to textToHTML()"); return null; } TranslatorReader in = null; Collection links = null; try { in = new TranslatorReader( context, new StringReader( pagedata ) ); in.addLocalLinkHook( localLinkHook ); in.addExternalLinkHook( extLinkHook ); in.addAttachmentLinkHook( attLinkHook ); result = FileUtil.readContents( in ); } catch( IOException e ) { log.error("Failed to scan page data: ", e); } finally { try { if( in != null ) in.close(); } catch( Exception e ) { log.fatal("Closing failed",e); } } return( result ); } /** * Updates all references for the given page. */ public void updateReferences( String pageName ) { String pageData = getPureText( pageName, WikiProvider.LATEST_VERSION ); m_referenceManager.updateReferences( pageName, scanWikiLinks( pageData ) ); } /** * Writes the WikiText of a page into the * page repository. * * @param page Page name * @param text The Wiki markup for the page. */ public void saveText( String page, String text ) { text = TextUtil.normalizePostData(text); // Hook into cross reference collection. m_referenceManager.updateReferences( page, scanWikiLinks( text ) ); try { m_pageManager.putPageText( new WikiPage(page), text ); } catch( ProviderException e ) { log.error( "Unable to put page", e ); } } /** * Retrieves the user name. It will check if user has been authenticated, * or he has a cookie with his username set. The cookie takes precedence, * which allows the wiki master to set up a site with a single master * password. Note that this behaviour will be changed in 2.1 or thereabouts. * Earlier versions than 1.9.42 preferred the authenticated name over * the cookie name. * * @param request HttpServletRequest that was called. * @return The WikiName of the user, or null, if no username was set. */ // FIXME: This is a terrible time waster, parsing the // textual data every time it is used. public String getUserName( HttpServletRequest request ) { if( request == null ) { return null; } // Get the user authentication - if he's not been authenticated // we then check from cookies. String author = request.getRemoteUser(); // // Try to fetch something from a cookie. // Cookie[] cookies = request.getCookies(); if( cookies != null ) { for( int i = 0; i < cookies.length; i++ ) { if( cookies[i].getName().equals( PREFS_COOKIE_NAME ) ) { UserProfile p = new UserProfile(cookies[i].getValue()); author = p.getName(); break; } } } // // Make sure that the author name is okay and a valid WikiName. // if( author != null ) { author = TranslatorReader.cleanLink( author ); } return author; } /** * If no author name has been set, then use the * IP address, if allowed. If no IP address is allowed, * then returns a default. * * @return Returns any sort of user name. Never returns null. */ public String getValidUserName( HttpServletRequest request ) { String user = getUserName( request ); if( user == null && m_storeIPAddress ) { user = request.getRemoteAddr(); } if( user == null ) { user = "unknown"; // FIXME: Magic } return user; } /** * @param request The HTTP Servlet request associated with this * transaction. * @since 1.5.1 */ public void saveText( String page, String text, HttpServletRequest request ) { text = TextUtil.normalizePostData(text); // Error protection or if the user info has been disabled. if( request == null || m_saveUserInfo == false ) { saveText( page, text ); } else { // Hook into cross reference collection. // Notice that this is definitely after the saveText() call above, // since it can be called externally and we only want this done once. m_referenceManager.updateReferences( page, scanWikiLinks( text ) );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -