MediaWiki  1.23.8
Message.php
Go to the documentation of this file.
1 <?php
159 class Message {
160 
167  protected $interface = true;
168 
175  protected $language = null;
176 
180  protected $key;
181 
185  protected $parameters = array();
186 
198  protected $format = 'parse';
199 
203  protected $useDatabase = true;
204 
208  protected $title = null;
209 
213  protected $content = null;
214 
218  protected $message;
219 
228  public function __construct( $key, $params = array(), Language $language = null ) {
229  global $wgLang;
230 
231  $this->key = $key;
232  $this->parameters = array_values( $params );
233  $this->language = $language ? $language : $wgLang;
234  }
235 
243  public function getKey() {
244  if ( is_array( $this->key ) ) {
245  // May happen if some kind of fallback is applied.
246  // For now, just use the first key. We really need a better solution.
247  return $this->key[0];
248  } else {
249  return $this->key;
250  }
251  }
252 
260  public function getParams() {
261  return $this->parameters;
262  }
263 
271  public function getFormat() {
272  return $this->format;
273  }
274 
282  public function getLanguage() {
283  return $this->language;
284  }
285 
298  public static function newFromKey( $key /*...*/ ) {
299  $params = func_get_args();
300  array_shift( $params );
301  return new self( $key, $params );
302  }
303 
316  public static function newFallbackSequence( /*...*/ ) {
317  $keys = func_get_args();
318  if ( func_num_args() == 1 ) {
319  if ( is_array( $keys[0] ) ) {
320  // Allow an array to be passed as the first argument instead
321  $keys = array_values( $keys[0] );
322  } else {
323  // Optimize a single string to not need special fallback handling
324  $keys = $keys[0];
325  }
326  }
327  return new self( $keys );
328  }
329 
340  public function params( /*...*/ ) {
341  $args = func_get_args();
342  if ( isset( $args[0] ) && is_array( $args[0] ) ) {
343  $args = $args[0];
344  }
345  $args_values = array_values( $args );
346  $this->parameters = array_merge( $this->parameters, $args_values );
347  return $this;
348  }
349 
363  public function rawParams( /*...*/ ) {
364  $params = func_get_args();
365  if ( isset( $params[0] ) && is_array( $params[0] ) ) {
366  $params = $params[0];
367  }
368  foreach ( $params as $param ) {
369  $this->parameters[] = self::rawParam( $param );
370  }
371  return $this;
372  }
373 
385  public function numParams( /*...*/ ) {
386  $params = func_get_args();
387  if ( isset( $params[0] ) && is_array( $params[0] ) ) {
388  $params = $params[0];
389  }
390  foreach ( $params as $param ) {
391  $this->parameters[] = self::numParam( $param );
392  }
393  return $this;
394  }
395 
407  public function durationParams( /*...*/ ) {
408  $params = func_get_args();
409  if ( isset( $params[0] ) && is_array( $params[0] ) ) {
410  $params = $params[0];
411  }
412  foreach ( $params as $param ) {
413  $this->parameters[] = self::durationParam( $param );
414  }
415  return $this;
416  }
417 
429  public function expiryParams( /*...*/ ) {
430  $params = func_get_args();
431  if ( isset( $params[0] ) && is_array( $params[0] ) ) {
432  $params = $params[0];
433  }
434  foreach ( $params as $param ) {
435  $this->parameters[] = self::expiryParam( $param );
436  }
437  return $this;
438  }
439 
451  public function timeperiodParams( /*...*/ ) {
452  $params = func_get_args();
453  if ( isset( $params[0] ) && is_array( $params[0] ) ) {
454  $params = $params[0];
455  }
456  foreach ( $params as $param ) {
457  $this->parameters[] = self::timeperiodParam( $param );
458  }
459  return $this;
460  }
461 
473  public function sizeParams( /*...*/ ) {
474  $params = func_get_args();
475  if ( isset( $params[0] ) && is_array( $params[0] ) ) {
476  $params = $params[0];
477  }
478  foreach ( $params as $param ) {
479  $this->parameters[] = self::sizeParam( $param );
480  }
481  return $this;
482  }
483 
495  public function bitrateParams( /*...*/ ) {
496  $params = func_get_args();
497  if ( isset( $params[0] ) && is_array( $params[0] ) ) {
498  $params = $params[0];
499  }
500  foreach ( $params as $param ) {
501  $this->parameters[] = self::bitrateParam( $param );
502  }
503  return $this;
504  }
505 
515  public function setContext( IContextSource $context ) {
516  $this->inLanguage( $context->getLanguage() );
517  $this->title( $context->getTitle() );
518  $this->interface = true;
519 
520  return $this;
521  }
522 
535  public function inLanguage( $lang ) {
536  if ( $lang instanceof Language || $lang instanceof StubUserLang ) {
537  $this->language = $lang;
538  } elseif ( is_string( $lang ) ) {
539  if ( $this->language->getCode() != $lang ) {
540  $this->language = Language::factory( $lang );
541  }
542  } else {
543  $type = gettype( $lang );
544  throw new MWException( __METHOD__ . " must be "
545  . "passed a String or Language object; $type given"
546  );
547  }
548  $this->interface = false;
549  return $this;
550  }
551 
561  public function inContentLanguage() {
562  global $wgForceUIMsgAsContentMsg;
563  if ( in_array( $this->key, (array)$wgForceUIMsgAsContentMsg ) ) {
564  return $this;
565  }
566 
568  $this->interface = false;
569  $this->language = $wgContLang;
570  return $this;
571  }
572 
583  public function setInterfaceMessageFlag( $interface ) {
584  $this->interface = (bool)$interface;
585  return $this;
586  }
587 
597  public function useDatabase( $useDatabase ) {
598  $this->useDatabase = (bool)$useDatabase;
599  return $this;
600  }
601 
611  public function title( $title ) {
612  $this->title = $title;
613  return $this;
614  }
615 
621  public function content() {
622  if ( !$this->content ) {
623  $this->content = new MessageContent( $this );
624  }
625 
626  return $this->content;
627  }
628 
636  public function toString() {
637  $string = $this->fetchMessage();
638 
639  if ( $string === false ) {
640  $key = htmlspecialchars( is_array( $this->key ) ? $this->key[0] : $this->key );
641  if ( $this->format === 'plain' ) {
642  return '<' . $key . '>';
643  }
644  return '&lt;' . $key . '&gt;';
645  }
646 
647  # Replace $* with a list of parameters for &uselang=qqx.
648  if ( strpos( $string, '$*' ) !== false ) {
649  $paramlist = '';
650  if ( $this->parameters !== array() ) {
651  $paramlist = ': $' . implode( ', $', range( 1, count( $this->parameters ) ) );
652  }
653  $string = str_replace( '$*', $paramlist, $string );
654  }
655 
656  # Replace parameters before text parsing
657  $string = $this->replaceParameters( $string, 'before' );
658 
659  # Maybe transform using the full parser
660  if ( $this->format === 'parse' ) {
661  $string = $this->parseText( $string );
662  $m = array();
663  if ( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $string, $m ) ) {
664  $string = $m[1];
665  }
666  } elseif ( $this->format === 'block-parse' ) {
667  $string = $this->parseText( $string );
668  } elseif ( $this->format === 'text' ) {
669  $string = $this->transformText( $string );
670  } elseif ( $this->format === 'escaped' ) {
671  $string = $this->transformText( $string );
672  $string = htmlspecialchars( $string, ENT_QUOTES, 'UTF-8', false );
673  }
674 
675  # Raw parameter replacement
676  $string = $this->replaceParameters( $string, 'after' );
677 
678  return $string;
679  }
680 
690  public function __toString() {
691  // PHP doesn't allow __toString to throw exceptions and will
692  // trigger a fatal error if it does. So, catch any exceptions.
693 
694  try {
695  return $this->toString();
696  } catch ( Exception $ex ) {
697  try {
698  trigger_error( "Exception caught in " . __METHOD__ . " (message " . $this->key . "): "
699  . $ex, E_USER_WARNING );
700  } catch ( Exception $ex ) {
701  // Doh! Cause a fatal error after all?
702  }
703 
704  if ( $this->format === 'plain' ) {
705  return '<' . $this->key . '>';
706  }
707  return '&lt;' . $this->key . '&gt;';
708  }
709  }
710 
718  public function parse() {
719  $this->format = 'parse';
720  return $this->toString();
721  }
722 
730  public function text() {
731  $this->format = 'text';
732  return $this->toString();
733  }
734 
742  public function plain() {
743  $this->format = 'plain';
744  return $this->toString();
745  }
746 
754  public function parseAsBlock() {
755  $this->format = 'block-parse';
756  return $this->toString();
757  }
758 
767  public function escaped() {
768  $this->format = 'escaped';
769  return $this->toString();
770  }
771 
779  public function exists() {
780  return $this->fetchMessage() !== false;
781  }
782 
791  public function isBlank() {
792  $message = $this->fetchMessage();
793  return $message === false || $message === '';
794  }
795 
803  public function isDisabled() {
804  $message = $this->fetchMessage();
805  return $message === false || $message === '' || $message === '-';
806  }
807 
815  public static function rawParam( $raw ) {
816  return array( 'raw' => $raw );
817  }
818 
826  public static function numParam( $num ) {
827  return array( 'num' => $num );
828  }
829 
837  public static function durationParam( $duration ) {
838  return array( 'duration' => $duration );
839  }
840 
848  public static function expiryParam( $expiry ) {
849  return array( 'expiry' => $expiry );
850  }
851 
859  public static function timeperiodParam( $period ) {
860  return array( 'period' => $period );
861  }
862 
870  public static function sizeParam( $size ) {
871  return array( 'size' => $size );
872  }
873 
881  public static function bitrateParam( $bitrate ) {
882  return array( 'bitrate' => $bitrate );
883  }
884 
895  protected function replaceParameters( $message, $type = 'before' ) {
896  $replacementKeys = array();
897  foreach ( $this->parameters as $n => $param ) {
898  list( $paramType, $value ) = $this->extractParam( $param );
899  if ( $type === $paramType ) {
900  $replacementKeys['$' . ( $n + 1 )] = $value;
901  }
902  }
903  $message = strtr( $message, $replacementKeys );
904  return $message;
905  }
906 
916  protected function extractParam( $param ) {
917  if ( is_array( $param ) ) {
918  if ( isset( $param['raw'] ) ) {
919  return array( 'after', $param['raw'] );
920  } elseif ( isset( $param['num'] ) ) {
921  // Replace number params always in before step for now.
922  // No support for combined raw and num params
923  return array( 'before', $this->language->formatNum( $param['num'] ) );
924  } elseif ( isset( $param['duration'] ) ) {
925  return array( 'before', $this->language->formatDuration( $param['duration'] ) );
926  } elseif ( isset( $param['expiry'] ) ) {
927  return array( 'before', $this->language->formatExpiry( $param['expiry'] ) );
928  } elseif ( isset( $param['period'] ) ) {
929  return array( 'before', $this->language->formatTimePeriod( $param['period'] ) );
930  } elseif ( isset( $param['size'] ) ) {
931  return array( 'before', $this->language->formatSize( $param['size'] ) );
932  } elseif ( isset( $param['bitrate'] ) ) {
933  return array( 'before', $this->language->formatBitrate( $param['bitrate'] ) );
934  } else {
935  $warning = 'Invalid parameter for message "' . $this->getKey() . '": ' .
936  htmlspecialchars( serialize( $param ) );
937  trigger_error( $warning, E_USER_WARNING );
938  $e = new Exception;
939  wfDebugLog( 'Bug58676', $warning . "\n" . $e->getTraceAsString() );
940 
941  return array( 'before', '[INVALID]' );
942  }
943  } elseif ( $param instanceof Message ) {
944  // Message objects should not be before parameters because
945  // then they'll get double escaped. If the message needs to be
946  // escaped, it'll happen right here when we call toString().
947  return array( 'after', $param->toString() );
948  } else {
949  return array( 'before', $param );
950  }
951  }
952 
962  protected function parseText( $string ) {
963  $out = MessageCache::singleton()->parse( $string, $this->title, /*linestart*/true, $this->interface, $this->language );
964  return $out instanceof ParserOutput ? $out->getText() : $out;
965  }
966 
976  protected function transformText( $string ) {
977  return MessageCache::singleton()->transform( $string, $this->interface, $this->language, $this->title );
978  }
979 
988  protected function fetchMessage() {
989  if ( !isset( $this->message ) ) {
991  if ( is_array( $this->key ) ) {
992  if ( !count( $this->key ) ) {
993  throw new MWException( "Given empty message key array." );
994  }
995  foreach ( $this->key as $key ) {
996  $message = $cache->get( $key, $this->useDatabase, $this->language );
997  if ( $message !== false && $message !== '' ) {
998  break;
999  }
1000  }
1001  $this->message = $message;
1002  } else {
1003  $this->message = $cache->get( $this->key, $this->useDatabase, $this->language );
1004  }
1005  }
1006  return $this->message;
1007  }
1008 
1009 }
1010 
1024 class RawMessage extends Message {
1025 
1035  public function __construct( $key, $params = array() ) {
1036  parent::__construct( $key, $params );
1037  // The key is the message.
1038  $this->message = $key;
1039  }
1040 
1046  public function fetchMessage() {
1047  // Just in case the message is unset somewhere.
1048  if ( !isset( $this->message ) ) {
1049  $this->message = $this->key;
1050  }
1051  return $this->message;
1052  }
1053 
1054 }
ParserOutput
Definition: ParserOutput.php:24
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
content
per default it will return the text for text based content
Definition: contenthandler.txt:107
text
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add text
Definition: design.txt:12
wfDebugLog
wfDebugLog( $logGroup, $text, $dest='all')
Send a line to a supplementary debug log file, if configured, or main debug log if not.
Definition: GlobalFunctions.php:1040
$n
$n
Definition: RandomTest.php:76
$params
$params
Definition: styleTest.css.php:40
$wgContLang
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the content language as $wgContLang
Definition: design.txt:56
true
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1530
key
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling but I prefer the flexibility This should also do the output encoding The system allocates a global one in $wgOut Title Represents the title of an and does all the work of translating among various forms such as plain database key
Definition: design.txt:25
title
to move a page</td >< td > &*You are moving the page across *A non empty talk page already exists under the new or *You uncheck the box below In those you will have to move or merge the page manually if desired</td >< td > be sure to &You are responsible for making sure that links continue to point where they are supposed to go Note that the page will &a page at the new title
Definition: All_system_messages.txt:2703
MWException
MediaWiki exception.
Definition: MWException.php:26
$out
$out
Definition: UtfNormalGenerate.php:167
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:188
MessageCache\singleton
static singleton()
Get the signleton instance of this class.
Definition: MessageCache.php:101
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
$size
$size
Definition: RandomTest.php:75
$value
$value
Definition: styleTest.css.php:45
StubUserLang
Stub object for the user language.
Definition: StubObject.php:190
IContextSource\getTitle
getTitle()
Get the Title object.
IContextSource
Interface for objects which can provide a context on request.
Definition: IContextSource.php:29
Content
Base interface for content objects.
Definition: Content.php:34
$args
if( $line===false) $args
Definition: cdb.php:62
Title
Represents a title within MediaWiki.
Definition: Title.php:35
$wgLang
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as $wgLang
Definition: design.txt:56
$cache
$cache
Definition: mcc.php:32
MessageContent
Wrapper allowing us to handle a system message as a Content object.
Definition: MessageContent.php:36
format
if the prop value should be in the metadata multi language array format
Definition: hooks.txt:1230
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
$keys
$keys
Definition: testCompression.php:63
Language\factory
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:184
$e
if( $useReadline) $e
Definition: eval.php:66
message
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing after in associative array form externallinks including delete and has completed for all link tables default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a message
Definition: hooks.txt:1624
Language
Internationalisation code.
Definition: Language.php:74
IContextSource\getLanguage
getLanguage()
Get the Language object.
$type
$type
Definition: testCompression.php:46