RevisionStoreCacheRecord.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * A RevisionStoreRecord loaded from the cache.
  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. namespace MediaWiki\Revision;
  23. use MediaWiki\User\UserIdentity;
  24. use MediaWiki\User\UserIdentityValue;
  25. use CommentStoreComment;
  26. use InvalidArgumentException;
  27. use Title;
  28. use User;
  29. /**
  30. * A cached RevisionStoreRecord. Ensures that changes performed "behind the back"
  31. * of the cache do not cause the revision record to deliver stale data.
  32. *
  33. * @since 1.33
  34. */
  35. class RevisionStoreCacheRecord extends RevisionStoreRecord {
  36. /**
  37. * @var callable
  38. */
  39. private $mCallback;
  40. /**
  41. * @note Avoid calling this constructor directly. Use the appropriate methods
  42. * in RevisionStore instead.
  43. *
  44. * @param callable $callback Callback for loading data. Signature: function ( $id ): object
  45. * @param Title $title The title of the page this Revision is associated with.
  46. * @param UserIdentity $user
  47. * @param CommentStoreComment $comment
  48. * @param object $row A row from the revision table. Use RevisionStore::getQueryInfo() to build
  49. * a query that yields the required fields.
  50. * @param RevisionSlots $slots The slots of this revision.
  51. * @param bool|string $dbDomain DB domain of the relevant wiki or false for the current one.
  52. */
  53. function __construct(
  54. $callback,
  55. Title $title,
  56. UserIdentity $user,
  57. CommentStoreComment $comment,
  58. $row,
  59. RevisionSlots $slots,
  60. $dbDomain = false
  61. ) {
  62. parent::__construct( $title, $user, $comment, $row, $slots, $dbDomain );
  63. $this->mCallback = $callback;
  64. }
  65. /**
  66. * Overridden to ensure that we return a fresh value and not a cached one.
  67. *
  68. * @return int
  69. */
  70. public function getVisibility() {
  71. if ( $this->mCallback ) {
  72. $this->loadFreshRow();
  73. }
  74. return parent::getVisibility();
  75. }
  76. /**
  77. * Overridden to ensure that we return a fresh value and not a cached one.
  78. *
  79. * @param int $audience
  80. * @param User|null $user
  81. *
  82. * @return UserIdentity The identity of the revision author, null if access is forbidden.
  83. */
  84. public function getUser( $audience = self::FOR_PUBLIC, User $user = null ) {
  85. if ( $this->mCallback ) {
  86. $this->loadFreshRow();
  87. }
  88. return parent::getUser( $audience, $user );
  89. }
  90. /**
  91. * Load a fresh row from the database to ensure we return updated information
  92. *
  93. * @throws RevisionAccessException if the row could not be loaded
  94. */
  95. private function loadFreshRow() {
  96. $freshRow = call_user_func( $this->mCallback, $this->mId );
  97. // Set to null to ensure we do not make unnecessary queries for subsequent getter calls,
  98. // and to allow the closure to be freed.
  99. $this->mCallback = null;
  100. if ( $freshRow ) {
  101. $this->mDeleted = intval( $freshRow->rev_deleted );
  102. try {
  103. $this->mUser = User::newFromAnyId(
  104. $freshRow->rev_user ?? null,
  105. $freshRow->rev_user_text ?? null,
  106. $freshRow->rev_actor ?? null
  107. );
  108. } catch ( InvalidArgumentException $ex ) {
  109. wfWarn(
  110. __METHOD__
  111. . ': '
  112. . $this->mTitle->getPrefixedDBkey()
  113. . ': '
  114. . $ex->getMessage()
  115. );
  116. $this->mUser = new UserIdentityValue( 0, 'Unknown user', 0 );
  117. }
  118. } else {
  119. throw new RevisionAccessException(
  120. 'Unable to load fresh row for rev_id: ' . $this->mId
  121. );
  122. }
  123. }
  124. }