Completed
Push — master ( 6a2e7b...b75141 )
by mw
20:14
created

PageDisplayOutputModifier   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 96.3%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 90
ccs 26
cts 27
cp 0.963
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setSubpageByNamespace() 0 3 1
A hideSubpageParent() 0 3 1
A modifyOutput() 0 15 4
A isSubpage() 0 17 3
A getPageTitle() 0 15 3
A hasEnabledSubpageByNamespace() 0 3 2
1
<?php
2
3
namespace SBL;
4
5
use OutputPage;
6
use Title;
7
use SMW\DataValueFactory;
8
use SMW\DIWikiPage;
9
10
/**
11
 * @license GNU GPL v2+
12
 * @since 1.0
13
 *
14
 * @author mwjames
15
 */
16
class PageDisplayOutputModifier {
17
18
	/**
19
	 * @var boolean
20
	 */
21
	private $hideSubpageParent;
22
23
	/**
24
	 * @var array
25
	 */
26
	private $subpageByNamespace;
27
28
	/**
29
	 * @since  1.0
30
	 *
31
	 * @param boolean $hideSubpageParent
32
	 */
33 3
	public function hideSubpageParent( $hideSubpageParent ) {
34 3
		$this->hideSubpageParent = $hideSubpageParent;
35 3
	}
36
37
	/**
38
	 * @since  1.0
39
	 *
40
	 * @param array $subpageByNamespace
41
	 */
42 3
	public function setSubpageByNamespace( array $subpageByNamespace ) {
43 3
		$this->subpageByNamespace = $subpageByNamespace;
44 3
	}
45
46
	/**
47
	 * @since  1.0
48
	 *
49
	 * @param OutputPage $outputPage
50
	 */
51 3
	public function modifyOutput( OutputPage $outputPage ) {
52
53 3
		$outputPage->addModuleStyles( 'ext.semanticbreadcrumblinks.styles' );
54 3
		$outputPage->addModules( 'ext.semanticbreadcrumblinks' );
55
56 3
		$title = $outputPage->getTitle();
57 2
58
		if ( !$this->hideSubpageParent || !$this->hasEnabledSubpageByNamespace( $title->getNamespace() ) ) {
59
			return;
60 1
		}
61 1
62 1
		if ( $this->isSubpage( $title ) ) {
63 1
			$outputPage->setPageTitle( $this->getPageTitle( $title ) );
64
		}
65 1
	}
66
67 1
	private function isSubpage( Title $title ) {
68
69 1
		if ( !$title->isSubpage() ) {
70 1
			return false;
71 1
		}
72
73
		$parts = explode( '/', $title->getText() );
74 1
75 1
		if ( count( $parts ) > 1 ) {
76 1
			unset( $parts[count( $parts ) - 1] );
77
		}
78 1
79
		$base = implode( '/', $parts );
80
81 2
		// #23 (Foo /Bar vs. Foo/ Bar)
82 2
		return substr( $base, -1 ) !== ' ';
83
	}
84
85
	private function getPageTitle( Title $title ) {
86
87
		$displayTitle = '';
88
89
		$dataValue = DataValueFactory::getInstance()->newDataItemValue(
0 ignored issues
show
Deprecated Code introduced by
The method SMW\DataValueFactory::newDataItemValue() has been deprecated with message: since 2.4, use DataValueFactory::newDataValueByItem

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...
90
			DIWikiPage::newFromTitle( $title )
91
		);
92
93
		// 2.4+
94
		if ( method_exists( $dataValue , 'getDisplayTitle' ) ) {
95
			$displayTitle = $dataValue->getDisplayTitle();
96
		}
97
98
		return $displayTitle !== '' ? $displayTitle : $title->getSubpageText();
99
	}
100
101
	private function hasEnabledSubpageByNamespace( $namespace ) {
102
		return isset( $this->subpageByNamespace[ $namespace ] ) && $this->subpageByNamespace[ $namespace ];
103
	}
104
105
}
106