Completed
Push — master ( 9c77e9...04aceb )
by
unknown
02:26
created

SubpageParentAnnotator::addAnnotation()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 16
cts 16
cp 1
rs 9.1448
c 0
b 0
f 0
cc 5
nc 4
nop 0
crap 5
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 $enableSubpageParentAnnotation = true;
27
28
	/**
29
	 * @since 1.3
30
	 *
31
	 * @param ParserData $parserData
32
	 */
33 5
	public function __construct( ParserData $parserData ) {
34 5
		$this->parserData = $parserData;
35 5
	}
36
37
	/**
38
	 * @since 1.3
39
	 *
40
	 * @param boolean $enableSubpageParentAnnotation
41
	 */
42 3
	public function enableSubpageParentAnnotation( $enableSubpageParentAnnotation ) {
43 3
		$this->enableSubpageParentAnnotation = (bool)$enableSubpageParentAnnotation;
44 3
	}
45
46
	/**
47
	 * @since  1.3
48
	 */
49 4
	public function addAnnotation() {
50
51 4
		$title = $this->parserData->getTitle();
52
53 4
		if ( !$this->enableSubpageParentAnnotation || strpos( $title->getText(), '/' ) === false ) {
54 1
			return;
55
		}
56
57 3
		$property = DIProperty::newFromUserLabel( SBL_PROP_PARENTPAGE );
58
59
		// Don't override any "man"-made annotation
60 3
		if ( $this->parserData->getSemanticData()->getPropertyValues( $property ) !== [] ) {
61 1
			return;
62
		}
63
64 2
		$base = $this->getBaseText( $title );
65
66
		// #23
67 2
		if ( substr( $base, -1 ) === ' ' ) {
68 1
			return;
69
		}
70
71 1
		$this->parserData->getSemanticData()->addPropertyObjectValue(
72 1
			$property,
73 1
			DIWikiPage::newFromText( $base, $title->getNamespace() )
74 1
		);
75
76 1
		$this->parserData->pushSemanticDataToParserOutput();
0 ignored issues
show
Deprecated Code introduced by
The method SMW\ParserData::pushSemanticDataToParserOutput() has been deprecated with message: since 3.0, use copyToParserOutput

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
77 1
	}
78
79
	// Don't rely on Title::getBaseText as it depends on the wgNamespacesWithSubpages
80
	// setting and if set false will return the normal text including its subparts
81 2
	private function getBaseText( $title ) {
82
83 2
		$parts = explode( '/', $title->getText() );
84
		# Don't discard the real title if there's no subpage involved
85 2
		if ( count( $parts ) > 1 ) {
86 2
			unset( $parts[count( $parts ) - 1] );
87 2
		}
88
89 2
		return implode( '/', $parts );
90
	}
91
92
}
93