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 ); } }