use of org.apache.wiki.api.core.Page in project jspwiki by apache.
the class PageRenamerTest method rename.
private void rename(final String src, final String dst) throws WikiException {
final Page p = m_engine.getManager(PageManager.class).getPage(src);
final Context context = Wiki.context().create(m_engine, p);
m_engine.getManager(PageRenamer.class).renamePage(context, src, dst, true);
}
use of org.apache.wiki.api.core.Page in project jspwiki by apache.
the class PageRenamerTest method testReferrerChangeMultiRename.
@Test
public void testReferrerChangeMultiRename() throws Exception {
m_engine.saveText("TestPage", "foofoo");
m_engine.saveText("TestPage2", "[TestPage]");
final Page p = m_engine.getManager(PageManager.class).getPage("TestPage");
final Context context = Wiki.context().create(m_engine, p);
m_engine.getManager(PageRenamer.class).renamePage(context, "TestPage", "FooTest", true);
m_engine.getManager(PageRenamer.class).renamePage(context, "FooTest", "BarTest", true);
final String data = m_engine.getManager(PageManager.class).getPureText("TestPage2", WikiProvider.LATEST_VERSION);
Assertions.assertEquals("[BarTest]", data.trim(), "no rename");
Collection<String> refs = m_engine.getManager(ReferenceManager.class).findReferrers("TestPage");
Assertions.assertNull(refs, "oldpage");
refs = m_engine.getManager(ReferenceManager.class).findReferrers("FooPage");
Assertions.assertNull(refs, "oldpage");
refs = m_engine.getManager(ReferenceManager.class).findReferrers("BarTest");
Assertions.assertEquals(1, refs.size(), "new size");
Assertions.assertEquals("TestPage2", refs.iterator().next(), "wrong ref");
}
use of org.apache.wiki.api.core.Page in project jspwiki by apache.
the class DefaultPageManagerTest method testDeletePageAndAttachments2.
@Test
public void testDeletePageAndAttachments2() throws Exception {
engine.saveText(NAME1, "Test");
Attachment att = Wiki.contents().attachment(engine, NAME1, "TestAtt.txt");
att.setAuthor("FirstPost");
engine.getManager(AttachmentManager.class).storeAttachment(att, engine.makeAttachmentFile());
final String files = engine.getWikiProperties().getProperty(FileSystemProvider.PROP_PAGEDIR);
final File saved = new File(files, NAME1 + FileSystemProvider.FILE_EXT);
final String atts = engine.getWikiProperties().getProperty(AttachmentProvider.PROP_STORAGEDIR);
final File attfile = new File(atts, NAME1 + "-att/TestAtt.txt-dir");
Assertions.assertTrue(saved.exists(), "Didn't create it!");
Assertions.assertTrue(attfile.exists(), "Attachment dir does not exist");
final Page page = engine.getManager(PageManager.class).getPage(NAME1, WikiProvider.LATEST_VERSION);
Assertions.assertNotNull(page, "page");
att = engine.getManager(AttachmentManager.class).getAttachmentInfo(NAME1 + "/TestAtt.txt");
engine.getManager(PageManager.class).deletePage(att.getName());
engine.getManager(PageManager.class).deletePage(NAME1);
Assertions.assertNull(engine.getManager(PageManager.class).getPage(NAME1), "Page not removed");
Assertions.assertNull(engine.getManager(PageManager.class).getPage(NAME1 + "/TestAtt.txt"), "Att not removed");
final Collection<String> refs = engine.getManager(ReferenceManager.class).findReferrers(NAME1);
Assertions.assertNull(refs, "referrers");
}
use of org.apache.wiki.api.core.Page in project jspwiki by apache.
the class DefaultPageManagerTest method testNonExistentPage2.
/**
* Check that calling pageExists( WikiPage ) works.
*/
@Test
public void testNonExistentPage2() throws Exception {
final Page page = Wiki.contents().page(engine, NAME1);
Assertions.assertFalse(engine.getManager(PageManager.class).wikiPageExists(page), "Page already exists");
}
use of org.apache.wiki.api.core.Page in project jspwiki by apache.
the class DefaultPageRenamer 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.
*/
@Override
public String renamePage(final Context context, final String renameFrom, final String renameTo, final boolean changeReferrers) throws WikiException {
// Sanity checks first
if (renameFrom == null || renameFrom.isEmpty()) {
throw new WikiException("From name may not be null or empty");
}
if (renameTo == null || renameTo.isEmpty()) {
throw new WikiException("To name may not be null or empty");
}
// Clean up the "to" -name so that it does not contain anything illegal
final 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.
final Engine engine = context.getEngine();
final Page fromPage = engine.getManager(PageManager.class).getPage(renameFrom);
if (fromPage == null) {
throw new WikiException("No such page " + renameFrom);
}
Page toPage = engine.getManager(PageManager.class).getPage(renameToClean);
if (toPage != null) {
throw new WikiException("Page already exists " + renameToClean);
}
final Set<String> referrers = getReferencesToChange(fromPage, engine);
// Do the actual rename by changing from the frompage to the topage, including all the attachments
// Remove references to attachments under old name
final List<Attachment> attachmentsOldName = engine.getManager(AttachmentManager.class).listAttachments(fromPage);
for (final Attachment att : attachmentsOldName) {
final Page fromAttPage = engine.getManager(PageManager.class).getPage(att.getName());
engine.getManager(ReferenceManager.class).pageRemoved(fromAttPage);
}
engine.getManager(PageManager.class).getProvider().movePage(renameFrom, renameToClean);
if (engine.getManager(AttachmentManager.class).attachmentsEnabled()) {
engine.getManager(AttachmentManager.class).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.getManager(PageManager.class).getPage(renameToClean);
if (toPage == null) {
throw new ProviderException("Rename seems to have failed for some strange reason - please check logs!");
}
toPage.setAttribute(Page.CHANGENOTE, fromPage.getName() + " ==> " + toPage.getName());
toPage.setAuthor(context.getCurrentUser().getName());
engine.getManager(PageManager.class).putPageText(toPage, engine.getManager(PageManager.class).getPureText(toPage));
// Update the references
engine.getManager(ReferenceManager.class).pageRemoved(fromPage);
engine.getManager(ReferenceManager.class).updateReferences(toPage);
// Update referrers
if (changeReferrers) {
updateReferrers(context, fromPage, toPage, referrers);
}
// re-index the page including its attachments
engine.getManager(SearchManager.class).reindexPage(toPage);
final Collection<Attachment> attachmentsNewName = engine.getManager(AttachmentManager.class).listAttachments(toPage);
for (final Attachment att : attachmentsNewName) {
final Page toAttPage = engine.getManager(PageManager.class).getPage(att.getName());
// add reference to attachment under new page name
engine.getManager(ReferenceManager.class).updateReferences(toAttPage);
engine.getManager(SearchManager.class).reindexPage(att);
}
firePageRenameEvent(renameFrom, renameToClean);
// Done, return the new name.
return renameToClean;
}
Aggregations