-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTalkBelow.php
143 lines (124 loc) · 4.3 KB
/
TalkBelow.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
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
<?php
use MediaWiki\Hook\SkinAfterContentHook;
use MediaWiki\MediaWikiServices;
use MediaWiki\Page\WikiPageFactory;
class TalkBelow implements SkinAfterContentHook {
/** @var WikiPageFactory */
private $wikiPageFactory;
public function __construct( WikiPageFactory $wikiPageFactory ) {
$this->wikiPageFactory = $wikiPageFactory;
}
/**
* @param OutputPage &$out
* @param Skin &$skin
*/
public static function onBeforePageDisplay( OutputPage &$out, Skin &$skin ) {
$out->addModuleStyles( 'mediawiki.ui.button' );
$out->addModules( 'ext.TalkBelow' );
}
/**
* @param array &$ids
*/
public static function onGetDoubleUnderscoreIDs( &$ids ) {
$ids[] = 'NOTALKBELOW';
}
/**
* Pass the config to JavaScript
*
* @param array &$vars
* @param string $skin
* @param Config $config
*/
public static function onResourceLoaderGetConfigVars( array &$vars, $skin, Config $config ) {
$vars['wgTalkBelowChangeTag'] = $config->get( 'TalkBelowChangeTag' );
}
/**
* Hide the Talk button
*
* @param SkinTemplate $skinTemplate
* @param array &$links
*/
public static function onSkinTemplateNavigationUniversal( SkinTemplate $skinTemplate, array &$links ) {
$title = $skinTemplate->getTitle();
if ( !$title->exists() ) {
return;
}
$parserOutput = $skinTemplate->getWikiPage()->getParserOutput();
if ( $parserOutput->getPageProperty( 'NOTALKBELOW' ) !== null ) {
return;
}
unset( $links['namespaces']['talk'] );
}
/**
* Show the talk below section
*
* @param string &$data
* @param Skin $skin
*/
public function onSkinAfterContent( &$data, $skin ) {
// Only show when viewing pages
$context = $skin->getContext();
$action = Action::getActionName( $context );
if ( $action !== 'view' ) {
return;
}
// Only show in pages that exist
$title = $skin->getTitle();
if ( !$title->exists() ) {
return;
}
// Don't show in talk pages
if ( $title->isTalkPage() ) {
return;
}
// Don't show in pages that can't have talk pages
if ( !$title->canHaveTalkPage() ) {
return;
}
// Don't show if __NOTALKBELOW__ is present
$parserOutput = $skin->getWikiPage()->getParserOutput();
if ( $parserOutput->getPageProperty( 'NOTALKBELOW' ) !== null ) {
return;
}
// If the talk page is a redirect, show the talk in the redirect target
$talk = $title->getTalkPage();
if ( $talk->isRedirect() ) {
$redirectLookup = MediaWikiServices::getInstance()->getRedirectLookup();
$talk = $redirectLookup->getRedirectTarget( $talk );
}
// Some pages, like the village pump, may have their talk page be a redirect to the subject page
if ( $title->equals( $talk ) ) {
return;
}
// Get the HTML of the talk page
$talkHtml = '';
if ( $talk->exists() ) {
$talkPage = $this->wikiPageFactory->newFromLinkTarget( $talk );
$talkOutput = $talkPage->getParserOutput();
$talkHtml = $talkOutput->getText( [ 'allowTOC' => false ] );
}
// Build the HTML of the discussion section, starting with the section heading and actions
$view = Html::rawElement( 'a', [ 'href' => $talk->getLinkURL() ], $context->msg( 'view' ) );
$edit = Html::rawElement( 'a', [ 'href' => $talk->getEditURL() ], $context->msg( 'edit' ) );
$bracket = Html::rawElement( 'span', [ 'class' => 'mw-editsection-bracket' ], '[' );
$divider = Html::rawElement( 'span', [ 'class' => 'mw-editsection-divider' ], ' | ' );
$bracket2 = Html::rawElement( 'span', [ 'class' => 'mw-editsection-bracket' ], ']' );
$editsection = $bracket . $view . $divider . $edit . $bracket2;
$wrapper = Html::rawElement( 'span', [ 'class' => 'mw-editsection' ], $editsection );
$heading = Html::rawElement( 'h1', [ 'id' => 'talkbelow-heading' ], $context->msg( 'talk' ) . $wrapper );
// Build the button to add a new topic. This will be replaced in the frontend, but is needed for non-JS users.
$addTopicButton = HTML::element(
'a',
[
'class' => [ 'mw-ui-button' ],
'href' => $talk->getLinkURL( 'action=edit§ion=new' ),
],
$context->msg( 'skin-action-addsection' )
);
$addTopicWrapper = Html::rawElement( 'div', [ 'id' => 'talkbelow-add-topic-button' ], $addTopicButton );
// Put everything together
$section = $heading . $talkHtml . $addTopicWrapper;
$section = Html::rawElement( 'div', [ 'id' => 'talkbelow-section', 'class' => 'noprint' ], $section );
$data = $section;
}
}