[ tag as returned by $this->refArg()
* @return string
*/
function stack( $str, $key = null ) {
if ( $key === null ) {
// No key
$this->mRefs[] = $str;
return $this->link( $this->mJ++, $str );
} else if ( is_string( $key ) )
// Valid key
if ( ! @is_array( $this->mRefs[$key] ) ) {
// First occourance
$this->mRefs[$key] = array(
'text' => $str,
'count' => 0
);
return $this->link( $key, $str, 0 );
} else
// We've been here before
return $this->link( $key, $str, ++$this->mRefs[$key]['count'] );
else
$this->croak( CITE_ERROR_STACK_INVALID_INPUT, serialize( array( $key, $str ) ) );
}
/**
* @return string
*/
function references( $str, $argv ) {
if ( $str !== null )
return $this->error( CITE_ERROR_REFERENCES_INVALID_INPUT );
else if ( count( $argv ) )
return $this->error( CITE_ERROR_REFERENCES_INVALID_PARAMETERS );
else
return $this->referencesFormat();
}
function referencesFormat() {
$ent = array();
foreach ( $this->mRefs as $k => $v )
$ent[] = $this->referencesFormatEntry( $k, $v );
$prefix = wfMsgForContentNoTrans( 'cite_references_prefix' );
$suffix = wfMsgForContentNoTrans( 'cite_references_suffix' );
$content = implode( "\n", $ent );
return $this->parse( $prefix . $content . $suffix );
}
function referencesFormatEntry( $key, $val ) {
global $wgContLang;
if ( ! is_array( $val ) )
return
wfMsgForContentNoTrans(
'cite_references_link_one',
$this->key( $key, false ),
$this->key( $key, true ),
$val
);
else {
$links = array();
for ( $i = 0; $i <= $val['count']; ++$i ) {
$links[] = wfMsgForContentNoTrans(
'cite_references_link_many_format',
$this->key( $key, true, $i ),
$wgContLang->formatNum( $i + 1 )
);
}
$list = $this->listToText( $links );
return
wfMsgForContentNoTrans( 'cite_references_link_many',
$this->key( $key, false ),
$list,
$val['text']
);
}
}
function key( $key, $ref, $num = null ) {
if ( $ref === true ) {
// _ref
$prefix = wfMsgForContent( 'cite_reference_link_prefix' );
$suffix = wfMsgForContent( 'cite_reference_link_suffix' );
if ( isset( $num ) )
$key = wfMsgForContentNoTrans( 'cite_reference_link_key_with_num', $key, $num );
} else if ( $ref === false ) {
// _note
$prefix = wfMsgForContent( 'cite_references_link_prefix' );
$suffix = wfMsgForContent( 'cite_references_link_suffix' );
if ( isset( $num ) )
$key = wfMsgForContentNoTrans( 'cite_reference_link_key_with_num', $key, $num );
}
return $prefix . $key . $suffix;
}
function link( $key, $val, $num = null ) {
global $wgContLang;
return
$this->parse(
wfMsgForContentNoTrans(
'cite_reference_link',
$this->key( $key, true, $num ),
$this->key( $key, false ),
$wgContLang->formatNum( ++$this->mI )
)
);
}
/**
* This does approximately the same thing as
* Langauge::listToText() but due to this being used for a
* slightly different purpose (people might not want , as the
* first seperator and not 'and' as the second, and this has to
* use messages from the content language) I'm rolling my own.
*
* @param array $l
* @return string
*/
function listToText( $arr ) {
$cnt = count( $arr );
$sep = wfMsgForContentNoTrans( 'cite_references_link_many_sep' );
$and = wfMsgForContentNoTrans( 'cite_references_link_many_and' );
if ( $cnt == 1 )
return (string)$arr[0];
else {
$t = array_slice( $arr, 0, $cnt - 1 );
return implode( $sep, $t ) . $and . $arr[$cnt - 1];
}
}
function parse( $in ) {
global $wgTitle;
$ret = $this->mParser->parse( $in, $wgTitle, $this->mParserOptions, false );
$text = $ret->getText();
return $this->fixTidy( $text );
// This had trouble with including stuff
//return $wgOut->parse( $in, false );
}
function fixTidy( $text ) {
global $wgUseTidy;
if ( ! $wgUseTidy )
return $text;
else {
$text = preg_replace( '/^]\s*/', '', $text );
$text = preg_replace( '/\s*<\/p>\s*/', '', $text );
wfDebugLog( 'misc', "'''$text'''" );
return $text;
}
}
function genParser() {
$this->mParser = new Parser;
$this->mParserOptions = new ParserOptions;
}
/**
* Initialize the parser hooks
*/
function setHooks() {
global $wgParser;
$wgParser->setHook( 'ref' , array( &$this, 'ref' ) );
$wgParser->setHook( 'references' , array( &$this, 'references' ) );
}
/**
* @param int $id ID for the error
* @return string
*/
function error( $id ) {
if ( $id > 0 )
// User errors are positive
return $this->parse( wfMsgforContent( 'cite_error', $id, wfMsgForContent( "cite_error_$id" ) ) );
else if ( $id < 0 )
return wfMsgforContent( 'cite_error', $id );
}
/**
* Die with a backtrace if something happens in the code which
* shouldn't have
*
* @param int $error ID for the error
* @param string $data Serialized error data
*/
function croak( $error, $data ) {
wfDebugDieBacktrace( wfMsgForContent( 'cite_croak', $this->error( $error ), $data ) );
}
/**#@-*/
}
new PersistentObject( new Cite );
}
/**#@-*/