BySubpageLinksFinder::findLinksBySubject()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace SBL;
4
5
use SMW\DIWikiPage;
6
use Title;
7
8
/**
9
 * @license GNU GPL v2+
10
 * @since 1.0
11
 *
12
 * @author mwjames
13
 */
14
class BySubpageLinksFinder {
15
16
	/**
17
	 * @var boolean
18
	 */
19
	private $isDiscoveryFallback = true;
20
21
	/**
22
	 * @var array
23
	 */
24
	private $antecedentHierarchyLinks = [];
25
26
	/**
27
	 * @since 1.0
28
	 *
29
	 * @param boolean $isDiscoveryFallback
30
	 */
31 11
	public function setSubpageDiscoveryFallback( $isDiscoveryFallback ) {
32 11
		$this->isDiscoveryFallback = $isDiscoveryFallback;
33 11
	}
34
35
	/**
36
	 * @since 1.0
37
	 *
38
	 * @return boolean
39
	 */
40 1
	public function isDiscoveryFallback() {
41 1
		return $this->isDiscoveryFallback;
42
	}
43
44
	/**
45
	 * @since  1.0
46
	 *
47
	 * @param DIWikiPage $subject
48
	 */
49 10
	public function findLinksBySubject( DIWikiPage $subject ) {
50
51 10
		$title = $subject->getTitle();
52
53
		// Use the text instead of the prefixedText to avoid a split
54
		// in cases where the NS contains a / (e.g. smw/schema:Foobar)
55 10
		$text = $title->getText();
56
57 10
		if ( !$this->canBuildLinksFromText( $text ) ) {
58 1
			return;
59
		}
60
61 9
		$this->buildHierarchicalLinksFromText( $text, $title->getNamespace() );
62 9
	}
63
64
	/**
65
	 * @since  1.0
66
	 *
67
	 * @return array
68
	 */
69 10
	public function getParents() {
70 10
		return $this->antecedentHierarchyLinks;
71
	}
72
73 10
	private function canBuildLinksFromText( $text ) {
74 10
		return preg_match( '/\//', $text );
75
	}
76
77 9
	private function buildHierarchicalLinksFromText( $text, $ns ) {
78
79 9
		$growinglink = '';
80 9
		$links = explode( '/', $text );
81
82
		// Remove the source
83 9
		array_pop( $links );
84
85 9
		foreach ( $links as $link ) {
86
87 9
			if ( $link !== '' && substr( $link, -1 ) !== ' ' ) {
88 6
				$growinglink .= $link;
89 6
				$this->antecedentHierarchyLinks[] = DIWikiPage::newFromTitle( Title::newFromText( $growinglink, $ns ) );
90 6
			}
91
92 9
			$growinglink .= '/';
93 9
		}
94 9
	}
95
96
}
97