Completed
Pull Request — master (#11)
by mw
02:56
created

SubpageParentAnnotator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 70
ccs 25
cts 25
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A setSubpageParentAnnotationState() 0 3 1
B addAnnotation() 0 22 4
A getBaseText() 0 10 2
1
<?php
2
3
namespace SBL;
4
5
use SMW\DIWikiPage;
6
use SMW\ParserData;
7
use SMW\DIProperty;
8
use Title;
9
10
/**
11
 * @license GNU GPL v2+
12
 * @since 1.3
13
 *
14
 * @author mwjames
15
 */
16
class SubpageParentAnnotator {
17
18
	/**
19
	 * @var ParserData
20
	 */
21
	private $parserData;
22
23
	/**
24
	 * @var boolean
25
	 */
26
	private $subpageParentAnnotationState = true;
27
28
	/**
29
	 * @since 1.3
30
	 *
31
	 * @param ParserData $parserData
32
	 */
33 4
	public function __construct( ParserData $parserData ) {
34 4
		$this->parserData = $parserData;
35 4
	}
36
37
	/**
38
	 * @since 1.3
39
	 *
40
	 * @param boolean $subpageParentAnnotationState
41
	 */
42 2
	public function setSubpageParentAnnotationState( $subpageParentAnnotationState ) {
43 2
		$this->subpageParentAnnotationState = (bool)$subpageParentAnnotationState;
44 2
	}
45
46
	/**
47
	 * @since  1.3
48
	 */
49 3
	public function addAnnotation() {
50
51 3
		$title = $this->parserData->getTitle();
52
53 3
		if ( !$this->subpageParentAnnotationState || strpos( $title->getText(), '/' ) === false ) {
54 1
			return;
55
		}
56
57 2
		$property = DIProperty::newFromUserLabel( SBL_PROP_PARENTPAGE );
58
59
		// Don't override any "man"-made annotation
60 2
		if ( $this->parserData->getSemanticData()->getPropertyValues( $property ) !== array() ) {
61 1
			return;
62
		}
63
64 1
		$this->parserData->getSemanticData()->addPropertyObjectValue(
65 1
			$property,
66 1
			DIWikiPage::newFromText( $this->getBaseText( $title ), $title->getNamespace() )
67 1
		);
68
69 1
		$this->parserData->pushSemanticDataToParserOutput();
70 1
	}
71
72
	// Don't rely on Title::getBaseText as it depends on the wgNamespacesWithSubpages
73
	// setting and if set false will return the normal text including its subparts
74 1
	private function getBaseText( $title ) {
75
76 1
		$parts = explode( '/', $title->getText() );
77
		# Don't discard the real title if there's no subpage involved
78 1
		if ( count( $parts ) > 1 ) {
79 1
			unset( $parts[count( $parts ) - 1] );
80 1
		}
81
82 1
		return implode( '/', $parts );
83
	}
84
85
}
86