info.bliki.wiki.template.Ifexist Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bliki-core Show documentation
Show all versions of bliki-core Show documentation
This is the core project.
The newest version!
package info.bliki.wiki.template;
import info.bliki.wiki.filter.ParsedPageName;
import info.bliki.wiki.model.IWikiModel;
import info.bliki.wiki.model.WikiModelContentException;
import info.bliki.wiki.namespaces.INamespace;
import java.util.List;
/**
* A template parser function for {{ #ifexist: ... }}
syntax. See
*
* Mediwiki's Help:Extension:ParserFunctions
*
*/
public class Ifexist extends AbstractTemplateFunction {
public final static ITemplateFunction CONST = new Ifexist();
@Override
public String parseFunction(List list, IWikiModel model, char[] src, int beginIndex, int endIndex, boolean isSubst) {
if (list.size() > 1) {
String wikiTopicName = isSubst ? list.get(0) : parseTrim(list.get(0), model);
final INamespace namespace = model.getNamespace();
// note: appended "#section" does not count for the check whether a page
// exists or not!
// -> strip off
ParsedPageName parsedPagename = ParsedPageName.parsePageName(model, wikiTopicName, namespace.getMain(), false, true);
String rawWikiContent = null;
// if parsing failed, e.g. double "::" at the page titles beginning, this
// is the same as if the page does not exist.
if (parsedPagename.valid) {
try {
rawWikiContent = model.getRawWikiContent(parsedPagename, null);
} catch (WikiModelContentException e) {
// the requested templateName doesn't exist
}
}
if (rawWikiContent != null) {
return isSubst ? list.get(1) : parseTrim(list.get(1), model);
} else {
// the requested templateName doesn't exist
if (list.size() >= 3) {
return isSubst ? list.get(2) : parseTrim(list.get(2), model);
}
}
}
return null;
}
}