forked from mathiasertl/NewArticleTemplates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewArticleTemplate.php
93 lines (78 loc) · 2.77 KB
/
NewArticleTemplate.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
<?php
$wgHooks['EditPage::showEditForm:initial'][] = 'newArticleTemplates';
$wgExtensionCredits['other'][] = array (
'name' => 'NewArticleTemplate',
'description' => 'Prefills new articles with a given article',
'version' => '1.2-1.24.0',
'author' => 'Mathias Ertl, Fabian Zeindl',
'url' => 'http://www.mediawiki.org/wiki/Extension:NewArticleTemplates',
);
/**
* preload returns the text that is in the article specified by $preload
*/
function preload( $preload ) {
if ( $preload === '' )
return '';
else {
// based on EditPage::getPreloadedText(), present until 1.23.x
$preloadTitle = Title::newFromText( $preload );
if ( isset( $preloadTitle ) && $preloadTitle->userCan('read') ) {
$rev=Revision::newFromTitle($preloadTitle);
if ( is_object( $rev ) ) {
$text = $rev->getText();
// Remove <noinclude> sections and <includeonly> tags from text
$text = StringUtils::delimiterReplace( '<noinclude>', '</noinclude>', '', $text );
$text = strtr( $text, array( '<includeonly>' => '', '</includeonly>' => '' ) );
return $text;
} else
return '';
}
}
}
/**
* called by Hook EditPage::showEditForm:initial.
* Simply preloads the textbox with a text that is defined in an
* article. Also see preload function above.
*/
function newArticleTemplates( $newPage ) {
global $wgNewArticleTemplatesEnable;
/* some checks */
if ( $newPage->mTitle->exists() or $newPage->firsttime != 1 or !$wgNewArticleTemplatesEnable )
return true;
global $wgNewArticleTemplatesNamespaces, $wgNewArticleTemplatesOnSubpages;
/* see if this is a subpage */
$title = $newPage->mTitle;
$isSubpage = false;
if ( $title->isSubpage() ) {
$baseTitle = Title::newFromText(
$title->getBaseText(),
$title->getNamespace() );
if ( $baseTitle->exists() ) {
$isSubpage = true;
}
}
/* we might want to return if this is a subpage */
if ( (! $wgNewArticleTemplatesOnSubpages) && $isSubpage )
return true;
$namespace = $title->getNamespace();
/* actually important code: */
if (array_key_exists($namespace, $wgNewArticleTemplatesNamespaces))
{
global $wgNewArticleTemplatesDefault, $wgNewArticleTemplates_PerNamespace;
if ( $wgNewArticleTemplates_PerNamespace[$namespace] )
$template = $wgNewArticleTemplates_PerNamespace[$namespace];
elseif ( $wgNewArticleTemplatesDefault )
$template = $wgNewArticleTemplatesDefault;
/* if this is a subpage, we want to to use $template/Subpage instead, if it exists */
if ( $isSubpage ) {
$subpageTemplate = Title::newFromText( $template . '/Subpage' );
if ( $subpageTemplate->exists() ) {
$template = $template . '/Subpage';
}
}
$newPage->textbox1 = preload( $template );
# $newPage->textbox1 = preload($wgNewArticleTemplatesDefault);
}
return true;
}
?>