Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
68.42% |
13 / 19 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| RevisionEntitySerializer | |
68.42% |
13 / 19 |
|
33.33% |
1 / 3 |
5.79 | |
0.00% |
0 / 1 |
| toArray | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
3 | |||
| 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 | |
| 26 | /** |
| 27 | * Converts a RevisionRecord to an array matching the fragment/mediawiki/state/entity/revision schema |
| 28 | */ |
| 29 | class RevisionEntitySerializer { |
| 30 | /** |
| 31 | * The earliest schema version supported by this serializer. |
| 32 | */ |
| 33 | private const SCHEMA_VERSION_EARLIEST = '2.0.0'; |
| 34 | |
| 35 | /** |
| 36 | * @param RevisionRecord $revisionRecord |
| 37 | * @param string $schemaVersion |
| 38 | * @return array |
| 39 | */ |
| 40 | public function toArray( |
| 41 | RevisionRecord $revisionRecord, |
| 42 | string $schemaVersion = self::SCHEMA_VERSION_EARLIEST |
| 43 | ): array { |
| 44 | $revAttrs = [ |
| 45 | 'rev_id' => $revisionRecord->getId(), |
| 46 | 'rev_dt' => EventSerializer::timestampToDt( $revisionRecord->getTimestamp() ), |
| 47 | 'is_minor_edit' => $revisionRecord->isMinor(), |
| 48 | 'rev_sha1' => $revisionRecord->getSha1(), |
| 49 | 'rev_size' => $revisionRecord->getSize() |
| 50 | ]; |
| 51 | |
| 52 | // Set rev_parent_id unless getParentId is null. |
| 53 | // A rev_parent_id value of 0 indicates that there is no parent revision, |
| 54 | // while null indicates that the parent revision is unknown. |
| 55 | // See: |
| 56 | // https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/core/+/b8ce64464379bff6fbc00d992e1946e4a155b7e9/includes/Revision/RevisionStoreRecord.php#82 |
| 57 | // https://phabricator.wikimedia.org/T420974#11887286 |
| 58 | if ( $revisionRecord->getParentId() !== null ) { |
| 59 | $revAttrs['rev_parent_id'] = $revisionRecord->getParentId(); |
| 60 | } |
| 61 | |
| 62 | // If getComment is null, don't set it. However, we still set comment is an |
| 63 | // empty string, to differentiate between a hidden comment and an empty comment |
| 64 | // left by the editor. |
| 65 | if ( $revisionRecord->getComment() !== null ) { |
| 66 | $revAttrs['comment'] = $revisionRecord->getComment()->text; |
| 67 | } |
| 68 | |
| 69 | // Include this revision's visibility settings. |
| 70 | $revAttrs += self::bitsToVisibilityAttrs( $revisionRecord->getVisibility() ); |
| 71 | |
| 72 | return $revAttrs; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Converts a revision visibility hidden bitfield to an array with keys |
| 77 | * of each of the possible visibility settings name mapped to a boolean. |
| 78 | * |
| 79 | * @param int $bitfield RevisionRecord $mDeleted bitfield. |
| 80 | * @return array |
| 81 | */ |
| 82 | public static function bitsToVisibilityAttrs( int $bitfield ): array { |
| 83 | return [ |
| 84 | 'is_content_visible' => self::isVisible( $bitfield, RevisionRecord::DELETED_TEXT ), |
| 85 | 'is_editor_visible' => self::isVisible( $bitfield, RevisionRecord::DELETED_USER ), |
| 86 | 'is_comment_visible' => self::isVisible( $bitfield, RevisionRecord::DELETED_COMMENT ), |
| 87 | ]; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Checks if RevisionRecord::DELETED_* field is set in the $hiddenBits |
| 92 | * |
| 93 | * @param int $hiddenBits revision visibility bitfield |
| 94 | * @param int $field RevisionRecord::DELETED_* field to check |
| 95 | * @return bool |
| 96 | */ |
| 97 | private static function isVisible( int $hiddenBits, int $field ): bool { |
| 98 | // It would probably be better to use the $revisionRecord-audienceCan method |
| 99 | // than use our custom logic here to compare the bits with RevisionRecord constants, |
| 100 | // But then we wouldn't have a way of computing the visibleness of a revision |
| 101 | // without a RevisionRecord instance, which is something we have to do in the case |
| 102 | // of ArticleRevisionVisibilitySetHook, where we only get the $oldBits, but not the |
| 103 | // old RevisionRecord. |
| 104 | return ( $hiddenBits & $field ) != $field; |
| 105 | } |
| 106 | |
| 107 | } |