VectorTemplate.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. <?php
  2. /**
  3. * Vector - Modern version of MonoBook with fresh look and many usability
  4. * improvements.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. * http://www.gnu.org/copyleft/gpl.html
  20. *
  21. * @file
  22. * @ingroup Skins
  23. */
  24. /**
  25. * QuickTemplate subclass for Vector
  26. * @ingroup Skins
  27. */
  28. class VectorTemplate extends BaseTemplate {
  29. /**
  30. * Outputs the entire contents of the HTML page
  31. */
  32. public function execute() {
  33. $this->data['namespace_urls'] = $this->data['content_navigation']['namespaces'];
  34. $this->data['view_urls'] = $this->data['content_navigation']['views'];
  35. $this->data['action_urls'] = $this->data['content_navigation']['actions'];
  36. $this->data['variant_urls'] = $this->data['content_navigation']['variants'];
  37. // Move the watch/unwatch star outside of the collapsed "actions" menu to the main "views" menu
  38. if ( $this->config->get( 'VectorUseIconWatch' ) ) {
  39. $mode = $this->getSkin()->getUser()->isWatched( $this->getSkin()->getRelevantTitle() )
  40. ? 'unwatch'
  41. : 'watch';
  42. if ( isset( $this->data['action_urls'][$mode] ) ) {
  43. $this->data['view_urls'][$mode] = $this->data['action_urls'][$mode];
  44. unset( $this->data['action_urls'][$mode] );
  45. }
  46. }
  47. // Naming conventions for Mustache parameters:
  48. // - Prefix "is" for boolean values.
  49. // - Prefix "msg-" for interface messages.
  50. // - Prefix "page-" for data relating to the current page (e.g. Title, WikiPage, or OutputPage).
  51. // - Prefix "html-" for raw HTML (in front of other keys, if applicable).
  52. // - Conditional values are null if absent.
  53. $params = [
  54. 'html-headelement' => $this->get( 'headelement', '' ),
  55. 'html-sitenotice' => $this->get( 'sitenotice', null ),
  56. 'html-indicators' => $this->getIndicators(),
  57. 'page-langcode' => $this->getSkin()->getTitle()->getPageViewLanguage()->getHtmlCode(),
  58. 'page-isarticle' => (bool)$this->data['isarticle'],
  59. // Remember that the string '0' is a valid title.
  60. // From OutputPage::getPageTitle, via ::setPageTitle().
  61. 'html-title' => $this->get( 'title', '' ),
  62. 'html-prebodyhtml' => $this->get( 'prebodyhtml', '' ),
  63. 'msg-tagline' => $this->getMsg( 'tagline' )->text(),
  64. // TODO: mediawiki/SkinTemplate should expose langCode and langDir properly.
  65. 'html-userlangattributes' => $this->get( 'userlangattributes', '' ),
  66. // From OutputPage::getSubtitle()
  67. 'html-subtitle' => $this->get( 'subtitle', '' ),
  68. // TODO: Use directly Skin::getUndeleteLink() directly.
  69. // Always returns string, cast to null if empty.
  70. 'html-undelete' => $this->get( 'undelete', null ) ?: null,
  71. // From Skin::getNewtalks(). Always returns string, cast to null if empty.
  72. 'html-newtalk' => $this->get( 'newtalk', '' ) ?: null,
  73. 'msg-jumptonavigation' => $this->getMsg( 'vector-jumptonavigation' )->text(),
  74. 'msg-jumptosearch' => $this->getMsg( 'vector-jumptosearch' )->text(),
  75. // Result of OutputPage::addHTML calls
  76. 'html-bodycontent' => $this->get( 'bodycontent' ),
  77. 'html-printfooter' => $this->get( 'printfooter', null ),
  78. 'html-catlinks' => $this->get( 'catlinks', '' ),
  79. 'html-dataAfterContent' => $this->get( 'dataAfterContent', '' ),
  80. // From MWDebug::getHTMLDebugLog (when $wgShowDebug is enabled)
  81. 'html-debuglog' => $this->get( 'debughtml', '' ),
  82. // From BaseTemplate::getTrail (handles bottom JavaScript)
  83. 'html-printtail' => $this->getTrail(),
  84. ];
  85. // TODO: Convert the rest to Mustache
  86. ob_start();
  87. ?>
  88. <div id="mw-navigation">
  89. <h2><?php $this->msg( 'navigation-heading' ) ?></h2>
  90. <div id="mw-head">
  91. <?php $this->renderNavigation( [ 'PERSONAL' ] ); ?>
  92. <div id="left-navigation">
  93. <?php $this->renderNavigation( [ 'NAMESPACES', 'VARIANTS' ] ); ?>
  94. </div>
  95. <div id="right-navigation">
  96. <?php $this->renderNavigation( [ 'VIEWS', 'ACTIONS', 'SEARCH' ] ); ?>
  97. </div>
  98. </div>
  99. <div id="mw-panel">
  100. <div id="p-logo" role="banner"><a class="mw-wiki-logo" href="<?php
  101. echo htmlspecialchars( $this->data['nav_urls']['mainpage']['href'] )
  102. ?>"<?php
  103. echo Xml::expandAttributes( Linker::tooltipAndAccesskeyAttribs( 'p-logo' ) )
  104. ?>></a></div>
  105. <?php $this->renderPortals( $this->data['sidebar'] ); ?>
  106. </div>
  107. </div>
  108. <?php Hooks::run( 'VectorBeforeFooter' ); ?>
  109. <div id="footer" role="contentinfo"<?php $this->html( 'userlangattributes' ) ?>>
  110. <?php
  111. foreach ( $this->getFooterLinks() as $category => $links ) {
  112. ?>
  113. <ul id="footer-<?php echo $category ?>">
  114. <?php
  115. foreach ( $links as $link ) {
  116. ?>
  117. <li id="footer-<?php echo $category ?>-<?php echo $link ?>"><?php $this->html( $link ) ?></li>
  118. <?php
  119. }
  120. ?>
  121. </ul>
  122. <?php
  123. }
  124. ?>
  125. <?php $footericons = $this->getFooterIcons( 'icononly' );
  126. if ( count( $footericons ) > 0 ) {
  127. ?>
  128. <ul id="footer-icons" class="noprint">
  129. <?php
  130. foreach ( $footericons as $blockName => $footerIcons ) {
  131. ?>
  132. <li id="footer-<?php echo htmlspecialchars( $blockName ); ?>ico">
  133. <?php
  134. foreach ( $footerIcons as $icon ) {
  135. echo $this->getSkin()->makeFooterIcon( $icon );
  136. }
  137. ?>
  138. </li>
  139. <?php
  140. }
  141. ?>
  142. </ul>
  143. <?php
  144. }
  145. ?>
  146. <div style="clear: both;"></div>
  147. </div>
  148. <?php
  149. $params['html-unported'] = ob_get_contents();
  150. ob_end_clean();
  151. // Prepare and output the HTML response
  152. $templates = new TemplateParser( __DIR__ . '/templates' );
  153. echo $templates->processTemplate( 'index', $params );
  154. }
  155. /**
  156. * Render a series of portals
  157. *
  158. * @param array $portals
  159. */
  160. protected function renderPortals( array $portals ) {
  161. // Force the rendering of the following portals
  162. if ( !isset( $portals['TOOLBOX'] ) ) {
  163. $portals['TOOLBOX'] = true;
  164. }
  165. if ( !isset( $portals['LANGUAGES'] ) ) {
  166. $portals['LANGUAGES'] = true;
  167. }
  168. // Render portals
  169. foreach ( $portals as $name => $content ) {
  170. if ( $content === false ) {
  171. continue;
  172. }
  173. // Numeric strings gets an integer when set as key, cast back - T73639
  174. $name = (string)$name;
  175. switch ( $name ) {
  176. case 'SEARCH':
  177. break;
  178. case 'TOOLBOX':
  179. $this->renderPortal( 'tb', $this->getToolbox(), 'toolbox', 'SkinTemplateToolboxEnd' );
  180. Hooks::run( 'VectorAfterToolbox' );
  181. break;
  182. case 'LANGUAGES':
  183. if ( $this->data['language_urls'] !== false ) {
  184. $this->renderPortal( 'lang', $this->data['language_urls'], 'otherlanguages' );
  185. }
  186. break;
  187. default:
  188. $this->renderPortal( $name, $content );
  189. break;
  190. }
  191. }
  192. }
  193. /**
  194. * @param string $name
  195. * @param array|string $content
  196. * @param null|string $msg
  197. * @param null|string|array $hook
  198. */
  199. protected function renderPortal( $name, $content, $msg = null, $hook = null ) {
  200. if ( $msg === null ) {
  201. $msg = $name;
  202. }
  203. $msgObj = $this->getMsg( $msg );
  204. $labelId = Sanitizer::escapeIdForAttribute( "p-$name-label" );
  205. ?>
  206. <div class="portal" role="navigation" id="<?php
  207. echo htmlspecialchars( Sanitizer::escapeIdForAttribute( "p-$name" ) )
  208. ?>"<?php
  209. echo Linker::tooltip( 'p-' . $name )
  210. ?> aria-labelledby="<?php echo htmlspecialchars( $labelId ) ?>">
  211. <h3<?php $this->html( 'userlangattributes' ) ?> id="<?php echo htmlspecialchars( $labelId )
  212. ?>"><?php
  213. echo htmlspecialchars( $msgObj->exists() ? $msgObj->text() : $msg );
  214. ?></h3>
  215. <div class="body">
  216. <?php
  217. if ( is_array( $content ) ) {
  218. ?>
  219. <ul>
  220. <?php
  221. foreach ( $content as $key => $val ) {
  222. echo $this->makeListItem( $key, $val );
  223. }
  224. if ( $hook !== null ) {
  225. // Avoid PHP 7.1 warning
  226. $skin = $this;
  227. Hooks::run( $hook, [ &$skin, true ] );
  228. }
  229. ?>
  230. </ul>
  231. <?php
  232. } else {
  233. // Allow raw HTML block to be defined by extensions
  234. echo $content;
  235. }
  236. $this->renderAfterPortlet( $name );
  237. ?>
  238. </div>
  239. </div>
  240. <?php
  241. }
  242. /**
  243. * Render one or more navigations elements by name, automatically reversed by css
  244. * when UI is in RTL mode
  245. *
  246. * @param array $elements
  247. */
  248. protected function renderNavigation( array $elements ) {
  249. // Render elements
  250. foreach ( $elements as $name => $element ) {
  251. switch ( $element ) {
  252. case 'NAMESPACES':
  253. ?>
  254. <div id="p-namespaces" role="navigation" class="vectorTabs<?php
  255. if ( count( $this->data['namespace_urls'] ) == 0 ) {
  256. echo ' emptyPortlet';
  257. }
  258. ?>" aria-labelledby="p-namespaces-label">
  259. <h3 id="p-namespaces-label"><?php $this->msg( 'namespaces' ) ?></h3>
  260. <ul<?php $this->html( 'userlangattributes' ) ?>>
  261. <?php
  262. foreach ( $this->data['namespace_urls'] as $key => $item ) {
  263. echo $this->makeListItem( $key, $item, [
  264. 'vector-wrap' => true,
  265. ] );
  266. }
  267. ?>
  268. </ul>
  269. </div>
  270. <?php
  271. break;
  272. case 'VARIANTS':
  273. ?>
  274. <div id="p-variants" role="navigation" class="vectorMenu<?php
  275. if ( count( $this->data['variant_urls'] ) == 0 ) {
  276. echo ' emptyPortlet';
  277. }
  278. ?>" aria-labelledby="p-variants-label">
  279. <?php
  280. // Replace the label with the name of currently chosen variant, if any
  281. $variantLabel = $this->getMsg( 'variants' )->text();
  282. foreach ( $this->data['variant_urls'] as $item ) {
  283. if ( isset( $item['class'] ) && stripos( $item['class'], 'selected' ) !== false ) {
  284. $variantLabel = $item['text'];
  285. break;
  286. }
  287. }
  288. ?>
  289. <input type="checkbox" class="vectorMenuCheckbox" aria-labelledby="p-variants-label" />
  290. <h3 id="p-variants-label">
  291. <span><?php echo htmlspecialchars( $variantLabel ) ?></span>
  292. </h3>
  293. <ul class="menu">
  294. <?php
  295. foreach ( $this->data['variant_urls'] as $key => $item ) {
  296. echo $this->makeListItem( $key, $item );
  297. }
  298. ?>
  299. </ul>
  300. </div>
  301. <?php
  302. break;
  303. case 'VIEWS':
  304. ?>
  305. <div id="p-views" role="navigation" class="vectorTabs<?php
  306. if ( count( $this->data['view_urls'] ) == 0 ) {
  307. echo ' emptyPortlet';
  308. }
  309. ?>" aria-labelledby="p-views-label">
  310. <h3 id="p-views-label"><?php $this->msg( 'views' ) ?></h3>
  311. <ul<?php $this->html( 'userlangattributes' ) ?>>
  312. <?php
  313. foreach ( $this->data['view_urls'] as $key => $item ) {
  314. echo $this->makeListItem( $key, $item, [
  315. 'vector-wrap' => true,
  316. 'vector-collapsible' => true,
  317. ] );
  318. }
  319. ?>
  320. </ul>
  321. </div>
  322. <?php
  323. break;
  324. case 'ACTIONS':
  325. ?>
  326. <div id="p-cactions" role="navigation" class="vectorMenu<?php
  327. if ( count( $this->data['action_urls'] ) == 0 ) {
  328. echo ' emptyPortlet';
  329. }
  330. ?>" aria-labelledby="p-cactions-label">
  331. <input type="checkbox" class="vectorMenuCheckbox" aria-labelledby="p-cactions-label" />
  332. <h3 id="p-cactions-label"><span><?php
  333. $this->msg( 'vector-more-actions' )
  334. ?></span></h3>
  335. <ul class="menu"<?php $this->html( 'userlangattributes' ) ?>>
  336. <?php
  337. foreach ( $this->data['action_urls'] as $key => $item ) {
  338. echo $this->makeListItem( $key, $item );
  339. }
  340. ?>
  341. </ul>
  342. </div>
  343. <?php
  344. break;
  345. case 'PERSONAL':
  346. ?>
  347. <div id="p-personal" role="navigation"<?php
  348. if ( count( $this->data['personal_urls'] ) == 0 ) {
  349. echo ' class="emptyPortlet"';
  350. }
  351. ?> aria-labelledby="p-personal-label">
  352. <h3 id="p-personal-label"><?php $this->msg( 'personaltools' ) ?></h3>
  353. <ul<?php $this->html( 'userlangattributes' ) ?>>
  354. <?php
  355. $notLoggedIn = '';
  356. if ( !$this->getSkin()->getUser()->isLoggedIn() &&
  357. User::groupHasPermission( '*', 'edit' )
  358. ) {
  359. $notLoggedIn =
  360. Html::element( 'li',
  361. [ 'id' => 'pt-anonuserpage' ],
  362. $this->getMsg( 'notloggedin' )->text()
  363. );
  364. }
  365. $personalTools = $this->getPersonalTools();
  366. $langSelector = '';
  367. if ( array_key_exists( 'uls', $personalTools ) ) {
  368. $langSelector = $this->makeListItem( 'uls', $personalTools[ 'uls' ] );
  369. unset( $personalTools[ 'uls' ] );
  370. }
  371. echo $langSelector;
  372. echo $notLoggedIn;
  373. foreach ( $personalTools as $key => $item ) {
  374. echo $this->makeListItem( $key, $item );
  375. }
  376. ?>
  377. </ul>
  378. </div>
  379. <?php
  380. break;
  381. case 'SEARCH':
  382. ?>
  383. <div id="p-search" role="search">
  384. <h3<?php $this->html( 'userlangattributes' ) ?>>
  385. <label for="searchInput"><?php $this->msg( 'search' ) ?></label>
  386. </h3>
  387. <form action="<?php $this->text( 'wgScript' ) ?>" id="searchform">
  388. <div<?php echo $this->config->get( 'VectorUseSimpleSearch' ) ? ' id="simpleSearch"' : '' ?>>
  389. <?php
  390. echo $this->makeSearchInput( [ 'id' => 'searchInput' ] );
  391. echo Html::hidden( 'title', $this->get( 'searchtitle' ) );
  392. /* We construct two buttons (for 'go' and 'fulltext' search modes),
  393. * but only one will be visible and actionable at a time (they are
  394. * overlaid on top of each other in CSS).
  395. * * Browsers will use the 'fulltext' one by default (as it's the
  396. * first in tree-order), which is desirable when they are unable
  397. * to show search suggestions (either due to being broken or
  398. * having JavaScript turned off).
  399. * * The mediawiki.searchSuggest module, after doing tests for the
  400. * broken browsers, removes the 'fulltext' button and handles
  401. * 'fulltext' search itself; this will reveal the 'go' button and
  402. * cause it to be used.
  403. */
  404. echo $this->makeSearchButton(
  405. 'fulltext',
  406. [ 'id' => 'mw-searchButton', 'class' => 'searchButton mw-fallbackSearchButton' ]
  407. );
  408. echo $this->makeSearchButton(
  409. 'go',
  410. [ 'id' => 'searchButton', 'class' => 'searchButton' ]
  411. );
  412. ?>
  413. </div>
  414. </form>
  415. </div>
  416. <?php
  417. break;
  418. }
  419. }
  420. }
  421. /**
  422. * @inheritDoc
  423. */
  424. public function makeLink( $key, $item, $options = [] ) {
  425. $html = parent::makeLink( $key, $item, $options );
  426. // Add an extra wrapper because our CSS is weird
  427. if ( isset( $options['vector-wrap'] ) && $options['vector-wrap'] ) {
  428. $html = Html::rawElement( 'span', [], $html );
  429. }
  430. return $html;
  431. }
  432. /**
  433. * @inheritDoc
  434. */
  435. public function makeListItem( $key, $item, $options = [] ) {
  436. // For fancy styling of watch/unwatch star
  437. if (
  438. $this->config->get( 'VectorUseIconWatch' )
  439. && ( $key === 'watch' || $key === 'unwatch' )
  440. ) {
  441. $item['class'] = rtrim( 'icon ' . $item['class'], ' ' );
  442. $item['primary'] = true;
  443. }
  444. // Add CSS class 'collapsible' to links which are not marked as "primary"
  445. if (
  446. isset( $options['vector-collapsible'] ) && $options['vector-collapsible'] ) {
  447. $item['class'] = rtrim( 'collapsible ' . $item['class'], ' ' );
  448. }
  449. return parent::makeListItem( $key, $item, $options );
  450. }
  451. }