FindAllRedirects.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. // COPYRIGHT: Openmoko Inc. 2010
  3. // LICENSE: GPL Version 3 or later
  4. // DESCRIPTION: Compute a list of all possible redirect words
  5. // by processing the MediaWiki language files
  6. // AUTHORS: Christopher Hall <hsw@openmoko.com>
  7. // check that some files are given
  8. if (sizeof($argv) < 2) {
  9. echo "usage: $argv[0] list_of_files\n";
  10. exit(1);
  11. }
  12. ?>
  13. #! /usr/bin/env python
  14. # -*- coding: utf-8 -*-
  15. #
  16. # *** WARNING: Generated file do not modify
  17. #
  18. # Generated on: <?php echo date('Y-m-d H:i:s'), ' by ', $argv[0]; ?>
  19. <?php
  20. include "mediawiki-offline/includes/Defines.php";
  21. // remove the program name from the list of files
  22. $list = array_slice($argv, 1);
  23. // key of this array will be the redirect word
  24. $redirects = array();
  25. // Read each of the language files and extract: $magicWords['redirect']
  26. foreach ($list as $file) {
  27. if ('file' === filetype($file)) {
  28. //echo "Processing: $file\n";
  29. $magicWords = array();
  30. $magicWords['redirect'] = array();
  31. include $file;
  32. if (is_array($magicWords) && isset($magicWords['redirect'])) {
  33. $m = $magicWords['redirect'];
  34. if (is_array($m)) {
  35. foreach ($m as $value) {
  36. if ("0" !== $value && 0 !== $value) {
  37. $value = mb_strtolower($value, 'UTF-8');
  38. $redirects[$value] = $value;
  39. //echo $value, "\n";
  40. }
  41. }
  42. } elseif (NULL !== $m) {
  43. echo '# $magicWords[\'redirect\'] is not an array for: ', $file, "\n";
  44. echo '# $magicWords[\'redirect\'] is: ', gettype($m), "\n";
  45. }
  46. } else {
  47. echo '# $magicWords is not an array for: ', $file, "\n";
  48. }
  49. }
  50. }
  51. // output the values
  52. sort($redirects);
  53. ?>
  54. import os, sys
  55. import re
  56. redirect_string = r'('
  57. <?php
  58. $flag = false;
  59. foreach ($redirects as $value) {
  60. echo 'redirect_string += \'';
  61. if ($flag) {
  62. echo '|';
  63. } else {
  64. $flag = true;
  65. }
  66. echo $value, "'\n";
  67. }
  68. ?>
  69. redirect_string += r')'
  70. start_string = r'\s*'
  71. end_string = r'[^\[]*\[\[(.*?)([#|].*?)?\]\]'
  72. regex = re.compile(start_string + redirect_string + end_string, re.IGNORECASE)
  73. def main():
  74. tests = [
  75. r'#redirect[[Just Testing]]',
  76. r'#айдау[[Just Testing]]',
  77. r'#リダイレクト[[Just Testing]]',
  78. r'#転送[[Just Testing]]',
  79. r'#転送[[Just Testing]]',
  80. ]
  81. for s in tests:
  82. m = regex.match(s)
  83. if m:
  84. print('matched: {0:s} : {1:s} => {3:s}'.format(s, m.group(1), m.group(2)))
  85. else:
  86. print('no match: {0:s}'.format(s))
  87. # run the program
  88. if __name__ == "__main__":
  89. main()