InstallDocFormatter.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * Installer-specific wikitext formatting.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. class InstallDocFormatter {
  23. /** @var string */
  24. private $text;
  25. public static function format( $text ) {
  26. $obj = new self( $text );
  27. return $obj->execute();
  28. }
  29. protected function __construct( $text ) {
  30. $this->text = $text;
  31. }
  32. protected function execute() {
  33. $text = $this->text;
  34. // Use Unix line endings, escape some wikitext stuff
  35. $text = str_replace( [ '<', '{{', '[[', '__', "\r" ],
  36. [ '&lt;', '&#123;&#123;', '&#91;&#91;', '&#95;&#95;', '' ], $text );
  37. // join word-wrapped lines into one
  38. do {
  39. $prev = $text;
  40. $text = preg_replace( "/\n([\\*#\t])([^\n]*?)\n([^\n#\\*:]+)/", "\n\\1\\2 \\3", $text );
  41. } while ( $text != $prev );
  42. // Replace tab indents with colons
  43. $text = preg_replace( '/^\t\t/m', '::', $text );
  44. $text = preg_replace( '/^\t/m', ':', $text );
  45. $linkStart = '<span class="config-plainlink">[';
  46. $linkEnd = ' $0]</span>';
  47. // turn (Tnnnn) into links
  48. $text = preg_replace(
  49. '/T\d+/',
  50. "{$linkStart}https://phabricator.wikimedia.org/$0{$linkEnd}",
  51. $text
  52. );
  53. // turn (bug nnnn) into links
  54. $text = preg_replace(
  55. '/bug (\d+)/',
  56. "{$linkStart}https://bugzilla.wikimedia.org/$1{$linkEnd}",
  57. $text
  58. );
  59. // add links to manual to every global variable mentioned
  60. $text = preg_replace(
  61. '/\$wg[a-z0-9_]+/i',
  62. "{$linkStart}https://www.mediawiki.org/wiki/Manual:$0{$linkEnd}",
  63. $text
  64. );
  65. return $text;
  66. }
  67. }