Completed
Push — master ( 756515...e0d7bf )
by Jeroen De
08:10 queued 08:07
created

Setup   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 38.32%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 149
ccs 23
cts 60
cp 0.3832
rs 10
wmc 10
lcom 1
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A run() 0 5 1
B registerCacheInvalidator() 0 45 4
B registerUnitTests() 0 28 3
B registerParserHooks() 0 33 1
1
<?php
2
3
namespace SubPageList;
4
5
use Parser;
6
use RecursiveDirectoryIterator;
7
use RecursiveIteratorIterator;
8
use Revision;
9
use SplFileInfo;
10
use Title;
11
use User;
12
use WikiPage;
13
14
/**
15
 * Object containing the logic to set up the SupPageList extension.
16
 *
17
 * @since 1.0
18
 *
19
 * @licence GNU GPL v2+
20
 * @author Jeroen De Dauw < [email protected] >
21
 */
22
class Setup {
23
24
	/**
25
	 * @var Extension
26
	 */
27
	private $extension;
28
29
	/**
30
	 * @var array[]
31
	 */
32
	private $hooks;
33
34
	/**
35
	 * @var string
36
	 */
37
	private $rootDirectory;
38
39
	/**
40
	 * @param Extension $extension
41
	 * @param array $hooks
42
	 * @param string $rootDirectory
43
	 */
44 1
	public function __construct( Extension $extension, array &$hooks, $rootDirectory ) {
45 1
		$this->hooks =& $hooks;
46 1
		$this->extension = $extension;
47 1
		$this->rootDirectory = $rootDirectory;
48 1
	}
49
50
	/**
51
	 * Sets up the SubPageList extension.
52
	 *
53
	 * @since 1.0
54
	 */
55 1
	public function run() {
56 1
		$this->registerParserHooks();
57 1
		$this->registerCacheInvalidator();
58 1
		$this->registerUnitTests();
59 1
	}
60
61 1
	private function registerParserHooks() {
62 1
		$extension = $this->extension;
63
64
		/**
65
		 * Called when the parser initialises for the first time.
66
		 * https://www.mediawiki.org/wiki/Manual:Hooks/ParserFirstCallInit
67
		 */
68
		$this->hooks['ParserFirstCallInit'][] = function( Parser &$parser ) use ( $extension ) {
69
			$hookRegistrant = $extension->getHookRegistrant( $parser );
70
71
			$hookRegistrant->registerFunctionHandler(
72
				$extension->getCountHookDefinition(),
73
				$extension->getCountHookHandler()
74
			);
75
76
			$hookRegistrant->registerFunctionHandler(
77
				$extension->getListHookDefinition(),
78
				$extension->getListHookHandler()
79
			);
80
81
			$hookRegistrant->registerHookHandler(
82
				$extension->getCountHookDefinition(),
83
				$extension->getCountHookHandler()
84
			);
85
86
			$hookRegistrant->registerHookHandler(
87
				$extension->getListHookDefinition(),
88
				$extension->getListHookHandler()
89
			);
90
91
			return true;
92
		};
93 1
	}
94
95 4
	private function registerCacheInvalidator() {
96 1
		$extension = $this->extension;
97
98
		/**
99
		 * Occurs after a new article has been created.
100
		 * https://www.mediawiki.org/wiki/Manual:Hooks/ArticleInsertComplete
101
		 */
102 1
		$this->hooks['ArticleInsertComplete'][]
103 1
			= function( WikiPage $article, User &$user, $text, $summary, $minorEdit,
0 ignored issues
show
Unused Code introduced by
The parameter $user is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $text is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $summary is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $minorEdit is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
104
			$watchThis, $sectionAnchor, &$flags, Revision $revision ) use ( $extension ) {
0 ignored issues
show
Unused Code introduced by
The parameter $watchThis is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $sectionAnchor is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $flags is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $revision is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
105
106 4
			if ( $extension->getSettings()->get( Settings::AUTO_REFRESH ) ) {
107
				$extension->getCacheInvalidator()->invalidateCaches( $article->getTitle() );
108
			}
109
110 4
			return true;
111
		};
112
113
		/**
114
		 * Occurs after the delete article request has been processed.
115
		 * https://www.mediawiki.org/wiki/Manual:Hooks/ArticleDeleteComplete
116
		 */
117
		$this->hooks['ArticleDeleteComplete'][] = function( &$article, User &$user, $reason, $id ) use ( $extension ) {
0 ignored issues
show
Unused Code introduced by
The parameter $user is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $reason is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
118
			if ( $extension->getSettings()->get( Settings::AUTO_REFRESH ) ) {
119
				$extension->getCacheInvalidator()->invalidateCaches( $article->getTitle() );
120
			}
121
122
			return true;
123
		};
124
125
		/**
126
		 * Occurs whenever a request to move an article is completed.
127
		 * https://www.mediawiki.org/wiki/Manual:Hooks/TitleMoveComplete
128
		 */
129
		$this->hooks['TitleMoveComplete'][] = function( Title &$title, Title &$newTitle, User &$user, $oldId, $newId ) use ( $extension ) {
0 ignored issues
show
Unused Code introduced by
The parameter $user is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $oldId is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $newId is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
130
			if ( $extension->getSettings()->get( Settings::AUTO_REFRESH ) ) {
131
				$invalidator = $extension->getCacheInvalidator();
132
133
				$invalidator->invalidateCaches( $title );
134
				$invalidator->invalidateCaches( $newTitle );
135
			}
136
137
			return true;
138
		};
139 1
	}
140
141 1
	private function registerUnitTests() {
142 1
		$rootDirectory = $this->rootDirectory;
143
144
		/**
145
		 * Hook to add PHPUnit test cases.
146
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/UnitTestsList
147
		 *
148
		 * @since 1.0
149
		 *
150
		 * @param array $files
151
		 *
152
		 * @return boolean
153
		 */
154
		$this->hooks['UnitTestsList'][]	= function( array &$files ) use ( $rootDirectory ) {
155
			$directoryIterator = new RecursiveDirectoryIterator( $rootDirectory . '/tests/' );
156
157
			/**
158
			 * @var SplFileInfo $fileInfo
159
			 */
160
			foreach ( new RecursiveIteratorIterator( $directoryIterator ) as $fileInfo ) {
161
				if ( substr( $fileInfo->getFilename(), -8 ) === 'Test.php' ) {
162
					$files[] = $fileInfo->getPathname();
163
				}
164
			}
165
166
			return true;
167
		};
168 1
	}
169
170
}
171