Completed
Push — address-as-title ( 9b6eeb...601934 )
by Peter
11:07
created

MapsLayerPage::hasUsableLayer()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 15
nc 7
nop 2
dl 0
loc 25
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Article in layer namespace with maps layer specific information rendering at
5
 * beginning and top of the page.
6
 *
7
 * @since 3.0 (class with same name in 0.7.1+ but different purpose, 100% rewritten in 3.0)
8
 *
9
 * @licence GNU GPL v2+
10
 * @author Jeroen De Dauw < [email protected] >
11
 * @author Daniel Werner
12
 */
13
class MapsLayerPage extends Article {
14
15
	protected $usageTitles = null;
16
17
	function __construct( Title $title, $oldId = null ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for __construct.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
18
		parent::__construct( $title, $oldId );
19
	}
20
21
	/**
22
	 * Designed similar to CategoryPage.
23
	 *
24
	 * @see Article::view
25
	 *
26
	 * @since 3.0
27
	 */
28
	public function view() {
29
		global $wgRequest, $wgUser;
30
31
		$diff = $wgRequest->getVal( 'diff' );
32
		$diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
33
34
		if( isset( $diff ) && $diffOnly ) {
35
			return parent::view();
36
		}
37
38
		if( !Hooks::run( 'MapsLayerPageView', [ &$this ] ) ) {
39
			return;
40
		}
41
42
		if( Maps_NS_LAYER == $this->mTitle->getNamespace() ) {
43
			$this->openShowLayer();
44
		}
45
46
		parent::view();
47
48
		if( Maps_NS_LAYER == $this->mTitle->getNamespace() ) {
49
			$this->closeShowLayer();
50
		}
51
	}
52
53
	/**
54
	 * Function called before page rendering.
55
	 *
56
	 * @since 3.0
57
	 */
58
	protected function openShowLayer() {
59
	}
60
61
	/**
62
	 * Function called at the end of page rendering.
63
	 *
64
	 * @since 3.0
65
	 */
66
	protected function closeShowLayer() {
67
		$this->renderUsage();
68
	}
69
70
	/**
71
	 * Renders the category-page like view which shows the usage of this
72
	 * layer page in other pages.
73
	 *
74
	 * @since 3.0
75
	 */
76
	public function renderUsage() {
77
		global $wgOut;
78
		$out = '';
0 ignored issues
show
Unused Code introduced by
$out is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
79
80
		$titles = $this->getUsageTitles();
81
82
		$viewer = new CategoryViewer( $this->mTitle, $this->getContext() );
83
		$viewer->limit = 9999; // just overwrite the default limit of pages displayed in a normal category
84
85
		// now add apges in sorted order to category viewer:
86
		foreach( $titles as $title ) {
87
			$viewer->addPage( $title, $title->getPrefixedText(), null );
88
		}
89
90
		//$wgOut->addHTML( $viewer->formatList( $viewer->articles, '' ) );
91
		$out  = "<div id=\"mw-pages\">\n";
92
		$out .= '<h2>' . wfMessage( 'maps-layerpage-usage', $this->mTitle->getText() )->text() . "</h2>\n";
93
94
		if( !empty( $viewer->articles ) ) {
95
			$out .= $viewer->formatList( $viewer->articles, $viewer->articles_start_char );
96
		} else {
97
			$out .= wfMessage( 'maps-layerpage-nousage' )->text();
98
		}
99
		$out .= "\n</div>";
100
101
		$wgOut->addHTML( $out );
102
	}
103
104
	/**
105
	 * returns all Titles using this layer page
106
	 *
107
	 * @since 3.0
108
	 *
109
	 * @return Array
110
	 */
111
	public function getUsageTitles() {
112
		// cached result:
113
		if( $this->usageTitles !== null ) {
114
			return $this->usageTitles;
115
		}
116
117
		$db = wfGetDB( DB_SLAVE );
118
		$items = $db->select(
119
			'templatelinks',
120
			'*',
121
			[
122
				'tl_title = "' . $this->mTitle->getDBkey() . '"',
123
				'tl_namespace = ' . $this->mTitle->getNamespace(),
124
			],
125
			__METHOD__
126
		);
127
128
		// get pages and sort them first:
129
		$titles = [];
130
		foreach( $items as $item ) {
131
			$title = Title::newFromID( $item->tl_from );
132
133
			if( $title !== null ) {
134
				$titles[ $title->getPrefixedText() ] = $title;
135
			}
136
		}
137
		unset( $items, $item );
138
		ksort( $titles, SORT_STRING );
139
140
		return $this->usageTitles = $titles;
141
	}
142
143
	/**
144
	 * Returns if the layer page has any layer defined which has a definition that is 'ok',
145
	 * meaning, the layer can be used in a map.
146
	 *
147
	 * @since 3.0
148
	 *
149
	 * @param string $name if set, only for the layer definition with this name will be searched.
150
	 * @param string $service if set, only layers supporthing this service will be taken in account.
151
	 *
152
	 * @return boolean
153
	 */
154
	public function hasUsableLayer( $name = null, $service = null ) {
155
		// if name is given, get only that layer to check on, otherwise all:
156
		if( $name !== null ) {
157
			$layer = MapsLayers::loadLayer( $this->getTitle(), $name );
158
159
			if( $layer === null ) {
160
				return false;
161
			}
162
			$layers = [ $layer ];
163
		} else {
164
			$layers = MapsLayers::loadLayerGroup( $this->getTitle() );
165
			$layers = $layers->getLayers();
166
		}
167
168
		// datermine whether any layer is usable:
169
		foreach( $layers as $layerName => $layer )  {
170
			if(
171
				$layer->isOk() // doesn't have to be 100% valid, just valid enough to be usable!
172
				&& ( $service === null || $layer->isSupportingService( $service ) )
173
			) {
174
				return true;
175
			}
176
		}
177
		return false;
178
	}
179
}
180