aboutsummaryrefslogtreecommitdiffstats
path: root/includes/historyblob/HistoryBlobUtils.php
blob: 05ce3d3d8e6115b4e53887c345c69f28b8fc2fc6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php

use MediaWiki\Storage\BlobAccessException;

class HistoryBlobUtils {

	/**
	 * Get the classes which are allowed to be contained in a text or ES row
	 *
	 * @return string[]
	 */
	public static function getAllowedClasses() {
		return [
			ConcatenatedGzipHistoryBlob::class,
			DiffHistoryBlob::class,
			HistoryBlobCurStub::class,
			HistoryBlobStub::class
		];
	}

	/**
	 * Unserialize a HistoryBlob
	 *
	 * @param string $str
	 * @param bool $allowDouble Allow double serialization
	 * @return HistoryBlob|HistoryBlobStub|HistoryBlobCurStub|null
	 */
	public static function unserialize( string $str, bool $allowDouble = false ) {
		$obj = unserialize( $str, [ 'allowed_classes' => self::getAllowedClasses() ] );
		if ( is_string( $obj ) && $allowDouble ) {
			// Correct for old double-serialization bug (needed by HistoryBlobStub only)
			$obj = unserialize( $obj, [ 'allowed_classes' => self::getAllowedClasses() ] );
		}
		foreach ( self::getAllowedClasses() as $class ) {
			if ( $obj instanceof $class ) {
				return $obj;
			}
		}
		return null;
	}

	/**
	 * Unserialize array data with no classes
	 *
	 * @param string $str
	 * @return array
	 */
	public static function unserializeArray( string $str ): array {
		$array = unserialize( $str, [ 'allowed_classes' => false ] );
		if ( !is_array( $array ) ) {
			throw new BlobAccessException( "Expected array in serialized string" );
		}
		return $array;
	}
}