Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
88.89% |
16 / 18 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| RevisionSlotEntitySerializer | |
88.89% |
16 / 18 |
|
50.00% |
1 / 2 |
6.05 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toArray | |
88.24% |
15 / 17 |
|
0.00% |
0 / 1 |
5.04 | |||
| 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\Content\IContentHandlerFactory; |
| 24 | use MediaWiki\Exception\MWUnknownContentModelException; |
| 25 | use MediaWiki\Revision\SlotRecord; |
| 26 | |
| 27 | /** |
| 28 | * Converts a SlotRecord into an array that matches the |
| 29 | * fragment/mediawiki/state/entity/revision_slot schema, |
| 30 | * without content bodies. |
| 31 | */ |
| 32 | class RevisionSlotEntitySerializer { |
| 33 | /** |
| 34 | * @var IContentHandlerFactory |
| 35 | */ |
| 36 | private IContentHandlerFactory $contentHandlerFactory; |
| 37 | |
| 38 | /** |
| 39 | * @param IContentHandlerFactory $contentHandlerFactory |
| 40 | */ |
| 41 | public function __construct( |
| 42 | IContentHandlerFactory $contentHandlerFactory |
| 43 | ) { |
| 44 | $this->contentHandlerFactory = $contentHandlerFactory; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @param SlotRecord $slotRecord |
| 49 | * @return array |
| 50 | */ |
| 51 | public function toArray( SlotRecord $slotRecord ): array { |
| 52 | $contentModel = $slotRecord->getModel(); |
| 53 | $contentFormat = $slotRecord->getFormat(); |
| 54 | |
| 55 | if ( $contentFormat === null ) { |
| 56 | try { |
| 57 | $contentHandler = $this->contentHandlerFactory->getContentHandler( $contentModel ); |
| 58 | $contentFormat = $contentHandler->getDefaultFormat(); |
| 59 | } catch ( MWUnknownContentModelException ) { |
| 60 | // Ignore, `content_format` is not required. |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | $slotAttrs = [ |
| 65 | 'slot_role' => $slotRecord->getRole(), |
| 66 | 'content_model' => $contentModel, |
| 67 | 'content_sha1' => $slotRecord->getSha1(), |
| 68 | 'content_size' => $slotRecord->getSize(), |
| 69 | ]; |
| 70 | |
| 71 | if ( $contentFormat !== null ) { |
| 72 | $slotAttrs['content_format'] = $contentFormat; |
| 73 | } |
| 74 | |
| 75 | if ( $slotRecord->hasOrigin() ) { |
| 76 | // unclear if necessary to guard against missing origin in this context but since it |
| 77 | // might fail on unsaved content we are better safe than sorry |
| 78 | $slotAttrs['origin_rev_id'] = $slotRecord->getOrigin(); |
| 79 | } |
| 80 | |
| 81 | return $slotAttrs; |
| 82 | } |
| 83 | } |