Completed
Push — master ( e11051...f173c0 )
by mw
10s
created

HookRegistry::setOption()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
3
namespace SUC;
4
5
use Hooks;
6
7
/**
8
 * @license GNU GPL v2+
9
 * @since 1.0
10
 *
11
 * @author mwjames
12
 */
13
class HookRegistry {
14
15
	/**
16
	 * @var array
17
	 */
18
	private $handlers = array();
19
20
	/**
21
	 * @var Options
22
	 */
23
	private $options;
24
25
	/**
26
	 * @since 1.0
27
	 *
28
	 * @param Options $options
29
	 */
30 2
	public function __construct( Options $options ) {
31 2
		$this->options = $options;
32
33 2
		$this->addCallbackHandlers(
34 2
			$this->options
35 2
		);
36 2
	}
37
38
	/**
39
	 * @since  1.0
40
	 *
41
	 * @param string $name
42
	 *
43
	 * @return boolean
44
	 */
45 1
	public function isRegistered( $name ) {
46 1
		return Hooks::isRegistered( $name );
47
	}
48
49
	/**
50
	 * @since  1.0
51
	 */
52
	public function clear() {
53
		foreach ( $this->handlers as $name => $callback ) {
54
			Hooks::clear( $name );
55
		}
56
	}
57
58
	/**
59
	 * @since  1.0
60
	 *
61
	 * @param string $name
62
	 *
63
	 * @return Callable|false
64
	 */
65 1
	public function getHandlerFor( $name ) {
66 1
		return isset( $this->handlers[$name] ) ? $this->handlers[$name] : false;
67
	}
68
69
	/**
70
	 * @since  1.0
71
	 */
72
	public function register() {
73
		foreach ( $this->handlers as $name => $callback ) {
74
			Hooks::register( $name, $callback );
75
		}
76
	}
77
78 2
	private function addCallbackHandlers( $options ) {
0 ignored issues
show
Coding Style introduced by
addCallbackHandlers uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
79
80
		/**
81
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/BeforePageDisplay
82
		 */
83 2
		$this->handlers['BeforePageDisplay'] = function ( &$outputPage, &$skin ) {
0 ignored issues
show
Unused Code introduced by
The parameter $skin 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...
84
85 1
			$outputPage->addModuleStyles( 'ext.summary.cards.styles' );
86
87 1
			$outputPage->addModules(
88
				array(
89
					'ext.summary.cards'
90 1
				)
91 1
			);
92
93 1
			return true;
94
		};
95
96
		/**
97
		 * Hook: NewRevisionFromEditComplete called when a revision was inserted
98
		 * due to an edit
99
		 *
100
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/NewRevisionFromEditComplete
101
		 */
102 2
		$this->handlers['NewRevisionFromEditComplete'] = function ( $wikiPage, $revision, $baseId, $user ) {
0 ignored issues
show
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...
Unused Code introduced by
The parameter $baseId 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 $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...
103 1
			return BackendCache::getInstance()->invalidateCache( $wikiPage->getTitle() );
104
		};
105
106
		/**
107
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/GetPreferences
108
		 */
109 2
		$this->handlers['GetPreferences'] = function ( $user, &$preferences ) {
110
111
			// Option to enable tooltip info
112 1
			$preferences['suc-tooltip-disabled'] = array(
113 1
				'type'          => 'toggle',
114 1
				'label-message' => 'suc-tooltip-disabled',
115 1
				'section'       => 'rendering/suc-options',
116
			);
117
118 1
			return true;
119
		};
120
121
		/**
122
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/ResourceLoaderGetConfigVars
123
		 */
124
		$this->handlers['ResourceLoaderGetConfigVars'] = function ( &$vars ) use ( $options ) {
125
126 1
			$contentLanguage = $GLOBALS['wgContLang'];
127 1
			$enabledNamespaceWithTemplate = array();
128 1
			$namespacesByContentLanguage = array();
129
130 1
			foreach ( $options->get( 'enabledNamespaceWithTemplate' ) as $ns => $template ) {
131
				$enabledNamespaceWithTemplate[$contentLanguage->getNsText( $ns )] = $template;
132 1
			}
133
134
			// Get literals to match and split when comparing href's during the
135
			// JS parse process
136 1
			foreach ( \MWNamespace::getCanonicalNamespaces() as $ns => $name ) {
137 1
				$namespacesByContentLanguage[$contentLanguage->getNsText( $ns )] = $name;
138 1
			}
139
140 1
			$vars['ext.suc.config'] = array(
141 1
				'tooltipRequestCacheTTL'          => $options->get( 'tooltipRequestCacheTTL' ),
142 1
				'cachePrefix'                     => $options->get( 'cachePrefix' ),
143 1
				'enabledForAnonUsers'             => $options->get( 'enabledForAnonUsers' ),
144 1
				'enabledNamespaceWithTemplate'    => $enabledNamespaceWithTemplate,
145 1
				'namespacesByContentLanguage'     => $namespacesByContentLanguage,
146 1
				'namespacesByCanonicalIdentifier' => array_flip( $namespacesByContentLanguage )
147 1
			);
148
149 1
			return true;
150
		};
151 2
	}
152
153
}
154