有没有检查MediaWiki页面标题是否有效的正则表达式或类似简单方法?

huangapple go评论125阅读模式
英文:

Is there a regular expession or similar simple approach for checking whether a MediaWiki PageTitle is valid?

问题

以下是您提供的内容的中文翻译:

https://www.mediawiki.org/wiki/Manual:Page_title 中列出了一些关于MediaWiki页面标题不得包含的条件。看起来使用这种方法来检查字符串是否是有效的MediaWiki页面标题并不容易。

有什么正则表达式或类似的简单方法来检查页面标题是否有效?

到目前为止,我找到的最好方法是一些Java代码(来自https://github.com/MER-C/wiki-java/blob/master/src/org/wikipedia/Wiki.java)。但是我的目标语言是Python。

  1. /**
  2. * 用于规范化MediaWiki标题的便捷方法。(将所有下划线转换为空格)。
  3. * @param s 要规范化的字符串
  4. * @return 规范化后的字符串
  5. * @throws IllegalArgumentException 如果标题无效
  6. * @throws IOException 如果发生网络错误(罕见)
  7. * @since 0.27
  8. */
  9. public String normalize(String s) throws IOException
  10. {
  11. // 移除前导冒号
  12. if (s.startsWith(":"))
  13. s = s.substring(1);
  14. if (s.isEmpty())
  15. return s;
  16. int ns = namespace(s);
  17. // 本地化命名空间名称
  18. if (ns != MAIN_NAMESPACE)
  19. {
  20. int colon = s.indexOf(":");
  21. s = namespaceIdentifier(ns) + s.substring(colon);
  22. }
  23. char[] temp = s.toCharArray();
  24. if (wgCapitalLinks)
  25. {
  26. // 将实际标题中的第一个字符转换为大写
  27. if (ns == MAIN_NAMESPACE)
  28. temp[0] = Character.toUpperCase(temp[0]);
  29. else
  30. {
  31. int index = namespaceIdentifier(ns).length() + 1; // + 1 用于冒号
  32. temp[index] = Character.toUpperCase(temp[index]);
  33. }
  34. }
  35. for (int i = 0; i < temp.length; i++)
  36. {
  37. switch (temp[i])
  38. {
  39. // 非法字符
  40. case '{':
  41. case '}':
  42. case '<':
  43. case '>':
  44. case '[':
  45. case ']':
  46. case '|':
  47. throw new IllegalArgumentException(s + " 是非法标题");
  48. case '_':
  49. temp[i] = ' ';
  50. break;
  51. }
  52. }
  53. // https://www.mediawiki.org/wiki/Unicode_normalization_considerations
  54. String temp2 = new String(temp).trim().replaceAll("\\s+", " ");
  55. return Normalizer.normalize(temp2, Normalizer.Form.NFC);
  56. }
英文:

https://www.mediawiki.org/wiki/Manual:Page_title states a lot of conditions for what a MediaWiki pageTitle may not contain. It looks like checking whether a string is a valid MediaWiki PageTitle is not quite easy with this approach.

What would be a regular expression or similar simple approach to check whether a page Title is valid?

The best i could find so far is some Java Code (from https://github.com/MER-C/wiki-java/blob/master/src/org/wikipedia/Wiki.java). My target language is python, though.

  1. /**
  2. * Convenience method for normalizing MediaWiki titles. (Converts all
  3. * underscores to spaces).
  4. * @param s the string to normalize
  5. * @return the normalized string
  6. * @throws IllegalArgumentException if the title is invalid
  7. * @throws IOException if a network error occurs (rare)
  8. * @since 0.27
  9. */
  10. public String normalize(String s) throws IOException
  11. {
  12. // remove leading colon
  13. if (s.startsWith(&quot;:&quot;))
  14. s = s.substring(1);
  15. if (s.isEmpty())
  16. return s;
  17. int ns = namespace(s);
  18. // localize namespace names
  19. if (ns != MAIN_NAMESPACE)
  20. {
  21. int colon = s.indexOf(&quot;:&quot;);
  22. s = namespaceIdentifier(ns) + s.substring(colon);
  23. }
  24. char[] temp = s.toCharArray();
  25. if (wgCapitalLinks)
  26. {
  27. // convert first character in the actual title to upper case
  28. if (ns == MAIN_NAMESPACE)
  29. temp[0] = Character.toUpperCase(temp[0]);
  30. else
  31. {
  32. int index = namespaceIdentifier(ns).length() + 1; // + 1 for colon
  33. temp[index] = Character.toUpperCase(temp[index]);
  34. }
  35. }
  36. for (int i = 0; i &lt; temp.length; i++)
  37. {
  38. switch (temp[i])
  39. {
  40. // illegal characters
  41. case &#39;{&#39;:
  42. case &#39;}&#39;:
  43. case &#39;&lt;&#39;:
  44. case &#39;&gt;&#39;:
  45. case &#39;[&#39;:
  46. case &#39;]&#39;:
  47. case &#39;|&#39;:
  48. throw new IllegalArgumentException(s + &quot; is an illegal title&quot;);
  49. case &#39;_&#39;:
  50. temp[i] = &#39; &#39;;
  51. break;
  52. }
  53. }
  54. // https://www.mediawiki.org/wiki/Unicode_normalization_considerations
  55. String temp2 = new String(temp).trim().replaceAll(&quot;\\s+&quot;, &quot; &quot;);
  56. return Normalizer.normalize(temp2, Normalizer.Form.NFC);
  57. }

答案1

得分: 1

以下是翻译好的部分:

如果您可以调用目标维基API进行规范化处理,那么这是一个规范化页面标题的API调用示例:

规范化后的标题将位于 /query/normalized/0/to。您可以一次发送多个要规范化的标题,用 | 分隔它们。

此示例摘自 https://www.mediawiki.org/wiki/API:Query#Example_2:_Title_normalization

英文:

If you can call the target wiki API to do the normalisation, then this is an example of API call that normalises page titles:

The normalised title ill be in /query/normalized/0/to. You can send several titles to normalise at once separating them with |.

The example is taken from https://www.mediawiki.org/wiki/API:Query#Example_2:_Title_normalization.

huangapple
  • 本文由 发表于 2020年7月28日 01:15:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63120292.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定