QuickTemplate.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 MediaWiki\MediaWikiServices;
  21. /**
  22. * Generic wrapper for template functions, with interface
  23. * compatible with what we use of PHPTAL 0.7.
  24. * @ingroup Skins
  25. */
  26. abstract class QuickTemplate {
  27. /**
  28. * @var array
  29. */
  30. public $data;
  31. /**
  32. * @var MediaWikiI18N
  33. */
  34. public $translator;
  35. /** @var Config $config */
  36. protected $config;
  37. /**
  38. * @param Config $config
  39. */
  40. function __construct( Config $config = null ) {
  41. $this->data = [];
  42. $this->translator = new MediaWikiI18N();
  43. if ( $config === null ) {
  44. wfDebug( __METHOD__ . ' was called with no Config instance passed to it' );
  45. $config = MediaWikiServices::getInstance()->getMainConfig();
  46. }
  47. $this->config = $config;
  48. }
  49. /**
  50. * Sets the value $value to $name
  51. * @param string $name
  52. * @param mixed $value
  53. */
  54. public function set( $name, $value ) {
  55. $this->data[$name] = $value;
  56. }
  57. /**
  58. * extends the value of data with name $name with the value $value
  59. * @since 1.25
  60. * @param string $name
  61. * @param mixed $value
  62. */
  63. public function extend( $name, $value ) {
  64. if ( $this->haveData( $name ) ) {
  65. $this->data[$name] = $this->data[$name] . $value;
  66. } else {
  67. $this->data[$name] = $value;
  68. }
  69. }
  70. /**
  71. * Gets the template data requested
  72. * @since 1.22
  73. * @param string $name Key for the data
  74. * @param mixed $default Optional default (or null)
  75. * @return mixed The value of the data requested or the deafult
  76. */
  77. public function get( $name, $default = null ) {
  78. if ( isset( $this->data[$name] ) ) {
  79. return $this->data[$name];
  80. } else {
  81. return $default;
  82. }
  83. }
  84. /**
  85. * @deprecated since 1.31 This function is a now-redundant optimisation intended
  86. * for very old versions of PHP. The use of references here makes the code
  87. * more fragile and is incompatible with plans like T140664. Use set() instead.
  88. * @param string $name
  89. * @param mixed &$value
  90. */
  91. public function setRef( $name, &$value ) {
  92. wfDeprecated( __METHOD__, '1.31' );
  93. $this->data[$name] =& $value;
  94. }
  95. /**
  96. * @param MediaWikiI18N &$t
  97. * @deprecate since 1.31 Use BaseTemplate::msg() or Skin::msg() instead for setting
  98. * message parameters.
  99. */
  100. public function setTranslator( &$t ) {
  101. wfDeprecated( __METHOD__, '1.31' );
  102. $this->translator = &$t;
  103. }
  104. /**
  105. * Main function, used by classes that subclass QuickTemplate
  106. * to show the actual HTML output
  107. */
  108. abstract public function execute();
  109. /**
  110. * @private
  111. * @param string $str
  112. */
  113. function text( $str ) {
  114. echo htmlspecialchars( $this->data[$str] );
  115. }
  116. /**
  117. * @private
  118. * @param string $str
  119. */
  120. function html( $str ) {
  121. echo $this->data[$str];
  122. }
  123. /**
  124. * @private
  125. * @param string $msgKey
  126. */
  127. function msg( $msgKey ) {
  128. echo htmlspecialchars( wfMessage( $msgKey )->text() );
  129. }
  130. /**
  131. * @private
  132. * @param string $msgKey
  133. */
  134. function msgHtml( $msgKey ) {
  135. echo wfMessage( $msgKey )->text();
  136. }
  137. /**
  138. * An ugly, ugly hack.
  139. * @private
  140. * @param string $msgKey
  141. */
  142. function msgWiki( $msgKey ) {
  143. global $wgOut;
  144. $text = wfMessage( $msgKey )->text();
  145. echo $wgOut->parse( $text );
  146. }
  147. /**
  148. * @private
  149. * @param string $str
  150. * @return bool
  151. */
  152. function haveData( $str ) {
  153. return isset( $this->data[$str] );
  154. }
  155. /**
  156. * @private
  157. *
  158. * @param string $msgKey
  159. * @return bool
  160. */
  161. function haveMsg( $msgKey ) {
  162. $msg = wfMessage( $msgKey );
  163. return $msg->exists() && !$msg->isDisabled();
  164. }
  165. /**
  166. * Get the Skin object related to this object
  167. *
  168. * @return Skin
  169. */
  170. public function getSkin() {
  171. return $this->data['skin'];
  172. }
  173. /**
  174. * Fetch the output of a QuickTemplate and return it
  175. *
  176. * @since 1.23
  177. * @return string
  178. */
  179. public function getHTML() {
  180. ob_start();
  181. $this->execute();
  182. $html = ob_get_contents();
  183. ob_end_clean();
  184. return $html;
  185. }
  186. }