in jspwiki-main/src/main/java/org/apache/wiki/plugin/InsertPage.java [86:210]
public String execute( final Context context, final Map<String, String> params ) throws PluginException {
final Engine engine = context.getEngine();
final StringBuilder res = new StringBuilder();
final String clazz = TextUtil.replaceEntities(params.get( PARAM_CLASS ));
final String includedPage = TextUtil.replaceEntities(params.get( PARAM_PAGENAME ));
String style = TextUtil.replaceEntities(params.get( PARAM_STYLE ));
final boolean showOnce = "once".equals( params.get( PARAM_SHOW ) );
final String defaultstr = params.get( PARAM_DEFAULT );
final int section = TextUtil.parseIntParameter(params.get( PARAM_SECTION ), -1 );
int maxlen = TextUtil.parseIntParameter(params.get( PARAM_MAXLENGTH ), -1 );
final ResourceBundle rb = Preferences.getBundle( context, Plugin.CORE_PLUGINS_RESOURCEBUNDLE );
if( style == null ) {
style = DEFAULT_STYLE;
}
if( maxlen == -1 ) {
maxlen = Integer.MAX_VALUE;
}
if( includedPage != null ) {
final Page page;
try {
final String pageName = engine.getFinalPageName( includedPage );
page = engine.getManager(PageManager.class).getPage(Objects.requireNonNullElse(pageName, includedPage));
} catch( final ProviderException e ) {
res.append( "<span class=\"error\">Page could not be found by the page provider.</span>" );
return res.toString();
}
if( page != null ) {
// Check for recursivity
List<String> previousIncludes = context.getVariable( ATTR_RECURSE );
if( previousIncludes != null ) {
if( previousIncludes.contains( page.getName() ) ) {
return "<span class=\"error\">Error: Circular reference - you can't include a page in itself!</span>";
}
} else {
previousIncludes = new ArrayList<>();
}
// Check for permissions
final AuthorizationManager mgr = engine.getManager( AuthorizationManager.class );
if( !mgr.checkPermission( context.getWikiSession(), PermissionFactory.getPagePermission( page, "view") ) ) {
res.append("<span class=\"error\">You do not have permission to view this included page.</span>");
return res.toString();
}
// Show Once
// Check for page-cookie, only include page if cookie is not yet set
String cookieName = "";
if( showOnce ) {
cookieName = ONCE_COOKIE + TextUtil.urlEncodeUTF8( page.getName() ).replaceAll( "\\+", "%20" );
if( HttpUtil.retrieveCookieValue( context.getHttpRequest(), cookieName ) != null ) {
return ""; //silent exit
}
}
// move here, after premature exit points (permissions, page-cookie)
previousIncludes.add( page.getName() );
context.setVariable( ATTR_RECURSE, previousIncludes );
/**
* We want inclusion to occur within the context of
* its own page, because we need the links to be correct.
*/
final Context includedContext = context.clone();
includedContext.setPage( page );
String pageData = engine.getManager( PageManager.class ).getPureText( page );
String moreLink = "";
if( section != -1 ) {
try {
pageData = TextUtil.getSection( pageData, section );
} catch( final IllegalArgumentException e ) {
throw new PluginException( e.getMessage() );
}
}
if( pageData.length() > maxlen ) {
pageData = pageData.substring( 0, maxlen )+" ...";
moreLink = "<p><a href=\""+context.getURL( ContextEnum.PAGE_VIEW.getRequestContext(),includedPage)+"\">"+rb.getString("insertpage.more")+"</a></p>";
}
res.append("<div class=\"inserted-page ");
if( clazz != null ) res.append( clazz );
if( !style.equals(DEFAULT_STYLE) ) res.append( "\" style=\"" ).append( style );
if( showOnce ) res.append( "\" data-once=\"" ).append( cookieName );
res.append("\" >");
res.append( engine.getManager( RenderingManager.class ).textToHTML( includedContext, pageData ) );
res.append( moreLink );
res.append("</div>");
//
// Remove the name from the stack; we're now done with this.
//
previousIncludes.remove( page.getName() );
context.setVariable( ATTR_RECURSE, previousIncludes );
} else {
if( defaultstr != null ) {
res.append( defaultstr );
} else {
res.append( "There is no page called '" ).append( includedPage ).append( "'. Would you like to " );
res.append( "<a href=\"" ).append( context.getURL( ContextEnum.PAGE_EDIT.getRequestContext(), includedPage ) ).append( "\">create it?</a>" );
}
}
} else {
res.append( "<span class=\"error\">" );
res.append( "You have to define a page!" );
res.append( "</span>" );
}
return res.toString();
}