Package org.wikipediacleaner.api.constants

Examples of org.wikipediacleaner.api.constants.WikiConfiguration


  public void executeSiteInformation(
      Map<String, String> properties)
          throws APIException {
    try {
      Element root = getRoot(properties, ApiRequest.MAX_ATTEMPTS);
      WikiConfiguration wikiConfiguration = getWiki().getWikiConfiguration();

      // Retrieve general information
      XPath xpa = XPath.newInstance("/api/query/general");
      Element generalNode = (Element) xpa.selectSingleNode(root);
      if (generalNode != null) {
        wikiConfiguration.setArticlePath(generalNode.getAttributeValue("articlepath"));
        wikiConfiguration.setScript(generalNode.getAttributeValue("script"));
        wikiConfiguration.setServer(generalNode.getAttributeValue("server"));
      }

      // Retrieve name spaces
      HashMap<Integer, Namespace> namespaces = null;
      xpa = XPath.newInstance("/api/query/namespaces/ns");
      List results = xpa.selectNodes(root);
      Iterator iter = results.iterator();
      namespaces = new HashMap<Integer, Namespace>();
      while (iter.hasNext()) {
        Element currentNode = (Element) iter.next();
        String title = currentNode.getText();
        String canonical = currentNode.getAttributeValue("canonical");
        String id = currentNode.getAttributeValue("id");
        EnumCaseSensitiveness caseSensitiveness = EnumCaseSensitiveness.getCase(currentNode.getAttributeValue("case"));
        boolean subPages = (currentNode.getAttribute("subpages") != null);
        Namespace ns = new Namespace(id, title, canonical, caseSensitiveness, subPages);
        namespaces.put(ns.getId(), ns);
      }

      // Retrieve name space aliases
      xpa = XPath.newInstance("/api/query/namespacealiases/ns");
      results = xpa.selectNodes(root);
      iter = results.iterator();
      while (iter.hasNext()) {
        Element currentNode = (Element) iter.next();
        Integer nsId = null;
        try {
          nsId = Integer.parseInt(currentNode.getAttributeValue("id"));
          Namespace namespace = namespaces.get(nsId);
          if (namespace != null) {
            namespace.addAlias(currentNode.getText());
          }
        } catch (NumberFormatException e) {
          //
        }
      }

      // Update name space list
      LinkedList<Namespace> list = new LinkedList<Namespace>(namespaces.values());
      wikiConfiguration.setNamespaces(list);

      // Retrieve languages
      List<Language> languages = new ArrayList<Language>();
      xpa = XPath.newInstance("/api/query/languages/lang");
      results = xpa.selectNodes(root);
      iter = results.iterator();
      while (iter.hasNext()) {
        Element currentNode = (Element) iter.next();
        String code = currentNode.getAttributeValue("code");
        String name = currentNode.getText();
        languages.add(new Language(code, name));
      }
      wikiConfiguration.setLanguages(languages);

      // Retrieve interwikis
      List<Interwiki> interwikis = new ArrayList<Interwiki>();
      xpa = XPath.newInstance("/api/query/interwikimap/iw");
      results = xpa.selectNodes(root);
      iter = results.iterator();
      while (iter.hasNext()) {
        Element currentNode = (Element) iter.next();
        String prefix = currentNode.getAttributeValue("prefix");
        boolean local = (currentNode.getAttribute("local") != null);
        String language = currentNode.getAttributeValue("language");
        String url = currentNode.getAttributeValue("url");
        interwikis.add(new Interwiki( prefix, local, language, url));
      }
      wikiConfiguration.setInterwikis(interwikis);

      // Retrieve magic words
      Map<String, MagicWord> magicWords = new HashMap<String, MagicWord>();
      xpa = XPath.newInstance("/api/query/magicwords/magicword");
      results = xpa.selectNodes(root);
      iter = results.iterator();
      XPath xpaAlias = XPath.newInstance("./aliases/alias");
      XPath xpaAliasValue = XPath.newInstance(".");
      while (iter.hasNext()) {
        Element currentNode = (Element) iter.next();
        String magicWord = currentNode.getAttributeValue("name");
        List<String> aliases = new ArrayList<String>();
        List resultsAlias = xpaAlias.selectNodes(currentNode);
        Iterator iterAlias = resultsAlias.iterator();
        while (iterAlias.hasNext()) {
          Element currentAlias = (Element) iterAlias.next();
          String alias = xpaAliasValue.valueOf(currentAlias);
          aliases.add(alias);
        }
        boolean caseSensitive = (currentNode.getAttribute("case-sensitive") != null);
        magicWords.put(
            magicWord,
            new MagicWord(magicWord, aliases, caseSensitive));
      }
      wikiConfiguration.setMagicWords(magicWords);
    } catch (JDOMException e) {
      log.error("Error loading namespaces", e);
      throw new APIException("Error parsing XML", e);
    }
  }
View Full Code Here


    List<PageElementExternalLink> links = analysis.getExternalLinks();
    if (links == null) {
      return result;
    }
    EnumWikipedia wiki = analysis.getWikipedia();
    WikiConfiguration wikiConf = wiki.getWikiConfiguration();
    String contents = analysis.getContents();
    for (PageElementExternalLink link : links) {
      if (link.hasSquare()) {
        String article = wikiConf.isArticleUrl(link.getLink());
        if ((article != null) &&
            (article.length() > 0)) {
          if (errors == null) {
            return true;
          }
View Full Code Here

    if ((ns == null) || (ns.intValue() != Namespace.MAIN)) {
      return false;
    }

    // Check each tag
    WikiConfiguration wikiConf = analysis.getWikiConfiguration();
    List<PageElementTag> tags = analysis.getCompleteTags(PageElementTag.TAG_HTML_A);
    if ((tags == null) || (tags.isEmpty())) {
      return false;
    }
    if (errors == null) {
      return true;
    }
    for (PageElementTag tag : tags) {
      boolean shouldKeep = true;

      int index = tag.getBeginIndex();
      if ((analysis.getSurroundingTag(PageElementTag.TAG_WIKI_CODE, index) != null) ||
          (analysis.getSurroundingTag(PageElementTag.TAG_WIKI_IMAGEMAP, index) != null) ||
          (analysis.getSurroundingTag(PageElementTag.TAG_WIKI_MATH, index) != null) ||
          (analysis.getSurroundingTag(PageElementTag.TAG_WIKI_NOWIKI, index) != null) ||
          (analysis.getSurroundingTag(PageElementTag.TAG_WIKI_PRE, index) != null) ||
          (analysis.getSurroundingTag(PageElementTag.TAG_WIKI_SOURCE, index) != null) ||
          (analysis.getSurroundingTag(PageElementTag.TAG_WIKI_SYNTAXHIGHLIGHT, index) != null)) {
        shouldKeep = false;
      }

      if (shouldKeep) {
        CheckErrorResult errorResult = createCheckErrorResult(
            analysis, tag.getCompleteBeginIndex(), tag.getCompleteEndIndex());
        if (tag.isFullTag()) {
          errorResult.addReplacement("");
        } else if (tag.isComplete()) {
          String internalText = analysis.getContents().substring(
              tag.getValueBeginIndex(), tag.getValueEndIndex());
          PageElementTag.Parameter href = tag.getParameter("href");
          String hrefValue = (href != null) ?href.getTrimmedValue() : null;
 
          // Check for link with "tel:" protocol as href
          if ((hrefValue != null) &&
              (hrefValue.startsWith("tel:"))) {
            errorResult.addReplacement(internalText, true);
          }
 
          // Check for link with internal link as href
          if (hrefValue != null) {
            String article = wikiConf.isArticleUrl(hrefValue);
            if ((article != null) && (article.length() > 0)) {
              errorResult.addReplacement(
                  PageElementInternalLink.createInternalLink(article, internalText),
                  true);
            }
View Full Code Here

    WPCConfiguration configuration = wikipedia.getConfiguration();

    setText(GT._("Retrieving MediaWiki API"));
    API api = APIFactory.getAPI();
    int lastCount = 0;
    WikiConfiguration wikiConfiguration = wikipedia.getWikiConfiguration();

    Stats stats = new Stats();
    try {
      if (!useList) {
        // Retrieve talk pages including a warning
        String warningTemplateName = configuration.getString(WPCConfigurationString.DAB_WARNING_TEMPLATE);
        setText(GT._("Retrieving talk pages including {0}", "{{" + warningTemplateName + "}}"));
        String templateTitle = wikiConfiguration.getPageTitle(
            Namespace.TEMPLATE,
            warningTemplateName);
        Page warningTemplate = DataManager.getPage(
            wikipedia, templateTitle, null, null, null);
        api.retrieveEmbeddedIn(
            wikipedia, warningTemplate,
            configuration.getEncyclopedicTalkNamespaces(),
            false);
        List<Page> warningTalkPages = warningTemplate.getRelatedPages(Page.RelatedPages.EMBEDDED_IN);
 
        // Construct list of articles with warning
        setText(GT._("Constructing list of articles with warning"));
        HashSet<Page> tmpWarningPages = new HashSet<Page>();
        for (Page warningPage : warningTalkPages) {
          String title = warningPage.getTitle();
          String todoSubpage = configuration.getString(WPCConfigurationString.TODO_SUBPAGE);
          if (title.endsWith("/" + todoSubpage)) {
            title = title.substring(0, title.length() - 1 - todoSubpage.length());
          }
          int colonIndex = title.indexOf(':');
          if (colonIndex >= 0) {
            for (Integer namespace : configuration.getEncyclopedicTalkNamespaces()) {
              Namespace namespaceTalk = wikiConfiguration.getNamespace(namespace);
              if ((namespaceTalk != null) &&
                  (namespaceTalk.isPossibleName(title.substring(0, colonIndex)))) {
                String tmpTitle = title.substring(colonIndex + 1);
                if (namespace != Namespace.MAIN_TALK) {
                  tmpTitle = wikiConfiguration.getPageTitle(namespace - 1, tmpTitle);
                }
                if ((start.length() == 0) || (start.compareTo(tmpTitle) < 0)) {
                  Page page = DataManager.getPage(wikipedia, tmpTitle, null, null, null);
                  if (!tmpWarningPages.contains(page)) {
                    tmpWarningPages.add(page);
View Full Code Here

      return title;
    }
    if (namespace == null) {
      return null;
    }
    WikiConfiguration wikiConfiguration = wikipedia.getWikiConfiguration();
    List<Namespace> namespaces = wikiConfiguration.getNamespaces();
    if (namespaces == null) {
      return null;
    }
    if (Namespace.MAIN_TALK == namespace.intValue()) {
      int colonIndex = title.indexOf(':');
      if ((colonIndex >= 0) && (colonIndex + 1 < title.length())) {
        return title.substring(colonIndex + 1);
      }
      return title;
    }
    Namespace n = wikiConfiguration.getNamespace(namespace.intValue() - 1);
    int firstColon = title.indexOf(':');
    if ((firstColon >= 0) && (n != null)) {
      return n.getTitle() + ":" + title.substring(firstColon + 1);
    }
    return null;
View Full Code Here

    if (!isArticle() ||
        (namespace == null) ||
        (wikipedia == null)) {
      return null;
    }
    WikiConfiguration wikiConfiguration = wikipedia.getWikiConfiguration();
    List<Namespace> namespaces = wikiConfiguration.getNamespaces();
    if (namespaces == null) {
      return null;
    }
    if (Namespace.MAIN == namespace.intValue()) {
      Namespace n = wikiConfiguration.getNamespace(Namespace.MAIN_TALK);
      if (n != null) {
        return n.getTitle() + ":" + title;
      }
      return "Talk:" + title;
    }
    Namespace n = wikiConfiguration.getNamespace(namespace.intValue() + 1);
    int firstColon = title.indexOf(':');
    if ((firstColon >= 0) && (n != null)) {
      return n.getTitle() + ":" + title.substring(firstColon + 1);
    }
    return null;
View Full Code Here

    if (analysis == null) {
      return false;
    }

    // Retrieve informations
    WikiConfiguration config = analysis.getWikiConfiguration();
    Namespace templateNamespace = config.getNamespace(Namespace.TEMPLATE);

    // Check every template to if template name contains template namespace
    Collection<PageElementTemplate> templates = analysis.getTemplates();
    if ((templates == null) || (templates.isEmpty())) {
      return false;
    }
    boolean result = false;
    for (PageElementTemplate template : templates) {
      String templateName = template.getTemplateName();
      int colonIndex = templateName.indexOf(':');
      String namespace = (colonIndex > 0) ? templateName.substring(0, colonIndex) : null;
      if ((colonIndex > 0) &&
          (templateNamespace.isPossibleName(namespace))) {

        // Test for special situations (functions)
        MagicWord magicWord = null;
        if (templateName.length() > colonIndex + 1) {
          String afterColon = templateName.substring(colonIndex + 1);
          colonIndex = afterColon.indexOf(':');
          if (colonIndex < 0) {
            magicWord = config.getFunctionMagicWord(afterColon, false);
          } else {
            magicWord = config.getFunctionMagicWord(afterColon.substring(0, colonIndex), true);
          }
        }

        // Raise error
        if (magicWord == null) {
View Full Code Here

    int pipeIndex = firstPipeIndex;
    tmpIndex++;
    int linkCount = 0;
    int templateCount = 0;
    List<Parameter> parameters = new ArrayList<Parameter>();
    WikiConfiguration wikiConfiguration = wikipedia.getWikiConfiguration();
    while (tmpIndex < contents.length()) {
      if ((templateCount <= 0) && (linkCount <= 0) && (contents.startsWith("]]", tmpIndex))) {
        String element = contents.substring(pipeIndex + 1, tmpIndex).trim();
        if (element.length() > 0) {
          MagicWord magicWord = wikiConfiguration.getImgMagicWord(element);
          Parameter param = new Parameter(
              pipeIndex + 1 - index, tmpIndex - index,
              element, magicWord);
          parameters.add(param);
        }
        pipeIndex = tmpIndex;
        return new PageElementImage(
            wikipedia, index, tmpIndex + 2,
            contents.substring(beginIndex, colonIndex),
            contents.substring(colonIndex + 1, firstPipeIndex),
            firstPipeIndex - index, parameters);
      } else if ((templateCount <= 0) && (linkCount <= 0) && (contents.charAt(tmpIndex) == '|')) {
        String element = contents.substring(pipeIndex + 1, tmpIndex).trim();
        if (element.length() > 0) {
          MagicWord magicWord = wikiConfiguration.getImgMagicWord(element);
          Parameter param = new Parameter(
              pipeIndex + 1 - index, tmpIndex - index,
              element, magicWord);
          parameters.add(param);
        }
View Full Code Here

   */
  public String getAlternateDescription() {
    if (parameters == null) {
      return null;
    }
    WikiConfiguration wikiConfiguration = wiki.getWikiConfiguration();
    for (Parameter param : parameters) {
      String contents = param.getContents();
      if ((contents != null) &&
          (param.getMagicWord() != null) &&
          (wikiConfiguration.getMagicWordByName(MagicWord.IMG_ALT).isPossibleAlias(contents))) {
        // TODO: Don't rely on "="
        int equalIndex = contents.indexOf("=");
        if (equalIndex >= 0) {
          return contents.substring(equalIndex + 1).trim();
        }
View Full Code Here

    WPCConfiguration configuration = wiki.getConfiguration();

    setText(GT._("Retrieving MediaWiki API"));
    API api = APIFactory.getAPI();
    int lastCount = 0;
    WikiConfiguration wikiConfiguration = wiki.getWikiConfiguration();

    Stats stats = new Stats();
    Map<String, List<String>> errors = null;
    try {
      if (!useList) {
        warningPages.clear();

        // Retrieve talk pages including a warning
        String warningTemplateName = configuration.getString(WPCConfigurationString.ISBN_WARNING_TEMPLATE);
        if (warningTemplateName != null) {
          setText(GT._("Retrieving talk pages including {0}", "{{" + warningTemplateName + "}}"));
          String templateTitle = wikiConfiguration.getPageTitle(
              Namespace.TEMPLATE,
              warningTemplateName);
          Page warningTemplate = DataManager.getPage(
              wiki, templateTitle, null, null, null);
          api.retrieveEmbeddedIn(
              wiki, warningTemplate,
              configuration.getEncyclopedicTalkNamespaces(),
              false);
          warningPages.addAll(warningTemplate.getRelatedPages(Page.RelatedPages.EMBEDDED_IN));
        }

        // Retrieve articles in categories for ISBN errors
        List<String> categories = configuration.getStringList(WPCConfigurationStringList.ISBN_ERRORS_CATEGORIES);
        if (categories != null) {
          for (String category : categories) {
            String categoryTitle = wikiConfiguration.getPageTitle(Namespace.CATEGORY, category);
            Page categoryPage = DataManager.getPage(wiki, categoryTitle, null, null, null);
            api.retrieveCategoryMembers(wiki, categoryPage, 0, false);
            List<Page> categoryMembers = categoryPage.getRelatedPages(
                Page.RelatedPages.CATEGORY_MEMBERS);
            if (categoryMembers != null) {
              warningPages.addAll(categoryMembers);
            }
          }
        }

        // Retrieve articles listed for ISBN errors in Check Wiki
        retrieveCheckWikiPages(70, warningPages); // Incorrect length
        retrieveCheckWikiPages(71, warningPages); // Incorrect X
        retrieveCheckWikiPages(72, warningPages); // Incorrect ISBN-10
        retrieveCheckWikiPages(73, warningPages); // Incorrect ISBN-13

        // Construct list of articles with warning
        setText(GT._("Constructing list of articles with warning"));
        HashSet<Page> tmpWarningPages = new HashSet<Page>();
        List<Integer> encyclopedicNamespaces = configuration.getEncyclopedicNamespaces();
        for (Page warningPage : warningPages) {

          // Get article page for talks pages and to do sub-pages
          String title = warningPage.getTitle();
          if (!warningPage.isArticle()) {
            String todoSubpage = configuration.getString(WPCConfigurationString.TODO_SUBPAGE);
            if (title.endsWith("/" + todoSubpage)) {
              title = title.substring(0, title.length() - 1 - todoSubpage.length());
            }
            Integer namespace = warningPage.getNamespace();
            if (namespace != null) {
              Namespace namespaceTalk = wikiConfiguration.getNamespace(namespace.intValue());
              if (namespaceTalk != null) {
                int colonIndex = title.indexOf(':');
                if (colonIndex >= 0) {
                  title = title.substring(colonIndex + 1);
                }
                if (namespace != Namespace.MAIN_TALK) {
                  title = wikiConfiguration.getPageTitle(namespace - 1, title);
                }
              }
            }
          }
View Full Code Here

TOP

Related Classes of org.wikipediacleaner.api.constants.WikiConfiguration

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.