use of org.apache.wiki.WikiEngine in project jspwiki by apache.
the class PageRenamer method renamePage.
/**
* Renames a page.
*
* @param context The current context.
* @param renameFrom The name from which to rename.
* @param renameTo The new name.
* @param changeReferrers If true, also changes all the referrers.
* @return The final new name (in case it had to be modified)
* @throws WikiException If the page cannot be renamed.
*/
public String renamePage(final WikiContext context, final String renameFrom, final String renameTo, final boolean changeReferrers) throws WikiException {
//
if (renameFrom == null || renameFrom.length() == 0) {
throw new WikiException("From name may not be null or empty");
}
if (renameTo == null || renameTo.length() == 0) {
throw new WikiException("To name may not be null or empty");
}
//
// Clean up the "to" -name so that it does not contain anything illegal
//
String renameToClean = MarkupParser.cleanLink(renameTo.trim());
if (renameToClean.equals(renameFrom)) {
throw new WikiException("You cannot rename the page to itself");
}
//
// Preconditions: "from" page must exist, and "to" page must not yet exist.
//
WikiEngine engine = context.getEngine();
WikiPage fromPage = engine.getPage(renameFrom);
if (fromPage == null) {
throw new WikiException("No such page " + renameFrom);
}
WikiPage toPage = engine.getPage(renameToClean);
if (toPage != null) {
throw new WikiException("Page already exists " + renameToClean);
}
//
// Options
//
m_camelCase = TextUtil.getBooleanProperty(engine.getWikiProperties(), JSPWikiMarkupParser.PROP_CAMELCASELINKS, m_camelCase);
Set<String> referrers = getReferencesToChange(fromPage, engine);
//
// Do the actual rename by changing from the frompage to the topage, including
// all of the attachments
//
// Remove references to attachments under old name
@SuppressWarnings("unchecked") Collection<Attachment> attachmentsOldName = engine.getAttachmentManager().listAttachments(fromPage);
for (Attachment att : attachmentsOldName) {
WikiPage fromAttPage = engine.getPage(att.getName());
engine.getReferenceManager().pageRemoved(fromAttPage);
}
engine.getPageManager().getProvider().movePage(renameFrom, renameToClean);
if (engine.getAttachmentManager().attachmentsEnabled()) {
engine.getAttachmentManager().getCurrentProvider().moveAttachmentsForPage(renameFrom, renameToClean);
}
//
// Add a comment to the page notifying what changed. This adds a new revision
// to the repo with no actual change.
//
toPage = engine.getPage(renameToClean);
if (toPage == null)
throw new InternalWikiException("Rename seems to have failed for some strange reason - please check logs!");
toPage.setAttribute(WikiPage.CHANGENOTE, fromPage.getName() + " ==> " + toPage.getName());
toPage.setAuthor(context.getCurrentUser().getName());
engine.getPageManager().putPageText(toPage, engine.getPureText(toPage));
//
// Update the references
//
engine.getReferenceManager().pageRemoved(fromPage);
engine.updateReferences(toPage);
//
if (changeReferrers) {
updateReferrers(context, fromPage, toPage, referrers);
}
//
// re-index the page including its attachments
//
engine.getSearchManager().reindexPage(toPage);
@SuppressWarnings("unchecked") Collection<Attachment> attachmentsNewName = engine.getAttachmentManager().listAttachments(toPage);
for (Attachment att : attachmentsNewName) {
WikiPage toAttPage = engine.getPage(att.getName());
// add reference to attachment under new page name
engine.updateReferences(toAttPage);
engine.getSearchManager().reindexPage(att);
}
// Currently not used internally by JSPWiki itself, but you can use it for something else.
WikiEventManager.fireEvent(this, new WikiPageRenameEvent(this, renameFrom, renameToClean));
//
return renameToClean;
}
use of org.apache.wiki.WikiEngine in project jspwiki by apache.
the class PageRenamer method updateReferrers.
/**
* This method finds all the pages which have anything to do with the fromPage and
* change any referrers it can figure out in that page.
*
* @param context WikiContext in which we operate
* @param fromPage The old page
* @param toPage The new page
*/
private void updateReferrers(WikiContext context, WikiPage fromPage, WikiPage toPage, Set<String> referrers) {
WikiEngine engine = context.getEngine();
// No referrers
if (referrers.isEmpty())
return;
for (String pageName : referrers) {
// small kludge.
if (pageName.equals(fromPage.getName())) {
pageName = toPage.getName();
}
WikiPage p = engine.getPage(pageName);
String sourceText = engine.getPureText(p);
String newText = replaceReferrerString(context, sourceText, fromPage.getName(), toPage.getName());
if (m_camelCase)
newText = replaceCCReferrerString(context, newText, fromPage.getName(), toPage.getName());
if (!sourceText.equals(newText)) {
p.setAttribute(WikiPage.CHANGENOTE, fromPage.getName() + " ==> " + toPage.getName());
p.setAuthor(context.getCurrentUser().getName());
try {
engine.getPageManager().putPageText(p, newText);
engine.updateReferences(p);
} catch (ProviderException e) {
//
// We fail with an error, but we will try to continue to rename
// other referrers as well.
//
log.error("Unable to perform rename.", e);
}
}
}
}
use of org.apache.wiki.WikiEngine in project jspwiki by apache.
the class CookieAuthenticationLoginModule method login.
/**
* @see javax.security.auth.spi.LoginModule#login()
*
* {@inheritDoc}
*/
public boolean login() throws LoginException {
// Otherwise, let's go and look for the cookie!
HttpRequestCallback hcb = new HttpRequestCallback();
// UserDatabaseCallback ucb = new UserDatabaseCallback();
WikiEngineCallback wcb = new WikiEngineCallback();
Callback[] callbacks = new Callback[] { hcb, wcb };
try {
m_handler.handle(callbacks);
HttpServletRequest request = hcb.getRequest();
String uid = getLoginCookie(request);
if (uid != null) {
WikiEngine engine = wcb.getEngine();
File cookieFile = getCookieFile(engine, uid);
if (cookieFile != null && cookieFile.exists() && cookieFile.canRead()) {
Reader in = null;
try {
in = new BufferedReader(new InputStreamReader(new FileInputStream(cookieFile), "UTF-8"));
String username = FileUtil.readContents(in);
if (log.isDebugEnabled()) {
log.debug("Logged in cookie authenticated name=" + username);
}
// If login succeeds, commit these principals/roles
m_principals.add(new WikiPrincipal(username, WikiPrincipal.LOGIN_NAME));
//
// Tag the file so that we know that it has been accessed recently.
//
cookieFile.setLastModified(System.currentTimeMillis());
return true;
} catch (IOException e) {
return false;
} finally {
if (in != null)
in.close();
}
}
}
} catch (IOException e) {
String message = "IO exception; disallowing login.";
log.error(message, e);
throw new LoginException(message);
} catch (UnsupportedCallbackException e) {
String message = "Unable to handle callback; disallowing login.";
log.error(message, e);
throw new LoginException(message);
}
return false;
}
use of org.apache.wiki.WikiEngine in project jspwiki by apache.
the class SpamFilter method getChange.
/**
* Creates a simple text string describing the added content.
*
* @param context
* @param newText
* @return Empty string, if there is no change.
*/
private static Change getChange(WikiContext context, String newText) {
WikiPage page = context.getPage();
StringBuffer change = new StringBuffer();
WikiEngine engine = context.getEngine();
// Get current page version
Change ch = new Change();
try {
String oldText = engine.getPureText(page.getName(), WikiProvider.LATEST_VERSION);
String[] first = Diff.stringToArray(oldText);
String[] second = Diff.stringToArray(newText);
Revision rev = Diff.diff(first, second, new MyersDiff());
if (rev == null || rev.size() == 0) {
return ch;
}
for (int i = 0; i < rev.size(); i++) {
Delta d = rev.getDelta(i);
if (d instanceof AddDelta) {
d.getRevised().toString(change, "", "\r\n");
ch.m_adds++;
} else if (d instanceof ChangeDelta) {
d.getRevised().toString(change, "", "\r\n");
ch.m_adds++;
} else if (d instanceof DeleteDelta) {
ch.m_removals++;
}
}
} catch (DifferentiationFailedException e) {
log.error("Diff failed", e);
}
//
// Don't forget to include the change note, too
//
String changeNote = (String) page.getAttribute(WikiPage.CHANGENOTE);
if (changeNote != null) {
change.append("\r\n");
change.append(changeNote);
}
//
if (page.getAuthor() != null) {
change.append("\r\n" + page.getAuthor());
}
ch.m_change = change.toString();
return ch;
}
use of org.apache.wiki.WikiEngine in project jspwiki by apache.
the class SpamFilter method insertInputFields.
/**
* This helper method adds all the input fields to your editor that the SpamFilter requires
* to check for spam. This <i>must</i> be in your editor form if you intend to use the SpamFilter.
*
* @param pageContext The PageContext
* @return A HTML string which contains input fields for the SpamFilter.
*/
public static final String insertInputFields(PageContext pageContext) {
WikiContext ctx = WikiContext.findContext(pageContext);
WikiEngine engine = ctx.getEngine();
StringBuilder sb = new StringBuilder();
if (engine.getContentEncoding().equals("UTF-8")) {
sb.append("<input name='encodingcheck' type='hidden' value='\u3041' />\n");
}
return sb.toString();
}
Aggregations