Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
63.33% |
19 / 30 |
|
20.00% |
1 / 5 |
CRAP | |
0.00% |
0 / 1 |
| RevisionEntitySerializer | |
63.33% |
19 / 30 |
|
20.00% |
1 / 5 |
16.96 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| toArray | |
94.44% |
17 / 18 |
|
0.00% |
0 / 1 |
6.01 | |||
| revisionSlotsToArray | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| bitsToVisibilityAttrs | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| isVisible | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 | * http://www.gnu.org/copyleft/gpl.html |
| 17 | * |
| 18 | * @file |
| 19 | * @author Andrew Otto <otto@wikimedia.org> |
| 20 | */ |
| 21 | namespace MediaWiki\Extension\EventBus\Serializers\MediaWiki; |
| 22 | |
| 23 | use MediaWiki\Extension\EventBus\Serializers\EventSerializer; |
| 24 | use MediaWiki\Revision\RevisionRecord; |
| 25 | use MediaWiki\Revision\RevisionSlots; |
| 26 | |
| 27 | /** |
| 28 | * Converts a RevisionRecord to an array matching the fragment/mediawiki/state/entity/revision schema |
| 29 | */ |
| 30 | class RevisionEntitySerializer { |
| 31 | |
| 32 | /** |
| 33 | * @var RevisionSlotEntitySerializer |
| 34 | */ |
| 35 | private RevisionSlotEntitySerializer $revisionSlotEntitySerializer; |
| 36 | |
| 37 | /** |
| 38 | * @var UserEntitySerializer |
| 39 | */ |
| 40 | private UserEntitySerializer $userEntitySerializer; |
| 41 | |
| 42 | /** |
| 43 | * @param RevisionSlotEntitySerializer $contentEntitySerializer |
| 44 | * @param UserEntitySerializer $userEntitySerializer |
| 45 | */ |
| 46 | public function __construct( |
| 47 | RevisionSlotEntitySerializer $contentEntitySerializer, |
| 48 | UserEntitySerializer $userEntitySerializer |
| 49 | ) { |
| 50 | $this->userEntitySerializer = $userEntitySerializer; |
| 51 | $this->revisionSlotEntitySerializer = $contentEntitySerializer; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @param RevisionRecord $revisionRecord |
| 56 | * @return array |
| 57 | */ |
| 58 | public function toArray( RevisionRecord $revisionRecord ): array { |
| 59 | $revAttrs = [ |
| 60 | 'rev_id' => $revisionRecord->getId(), |
| 61 | 'rev_dt' => EventSerializer::timestampToDt( $revisionRecord->getTimestamp() ), |
| 62 | 'is_minor_edit' => $revisionRecord->isMinor(), |
| 63 | 'rev_sha1' => $revisionRecord->getSha1(), |
| 64 | 'rev_size' => $revisionRecord->getSize() |
| 65 | ]; |
| 66 | |
| 67 | // The rev_parent_id attribute is not required, but when supplied |
| 68 | // must have a minimum value of 1, so omit it entirely when there is no |
| 69 | // parent revision (i.e. page creation). |
| 70 | if ( $revisionRecord->getParentId() !== null && $revisionRecord->getParentId() > 0 ) { |
| 71 | $revAttrs['rev_parent_id'] = $revisionRecord->getParentId(); |
| 72 | } |
| 73 | |
| 74 | // If getComment is null, don't set it. However, we still set comment is an |
| 75 | // empty string, to differentiate between a hidden comment and an empty comment |
| 76 | // left by the editor. |
| 77 | if ( $revisionRecord->getComment() !== null ) { |
| 78 | $revAttrs['comment'] = $revisionRecord->getComment()->text; |
| 79 | } |
| 80 | |
| 81 | if ( $revisionRecord->getUser() ) { |
| 82 | $revAttrs['editor'] = $this->userEntitySerializer->toArray( $revisionRecord->getUser() ); |
| 83 | } |
| 84 | |
| 85 | // Include this revision's visibility settings. |
| 86 | $revAttrs += self::bitsToVisibilityAttrs( $revisionRecord->getVisibility() ); |
| 87 | |
| 88 | // Include info about the revision content slots, as long as slots are not empty. |
| 89 | // Note that this DOES NOT include actual content bodies, |
| 90 | // just metadata about the content in each slot. |
| 91 | $contentSlots = $this->revisionSlotsToArray( $revisionRecord->getSlots() ); |
| 92 | if ( $contentSlots ) { |
| 93 | $revAttrs['content_slots'] = $contentSlots; |
| 94 | } |
| 95 | |
| 96 | return $revAttrs; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Converts RevisionSlots to a fragment/mediawiki/state/entity/revision_slots map type entity |
| 101 | * @param RevisionSlots $revisionSlots |
| 102 | * @return array |
| 103 | */ |
| 104 | public function revisionSlotsToArray( RevisionSlots $revisionSlots ): array { |
| 105 | $slotsAttrs = []; |
| 106 | foreach ( $revisionSlots->getSlots() as $slotRole => $slotRecord ) { |
| 107 | $slotsAttrs[$slotRole] = $this->revisionSlotEntitySerializer->toArray( $slotRecord ); |
| 108 | } |
| 109 | return $slotsAttrs; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Converts a revision visibility hidden bitfield to an array with keys |
| 114 | * of each of the possible visibility settings name mapped to a boolean. |
| 115 | * |
| 116 | * @param int $bitfield RevisionRecord $mDeleted bitfield. |
| 117 | * @return array |
| 118 | */ |
| 119 | public static function bitsToVisibilityAttrs( int $bitfield ): array { |
| 120 | return [ |
| 121 | 'is_content_visible' => self::isVisible( $bitfield, RevisionRecord::DELETED_TEXT ), |
| 122 | 'is_editor_visible' => self::isVisible( $bitfield, RevisionRecord::DELETED_USER ), |
| 123 | 'is_comment_visible' => self::isVisible( $bitfield, RevisionRecord::DELETED_COMMENT ), |
| 124 | ]; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Checks if RevisionRecord::DELETED_* field is set in the $hiddenBits |
| 129 | * |
| 130 | * @param int $hiddenBits revision visibility bitfield |
| 131 | * @param int $field RevisionRecord::DELETED_* field to check |
| 132 | * @return bool |
| 133 | */ |
| 134 | private static function isVisible( int $hiddenBits, int $field ): bool { |
| 135 | // It would probably be better to use the $revisionRecord-audienceCan method |
| 136 | // than use our custom logic here to compare the bits with RevisionRecord constants, |
| 137 | // But then we wouldn't have a way of computing the visibleness of a revision |
| 138 | // without a RevisionRecord instance, which is something we have to do in the case |
| 139 | // of ArticleRevisionVisibilitySetHook, where we only get the $oldBits, but not the |
| 140 | // old RevisionRecord. |
| 141 | return ( $hiddenBits & $field ) != $field; |
| 142 | } |
| 143 | |
| 144 | } |