Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.24% covered (warning)
88.24%
15 / 17
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
HistoryBlobUtils
88.24% covered (warning)
88.24%
15 / 17
33.33% covered (danger)
33.33%
1 / 3
8.10
0.00% covered (danger)
0.00%
0 / 1
 getAllowedClasses
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 unserialize
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
5.07
 unserializeArray
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
1<?php
2
3use MediaWiki\Storage\BlobAccessException;
4
5class HistoryBlobUtils {
6
7    /**
8     * Get the classes which are allowed to be contained in a text or ES row
9     *
10     * @return string[]
11     */
12    public static function getAllowedClasses() {
13        return [
14            ConcatenatedGzipHistoryBlob::class,
15            DiffHistoryBlob::class,
16            HistoryBlobCurStub::class,
17            HistoryBlobStub::class
18        ];
19    }
20
21    /**
22     * Unserialize a HistoryBlob
23     *
24     * @param string $str
25     * @param bool $allowDouble Allow double serialization
26     * @return HistoryBlob|HistoryBlobStub|HistoryBlobCurStub|null
27     */
28    public static function unserialize( string $str, bool $allowDouble = false ) {
29        $obj = unserialize( $str, [ 'allowed_classes' => self::getAllowedClasses() ] );
30        if ( is_string( $obj ) && $allowDouble ) {
31            // Correct for old double-serialization bug (needed by HistoryBlobStub only)
32            $obj = unserialize( $obj, [ 'allowed_classes' => self::getAllowedClasses() ] );
33        }
34        foreach ( self::getAllowedClasses() as $class ) {
35            if ( $obj instanceof $class ) {
36                return $obj;
37            }
38        }
39        return null;
40    }
41
42    /**
43     * Unserialize array data with no classes
44     *
45     * @param string $str
46     * @return array
47     */
48    public static function unserializeArray( string $str ): array {
49        $array = unserialize( $str, [ 'allowed_classes' => false ] );
50        if ( !is_array( $array ) ) {
51            throw new BlobAccessException( "Expected array in serialized string" );
52        }
53        return $array;
54    }
55}