Пример #1
0
  /**
   * Attempts to transfer an enwp file to Commons
   *
   * @return True on success.
   */
  protected boolean doTransfer() {
    try {
      root = ParsedItem.parse(enwp, wpFN);

      imgInfoL = enwp.getImageInfo(wpFN);
      uploader = imgInfoL.get(imgInfoL.size() - 1).user;

      procText();
      String t = gen();

      if (mtc.dryRun) {
        System.out.println(t);
        return true;
      } else if (t != null
          && Toolbox.downloadFile(imgInfoL.get(0).url, localFN)
          && com.upload(Paths.get(localFN), comFN, t, String.format(Config.tFrom, wpFN)))
        return enwp.edit(
            wpFN,
            String.format("{{subst:ncd|%s|reviewer=%s}}%n", comFN, enwp.whoami())
                + enwp.getPageText(wpFN).replaceAll(mtc.mtcRegex, ""),
            Config.tTo);
    } catch (Throwable e) {
      e.printStackTrace();
    }

    return false;
  }
Пример #2
0
  /** Processes parsed text and templates from the API */
  private void procText() {
    ArrayList<Template> masterTPL = root.getTemplateR();

    // Normalize license and special template titles
    for (Template t : masterTPL) {
      String tp = enwp.whichNS(t.title).equals(NS.TEMPLATE) ? enwp.nss(t.title) : t.title;
      if (mtc.tpMap.containsKey(tp)) t.title = mtc.tpMap.get(tp);
    }

    ArrayList<Template> tpl = new ArrayList<>(masterTPL);

    // Filter Templates which are not on Commons
    HashSet<String> ncomT =
        FL.toSet(
            MQuery.exists(com, false, FL.toAL(tpl.stream().map(t -> "Template:" + t.title)))
                .stream()
                .map(com::nss));
    for (Template t : new ArrayList<>(tpl)) if (ncomT.contains(t.title)) tpl.remove(t.drop());

    // Process special Templates
    Template info = null;
    for (Template t : new ArrayList<>(tpl))
      switch (t.title) {
        case "Information":
          info = t;
          tpl.remove(t.drop());
          break;
        case "Self":
          if (!t.has("author"))
            t.put("author", String.format("{{User at project|%s|w|en}}", uploader));
          break;
        case "PD-self":
          t.title = "PD-user-en";
          t.put("1", uploader);
          break;
        case "GFDL-self-with-disclaimers":
          t.title = "GFDL-user-en-with-disclaimers";
          t.put("1", uploader);
          break;
        case "GFDL-self":
          t.title = "GFDL-self-en";
          t.put("author", String.format("{{User at project|%s|w|en}}", uploader));
          break;
        case "Copy to Wikimedia Commons":
          tpl.remove(t.drop());
          break;
        default:
          break;
      }

    // Add any Commons-compatible top-level templates to License section.
    tpl.retainAll(root.tplates);
    for (Template t : tpl) licSection += String.format("%s%n", t);

    // Create and fill in missing {{Information}} fields with default values.
    info = filterFillInfo(info == null ? new Template("Information") : info);

    // Append any additional Strings to the description.
    if (!root.contents.isEmpty())
      info.append("Description", "\n" + String.join("\n", root.contents));

    // Convert {{Information}} to String and save result.
    sumSection += info.toString(true) + "\n";

    // Extract the first caption table and move it to the end of the sumSection
    String x = "";
    Matcher m = captionRegex.matcher(sumSection);
    if (m.find()) {
      x = m.group().trim();
      sumSection = m.reset().replaceAll("");
      sumSection += x + "\n";
    }
  }