summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Utils/File/ContentsReader.php
blob: a9ac8a374ea27f45b40d8d504898061174416917 (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
<?php

namespace SMW\Tests\Utils\File;

use RuntimeException;

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

	/**
	 * @since 3.0
	 *
	 * @param string $file
	 *
	 * @return string
	 */
	public static function readContentsFrom( $file ) {

		$file = str_replace( [ '\\', '/' ], DIRECTORY_SEPARATOR, $file );

		if ( !is_readable( $file ) ) {
			throw new RuntimeException( "Could not open or read: $file" );
		}

		$contents = file_get_contents( $file );

		// http://php.net/manual/en/function.file-get-contents.php
		$contents = mb_convert_encoding(
			$contents,
			'UTF-8',
			mb_detect_encoding( $contents, 'UTF-8, ISO-8859-1, ISO-8859-2', true )
		);

		// https://stackoverflow.com/questions/2115549/php-difference-between-r-n-and-n
		return str_replace( "\r\n", "\n", $contents );
	}

}