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
3// NO_NAMESPACE Compatibility with serialized objects in permanent storage
4
5use MediaWiki\Storage\BlobAccessException;
6
7class HistoryBlobUtils {
8
9    /**
10     * Get the classes which are allowed to be contained in a text or ES row
11     *
12     * @return string[]
13     */
14    public static function getAllowedClasses() {
15        return [
16            ConcatenatedGzipHistoryBlob::class,
17            DiffHistoryBlob::class,
18            HistoryBlobCurStub::class,
19            HistoryBlobStub::class
20        ];
21    }
22
23    /**
24     * Unserialize a HistoryBlob
25     *
26     * @param string $str
27     * @param bool $allowDouble Allow double serialization
28     * @return HistoryBlob|HistoryBlobStub|HistoryBlobCurStub|null
29     */
30    public static function unserialize( string $str, bool $allowDouble = false ) {
31        $obj = unserialize( $str, [ 'allowed_classes' => self::getAllowedClasses() ] );
32        if ( is_string( $obj ) && $allowDouble ) {
33            // Correct for old double-serialization bug (needed by HistoryBlobStub only)
34            $obj = unserialize( $obj, [ 'allowed_classes' => self::getAllowedClasses() ] );
35        }
36        foreach ( self::getAllowedClasses() as $class ) {
37            if ( $obj instanceof $class ) {
38                return $obj;
39            }
40        }
41        return null;
42    }
43
44    /**
45     * Unserialize array data with no classes
46     *
47     * @param string $str
48     * @return array
49     */
50    public static function unserializeArray( string $str ): array {
51        $array = unserialize( $str, [ 'allowed_classes' => false ] );
52        if ( !is_array( $array ) ) {
53            throw new BlobAccessException( "Expected array in serialized string" );
54        }
55        return $array;
56    }
57}