summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/DataValues/ValueFormatters/CodeStringValueFormatter.php
blob: d6f3a069971753fc4a9f12d145bd1070e8a1ccf6 (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
<?php

namespace SMW\DataValues\ValueFormatters;

use SMWDataValue as DataValue;
use SMWOutputs as Outputs;

/**
 * @license GNU GPL v2+
 * @since 2.4
 *
 * @author mwjames
 */
class CodeStringValueFormatter extends StringValueFormatter {

	/**
	 * @since 2.4
	 *
	 * {@inheritDoc}
	 */
	public function isFormatterFor( DataValue $dataValue ) {
		return $dataValue->getTypeID() === '_cod';
	}

	/**
	 * @see StringValueFormatter::doFormat
	 */
	protected function doFormat( $dataValue, $type, $linker ) {

		$abbreviate = $type === self::WIKI_LONG || $type === self::HTML_LONG;
		$text = $dataValue->getDataItem()->getString();

		// Escape and wrap values of type Code. The result is escaped to be
		// HTML-safe (it will also work in wiki context). The result will
		// contain mark-up that must not be escaped again.

		Outputs::requireResource( 'ext.smw.style' );

		if ( $this->isJson( $text ) ) {
			$result = self::asJson( $text );
		} else {
			// This disables all active wiki and HTML markup:
			$result = str_replace(
				[ '<code>', '</code>', '<nowiki>', '</nowiki>', '<', '>', ' ', '[', '{', '=', "'", ':', "\n", '&#x005B;' ],
				[ '', '', '', '', '&lt;', '&gt;', '&#160;', '&#91;', '&#x007B;', '&#x003D;', '&#x0027;', '&#58;', "<br />", '&#91;' ],
				$text
			);
		}

		if ( $abbreviate ) {
			$result = "<div style=\"min-height:5em; overflow:auto;\">$result</div>";
		}

		return "<div class=\"smwpre\">$result</div>";
	}

	/**
	 * @since 2.5
	 *
	 * @param string $string
	 *
	 * @return string
	 */
	public static function asJson( $string, $flag = 0 ) {

		if ( $flag > 0 ) {
			return json_encode( json_decode( $string ), $flag );
		}

		return json_encode( json_decode( $string ), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
	}

	private function isJson( $string ) {

		// Don't bother
		if ( substr( $string, 0, 1 ) !== '{' ) {
			return false;
		}

		json_decode( $string );

		return ( json_last_error() == JSON_ERROR_NONE );
	}

}