-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultiBoilerplate.php
executable file
·163 lines (145 loc) · 6.97 KB
/
MultiBoilerplate.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
/**
* Allows a boilerplate to be selected from a drop down box located above the
* edit form when editing non-exstant pages or, optionally (based upon
* configuration variable $wgMultiBoilerplateOverwrite), load the template
* over the current contents.
*
* @file
* @ingroup Extensions
*
* @link http://www.mediawiki.org/wiki/Extension:MultiBoilerplate
*
* @author Robert Leverington <robert@rhl.me.uk>
* @copyright Copyright © 2007 - 2009 Robert Leverington.
* @copyright Copyright © 2009 Al Maghi.
* @copyright Copyright © 2012 Daniel Yount @icarusfactor
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
*
* This is a modified version of the original to give separate namespace page
* templates. I will keep this separate from teh main tree until I get it to have
* more than one namespace template.
*/
// If this is run directly from the web die as this is not a valid entry point.
if( !defined( 'MEDIAWIKI' ) ) die( 'Invalid entry point.' );
// Extension credits.
$wgExtensionCredits[ 'other' ][] = array(
'path' => __FILE__,
'name' => 'MultiBoilerplate',
'descriptionmsg' => 'multiboilerplate-desc',
'author' => array( 'Robert Leverington', 'Al Maghi' , 'Daniel Yount aka icarusfactor' ),
'url' => 'https://www.mediawiki.org/wiki/Extension:MultiBoilerplate',
'version' => '1.8.1',
);
// Hook into EditPage::showEditForm:initial to modify the edit page header.
$wgHooks[ 'EditPage::showEditForm:initial' ][] = 'efMultiBoilerplate';
// Set extension messages file.
$dir = dirname( __FILE__ ) . '/';
$wgExtensionMessagesFiles[ 'MultiBoilerplate' ] = $dir . 'MultiBoilerplate.i18n.php';
$wgExtensionMessagesFiles[ 'MultiBoilerplate' ] = $dir . 'MultiBoilerplate.i18n.php';
$wgAutoloadClasses['SpecialBoilerplates'] = $dir . 'SpecialBoilerplates_body.php';
$wgSpecialPages['Boilerplates'] = 'SpecialBoilerplates';
$wgSpecialPageGroups['Boilerplates'] = 'wiki'; //section of [[Special:SpecialPages]]
// Default configuration variables.
/* Array of boilerplate names to boilerplate pages to load, for example:
* e.g. $wgMultiBoilerplateOptions[ 'My Boilerplate' ] = 'Template:My Boilerplate';
* If set to false then the MediaWiki:multiboilerplate message is used to configure
* boilerplates in the format of:
* "* Boilerplate Name|Template:Boilerplate Template"
*/
$wgMultiBoilerplateOptions = array();
/* Whether or not to show the form when editing pre-existing pages. */
$wgMultiBoilerplateOverwrite = false;
/* Whether or not to display a special page listing boilerplates.
* If set to true then the special page exists. */
$wgMultiBoilerplateDiplaySpecialPage = false;
$wgHooks['SpecialPage_initList'][]='efBoilerplateDisplaySpecialPage';
function efBoilerplateDisplaySpecialPage( &$aSpecialPages ) {
global $wgMultiBoilerplateDiplaySpecialPage;
if ( !$wgMultiBoilerplateDiplaySpecialPage ) {
unset( $aSpecialPages['Boilerplates'] );
}
return true;
}
/**
* Generate the form to be displayed at the top of the edit page and insert it.
* @param $form EditPage object.
* @return true
*/
function efMultiBoilerplate( $form ) {
// Get various variables needed for this extension.
global $wgMultiBoilerplateOptions, $wgMultiBoilerplateOverwrite, $wgTitle, $wgRequest, $wgMultiBoilerplatePrefixText;
// If $wgMultiBoilerplateOverwrite is true then detect whether
// the current page exists or not and if it does return true
// to end execution of this function.
if( !$wgMultiBoilerplateOverwrite && $wgTitle->exists( $wgTitle->getArticleID() ) ) return true;
// If $wgMultiBoilerplatePrefix is true then detect whether
// the current page matches or not and if it does not match return true
// to end execution of this function.
if( !$wgMultiBoilerplatePrefix )
{
$patternboilerplate = '/^'.$wgMultiBoilerplatePrefixText.'/';
if( !preg_match( $patternboilerplate , $wgTitle->getText() ) ) return true;
}
// Generate the options list used inside the boilerplate selection box.
// If $wgMultiBoilerplateOptions is an array then use that, else fall back
// to the MediaWiki:Multiboilerplate message.
if( is_array( $wgMultiBoilerplateOptions ) ) {
$options = '';
foreach( $wgMultiBoilerplateOptions as $name => $template ) {
$selected = false;
if( $wgRequest->getVal( 'boilerplate' ) == $template ) $selected = true;
$options .= Xml::option( $name, $template, $selected );
}
} else {
$things = wfMsgForContent( 'multiboilerplate' );
$options = '';
$things = explode( "\n", str_replace( "\r", "\n", str_replace( "\r\n", "\n", $things ) ) ); // Ensure line-endings are \n
foreach( $things as $row ) {
if ( substr( ltrim( $row ), 0, 1)==="*" ) {
$row = ltrim( $row, '* ' ); // Remove the asterix (and a space if found) from the start of the line.
$row = explode( '|', $row );
if( !isset( $row[ 1 ] ) ) return true; // Invalid syntax, abort.
$selected = false;
if( $wgRequest->getVal( 'boilerplate' ) == $row[ 1 ] ) $selected = true;
$options .= Xml::option( $row[ 0 ], $row[ 1 ], $selected );
}
}
}
// No options found in either configuration file, abort.
if( $options == '' ) return true;
// Append the selection form to the top of the edit page.
$form->editFormPageTop .=
Xml::openElement( 'form', array( 'id' => 'multiboilerplateform', 'name' => 'multiboilerplateform', 'method' => 'get', 'action' => $wgTitle->getEditURL() ) ) .
Xml::openElement( 'fieldset' ) .
Xml::element( 'legend', null, wfMsg( 'multiboilerplate-legend' ) ) .
Xml::openElement( 'label' ) .
wfMsg( 'multiboilerplate-label' ) .
Xml::openElement( 'select', array( 'name' => 'boilerplate' ) ) .
$options .
Xml::closeElement( 'select' ) .
Xml::closeElement( 'label' ) .
' ' .
Html::Hidden( 'action', 'edit' ) .
Html::Hidden( 'title', $wgRequest->getText( 'title' ) ) .
Xml::submitButton( wfMsg( 'multiboilerplate-submit' ) ) .
Xml::closeElement( 'fieldset' ) .
Xml::closeElement( 'form' );
// If the Load button has been pushed replace the article text with the boilerplate.
if( $wgRequest->getText( 'boilerplate', false ) ) {
$plate = new Article( Title::newFromURL( $wgRequest->getVal( 'boilerplate' ) ) );
$content = $plate->fetchContent();
/* Strip out noinclude tags and contained data, and strip includeonly
* tags (but retain contained data). If a function exists in the
* parser exists to do this it would be nice to replace this with it (I
* found one with a name as if it would do this, but it didn't seam to
* work).
*/
$content = preg_replace( '#<noinclude>(.*?)</noinclude>#ims', '', $content );
$content = preg_replace( '#<includeonly>(.*?)</includeonly>#ims', '$1', $content );
// TODO: Handle <onlyinclude> tags.
$form->textbox1 = $content;
}
// Return true so things don't break.
return true;
}