summaryrefslogtreecommitdiff
path: root/extensions/Gadgets/SpecialGadgets.php
blob: a53160f66886d12200d633281adb8fa2809c774b (plain)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/**
 * Special:Gadgets, provides a preview of MediaWiki:Gadgets.
 *
 * @file
 * @ingroup SpecialPage
 * @author Daniel Kinzler, brightbyte.de
 * @copyright © 2007 Daniel Kinzler
 * @license GNU General Public License 2.0 or later
 */

class SpecialGadgets extends SpecialPage {
	public function __construct() {
		parent::__construct( 'Gadgets', '', true );
	}

	/**
	 * Main execution function
	 * @param $par array Parameters passed to the page
	 */
	public function execute( $par ) {
		$parts = explode( '/', $par );

		if ( count( $parts ) == 2 && $parts[0] == 'export' ) {
			$this->showExportForm( $parts[1] );
		} else {
			$this->showMainForm();
		}
	}

	/**
	 * Displays form showing the list of installed gadgets
	 */
	public function showMainForm() {
		global $wgContLang;

		$output = $this->getOutput();
		$this->setHeaders();
		$output->setPagetitle( $this->msg( 'gadgets-title' ) );
		$output->addWikiMsg( 'gadgets-pagetext' );

		$gadgets = GadgetRepo::singleton()->getStructuredList();
		if ( !$gadgets ) {
			return;
		}

		$lang = $this->getLanguage();
		$langSuffix = "";
		if ( $lang->getCode() != $wgContLang->getCode() ) {
			$langSuffix = "/" . $lang->getCode();
		}

		$listOpen = false;

		$editInterfaceMessage = $this->getUser()->isAllowed( 'editinterface' )
			? 'edit'
			: 'viewsource';

		foreach ( $gadgets as $section => $entries ) {
			if ( $section !== false && $section !== '' ) {
				$t = Title::makeTitleSafe( NS_MEDIAWIKI, "Gadget-section-$section$langSuffix" );
				$lnkTarget = $t
					? Linker::link( $t, $this->msg( $editInterfaceMessage )->escaped(), array(), array( 'action' => 'edit' ) )
					: htmlspecialchars( $section );
				$lnk =  "&#160; &#160; [$lnkTarget]";

				$ttext = $this->msg( "gadget-section-$section" )->parse();

				if ( $listOpen ) {
					$output->addHTML( Xml::closeElement( 'ul' ) . "\n" );
					$listOpen = false;
				}

				$output->addHTML( Html::rawElement( 'h2', array(), $ttext . $lnk ) . "\n" );
			}

			/**
			 * @var $gadget Gadget
			 */
			foreach ( $entries as $gadget ) {
				$t = Title::makeTitleSafe( NS_MEDIAWIKI, "Gadget-{$gadget->getName()}$langSuffix" );

				if ( !$t ) {
					continue;
				}

				$links = array();
				$links[] = Linker::link(
					$t,
					$this->msg( $editInterfaceMessage )->escaped(),
					array(),
					array( 'action' => 'edit' )
				);
				$links[] = Linker::link(
					$this->getPageTitle( "export/{$gadget->getName()}" ),
					$this->msg( 'gadgets-export' )->escaped()
				);

				$ttext = $this->msg( "gadget-{$gadget->getName()}" )->parse();

				if ( !$listOpen ) {
					$listOpen = true;
					$output->addHTML( Xml::openElement( 'ul' ) );
				}

				$lnk = '&#160;&#160;' .
					$this->msg( 'parentheses' )->rawParams( $lang->pipeList( $links ) )->escaped();
				$output->addHTML( Xml::openElement( 'li' ) .
						$ttext . $lnk . "<br />" .
						$this->msg( 'gadgets-uses' )->escaped() .
						$this->msg( 'colon-separator' )->escaped()
				);

				$lnk = array();
				foreach ( $gadget->getScriptsAndStyles() as $codePage ) {
					$t = Title::makeTitleSafe( NS_MEDIAWIKI, $codePage );

					if ( !$t ) {
						continue;
					}

					$lnk[] = Linker::link( $t, htmlspecialchars( $t->getText() ) );
				}
				$output->addHTML( $lang->commaList( $lnk ) );
				if ( $gadget->getLegacyScripts() ) {
					$output->addHTML( '<br />' . Html::rawElement(
						'span',
						array( 'class' => 'mw-gadget-legacy errorbox' ),
						$this->msg( 'gadgets-legacy' )->parse()
					) );
				}

				$rights = array();
				foreach ( $gadget->getRequiredRights() as $right ) {
					$rights[] = '* ' . $this->msg( "right-$right" )->plain();
				}
				if ( count( $rights ) ) {
					$output->addHTML( '<br />' .
							$this->msg( 'gadgets-required-rights', implode( "\n", $rights ), count( $rights ) )->parse()
					);
				}

				$skins = array();
				$validskins = Skin::getSkinNames();
				foreach ( $gadget->getRequiredSkins() as $skinid ) {
					if ( isset( $validskins[$skinid] ) ) {
						$skins[] = $this->msg( "skinname-$skinid" )->plain();
					} else {
						$skins[] = $skinid;
					}
				}
				if ( count( $skins ) ) {
					$output->addHTML(
						'<br />' .
						$this->msg( 'gadgets-required-skins', $lang->commaList( $skins ) )
							->numParams( count( $skins ) )->parse()
					);
				}

				if ( $gadget->isOnByDefault() ) {
					$output->addHTML( '<br />' . $this->msg( 'gadgets-default' )->parse() );
				}

				$output->addHTML( Xml::closeElement( 'li' ) . "\n" );
			}
		}

		if ( $listOpen ) {
			$output->addHTML( Xml::closeElement( 'ul' ) . "\n" );
		}
	}

	/**
	 * Exports a gadget with its dependencies in a serialized form
	 * @param $gadget String Name of gadget to export
	 */
	public function showExportForm( $gadget ) {
		global $wgScript;

		$output = $this->getOutput();
		try {
			$g = GadgetRepo::singleton()->getGadget( $gadget );
		} catch ( InvalidArgumentException $e ) {
			$output->showErrorPage( 'error', 'gadgets-not-found', array( $gadget ) );
			return;
		}

		$this->setHeaders();
		$output->setPagetitle( $this->msg( 'gadgets-export-title' ) );
		$output->addWikiMsg( 'gadgets-export-text', $gadget, $g->getDefinition() );

		$exportList = "MediaWiki:gadget-$gadget\n";
		foreach ( $g->getScriptsAndStyles() as $page ) {
			$exportList .= "MediaWiki:$page\n";
		}

		$output->addHTML( Html::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) )
			. Html::hidden( 'title', SpecialPage::getTitleFor( 'Export' )->getPrefixedDBKey() )
			. Html::hidden( 'pages', $exportList )
			. Html::hidden( 'wpDownload', '1' )
			. Html::hidden( 'templates', '1' )
			. Xml::submitButton( $this->msg( 'gadgets-export-download' )->text() )
			. Html::closeElement( 'form' )
		);
	}

	protected function getGroupName() {
		return 'wiki';
	}
}