SlotDiffRenderer.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * Renders a diff for a single slot.
  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. * @ingroup DifferenceEngine
  22. */
  23. use Wikimedia\Assert\Assert;
  24. /**
  25. * Renders a diff for a single slot (that is, a diff between two content objects).
  26. *
  27. * Callers should obtain instances of this class by invoking ContentHandler::getSlotDiffRenderer
  28. * on the content handler of the new content object (ie. the one shown on the right side
  29. * of the diff), or of the old one if the new one does not exist.
  30. *
  31. * The default implementation just does a text diff on the native text representation.
  32. * Content handler extensions can subclass this to provide a more appropriate diff method by
  33. * overriding ContentHandler::getSlotDiffRendererInternal. Other extensions that want to interfere
  34. * with diff generation in some way can use the GetSlotDiffRenderer hook.
  35. *
  36. * @ingroup DifferenceEngine
  37. */
  38. abstract class SlotDiffRenderer {
  39. /**
  40. * Get a diff between two content objects. One of them might be null (meaning a slot was
  41. * created or removed), but both cannot be. $newContent (or if it's null then $oldContent)
  42. * must have the same content model that was used to obtain this diff renderer.
  43. * @param Content|null $oldContent
  44. * @param Content|null $newContent
  45. * @return string HTML, one or more <tr> tags.
  46. */
  47. abstract public function getDiff( Content $oldContent = null, Content $newContent = null );
  48. /**
  49. * Add modules needed for correct styling/behavior of the diff.
  50. * @param OutputPage $output
  51. */
  52. public function addModules( OutputPage $output ) {
  53. }
  54. /**
  55. * Return any extra keys to split the diff cache by.
  56. * @return array
  57. */
  58. public function getExtraCacheKeys() {
  59. return [];
  60. }
  61. /**
  62. * Helper method to normalize the input of getDiff().
  63. * Verifies that at least one of $oldContent and $newContent is not null, verifies that
  64. * they are instances of one of the allowed classes (if provided), and replaces null with
  65. * empty content.
  66. * @param Content|null &$oldContent
  67. * @param Content|null &$newContent
  68. * @param string|array|null $allowedClasses
  69. */
  70. protected function normalizeContents(
  71. Content &$oldContent = null, Content &$newContent = null, $allowedClasses = null
  72. ) {
  73. if ( !$oldContent && !$newContent ) {
  74. throw new InvalidArgumentException( '$oldContent and $newContent cannot both be null' );
  75. }
  76. if ( $allowedClasses ) {
  77. if ( is_array( $allowedClasses ) ) {
  78. $allowedClasses = implode( '|', $allowedClasses );
  79. }
  80. Assert::parameterType( $allowedClasses . '|null', $oldContent, '$oldContent' );
  81. Assert::parameterType( $allowedClasses . '|null', $newContent, '$newContent' );
  82. }
  83. if ( !$oldContent ) {
  84. $oldContent = $newContent->getContentHandler()->makeEmptyContent();
  85. } elseif ( !$newContent ) {
  86. $newContent = $oldContent->getContentHandler()->makeEmptyContent();
  87. }
  88. }
  89. }