forked from wikimedia/mediawiki-extensions-FlexDiagrams
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FDHooks.php
103 lines (91 loc) · 2.84 KB
/
FDHooks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
/**
* Static functions called by various outside hooks, as well as by
* extension.json.
*
* @author Yaron Koren
* @file
* @ingroup FlexDiagrams
*/
class FDHooks {
public static function registerExtension() {
define( 'CONTENT_MODEL_FD_BPMN', 'flexdiagrams-bpmn' );
define( 'CONTENT_MODEL_FD_GANTT', 'flexdiagrams-gantt' );
define( 'CONTENT_MODEL_FD_DRAWIO', 'flexdiagrams-drawio' );
define( 'CONTENT_MODEL_FD_MERMAID', 'flexdiagrams-mermaid' );
}
/**
* Register the namespaces for Flex Diagrams.
* @see https://www.mediawiki.org/wiki/Manual:Hooks/CanonicalNamespaces
*
* @param array &$list
*/
public static function registerNamespaces( array &$list ) {
if ( !defined( 'FD_NS_BPMN' ) ) {
define( 'FD_NS_BPMN', 740 );
define( 'FD_NS_BPMN_TALK', 741 );
}
if ( !defined( 'FD_NS_GANTT' ) ) {
define( 'FD_NS_GANTT', 742 );
define( 'FD_NS_GANTT_TALK', 743 );
}
if ( !defined( 'FD_NS_MERMAID' ) ) {
define( 'FD_NS_MERMAID', 744 );
define( 'FD_NS_MERMAID_TALK', 745 );
}
if ( !defined( 'FD_NS_DRAWIO' ) ) {
define( 'FD_NS_DRAWIO', 746 );
define( 'FD_NS_DRAWIO_TALK', 747 );
}
$list[FD_NS_BPMN] = 'BPMN';
$list[FD_NS_BPMN_TALK] = 'BPMN_talk';
$list[FD_NS_GANTT] = 'Gantt';
$list[FD_NS_GANTT_TALK] = 'Gantt_talk';
$list[FD_NS_DRAWIO] = 'Drawio';
$list[FD_NS_DRAWIO_TALK] = 'Drawio_talk';
$list[FD_NS_MERMAID] = 'Mermaid';
$list[FD_NS_MERMAID_TALK] = 'Mermaid_talk';
}
public static function registerParserFunctions( &$parser ) {
$parser->setFunctionHook( 'display_diagram', [ 'FDDisplayDiagram', 'run' ] );
}
static function setGlobalJSVariables( &$vars ) {
global $wgServer, $wgScript;
$vars['wgServer'] = $wgServer;
$vars['wgScript'] = $wgScript;
}
public static function disableParserCache( Parser &$parser, string &$text ) {
$title = $parser->getTitle();
$ns = $title->getNamespace();
if ( $ns == FD_NS_BPMN || $ns == FD_NS_GANTT || $ns == FD_NS_MERMAID ) {
$parser->getOutput()->updateCacheExpiry( 0 );
}
}
/**
* Called by the HtmlPageLinkRendererEnd hook.
*
* Point red links to any diagram pages to "action=editdiagram".
*
* @param LinkRenderer $linkRenderer
* @param Title $target
* @param bool $isKnown
* @param string &$text
* @param array &$attribs
* @param bool &$ret
*/
static function linkToEditDiagramAction( MediaWiki\Linker\LinkRenderer $linkRenderer, $target, $isKnown,
&$text, &$attribs, &$ret ) {
global $wgFlexDiagramsEnabledFormats;
// If it's not a broken (red) link, exit.
if ( $isKnown ) {
return;
}
$namespace = $target->getNamespace();
if ( !in_array( $namespace, $wgFlexDiagramsEnabledFormats ) ) {
return;
}
// The class of $target can be either Title or TitleValue.
$title = Title::newFromLinkTarget( $target );
$attribs['href'] = $title->getLinkURL( [ 'action' => 'editdiagram', 'redlink' => '1' ] );
}
}