private void addWikiTranslation(String key, String message, Locale locale) throws XWikiException {
    XWikiDocument document =
        this.oldcore
            .getMockXWiki()
            .getDocument(this.defaultWikiTranslationReference, this.oldcore.getXWikiContext());

    if (!locale.equals(Locale.ROOT)) {
      XWikiDocument translatedDocument =
          document.getTranslatedDocument(locale, this.oldcore.getXWikiContext());
      if (translatedDocument == document) {
        translatedDocument = new XWikiDocument(document.getDocumentReference(), locale);
        translatedDocument.setDefaultLocale(document.getDefaultLocale());
      }
      document = translatedDocument;
    }

    document.setSyntax(Syntax.PLAIN_1_0);

    StringBuilder builder = new StringBuilder(document.getContent());

    builder.append('\n');
    builder.append(key);
    builder.append('=');
    builder.append(message);

    document.setContent(builder.toString());

    this.oldcore.getMockXWiki().saveDocument(document, "", this.oldcore.getXWikiContext());

    setBundles(document.getFullName());
  }
Esempio n. 2
0
  protected static BaseClass getXWikiGroupRelationClass(XWikiContext context)
      throws XWikiException {
    XWikiDocument doc;
    XWiki xwiki = context.getWiki();
    boolean needsUpdate = false;

    try {
      doc = xwiki.getDocument("XWiki.GroupRelationClass", context);
    } catch (Exception e) {
      doc = new XWikiDocument();
      doc.setSpace("XWiki");
      doc.setName("GroupRelationClass");
      needsUpdate = true;
    }

    BaseClass bclass = doc.getxWikiClass();
    bclass.setName("XWiki.GroupRelationClass");
    needsUpdate |= bclass.addTextField("name", "Name", 30);
    needsUpdate |= bclass.addTextField("parentpage", "Parent", 30);
    needsUpdate |= bclass.addTextAreaField("description", "Description", 40, 5);

    String content = doc.getContent();
    if ((content == null) || (content.equals(""))) {
      needsUpdate = true;
      doc.setContent("1 XWikiGroup");
      doc.setSyntax(Syntax.XWIKI_1_0);
    }

    if (needsUpdate) xwiki.saveDocument(doc, context);
    return bclass;
  }
  private BaseClass getSubscriptionClass(XWikiContext context) throws XWikiException {
    XWikiDocument doc;
    XWiki xwiki = context.getWiki();
    boolean needsUpdate = false;

    try {
      doc = xwiki.getDocument(CelementsCalendarPlugin.SUBSCRIPTION_CLASS, context);
    } catch (Exception e) {
      doc = new XWikiDocument();
      doc.setSpace(CelementsCalendarPlugin.SUBSCRIPTION_CLASS_SPACE);
      doc.setName(CelementsCalendarPlugin.SUBSCRIPTION_CLASS_DOC);
      needsUpdate = true;
    }

    BaseClass bclass = doc.getxWikiClass();
    bclass.setName(CelementsCalendarPlugin.SUBSCRIPTION_CLASS);
    needsUpdate |= bclass.addTextField("subscriber", "subscriber", 30);
    needsUpdate |= bclass.addBooleanField("doSubscribe", "doSubscribe", "yesno");

    String content = doc.getContent();
    if ((content == null) || (content.equals(""))) {
      needsUpdate = true;
      doc.setContent(" ");
    }

    if (needsUpdate) {
      xwiki.saveDocument(doc, context);
    }
    return bclass;
  }
  @Test
  public void testOnEvent_update_ed_includeDocFields_noChange() {
    Event event = new DocumentUpdatedEvent();
    listener.includeDocFields = true;

    expect(remoteObsManContextMock.isRemoteState()).andReturn(false).once();
    expect(docMock.getXObject(eq(classRef))).andReturn(new BaseObject()).once();
    expect(origDocMock.getXObject(eq(classRef))).andReturn(new BaseObject()).once();
    expect(docMock.getTitle()).andReturn("").once();
    expect(origDocMock.getTitle()).andReturn("").once();
    expect(docMock.getContent()).andReturn("").once();
    expect(origDocMock.getContent()).andReturn("").once();

    replayDefault();
    listener.onEvent(event, docMock, context);
    verifyDefault();
  }
  public String handleMatch(MatchResult result, FilterContext context) {
    String id = null;
    String title = result.group(0);
    String level = result.group(1);
    int level_i = (level.length() + 1) / 2;
    String hlevel = (level_i <= 6 ? level_i : 6) + "";
    String text = result.group(3);
    String numbering = "";

    RenderContext rcontext = context.getRenderContext();
    XWikiContext xcontext =
        ((XWikiRadeoxRenderEngine) rcontext.getRenderEngine()).getXWikiContext();
    VelocityContext vcontext = (VelocityContext) xcontext.get("vcontext");
    XWikiDocument doc = xcontext.getDoc();

    LOGGER.debug("Processing '" + text + "'");
    // generate unique ID of the heading
    IdGenerator idGenerator = (IdGenerator) xcontext.get("headingsIdGenerator");
    if (idGenerator == null) {
      idGenerator = new IdGenerator();
      xcontext.put("headingsIdGenerator", idGenerator);
    }

    id = idGenerator.generateUniqueId("H", text);
    LOGGER.debug("Generated heading id '" + id + "'");

    // add numbering if the flag is set

    if (xcontext.containsKey(TOC_NUMBERED)
        && ((Boolean) xcontext.get(TOC_NUMBERED)).booleanValue()) {
      // This is the old place where the data was placed, but this requires programming
      // rights. Instead, we now use vcontext.
      if (xcontext.containsKey(TOC_DATA)) {
        Map tocEntry = (Map) ((Map) xcontext.get(TOC_DATA)).get(id);
        if (tocEntry != null) {
          numbering = (String) tocEntry.get(TOCGenerator.TOC_DATA_NUMBERING) + " ";
        }
      } else if (vcontext != null && vcontext.containsKey(TOC_DATA)) {
        Map tocEntry = (Map) ((Map) vcontext.get(TOC_DATA)).get(id);
        if (tocEntry != null) {
          numbering = (String) tocEntry.get(TOCGenerator.TOC_DATA_NUMBERING) + " ";
        }
      }
    }

    String heading = formatter.format(new Object[] {id, numbering, text, hlevel});

    // Only show the section edit button for view action and when the user has edit rights on
    // the current document
    boolean showEditButton = false;
    if (xcontext.getWiki().hasSectionEdit(xcontext) && ("view".equals(xcontext.getAction()))) {
      try {
        // TODO: The user should always be set and never be null when this code gets
        // executed. Unfortunately this is currently happening. It should be set to XWiki
        // Guest immediatly in the initialization phase.
        // TODO: Similarly the current document should never be null when this code gets
        // executed as it would mean we're trying to render the headings for a null
        // document and that doesn't make sense...
        if ((doc != null)
            && ((xcontext.getUser() != null)
                && xcontext
                    .getWiki()
                    .getRightService()
                    .hasAccessLevel("edit", xcontext.getUser(), doc.getFullName(), xcontext))) {
          showEditButton = true;
        }
      } catch (XWikiException e) {
        // TODO: Remove this try/catch block by removing the throw exception on
        // hasAccessLevel() as it never throws any exception...
      }
    }

    Object beforeAction = xcontext.get("action");
    if (showEditButton) {
      if (beforeAction != null) {
        if (!beforeAction.toString().equals("HeadingFilter")) {
          xcontext.put("action", "HeadingFilter");
          sectionNumber = 0;
        }
      }

      if (level.equals("1") || level.equals("1.1")) {
        // This check is needed so that only the document content generates sectionedit
        // links.
        // TODO: Find a better way to make this check, as this prevents generating links for
        // titles that are transformed by velocity (1.1 about $doc.fullName) or by radeox
        // (1.1 This is *important*).
        if (doc != null && doc.getContent().indexOf(title.trim()) != -1) {
          // TODO: This is unstable, meaning that it works in the current skin, but it might
          // fail if there are other headings processed before the document content.
          sectionNumber++;
          StringBuffer editparams = new StringBuffer();
          if (xcontext.getWiki().getEditorPreference(xcontext).equals("wysiwyg")) {
            editparams.append("xpage=wysiwyg&amp;section=").append(sectionNumber);
          } else {
            editparams.append("section=").append(sectionNumber);
          }
          try {
            if ((xcontext.getWiki().isMultiLingual(xcontext))
                && (doc.getRealLanguage(xcontext) != null)) {
              editparams.append("&amp;language=").append(doc.getRealLanguage(xcontext));
            }
          } catch (XWikiException e) {
          }

          String url = doc.getURL("edit", editparams.toString(), xcontext);
          return heading
              + "<span class='edit_section'>&#91;"
              + "<a style='text-decoration: none;' title='Edit section: "
              + text.replaceAll("'", "&#39;")
              + "' href='"
              + url
              + "'>"
              + "edit"
              + "</a>&#93;</span>";
        }
      }
    }

    return heading;
  }
  private BaseClass getCalendarClass(XWikiContext context) throws XWikiException {
    XWikiDocument doc;
    XWiki xwiki = context.getWiki();
    boolean needsUpdate = false;

    try {
      doc = xwiki.getDocument(CelementsCalendarPlugin.CLASS_CALENDAR, context);
    } catch (Exception e) {
      doc = new XWikiDocument();
      doc.setSpace(CelementsCalendarPlugin.CLASS_CALENDAR_SPACE);
      doc.setName(CelementsCalendarPlugin.CLASS_CALENDAR_DOC);
      needsUpdate = true;
    }

    BaseClass bclass = doc.getxWikiClass();
    bclass.setName(CelementsCalendarPlugin.CLASS_CALENDAR);
    needsUpdate |=
        bclass.addTextField(
            CelementsCalendarPlugin.PROPERTY_CALENDAR_SPACE,
            CelementsCalendarPlugin.PROPERTY_CALENDAR_SPACE,
            30);
    String hql = "select doc.fullName from XWikiDocument as doc, BaseObject as obj,";
    hql += " IntegerProperty as int ";
    hql += "where obj.name=doc.fullName ";
    hql += "and not doc.fullName='$doc.getFullName()' ";
    hql += "and obj.className='" + CelementsCalendarPlugin.CLASS_CALENDAR + "' ";
    hql += "and int.id.id=obj.id ";
    hql += "and int.id.name='" + CelementsCalendarPlugin.PROPERTY_IS_SUBSCRIBABLE + "' ";
    hql += "and int.value='1' ";
    hql += "order by doc.fullName asc";
    needsUpdate |=
        bclass.addDBListField(
            CelementsCalendarPlugin.PROPERTY_SUBSCRIBE_TO,
            CelementsCalendarPlugin.PROPERTY_SUBSCRIBE_TO,
            5,
            true,
            hql);
    needsUpdate |=
        bclass.addTextField(
            CelementsCalendarPlugin.PROPERTY_OVERVIEW_COLUMN_CONFIG,
            CelementsCalendarPlugin.PROPERTY_OVERVIEW_COLUMN_CONFIG,
            30);
    needsUpdate |=
        bclass.addTextField(
            CelementsCalendarPlugin.PROPERTY_EVENT_COLUMN_CONFIG,
            CelementsCalendarPlugin.PROPERTY_EVENT_COLUMN_CONFIG,
            30);
    needsUpdate |=
        bclass.addNumberField(
            CelementsCalendarPlugin.PROPERTY_EVENT_PER_PAGE,
            CelementsCalendarPlugin.PROPERTY_EVENT_PER_PAGE,
            5,
            "integer");
    needsUpdate |=
        bclass.addBooleanField(
            CelementsCalendarPlugin.PROPERTY_HAS_MORE_LINK,
            CelementsCalendarPlugin.PROPERTY_HAS_MORE_LINK,
            "yesno");
    needsUpdate |=
        bclass.addBooleanField(
            CelementsCalendarPlugin.PROPERTY_IS_SUBSCRIBABLE,
            CelementsCalendarPlugin.PROPERTY_IS_SUBSCRIBABLE,
            "yesno");

    String content = doc.getContent();
    if ((content == null) || (content.equals(""))) {
      needsUpdate = true;
      doc.setContent(" ");
    }

    if (needsUpdate) {
      xwiki.saveDocument(doc, context);
    }
    return bclass;
  }
  private BaseClass getCalendarEventClass(XWikiContext context) throws XWikiException {
    XWikiDocument doc;
    XWiki xwiki = context.getWiki();
    boolean needsUpdate = false;

    try {
      doc = xwiki.getDocument(CelementsCalendarPlugin.CLASS_EVENT, context);
    } catch (Exception e) {
      doc = new XWikiDocument();
      doc.setSpace(CelementsCalendarPlugin.CLASS_EVENT_SPACE);
      doc.setName(CelementsCalendarPlugin.CLASS_EVENT_DOC);
      needsUpdate = true;
    }

    BaseClass bclass = doc.getxWikiClass();
    bclass.setName(CelementsCalendarPlugin.CLASS_EVENT);
    needsUpdate |=
        bclass.addTextField(
            CelementsCalendarPlugin.PROPERTY_LANG, CelementsCalendarPlugin.PROPERTY_LANG, 30);
    needsUpdate |=
        bclass.addTextField(
            CelementsCalendarPlugin.PROPERTY_TITLE, CelementsCalendarPlugin.PROPERTY_TITLE, 30);
    needsUpdate |=
        bclass.addTextAreaField(
            CelementsCalendarPlugin.PROPERTY_TITLE_RTE,
            CelementsCalendarPlugin.PROPERTY_TITLE_RTE,
            80,
            15);
    needsUpdate |=
        bclass.addTextAreaField(
            CelementsCalendarPlugin.PROPERTY_DESCRIPTION,
            CelementsCalendarPlugin.PROPERTY_DESCRIPTION,
            80,
            15);
    needsUpdate |=
        bclass.addTextField(
            CelementsCalendarPlugin.PROPERTY_LOCATION,
            CelementsCalendarPlugin.PROPERTY_LOCATION,
            30);
    needsUpdate |=
        bclass.addTextAreaField(
            CelementsCalendarPlugin.PROPERTY_LOCATION_RTE,
            CelementsCalendarPlugin.PROPERTY_LOCATION_RTE,
            80,
            15);
    needsUpdate |=
        bclass.addDateField(
            CelementsCalendarPlugin.PROPERTY_EVENT_DATE,
            CelementsCalendarPlugin.PROPERTY_EVENT_DATE,
            null,
            0);
    needsUpdate |=
        bclass.addDateField(
            CelementsCalendarPlugin.PROPERTY_EVENT_DATE_END,
            CelementsCalendarPlugin.PROPERTY_EVENT_DATE_END,
            null,
            0);
    needsUpdate |=
        bclass.addBooleanField(
            CelementsCalendarPlugin.PROPERTY_EVENT_IS_SUBSCRIBABLE,
            CelementsCalendarPlugin.PROPERTY_EVENT_IS_SUBSCRIBABLE,
            "yesno");

    if (!"internal".equals(bclass.getCustomMapping())) {
      needsUpdate = true;
      bclass.setCustomMapping("internal");
    }

    String content = doc.getContent();
    if ((content == null) || (content.equals(""))) {
      needsUpdate = true;
      doc.setContent(" ");
    }

    if (needsUpdate) {
      xwiki.saveDocument(doc, context);
    }
    return bclass;
  }