MediaWiki  master
QuickTemplate.php
Go to the documentation of this file.
1 <?php
21 use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
23 
60 abstract class QuickTemplate {
61  use ProtectedHookAccessorTrait;
62 
66  public $data;
67 
69  protected $config;
70 
72  private $deprecated = [];
73 
77  public function __construct( Config $config = null ) {
78  $this->data = [];
79  if ( $config === null ) {
80  wfDebug( __METHOD__ . ' was called with no Config instance passed to it' );
81  $config = MediaWikiServices::getInstance()->getMainConfig();
82  }
83  $this->config = $config;
84  }
85 
93  public function deprecate( string $name, string $version ) {
94  $this->deprecated[$name] = $version;
95  }
96 
102  public function set( $name, $value ) {
103  $this->data[$name] = $value;
104  }
105 
112  public function extend( $name, $value ) {
113  if ( $this->haveData( $name ) ) {
114  $this->data[$name] .= $value;
115  } else {
116  $this->data[$name] = $value;
117  }
118  }
119 
125  private function checkDeprecationStatus( string $name ) {
126  $deprecated = $this->deprecated[ $name ] ?? false;
127  if ( $deprecated ) {
128  wfDeprecated(
129  'QuickTemplate::(get/html/text/haveData) with parameter `' . $name . '`',
130  $deprecated
131  );
132  }
133  }
134 
143  public function get( $name, $default = null ) {
144  $this->checkDeprecationStatus( $name );
145  return $this->data[$name] ?? $default;
146  }
147 
152  abstract public function execute();
153 
158  protected function text( $str ) {
159  $this->checkDeprecationStatus( $str );
160  echo htmlspecialchars( $this->data[$str] );
161  }
162 
167  public function html( $str ) {
168  $this->checkDeprecationStatus( $str );
169  echo $this->data[$str];
170  }
171 
175  public function msg( $msgKey ) {
176  echo htmlspecialchars( wfMessage( $msgKey )->text() );
177  }
178 
183  private function haveData( $str ) {
184  $this->checkDeprecationStatus( $str );
185  return isset( $this->data[$str] );
186  }
187 
192  protected function haveMsg( $msgKey ) {
193  $msg = wfMessage( $msgKey );
194  return $msg->exists() && !$msg->isDisabled();
195  }
196 
202  public function getSkin() {
203  return $this->data['skin'];
204  }
205 
212  public function getHTML() {
213  ob_start();
214  $this->execute();
215  $html = ob_get_contents();
216  ob_end_clean();
217  return $html;
218  }
219 }
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
Service locator for MediaWiki core services.
PHP-based skin template that holds data.
getSkin()
Get the Skin object related to this object.
__construct(Config $config=null)
getHTML()
Fetch the output of a QuickTemplate and return it.
extend( $name, $value)
extends the value of data with name $name with the value $value
execute()
Main function, used by classes that subclass QuickTemplate to show the actual HTML output.
haveMsg( $msgKey)
deprecate(string $name, string $version)
Sets a template key as deprecated.
Interface for configuration instances.
Definition: Config.php:30