forked from wikimedia/mediawiki-extensions-FlexDiagrams
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFDHooks.php
More file actions
152 lines (135 loc) · 4.29 KB
/
FDHooks.php
File metadata and controls
152 lines (135 loc) · 4.29 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
* Static functions called by various outside hooks, as well as by
* extension.json.
*
* @author Yaron Koren
* @file
* @ingroup FlexDiagrams
*/
use MediaWiki\MediaWikiServices;
use MediaWiki\Title\Title;
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' );
define( 'CONTENT_MODEL_FD_DOT', 'flexdiagrams-dot' );
}
/**
* 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 );
}
if ( !defined( 'FD_NS_DOT' ) ) {
define( 'FD_NS_DOT', 748 );
define( 'FD_NS_DOT_TALK', 749 );
}
$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';
$list[FD_NS_DOT] = 'DOT';
$list[FD_NS_DOT_TALK] = 'DOT_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;
}
/**
* Called by the ArticleRevisionViewCustom hook.
*
* Display, in the "subtitle", the names of any pages that link to the diagram
* currently being seen.
* (The argument types can be added to the header once support for MW < 1.41 is
* removed.)
*
* @param RevisionRecord $revision
* @param Title $title
* @param int $oldId
* @param OutputPage $output
*/
public static function displayIncomingLinks( $revision, $title, $oldId, $output ) {
global $wgFlexDiagramsEnabledFormats;
$namespace = $title->getNamespace();
if ( !in_array( $namespace, $wgFlexDiagramsEnabledFormats ) ) {
return;
}
$linksToThisPage = $title->getLinksTo();
if ( count( $linksToThisPage ) == 0 ) {
return;
}
$linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
$linkingPagesText = '';
foreach ( $linksToThisPage as $i => $linkingTitle ) {
if ( $i > 0 ) {
$linkingPagesText .= ', ';
}
$linkingTitleName = $linkingTitle->getFullText();
$linkingPagesText .= "[[:$linkingTitleName|$linkingTitleName]]";
}
$text = wfMessage( 'flexdiagrams-linkingpages', $linkingPagesText )->parse();
$output->addSubtitle( $text );
}
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 || $ns == FD_NS_DOT ) {
$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' ] );
}
}