summaryrefslogtreecommitdiff
path: root/bin/wiki/vendor/addwiki/mediawiki-datamodel/src/Content.php
blob: c1372d46ef60b30261a1f0186d6fb2e9fc314933 (plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php

namespace Mediawiki\DataModel;

use LogicException;

/**
 * Class Representing the content of a revision
 * @author Addshore
 */
class Content {

	/**
	 * @var string sha1 hash of the object content upon creation
	 */
	private $initialHash;

	/**
	 * @var mixed
	 */
	private $data;

	/**
	 * @var string|null
	 */
	private $model;

	/**
	 * Should always be called AFTER overriding constructors so a hash can be created
	 *
	 * @param mixed $data
	 * @param string|null $model
	 */
	public function __construct( $data, $model = null ) {
		$this->data = $data;
		$this->model = $model;
		$this->initialHash = $this->getHash();
	}

	/**
	 * @return string
	 */
	public function getModel() {
		return $this->model;
	}

	/**
	 * Returns a sha1 hash of the content
	 *
	 * @throws LogicException
	 * @return string
	 */
	public function getHash() {
		$data = $this->getData();
		if( is_object( $data ) ) {
			if( method_exists( $data, 'getHash' ) ) {
				return $data->getHash();
			} else {
				return sha1( serialize( $data ) );
			}
		}
		if( is_string( $data ) ) {
			return sha1( $data );
		}
		throw new LogicException( "Cant get hash for data of type: " . gettype( $data ) );
	}

	/**
	 * Has the content been changed since object construction (this shouldn't happen!)
	 * @return bool
	 */
	public function hasChanged() {
		return $this->initialHash !== $this->getHash();
	}

	/**
	 * @return mixed
	 */
	public function getData() {
		return $this->data;
	}

}