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; |
|
|
|
|
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 ) { |
|
|
|
|
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() { |
|
|
|
|
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
|
|
|
|
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state