Completed
Push — master ( 85d1e0...3dc773 )
by
unknown
04:22
created

Hooks::parse()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 24
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
ccs 0
cts 13
cp 0
rs 8.5125
cc 5
eloc 10
nc 2
nop 2
crap 30
1
<?php
2
3
/**
4
 * File holding the Lingo\Hooks class
5
 *
6
 * This file is part of the MediaWiki extension Lingo.
7
 *
8
 * @copyright 2011 - 2016, Stephan Gambke
9
 * @license   GNU General Public License, version 2 (or any later version)
10
 *
11
 * The Lingo extension is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by the Free
13
 * Software Foundation; either version 2 of the License, or (at your option) any
14
 * later version.
15
 *
16
 * The Lingo extension is distributed in the hope that it will be useful, but
17
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19
 * details.
20
 *
21
 * You should have received a copy of the GNU General Public License along
22
 * with this program. If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 * @author Stephan Gambke
25
 * @file
26
 * @ingroup Lingo
27
 */
28
29
namespace Lingo;
30
use MagicWord;
31
use Parser;
32
use PPFrame;
33
34
/**
35
 * The Lingo\Hooks class.
36
 *
37
 * It contains the hook handlers of the extension
38
 *
39
 * @ingroup Lingo
40
 */
41
class Hooks {
42
43
	/**
44
	 * Hooks into ParserAfterParse.
45
	 *
46
	 * @param Parser $parser
47
	 * @param String $text
48
	 * @return Boolean
49
	 */
50
	public static function parse( &$parser, &$text ) {
51
52
		global $wgexLingoUseNamespaces;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
53
54
		$title = $parser->getTitle();
55
56
		// parse if
57
		if ( !isset( $parser->mDoubleUnderscores[ 'noglossary' ] ) && // __NOGLOSSARY__ not present and
58
			(
59
				!$title || // title not set or
60
				!isset( $wgexLingoUseNamespaces[ $title->getNamespace() ] ) || // namespace not explicitly forbidden (i.e. not in list of namespaces and set to false) or
61
				$wgexLingoUseNamespaces[ $title->getNamespace() ] // namespace explicitly allowed
62
			)
63
		) {
64
65
			// unstrip strip items of the 'general' group
66
			// this will be done again by parse when this hook returns, but it should not hurt to do this twice
67
			// Only problem is with other hook handlers that might not expect strip items to be unstripped already
68
			$text = $parser->mStripState->unstripGeneral( $text );
69
			LingoParser::parse( $parser, $text );
70
		}
71
72
		return true;
73
	}
74
75
	/**
76
	 * Creates tag hook(s)
77
	 */
78
	public static function registerTags( Parser $parser ) {
79
		$parser->setHook( 'noglossary', 'Lingo\Hooks::noglossaryTagRenderer' );
80
		return true;
81
	}
82
83
	/**
84
	 * Sets hook on 'noglossary' tag
85
	 * @static
86
	 * @param $input
87
	 * @param array $args
88
	 * @param Parser $parser
89
	 * @param PPFrame $frame
90
	 * @return string
91
	 */
92
	public static function noglossaryTagRenderer( $input, array $args, Parser $parser, PPFrame $frame ) {
0 ignored issues
show
Unused Code introduced by
The parameter $args 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...
93
		$output = $parser->recursiveTagParse( $input, $frame );
94
		return '<span class="noglossary">' . $output . '</span>';
95
	}
96
97
	/**
98
	 * Deferred settings
99
	 * - registration of _NOGLOSSARY_ magic word
100
	 * - extension description shown on Special:Version
101
	 */
102
	public static function initExtension() {
0 ignored issues
show
Coding Style introduced by
initExtension 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...
103
		MagicWord::$mDoubleUnderscoreIDs[] = 'noglossary';
104
105
		foreach ( $GLOBALS[ 'wgExtensionCredits' ][ 'parserhook' ] as $index => $description ) {
106
			if ( $GLOBALS[ 'wgExtensionCredits' ][ 'parserhook' ][ $index ][ 'name' ] === 'Lingo' ) {
107
				$GLOBALS[ 'wgExtensionCredits' ][ 'parserhook' ][ $index ][ 'description' ] =
108
					wfMessage( 'lingo-desc', $GLOBALS[ 'wgexLingoPage' ] ? $GLOBALS[ 'wgexLingoPage' ] : wfMessage( 'lingo-terminologypagename' )->inContentLanguage()->text() )->text();
109
			}
110
		}
111
	}
112
}
113
114