From 61095ced9fdc6f44f5bbd0a653feb370f16e6d95 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Niklas=20Laxstr=C3=B6m?= Date: Mon, 22 Mar 2010 17:05:32 +0000 Subject: [PATCH] New message class up for discussion and development. Not ment for use yet. It is just isolated file so it really doesn't need branch either. --- includes/AutoLoader.php | 1 + includes/Message.php | 178 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 179 insertions(+) create mode 100644 includes/Message.php diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 01b0d9383f..f32db5cac9 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -163,6 +163,7 @@ $wgAutoloadLocalClasses = array( 'MediaWiki_I18N' => 'includes/SkinTemplate.php', 'MediaWiki' => 'includes/Wiki.php', 'MemCachedClientforWiki' => 'includes/memcached-client.php', + 'Message' => 'includes/Message.php', 'MessageCache' => 'includes/MessageCache.php', 'MimeMagic' => 'includes/MimeMagic.php', 'MWException' => 'includes/Exception.php', diff --git a/includes/Message.php b/includes/Message.php new file mode 100644 index 0000000000..7a3ccda32d --- /dev/null +++ b/includes/Message.php @@ -0,0 +1,178 @@ +key = $key; + } + + /** + * Factory function that is just wrapper for the real constructor. It is + * intented to be used instead of the real constructor, because it allows + * chaining method calls, while new objects don't. + * //FIXME: key or get or something else? + * @param $key String: message key + * @return Message: reference to the object + */ + public function key( $key ) { + return new Message( $key ); + } + + /** + * Adds parameters to the paramater list of this message. + * @params String: parameter + * @return Message: reference to the object + */ + public function param( $value ) { + $this->parameters[] = $value; + return $this; + } + + /** + * Adds parameters to the paramater list of this message. + * @params Vargars: parameters + * @return Message: reference to the object + */ + public function params( /*...*/ ) { + $this->paramList( func_get_args() ); + return $this; + } + + /** + * Adds a list of parameters to the parameter list of this message. + * @param $value Array: list of parameters, array keys will be ignored. + * @return Message: reference to the object + */ + + public function paramList( array $values ) { + $this->parameters += array_values( $values ); + return $this; + } + + /** + * + */ + public function rawParam( $value ) { + $this->parameters[] = array( 'raw' => $value ); + return $this; + } + + public function parse() { + $string = $this->parseAsBlock( $string ); + $m = array(); + if( preg_match( '/^

(.*)\n?<\/p>\n?$/sU', $string, $m ) ) { + $string = $m[1]; + } + return $string; + } + + public function text() { + $string = $this->getMessageText(); + $string = $this->replaceParameters( 'before' ); + $string = $this->parseText( $string ); + $string = $this->replaceParameters( 'after' ); + $m = array(); + if( preg_match( '/^

(.*)\n?<\/p>\n?$/sU', $string, $m ) ) { + $string = $m[1]; + } + return $string; + } + + public function plain() { + $string = $this->getMessageText(); + $string = $this->replaceParameters( 'before' ); + $string = $this->transformText( $string ); + $string = $this->replaceParameters( 'after' ); + return $string; + } + + public function parseAsBlock() { + $string = $this->getMessageText(); + $string = $this->replaceParameters( 'before' ); + $string = $this->parseText( $string ); + $string = $this->replaceParameters( 'after' ); + return $string; + } + + public function escaped() { + return htmlspecialchars( $this->plain() ); + } + + protected function replaceParamaters( $type = 'before' ) { + $replacementKeys = array(); + foreach( $args as $n => $param ) { + if ( $type === 'before' && !isset( $param['raw'] ) ) { + $replacementKeys['$' . ($n + 1)] = $param; + } elseif ( $type === 'after' && isset( $param['raw'] ) ) { + $replacementKeys['$' . ($n + 1)] = $param['raw']; + } + } + $message = strtr( $message, $replacementKeys ); + return $message; + } + + // FIXME: handle properly + protected function getLanguage() { + return $this->language === null ? true : $this->language; + } + + protected function parseText( $string ) { + global $wgOut; + return $wgOut->parse( $string, true, (bool) $this->getLanguage() ); + } + + protected function transformText( $string ) { + global $wgMessageCache; + if ( !isset( $wgMessageCache ) ) return $string; + // FIXME: handle second param correctly + return $wgMessageCache->transform( $string, true, $this->getLanguage() ); + } + + protected function getMessageText() { + return wfMsgGetKey( $this->key, /*DB*/true, $this->getLanguage(), /*Transform*/false ); + } + +} \ No newline at end of file -- 2.20.1