BaseTemplate.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. */
  20. use Wikimedia\WrappedString;
  21. use Wikimedia\WrappedStringList;
  22. /**
  23. * New base template for a skin's template extended from QuickTemplate
  24. * this class features helper methods that provide common ways of interacting
  25. * with the data stored in the QuickTemplate
  26. */
  27. abstract class BaseTemplate extends QuickTemplate {
  28. /**
  29. * Get a Message object with its context set
  30. *
  31. * @param string $name Message name
  32. * @param mixed $params,... Message params
  33. * @return Message
  34. */
  35. public function getMsg( $name /* ... */ ) {
  36. return $this->getSkin()->msg( ...func_get_args() );
  37. }
  38. function msg( $str ) {
  39. echo $this->getMsg( $str )->escaped();
  40. }
  41. /**
  42. * @param string $str
  43. * @warning You should never use this method. I18n messages should be escaped
  44. * @deprecated 1.32 Use ->msg() or ->msgWiki() instead.
  45. * @suppress SecurityCheck-XSS
  46. * @return-taint exec_html
  47. */
  48. function msgHtml( $str ) {
  49. wfDeprecated( __METHOD__, '1.32' );
  50. echo $this->getMsg( $str )->text();
  51. }
  52. function msgWiki( $str ) {
  53. echo $this->getMsg( $str )->parseAsBlock();
  54. }
  55. /**
  56. * Create an array of common toolbox items from the data in the quicktemplate
  57. * stored by SkinTemplate.
  58. * The resulting array is built according to a format intended to be passed
  59. * through makeListItem to generate the html.
  60. * @return array
  61. */
  62. function getToolbox() {
  63. $toolbox = [];
  64. if ( isset( $this->data['nav_urls']['whatlinkshere'] )
  65. && $this->data['nav_urls']['whatlinkshere']
  66. ) {
  67. $toolbox['whatlinkshere'] = $this->data['nav_urls']['whatlinkshere'];
  68. $toolbox['whatlinkshere']['id'] = 't-whatlinkshere';
  69. }
  70. if ( isset( $this->data['nav_urls']['recentchangeslinked'] )
  71. && $this->data['nav_urls']['recentchangeslinked']
  72. ) {
  73. $toolbox['recentchangeslinked'] = $this->data['nav_urls']['recentchangeslinked'];
  74. $toolbox['recentchangeslinked']['msg'] = 'recentchangeslinked-toolbox';
  75. $toolbox['recentchangeslinked']['id'] = 't-recentchangeslinked';
  76. $toolbox['recentchangeslinked']['rel'] = 'nofollow';
  77. }
  78. if ( isset( $this->data['feeds'] ) && $this->data['feeds'] ) {
  79. $toolbox['feeds']['id'] = 'feedlinks';
  80. $toolbox['feeds']['links'] = [];
  81. foreach ( $this->data['feeds'] as $key => $feed ) {
  82. $toolbox['feeds']['links'][$key] = $feed;
  83. $toolbox['feeds']['links'][$key]['id'] = "feed-$key";
  84. $toolbox['feeds']['links'][$key]['rel'] = 'alternate';
  85. $toolbox['feeds']['links'][$key]['type'] = "application/{$key}+xml";
  86. $toolbox['feeds']['links'][$key]['class'] = 'feedlink';
  87. }
  88. }
  89. foreach ( [ 'contributions', 'log', 'blockip', 'emailuser',
  90. 'userrights', 'upload', 'specialpages' ] as $special
  91. ) {
  92. if ( isset( $this->data['nav_urls'][$special] ) && $this->data['nav_urls'][$special] ) {
  93. $toolbox[$special] = $this->data['nav_urls'][$special];
  94. $toolbox[$special]['id'] = "t-$special";
  95. }
  96. }
  97. if ( isset( $this->data['nav_urls']['print'] ) && $this->data['nav_urls']['print'] ) {
  98. $toolbox['print'] = $this->data['nav_urls']['print'];
  99. $toolbox['print']['id'] = 't-print';
  100. $toolbox['print']['rel'] = 'alternate';
  101. $toolbox['print']['msg'] = 'printableversion';
  102. }
  103. if ( isset( $this->data['nav_urls']['permalink'] ) && $this->data['nav_urls']['permalink'] ) {
  104. $toolbox['permalink'] = $this->data['nav_urls']['permalink'];
  105. $toolbox['permalink']['id'] = 't-permalink';
  106. }
  107. if ( isset( $this->data['nav_urls']['info'] ) && $this->data['nav_urls']['info'] ) {
  108. $toolbox['info'] = $this->data['nav_urls']['info'];
  109. $toolbox['info']['id'] = 't-info';
  110. }
  111. // Avoid PHP 7.1 warning from passing $this by reference
  112. $template = $this;
  113. Hooks::run( 'BaseTemplateToolbox', [ &$template, &$toolbox ] );
  114. return $toolbox;
  115. }
  116. /**
  117. * Create an array of personal tools items from the data in the quicktemplate
  118. * stored by SkinTemplate.
  119. * The resulting array is built according to a format intended to be passed
  120. * through makeListItem to generate the html.
  121. * This is in reality the same list as already stored in personal_urls
  122. * however it is reformatted so that you can just pass the individual items
  123. * to makeListItem instead of hardcoding the element creation boilerplate.
  124. * @return array
  125. */
  126. function getPersonalTools() {
  127. $personal_tools = [];
  128. foreach ( $this->get( 'personal_urls' ) as $key => $plink ) {
  129. # The class on a personal_urls item is meant to go on the <a> instead
  130. # of the <li> so we have to use a single item "links" array instead
  131. # of using most of the personal_url's keys directly.
  132. $ptool = [
  133. 'links' => [
  134. [ 'single-id' => "pt-$key" ],
  135. ],
  136. 'id' => "pt-$key",
  137. ];
  138. if ( isset( $plink['active'] ) ) {
  139. $ptool['active'] = $plink['active'];
  140. }
  141. foreach ( [ 'href', 'class', 'text', 'dir', 'data', 'exists' ] as $k ) {
  142. if ( isset( $plink[$k] ) ) {
  143. $ptool['links'][0][$k] = $plink[$k];
  144. }
  145. }
  146. $personal_tools[$key] = $ptool;
  147. }
  148. return $personal_tools;
  149. }
  150. function getSidebar( $options = [] ) {
  151. // Force the rendering of the following portals
  152. $sidebar = $this->data['sidebar'];
  153. if ( !isset( $sidebar['SEARCH'] ) ) {
  154. $sidebar['SEARCH'] = true;
  155. }
  156. if ( !isset( $sidebar['TOOLBOX'] ) ) {
  157. $sidebar['TOOLBOX'] = true;
  158. }
  159. if ( !isset( $sidebar['LANGUAGES'] ) ) {
  160. $sidebar['LANGUAGES'] = true;
  161. }
  162. if ( !isset( $options['search'] ) || $options['search'] !== true ) {
  163. unset( $sidebar['SEARCH'] );
  164. }
  165. if ( isset( $options['toolbox'] ) && $options['toolbox'] === false ) {
  166. unset( $sidebar['TOOLBOX'] );
  167. }
  168. if ( isset( $options['languages'] ) && $options['languages'] === false ) {
  169. unset( $sidebar['LANGUAGES'] );
  170. }
  171. $boxes = [];
  172. foreach ( $sidebar as $boxName => $content ) {
  173. if ( $content === false ) {
  174. continue;
  175. }
  176. switch ( $boxName ) {
  177. case 'SEARCH':
  178. // Search is a special case, skins should custom implement this
  179. $boxes[$boxName] = [
  180. 'id' => 'p-search',
  181. 'header' => $this->getMsg( 'search' )->text(),
  182. 'generated' => false,
  183. 'content' => true,
  184. ];
  185. break;
  186. case 'TOOLBOX':
  187. $msgObj = $this->getMsg( 'toolbox' );
  188. $boxes[$boxName] = [
  189. 'id' => 'p-tb',
  190. 'header' => $msgObj->exists() ? $msgObj->text() : 'toolbox',
  191. 'generated' => false,
  192. 'content' => $this->getToolbox(),
  193. ];
  194. break;
  195. case 'LANGUAGES':
  196. if ( $this->data['language_urls'] !== false ) {
  197. $msgObj = $this->getMsg( 'otherlanguages' );
  198. $boxes[$boxName] = [
  199. 'id' => 'p-lang',
  200. 'header' => $msgObj->exists() ? $msgObj->text() : 'otherlanguages',
  201. 'generated' => false,
  202. 'content' => $this->data['language_urls'] ?: [],
  203. ];
  204. }
  205. break;
  206. default:
  207. $msgObj = $this->getMsg( $boxName );
  208. $boxes[$boxName] = [
  209. 'id' => "p-$boxName",
  210. 'header' => $msgObj->exists() ? $msgObj->text() : $boxName,
  211. 'generated' => true,
  212. 'content' => $content,
  213. ];
  214. break;
  215. }
  216. }
  217. // HACK: Compatibility with extensions still using SkinTemplateToolboxEnd
  218. $hookContents = null;
  219. if ( isset( $boxes['TOOLBOX'] ) ) {
  220. ob_start();
  221. // We pass an extra 'true' at the end so extensions using BaseTemplateToolbox
  222. // can abort and avoid outputting double toolbox links
  223. // Avoid PHP 7.1 warning from passing $this by reference
  224. $template = $this;
  225. Hooks::run( 'SkinTemplateToolboxEnd', [ &$template, true ] );
  226. $hookContents = ob_get_contents();
  227. ob_end_clean();
  228. if ( !trim( $hookContents ) ) {
  229. $hookContents = null;
  230. }
  231. }
  232. // END hack
  233. if ( isset( $options['htmlOnly'] ) && $options['htmlOnly'] === true ) {
  234. foreach ( $boxes as $boxName => $box ) {
  235. if ( is_array( $box['content'] ) ) {
  236. $content = '<ul>';
  237. foreach ( $box['content'] as $key => $val ) {
  238. $content .= "\n " . $this->makeListItem( $key, $val );
  239. }
  240. // HACK, shove the toolbox end onto the toolbox if we're rendering itself
  241. if ( $hookContents ) {
  242. $content .= "\n $hookContents";
  243. }
  244. // END hack
  245. $content .= "\n</ul>\n";
  246. $boxes[$boxName]['content'] = $content;
  247. }
  248. }
  249. } else {
  250. if ( $hookContents ) {
  251. $boxes['TOOLBOXEND'] = [
  252. 'id' => 'p-toolboxend',
  253. 'header' => $boxes['TOOLBOX']['header'],
  254. 'generated' => false,
  255. 'content' => "<ul>{$hookContents}</ul>",
  256. ];
  257. // HACK: Make sure that TOOLBOXEND is sorted next to TOOLBOX
  258. $boxes2 = [];
  259. foreach ( $boxes as $key => $box ) {
  260. if ( $key === 'TOOLBOXEND' ) {
  261. continue;
  262. }
  263. $boxes2[$key] = $box;
  264. if ( $key === 'TOOLBOX' ) {
  265. $boxes2['TOOLBOXEND'] = $boxes['TOOLBOXEND'];
  266. }
  267. }
  268. $boxes = $boxes2;
  269. // END hack
  270. }
  271. }
  272. return $boxes;
  273. }
  274. /**
  275. * @param string $name
  276. */
  277. protected function renderAfterPortlet( $name ) {
  278. echo $this->getAfterPortlet( $name );
  279. }
  280. /**
  281. * Allows extensions to hook into known portlets and add stuff to them
  282. *
  283. * @param string $name
  284. *
  285. * @return string html
  286. * @since 1.29
  287. */
  288. protected function getAfterPortlet( $name ) {
  289. $html = '';
  290. $content = '';
  291. Hooks::run( 'BaseTemplateAfterPortlet', [ $this, $name, &$content ] );
  292. if ( $content !== '' ) {
  293. $html = Html::rawElement(
  294. 'div',
  295. [ 'class' => [ 'after-portlet', 'after-portlet-' . $name ] ],
  296. $content
  297. );
  298. }
  299. return $html;
  300. }
  301. /**
  302. * Makes a link, usually used by makeListItem to generate a link for an item
  303. * in a list used in navigation lists, portlets, portals, sidebars, etc...
  304. *
  305. * @param string $key Usually a key from the list you are generating this
  306. * link from.
  307. * @param array $item Contains some of a specific set of keys.
  308. *
  309. * The text of the link will be generated either from the contents of the
  310. * "text" key in the $item array, if a "msg" key is present a message by
  311. * that name will be used, and if neither of those are set the $key will be
  312. * used as a message name.
  313. *
  314. * If a "href" key is not present makeLink will just output htmlescaped text.
  315. * The "href", "id", "class", "rel", and "type" keys are used as attributes
  316. * for the link if present.
  317. *
  318. * If an "id" or "single-id" (if you don't want the actual id to be output
  319. * on the link) is present it will be used to generate a tooltip and
  320. * accesskey for the link.
  321. *
  322. * The keys "context" and "primary" are ignored; these keys are used
  323. * internally by skins and are not supposed to be included in the HTML
  324. * output.
  325. *
  326. * If you don't want an accesskey, set $item['tooltiponly'] = true;
  327. *
  328. * If a "data" key is present, it must be an array, where the keys represent
  329. * the data-xxx properties with their provided values. For example,
  330. * $item['data'] = [
  331. * 'foo' => 1,
  332. * 'bar' => 'baz',
  333. * ];
  334. * will render as element properties:
  335. * data-foo='1' data-bar='baz'
  336. *
  337. * @param array $options Can be used to affect the output of a link.
  338. * Possible options are:
  339. * - 'text-wrapper' key to specify a list of elements to wrap the text of
  340. * a link in. This should be an array of arrays containing a 'tag' and
  341. * optionally an 'attributes' key. If you only have one element you don't
  342. * need to wrap it in another array. eg: To use <a><span>...</span></a>
  343. * in all links use [ 'text-wrapper' => [ 'tag' => 'span' ] ]
  344. * for your options.
  345. * - 'link-class' key can be used to specify additional classes to apply
  346. * to all links.
  347. * - 'link-fallback' can be used to specify a tag to use instead of "<a>"
  348. * if there is no link. eg: If you specify 'link-fallback' => 'span' than
  349. * any non-link will output a "<span>" instead of just text.
  350. *
  351. * @return string
  352. */
  353. function makeLink( $key, $item, $options = [] ) {
  354. if ( isset( $item['text'] ) ) {
  355. $text = $item['text'];
  356. } else {
  357. $text = wfMessage( $item['msg'] ?? $key )->text();
  358. }
  359. $html = htmlspecialchars( $text );
  360. if ( isset( $options['text-wrapper'] ) ) {
  361. $wrapper = $options['text-wrapper'];
  362. if ( isset( $wrapper['tag'] ) ) {
  363. $wrapper = [ $wrapper ];
  364. }
  365. while ( count( $wrapper ) > 0 ) {
  366. $element = array_pop( $wrapper );
  367. $html = Html::rawElement( $element['tag'], $element['attributes'] ?? null, $html );
  368. }
  369. }
  370. if ( isset( $item['href'] ) || isset( $options['link-fallback'] ) ) {
  371. $attrs = $item;
  372. foreach ( [ 'single-id', 'text', 'msg', 'tooltiponly', 'context', 'primary',
  373. 'tooltip-params', 'exists' ] as $k ) {
  374. unset( $attrs[$k] );
  375. }
  376. if ( isset( $attrs['data'] ) ) {
  377. foreach ( $attrs['data'] as $key => $value ) {
  378. $attrs[ 'data-' . $key ] = $value;
  379. }
  380. unset( $attrs[ 'data' ] );
  381. }
  382. if ( isset( $item['id'] ) && !isset( $item['single-id'] ) ) {
  383. $item['single-id'] = $item['id'];
  384. }
  385. $tooltipParams = [];
  386. if ( isset( $item['tooltip-params'] ) ) {
  387. $tooltipParams = $item['tooltip-params'];
  388. }
  389. if ( isset( $item['single-id'] ) ) {
  390. $tooltipOption = isset( $item['exists'] ) && $item['exists'] === false ? 'nonexisting' : null;
  391. if ( isset( $item['tooltiponly'] ) && $item['tooltiponly'] ) {
  392. $title = Linker::titleAttrib( $item['single-id'], $tooltipOption, $tooltipParams );
  393. if ( $title !== false ) {
  394. $attrs['title'] = $title;
  395. }
  396. } else {
  397. $tip = Linker::tooltipAndAccesskeyAttribs(
  398. $item['single-id'],
  399. $tooltipParams,
  400. $tooltipOption
  401. );
  402. if ( isset( $tip['title'] ) && $tip['title'] !== false ) {
  403. $attrs['title'] = $tip['title'];
  404. }
  405. if ( isset( $tip['accesskey'] ) && $tip['accesskey'] !== false ) {
  406. $attrs['accesskey'] = $tip['accesskey'];
  407. }
  408. }
  409. }
  410. if ( isset( $options['link-class'] ) ) {
  411. if ( isset( $attrs['class'] ) ) {
  412. $attrs['class'] .= " {$options['link-class']}";
  413. } else {
  414. $attrs['class'] = $options['link-class'];
  415. }
  416. }
  417. $html = Html::rawElement( isset( $attrs['href'] )
  418. ? 'a'
  419. : $options['link-fallback'], $attrs, $html );
  420. }
  421. return $html;
  422. }
  423. /**
  424. * Generates a list item for a navigation, portlet, portal, sidebar... list
  425. *
  426. * @param string $key Usually a key from the list you are generating this link from.
  427. * @param array $item Array of list item data containing some of a specific set of keys.
  428. * The "id", "class" and "itemtitle" keys will be used as attributes for the list item,
  429. * if "active" contains a value of true a "active" class will also be appended to class.
  430. *
  431. * @param array $options
  432. *
  433. * If you want something other than a "<li>" you can pass a tag name such as
  434. * "tag" => "span" in the $options array to change the tag used.
  435. * link/content data for the list item may come in one of two forms
  436. * A "links" key may be used, in which case it should contain an array with
  437. * a list of links to include inside the list item, see makeLink for the
  438. * format of individual links array items.
  439. *
  440. * Otherwise the relevant keys from the list item $item array will be passed
  441. * to makeLink instead. Note however that "id" and "class" are used by the
  442. * list item directly so they will not be passed to makeLink
  443. * (however the link will still support a tooltip and accesskey from it)
  444. * If you need an id or class on a single link you should include a "links"
  445. * array with just one link item inside of it. You can also set "link-class" in
  446. * $item to set a class on the link itself. If you want to add a title
  447. * to the list item itself, you can set "itemtitle" to the value.
  448. * $options is also passed on to makeLink calls
  449. *
  450. * @return string
  451. */
  452. function makeListItem( $key, $item, $options = [] ) {
  453. if ( isset( $item['links'] ) ) {
  454. $links = [];
  455. foreach ( $item['links'] as $linkKey => $link ) {
  456. $links[] = $this->makeLink( $linkKey, $link, $options );
  457. }
  458. $html = implode( ' ', $links );
  459. } else {
  460. $link = $item;
  461. // These keys are used by makeListItem and shouldn't be passed on to the link
  462. foreach ( [ 'id', 'class', 'active', 'tag', 'itemtitle' ] as $k ) {
  463. unset( $link[$k] );
  464. }
  465. if ( isset( $item['id'] ) && !isset( $item['single-id'] ) ) {
  466. // The id goes on the <li> not on the <a> for single links
  467. // but makeSidebarLink still needs to know what id to use when
  468. // generating tooltips and accesskeys.
  469. $link['single-id'] = $item['id'];
  470. }
  471. if ( isset( $link['link-class'] ) ) {
  472. // link-class should be set on the <a> itself,
  473. // so pass it in as 'class'
  474. $link['class'] = $link['link-class'];
  475. unset( $link['link-class'] );
  476. }
  477. $html = $this->makeLink( $key, $link, $options );
  478. }
  479. $attrs = [];
  480. foreach ( [ 'id', 'class' ] as $attr ) {
  481. if ( isset( $item[$attr] ) ) {
  482. $attrs[$attr] = $item[$attr];
  483. }
  484. }
  485. if ( isset( $item['active'] ) && $item['active'] ) {
  486. if ( !isset( $attrs['class'] ) ) {
  487. $attrs['class'] = '';
  488. }
  489. $attrs['class'] .= ' active';
  490. $attrs['class'] = trim( $attrs['class'] );
  491. }
  492. if ( isset( $item['itemtitle'] ) ) {
  493. $attrs['title'] = $item['itemtitle'];
  494. }
  495. return Html::rawElement( $options['tag'] ?? 'li', $attrs, $html );
  496. }
  497. function makeSearchInput( $attrs = [] ) {
  498. $realAttrs = [
  499. 'type' => 'search',
  500. 'name' => 'search',
  501. 'placeholder' => wfMessage( 'searchsuggest-search' )->text(),
  502. ];
  503. $realAttrs = array_merge( $realAttrs, Linker::tooltipAndAccesskeyAttribs( 'search' ), $attrs );
  504. return Html::element( 'input', $realAttrs );
  505. }
  506. function makeSearchButton( $mode, $attrs = [] ) {
  507. switch ( $mode ) {
  508. case 'go':
  509. case 'fulltext':
  510. $realAttrs = [
  511. 'type' => 'submit',
  512. 'name' => $mode,
  513. 'value' => wfMessage( $mode == 'go' ? 'searcharticle' : 'searchbutton' )->text(),
  514. ];
  515. $realAttrs = array_merge(
  516. $realAttrs,
  517. Linker::tooltipAndAccesskeyAttribs( "search-$mode" ),
  518. $attrs
  519. );
  520. return Html::element( 'input', $realAttrs );
  521. case 'image':
  522. $buttonAttrs = [
  523. 'type' => 'submit',
  524. 'name' => 'button',
  525. ];
  526. $buttonAttrs = array_merge(
  527. $buttonAttrs,
  528. Linker::tooltipAndAccesskeyAttribs( 'search-fulltext' ),
  529. $attrs
  530. );
  531. unset( $buttonAttrs['src'] );
  532. unset( $buttonAttrs['alt'] );
  533. unset( $buttonAttrs['width'] );
  534. unset( $buttonAttrs['height'] );
  535. $imgAttrs = [
  536. 'src' => $attrs['src'],
  537. 'alt' => $attrs['alt'] ?? wfMessage( 'searchbutton' )->text(),
  538. 'width' => $attrs['width'] ?? null,
  539. 'height' => $attrs['height'] ?? null,
  540. ];
  541. return Html::rawElement( 'button', $buttonAttrs, Html::element( 'img', $imgAttrs ) );
  542. default:
  543. throw new MWException( 'Unknown mode passed to BaseTemplate::makeSearchButton' );
  544. }
  545. }
  546. /**
  547. * Returns an array of footerlinks trimmed down to only those footer links that
  548. * are valid.
  549. * If you pass "flat" as an option then the returned array will be a flat array
  550. * of footer icons instead of a key/value array of footerlinks arrays broken
  551. * up into categories.
  552. * @param string|null $option
  553. * @return array|mixed
  554. */
  555. function getFooterLinks( $option = null ) {
  556. $footerlinks = $this->get( 'footerlinks' );
  557. // Reduce footer links down to only those which are being used
  558. $validFooterLinks = [];
  559. foreach ( $footerlinks as $category => $links ) {
  560. $validFooterLinks[$category] = [];
  561. foreach ( $links as $link ) {
  562. if ( isset( $this->data[$link] ) && $this->data[$link] ) {
  563. $validFooterLinks[$category][] = $link;
  564. }
  565. }
  566. if ( count( $validFooterLinks[$category] ) <= 0 ) {
  567. unset( $validFooterLinks[$category] );
  568. }
  569. }
  570. if ( $option == 'flat' ) {
  571. // fold footerlinks into a single array using a bit of trickery
  572. $validFooterLinks = array_merge( ...array_values( $validFooterLinks ) );
  573. }
  574. return $validFooterLinks;
  575. }
  576. /**
  577. * Returns an array of footer icons filtered down by options relevant to how
  578. * the skin wishes to display them.
  579. * If you pass "icononly" as the option all footer icons which do not have an
  580. * image icon set will be filtered out.
  581. * If you pass "nocopyright" then MediaWiki's copyright icon will not be included
  582. * in the list of footer icons. This is mostly useful for skins which only
  583. * display the text from footericons instead of the images and don't want a
  584. * duplicate copyright statement because footerlinks already rendered one.
  585. * @param string|null $option
  586. * @return array
  587. */
  588. function getFooterIcons( $option = null ) {
  589. // Generate additional footer icons
  590. $footericons = $this->get( 'footericons' );
  591. if ( $option == 'icononly' ) {
  592. // Unset any icons which don't have an image
  593. foreach ( $footericons as &$footerIconsBlock ) {
  594. foreach ( $footerIconsBlock as $footerIconKey => $footerIcon ) {
  595. if ( !is_string( $footerIcon ) && !isset( $footerIcon['src'] ) ) {
  596. unset( $footerIconsBlock[$footerIconKey] );
  597. }
  598. }
  599. }
  600. // Redo removal of any empty blocks
  601. foreach ( $footericons as $footerIconsKey => &$footerIconsBlock ) {
  602. if ( count( $footerIconsBlock ) <= 0 ) {
  603. unset( $footericons[$footerIconsKey] );
  604. }
  605. }
  606. } elseif ( $option == 'nocopyright' ) {
  607. unset( $footericons['copyright']['copyright'] );
  608. if ( count( $footericons['copyright'] ) <= 0 ) {
  609. unset( $footericons['copyright'] );
  610. }
  611. }
  612. return $footericons;
  613. }
  614. /**
  615. * Renderer for getFooterIcons and getFooterLinks
  616. *
  617. * @param string $iconStyle $option for getFooterIcons: "icononly", "nocopyright"
  618. * @param string $linkStyle $option for getFooterLinks: "flat"
  619. *
  620. * @return string html
  621. * @since 1.29
  622. */
  623. protected function getFooter( $iconStyle = 'icononly', $linkStyle = 'flat' ) {
  624. $validFooterIcons = $this->getFooterIcons( $iconStyle );
  625. $validFooterLinks = $this->getFooterLinks( $linkStyle );
  626. $html = '';
  627. if ( count( $validFooterIcons ) + count( $validFooterLinks ) > 0 ) {
  628. $html .= Html::openElement( 'div', [
  629. 'id' => 'footer-bottom',
  630. 'role' => 'contentinfo',
  631. 'lang' => $this->get( 'userlang' ),
  632. 'dir' => $this->get( 'dir' )
  633. ] );
  634. $footerEnd = Html::closeElement( 'div' );
  635. } else {
  636. $footerEnd = '';
  637. }
  638. foreach ( $validFooterIcons as $blockName => $footerIcons ) {
  639. $html .= Html::openElement( 'div', [
  640. 'id' => Sanitizer::escapeIdForAttribute( "f-{$blockName}ico" ),
  641. 'class' => 'footer-icons'
  642. ] );
  643. foreach ( $footerIcons as $icon ) {
  644. $html .= $this->getSkin()->makeFooterIcon( $icon );
  645. }
  646. $html .= Html::closeElement( 'div' );
  647. }
  648. if ( count( $validFooterLinks ) > 0 ) {
  649. $html .= Html::openElement( 'ul', [ 'id' => 'f-list', 'class' => 'footer-places' ] );
  650. foreach ( $validFooterLinks as $aLink ) {
  651. $html .= Html::rawElement(
  652. 'li',
  653. [ 'id' => Sanitizer::escapeIdForAttribute( $aLink ) ],
  654. $this->get( $aLink )
  655. );
  656. }
  657. $html .= Html::closeElement( 'ul' );
  658. }
  659. $html .= $this->getClear() . $footerEnd;
  660. return $html;
  661. }
  662. /**
  663. * Get a div with the core visualClear class, for clearing floats
  664. *
  665. * @return string html
  666. * @since 1.29
  667. */
  668. protected function getClear() {
  669. return Html::element( 'div', [ 'class' => 'visualClear' ] );
  670. }
  671. /**
  672. * Get the suggested HTML for page status indicators: icons (or short text snippets) usually
  673. * displayed in the top-right corner of the page, outside of the main content.
  674. *
  675. * Your skin may implement this differently, for example by handling some indicator names
  676. * specially with a different UI. However, it is recommended to use a `<div class="mw-indicator"
  677. * id="mw-indicator-<id>" />` as a wrapper element for each indicator, for better compatibility
  678. * with extensions and user scripts.
  679. *
  680. * The raw data is available in `$this->data['indicators']` as an associative array (keys:
  681. * identifiers, values: contents) internally ordered by keys.
  682. *
  683. * @return string HTML
  684. * @since 1.25
  685. */
  686. public function getIndicators() {
  687. $out = "<div class=\"mw-indicators mw-body-content\">\n";
  688. foreach ( $this->data['indicators'] as $id => $content ) {
  689. $out .= Html::rawElement(
  690. 'div',
  691. [
  692. 'id' => Sanitizer::escapeIdForAttribute( "mw-indicator-$id" ),
  693. 'class' => 'mw-indicator',
  694. ],
  695. $content
  696. ) . "\n";
  697. }
  698. $out .= "</div>\n";
  699. return $out;
  700. }
  701. /**
  702. * Output getTrail
  703. */
  704. function printTrail() {
  705. echo $this->getTrail();
  706. }
  707. /**
  708. * Get the basic end-page trail including bottomscripts, reporttime, and
  709. * debug stuff. This should be called right before outputting the closing
  710. * body and html tags.
  711. *
  712. * @return string|WrappedStringList HTML
  713. * @since 1.29
  714. */
  715. public function getTrail() {
  716. return WrappedString::join( "\n", [
  717. MWDebug::getDebugHTML( $this->getSkin()->getContext() ),
  718. $this->get( 'bottomscripts' ),
  719. $this->get( 'reporttime' )
  720. ] );
  721. }
  722. }