summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/tests/phpUnitEnvironment.php
blob: 0a05c762471dd0a352e9396625e86f749727be69 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php

/**
 * @private
 *
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class PHPUnitEnvironment {

	/**
	 * @var array
	 */
	private $gitHead = [];

	/**
	 * @param array $args
	 *
	 * @return boolean
	 */
	public function hasDebugRequest( $args ) {
		return array_search( '--debug', $args ) || array_search( '--debug-tests', $args );
	}

	public function emptyDebugVars() {
		$GLOBALS['wgDebugLogGroups'] = [];
		$GLOBALS['wgDebugLogFile'] = '';
	}

	/**
	 * @return boolean
	 */
	public function enabledDebugLogs() {
		return $GLOBALS['wgDebugLogGroups'] !== [] || $GLOBALS['wgDebugLogFile'] !== '';
	}

	/**
	 * @return boolean|integer
	 */
	public function getXdebugInfo() {

		if ( extension_loaded( 'xdebug' ) && xdebug_is_enabled() ) {
			return phpversion( 'xdebug' );
		}

		return false;
	}

	/**
	 * @return string
	 */
	public function getSiteLanguageCode() {
		return $GLOBALS['wgLanguageCode'];
	}

	/**
	 * @return string
	 */
	public function executionTime() {
		$dateTimeUtc = new \DateTime( 'now', new \DateTimeZone( 'UTC' ) );
		return $dateTimeUtc->format( 'Y-m-d h:i' );
	}

	/**
	 * @param string $id
	 *
	 * @return array
	 */
	public function getVersion( $id, $extra = [] ) {

		$info = [];

		if ( $id === 'smw' ) {
			$store = str_replace(
				[ '{', '}', '"', '(SMW', ':(', '))', ',' ],
				[ '(', ')', '', 'SMW', ' (', ')', ', ' ],
				json_encode( smwfGetStore()->getInfo(), JSON_UNESCAPED_SLASHES )
			);

			$info = [
				SemanticMediaWiki::getVersion(),
				'git: ' . $this->getGitInfo( 'smw' ),
				$store
			] + $extra;
		}

		if ( $id === 'mw' ) {
			$info = [
				$GLOBALS['wgVersion'],
				'git: ' . $this->getGitInfo( 'mw' )
			] + $extra;
		}

		return implode( ', ', $info );
	}

	/**
	 * @param string $id
	 *
	 * @return string
	 */
	public function getGitInfo( $id ) {

		if ( $this->gitHead === [] && class_exists( 'GitInfo' ) ) {
			$this->gitHead = [
				'mw' => '',
				'smw' => ''
			];

			$this->gitHead['mw'] = GitInfo::headSHA1();

			if ( $this->gitHead['mw'] ) {
				$this->gitHead['mw'] = substr( $this->gitHead['mw'], 0, 7 );
			} else {
				$this->gitHead['mw'] = 'N/A';
			}

			$gitInfo = new GitInfo( __DIR__ . '/..' );
			$this->gitHead['smw'] = $gitInfo->getHeadSHA1();

			if ( $this->gitHead['smw'] ) {
				$this->gitHead['smw'] = substr( $this->gitHead['smw'], 0, 7 );
			}
		}

		if ( isset( $this->gitHead[$id] ) ) {
			return $this->gitHead[$id];
		}
	}

	/**
	 * @param string $arg1
	 * @param string|array $arg2
	 *
	 * @return string
	 */
	public function writeLn( $arg1, $arg2 ) {
		return print sprintf( "%-20s%s\n", $arg1, $arg2 );
	}

	/**
	 * @param string $arg1
	 * @param string|array $arg2
	 *
	 * @return string
	 */
	public function writeNewLn( $arg1 = '', $arg2 = '' ) {

		if ( $arg1 === '' && $arg2 === '' ) {
			return print "\n";
		}

		return print sprintf( "\n%-20s%s\n", $arg1, $arg2 );
	}

}