WordAccumulator.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Copyright © 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
  4. * You may copy this code freely under the conditions of the GPL.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. * http://www.gnu.org/copyleft/gpl.html
  20. *
  21. * @file
  22. * @ingroup DifferenceEngine
  23. * @defgroup DifferenceEngine DifferenceEngine
  24. */
  25. namespace MediaWiki\Diff;
  26. /**
  27. * Stores, escapes and formats the results of word-level diff
  28. *
  29. * @private
  30. * @ingroup DifferenceEngine
  31. */
  32. class WordAccumulator {
  33. public $insClass = ' class="diffchange diffchange-inline"';
  34. public $delClass = ' class="diffchange diffchange-inline"';
  35. private $lines = [];
  36. private $line = '';
  37. private $group = '';
  38. private $tag = '';
  39. /**
  40. * @param string $new_tag
  41. */
  42. private function flushGroup( $new_tag ) {
  43. if ( $this->group !== '' ) {
  44. if ( $this->tag == 'ins' ) {
  45. $this->line .= "<ins{$this->insClass}>" . htmlspecialchars( $this->group ) . '</ins>';
  46. } elseif ( $this->tag == 'del' ) {
  47. $this->line .= "<del{$this->delClass}>" . htmlspecialchars( $this->group ) . '</del>';
  48. } else {
  49. $this->line .= htmlspecialchars( $this->group );
  50. }
  51. }
  52. $this->group = '';
  53. $this->tag = $new_tag;
  54. }
  55. /**
  56. * @param string $new_tag
  57. */
  58. private function flushLine( $new_tag ) {
  59. $this->flushGroup( $new_tag );
  60. if ( $this->line != '' ) {
  61. array_push( $this->lines, $this->line );
  62. } else {
  63. # make empty lines visible by inserting an NBSP
  64. array_push( $this->lines, "\u{00A0}" );
  65. }
  66. $this->line = '';
  67. }
  68. /**
  69. * @param string[] $words
  70. * @param string $tag
  71. */
  72. public function addWords( $words, $tag = '' ) {
  73. if ( $tag != $this->tag ) {
  74. $this->flushGroup( $tag );
  75. }
  76. foreach ( $words as $word ) {
  77. // new-line should only come as first char of word.
  78. if ( $word == '' ) {
  79. continue;
  80. }
  81. if ( $word[0] == "\n" ) {
  82. $this->flushLine( $tag );
  83. $word = substr( $word, 1 );
  84. }
  85. // FIXME: Don't use assert()
  86. // phpcs:ignore MediaWiki.Usage.ForbiddenFunctions.assert
  87. assert( !strstr( $word, "\n" ) );
  88. $this->group .= $word;
  89. }
  90. }
  91. /**
  92. * @return string[]
  93. */
  94. public function getLines() {
  95. $this->flushLine( '~done' );
  96. return $this->lines;
  97. }
  98. }