MediaWikiNsContentMapper::findTemplateForType()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
cc 3
nc 4
nop 1
crap 3
1
<?php
2
3
namespace SCI;
4
5
use SMW\MediaWiki\MediaWikiNsContentReader;
0 ignored issues
show
Bug introduced by
The type SMW\MediaWiki\MediaWikiNsContentReader was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
7
/**
8
 * @license GNU GPL v2+
9
 * @since 1.0
10
 *
11
 * @author mwjames
12
 */
13
class MediaWikiNsContentMapper {
14
15
	/**
16
	 * @var MediaWikiNsContentReader
17
	 */
18
	private $mediaWikiNsContentReader;
19
20
	/**
21
	 * @var array
22
	 */
23
	private static $identifierToPropertyMap = [];
24
25
	/**
26
	 * @var array
27
	 */
28
	private static $typeToTemplateMap = [];
29
30
	/**
31
	 * Only set during testing to circumvent the MessageCache
32
	 *
33
	 * @var boolean
34
	 */
35
	public static $skipMessageCache = false;
36
37
	/**
38
	 * @since 1.0
39
	 *
40
	 * @param MediaWikiNsContentReader $mediaWikiNsContentReader
41
	 */
42 24
	public function __construct( MediaWikiNsContentReader $mediaWikiNsContentReader ) {
43 24
		$this->mediaWikiNsContentReader = $mediaWikiNsContentReader;
44 24
	}
45
46
	/**
47
	 * @since 1.0
48
	 */
49 14
	public static function clear() {
50 14
		self::$typeToTemplateMap = [];
51 14
		self::$identifierToPropertyMap = [];
52 14
		self::$skipMessageCache = false;
53 14
	}
54
55
	/**
56
	 * @since 1.0
57
	 *
58
	 * @param string $id
59
	 *
60
	 * @return string
61
	 */
62 23
	public function findPropertyForId( $id ) {
63
64 23
		$id = strtolower( trim( $id ) );
65
66 23
		if ( self::$identifierToPropertyMap === [] ) {
67
68
			// Fixed by definition due to usage of
69
			// pre-defined properties
70
			self::$identifierToPropertyMap = [
71 15
				'viaf'          => SCI_PROP_VIAF,
72 15
				'doi'           => SCI_PROP_DOI,
73 15
				'oclc'          => SCI_PROP_OCLC,
74 15
				'olid'          => SCI_PROP_OLID,
75 15
				'pmcid'         => SCI_PROP_PMCID,
76 15
				'pmid'          => SCI_PROP_PMID,
77 15
				'reference'     => SCI_PROP_CITE_KEY,
78 15
				'citation text' => SCI_PROP_CITE_TEXT,
79 15
				'sortkey'       => '_SKEY'
80 15
			] + $this->parseContentFor( "sci-property-definition" );
81
		}
82
83 23
		return isset( self::$identifierToPropertyMap[$id] ) ? self::$identifierToPropertyMap[$id] : null;
84
	}
85
86
	/**
87
	 * @since 1.0
88
	 *
89
	 * @param string $type
90
	 *
91
	 * @return string
92
	 */
93 2
	public function findTemplateForType( $type ) {
94
95 2
		$type = strtolower( trim( $type ) );
96
97 2
		if ( self::$typeToTemplateMap === [] ) {
98 2
			self::$typeToTemplateMap = $this->parseContentFor( "sci-template-definition" );
99
		}
100
101 2
		return isset( self::$typeToTemplateMap[$type] ) ? self::$typeToTemplateMap[$type] : null;
102
	}
103
104
	/**
105
	 * @param string $name
106
	 */
107 15
	private function parseContentFor( $name ) {
108
109 15
		if ( self::$skipMessageCache ) {
110 14
			$this->mediaWikiNsContentReader->skipMessageCache();
111
		}
112
113 15
		$contents = (string)$this->mediaWikiNsContentReader->read( $name );
114
115 15
		if ( $contents === '' ) {
116 12
			return [];
117
		}
118
119 4
		$contentMap = array_map( 'trim', preg_split( "([\n][\s]?)", $contents ) );
120 4
		$list = [];
121
122
		// Check whether the first line is infact an explanation
123 4
		if ( strpos( $contentMap[0], '|' ) === false ) {
124 4
			array_shift( $contentMap );
125
		}
126
127 4
		foreach ( $contentMap as $map ) {
128
129 3
			if ( strpos( $map, '|' ) === false ) {
130
				continue;
131
			}
132
133 3
			list( $id, $value ) = explode( '|', $map, 2 );
134 3
			$list[str_replace( '_', ' ', strtolower( trim( $id ) ) )] = $value;
135
		}
136
137 4
		return $list;
138
	}
139
140
}
141