2005-12-21 01:08:25 +00:00
|
|
|
<?php
|
|
|
|
if ( ! defined( 'MEDIAWIKI' ) )
|
2006-01-27 10:14:45 +00:00
|
|
|
die();
|
2005-12-21 01:08:25 +00:00
|
|
|
/**#@+
|
2005-12-25 19:39:22 +00:00
|
|
|
* A parser extension that adds two tags, <ref> and <references> for adding
|
|
|
|
* citations to pages
|
|
|
|
*
|
2007-01-20 15:10:35 +00:00
|
|
|
* @addtogroup Extensions
|
2005-12-21 01:08:25 +00:00
|
|
|
*
|
2005-12-25 19:39:22 +00:00
|
|
|
* @link http://meta.wikimedia.org/wiki/Cite/Cite.php Documentation
|
2006-01-07 09:24:48 +00:00
|
|
|
* @link http://www.w3.org/TR/html4/struct/text.html#edef-CITE <cite> definition in HTML
|
|
|
|
* @link http://www.w3.org/TR/2005/WD-xhtml2-20050527/mod-text.html#edef_text_cite <cite> definition in XHTML 2.0
|
2005-12-25 19:39:22 +00:00
|
|
|
*
|
2006-01-12 15:14:46 +00:00
|
|
|
* @bug 4579
|
|
|
|
*
|
2005-12-21 01:08:25 +00:00
|
|
|
* @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
|
|
|
|
* @copyright Copyright © 2005, Ævar Arnfjörð Bjarmason
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
|
|
|
|
*/
|
|
|
|
|
|
|
|
$wgExtensionFunctions[] = 'wfCite';
|
|
|
|
$wgExtensionCredits['parserhook'][] = array(
|
|
|
|
'name' => 'Cite',
|
2008-02-27 14:13:10 +00:00
|
|
|
'version' => preg_replace('/^.* (\d\d\d\d-\d\d-\d\d) .*$/', '\1', '$LastChangedDate$'), #just the date of the last change
|
2005-12-21 01:08:25 +00:00
|
|
|
'author' => 'Ævar Arnfjörð Bjarmason',
|
2008-01-31 14:38:12 +00:00
|
|
|
'description' => 'Adds <nowiki><ref[ name=id]></nowiki> and <nowiki><references/></nowiki> tags, for citations', // kept for b/c
|
|
|
|
'descriptionmsg' => 'cite_desc',
|
2007-12-16 18:45:23 +00:00
|
|
|
'url' => 'http://www.mediawiki.org/wiki/Extension:Cite/Cite.php'
|
2005-12-21 01:08:25 +00:00
|
|
|
);
|
2006-11-11 14:36:47 +00:00
|
|
|
$wgParserTestFiles[] = dirname( __FILE__ ) . "/citeParserTests.txt";
|
2007-12-16 20:18:33 +00:00
|
|
|
$wgExtensionMessagesFiles['Cite'] = dirname( __FILE__ ) . "/Cite.i18n.php";
|
2006-01-27 10:14:45 +00:00
|
|
|
|
2008-03-21 15:25:26 +00:00
|
|
|
define( 'CITE_DEFAULT_GROUP', '');
|
|
|
|
/**
|
|
|
|
* The emergency shut-off switch. Override in local settings to disable
|
|
|
|
* groups; or remove all references from this file to enable unconditionally
|
|
|
|
*/
|
|
|
|
$wgAllowCiteGroups = true;
|
|
|
|
|
2006-07-15 14:00:29 +00:00
|
|
|
function wfCite() {
|
2005-12-21 01:08:25 +00:00
|
|
|
class Cite {
|
2005-12-23 02:34:39 +00:00
|
|
|
/**#@+
|
|
|
|
* @access private
|
|
|
|
*/
|
2007-12-16 18:45:23 +00:00
|
|
|
|
2005-12-23 02:34:39 +00:00
|
|
|
/**
|
|
|
|
* Datastructure representing <ref> input, in the format of:
|
|
|
|
* <code>
|
|
|
|
* array(
|
|
|
|
* 'user supplied' => array(
|
|
|
|
* 'text' => 'user supplied reference & key',
|
|
|
|
* 'count' => 1, // occurs twice
|
2005-12-29 21:56:00 +00:00
|
|
|
* 'number' => 1, // The first reference, we want
|
|
|
|
* // all occourances of it to
|
|
|
|
* // use the same number
|
2005-12-23 02:34:39 +00:00
|
|
|
* ),
|
|
|
|
* 0 => 'Anonymous reference',
|
|
|
|
* 1 => 'Another anonymous reference',
|
|
|
|
* 'some key' => array(
|
|
|
|
* 'text' => 'this one occurs once'
|
2005-12-29 21:56:00 +00:00
|
|
|
* 'count' => 0,
|
|
|
|
* 'number' => 4
|
2005-12-23 02:34:39 +00:00
|
|
|
* ),
|
|
|
|
* 3 => 'more stuff'
|
|
|
|
* );
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* This works because:
|
|
|
|
* * PHP's datastructures are guarenteed to be returned in the
|
|
|
|
* order that things are inserted into them (unless you mess
|
|
|
|
* with that)
|
|
|
|
* * User supplied keys can't be integers, therefore avoiding
|
|
|
|
* conflict with anonymous keys
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
**/
|
2005-12-21 01:08:25 +00:00
|
|
|
var $mRefs = array();
|
2007-12-16 18:45:23 +00:00
|
|
|
|
2005-12-23 02:34:39 +00:00
|
|
|
/**
|
|
|
|
* Count for user displayed output (ref[1], ref[2], ...)
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
var $mOutCnt = 0;
|
2008-03-21 15:25:26 +00:00
|
|
|
var $mGroupCnt = array();
|
2005-12-23 02:34:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal counter for anonymous references, seperate from
|
|
|
|
* $mOutCnt because anonymous references won't increment it,
|
|
|
|
* but will incremement $mOutCnt
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
var $mInCnt = 0;
|
2005-12-29 21:56:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The backlinks, in order, to pass as $3 to
|
|
|
|
* 'cite_references_link_many_format', defined in
|
|
|
|
* 'cite_references_link_many_format_backlink_labels
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $mBacklinkLabels;
|
2007-12-16 18:45:23 +00:00
|
|
|
|
2005-12-23 02:34:39 +00:00
|
|
|
/**
|
|
|
|
* @var object
|
|
|
|
*/
|
2006-02-21 19:56:09 +00:00
|
|
|
var $mParser;
|
2007-12-16 18:45:23 +00:00
|
|
|
|
2006-04-16 06:22:42 +00:00
|
|
|
/**
|
|
|
|
* True when a <ref> or <references> tag is being processed.
|
|
|
|
* Used to avoid infinite recursion
|
2007-12-16 18:45:23 +00:00
|
|
|
*
|
2006-04-16 06:22:42 +00:00
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
var $mInCite = false;
|
2007-12-16 18:45:23 +00:00
|
|
|
|
2005-12-21 01:08:25 +00:00
|
|
|
/**#@-*/
|
|
|
|
|
2005-12-23 02:34:39 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
2005-12-21 01:08:25 +00:00
|
|
|
function Cite() {
|
|
|
|
$this->setHooks();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**#@+ @access private */
|
|
|
|
|
2005-12-23 02:34:39 +00:00
|
|
|
/**
|
|
|
|
* Callback function for <ref>
|
|
|
|
*
|
|
|
|
* @param string $str Input
|
|
|
|
* @param array $argv Arguments
|
|
|
|
* @return string
|
|
|
|
*/
|
2006-02-21 19:56:09 +00:00
|
|
|
function ref( $str, $argv, $parser ) {
|
2008-01-10 20:09:12 +00:00
|
|
|
wfLoadExtensionMessages( 'Cite' );
|
2006-04-16 06:22:42 +00:00
|
|
|
if ( $this->mInCite ) {
|
|
|
|
return htmlspecialchars( "<ref>$str</ref>" );
|
|
|
|
} else {
|
|
|
|
$this->mInCite = true;
|
|
|
|
$ret = $this->guardedRef( $str, $argv, $parser );
|
|
|
|
$this->mInCite = false;
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
}
|
2007-12-16 18:45:23 +00:00
|
|
|
|
2008-03-21 15:25:26 +00:00
|
|
|
function guardedRef( $str, $argv, $parser, $default_group=CITE_DEFAULT_GROUP ) {
|
2006-02-21 19:56:09 +00:00
|
|
|
$this->mParser = $parser;
|
2007-12-16 18:49:10 +00:00
|
|
|
|
|
|
|
# The key here is the "name" attribute.
|
2008-03-21 15:25:26 +00:00
|
|
|
list($key,$group) = $this->refArg( $argv );
|
2007-12-16 18:45:23 +00:00
|
|
|
|
2007-12-16 18:49:10 +00:00
|
|
|
if( $str === '' ) {
|
|
|
|
# <ref ...></ref>. This construct is always invalid: either
|
|
|
|
# it's a contentful ref, or it's a named duplicate and should
|
|
|
|
# be <ref ... />.
|
2007-12-16 20:18:33 +00:00
|
|
|
return $this->error( 'cite_error_ref_no_input' );
|
2007-12-16 18:49:10 +00:00
|
|
|
}
|
2007-12-16 18:45:23 +00:00
|
|
|
|
2007-12-16 18:49:10 +00:00
|
|
|
if( $key === false ) {
|
|
|
|
# TODO: Comment this case; what does this condition mean?
|
2007-12-16 20:18:33 +00:00
|
|
|
return $this->error( 'cite_error_ref_too_many_keys' );
|
2007-12-16 18:49:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( $str === null and $key === null ) {
|
|
|
|
# Something like <ref />; this makes no sense.
|
2007-12-16 20:18:33 +00:00
|
|
|
return $this->error( 'cite_error_ref_no_key' );
|
2007-12-16 18:49:10 +00:00
|
|
|
}
|
|
|
|
|
2007-12-27 22:18:40 +00:00
|
|
|
if( preg_match( '/^[0-9]+$/', $key ) ) {
|
2007-12-16 18:49:10 +00:00
|
|
|
# Numeric names mess up the resulting id's, potentially produ-
|
|
|
|
# cing duplicate id's in the XHTML. The Right Thing To Do
|
|
|
|
# would be to mangle them, but it's not really high-priority
|
|
|
|
# (and would produce weird id's anyway).
|
2007-12-16 20:18:33 +00:00
|
|
|
return $this->error( 'cite_error_ref_numeric_key' );
|
2007-12-16 18:49:10 +00:00
|
|
|
}
|
2008-03-21 15:25:26 +00:00
|
|
|
|
|
|
|
#Split these into groups.
|
|
|
|
if( $group === null ) {
|
|
|
|
$group = $default_group;
|
|
|
|
}
|
2007-12-16 18:49:10 +00:00
|
|
|
|
|
|
|
if( is_string( $key ) or is_string( $str ) ) {
|
|
|
|
# We don't care about the content: if the key exists, the ref
|
|
|
|
# is presumptively valid. Either it stores a new ref, or re-
|
|
|
|
# fers to an existing one. If it refers to a nonexistent ref,
|
|
|
|
# we'll figure that out later. Likewise it's definitely valid
|
|
|
|
# if there's any content, regardless of key.
|
2008-03-21 15:25:26 +00:00
|
|
|
return $this->stack( $str, $key, $group );
|
2007-12-16 18:49:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Not clear how we could get here, but something is probably
|
|
|
|
# wrong with the types. Let's fail fast.
|
2007-12-16 20:18:33 +00:00
|
|
|
$this->croak( 'cite_error_key_str_invalid', serialize( "$str; $key" ) );
|
2005-12-21 01:08:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the arguments to the <ref> tag
|
|
|
|
*
|
2006-01-07 09:24:48 +00:00
|
|
|
* @static
|
2005-12-21 01:08:25 +00:00
|
|
|
*
|
2006-01-07 09:24:48 +00:00
|
|
|
* @param array $argv The argument vector
|
2005-12-21 01:08:25 +00:00
|
|
|
* @return mixed false on invalid input, a string on valid
|
|
|
|
* input and null on no input
|
|
|
|
*/
|
|
|
|
function refArg( $argv ) {
|
2008-03-21 15:25:26 +00:00
|
|
|
global $wgAllowCiteGroups;
|
2005-12-24 18:03:21 +00:00
|
|
|
$cnt = count( $argv );
|
2008-03-21 15:25:26 +00:00
|
|
|
$group = null;
|
|
|
|
$key = null;
|
|
|
|
|
|
|
|
if ( $cnt > 2 )
|
|
|
|
// There should only be one key and one group
|
2005-12-21 01:08:25 +00:00
|
|
|
return false;
|
2008-03-21 15:25:26 +00:00
|
|
|
else if ( $cnt >= 1 ) {
|
|
|
|
if ( isset( $argv['name'] ) ) {
|
2005-12-24 18:03:21 +00:00
|
|
|
// Key given.
|
2008-03-21 15:25:26 +00:00
|
|
|
$key = $this->validateName( $argv['name'] );
|
|
|
|
unset( $argv['name']);
|
|
|
|
--$cnt;
|
|
|
|
}
|
|
|
|
if ( isset( $argv['group'] ) ){
|
|
|
|
if (! $wgAllowCiteGroups ) return array(false); //remove when groups are fully tested.
|
|
|
|
// Group given.
|
|
|
|
$group = $this->validateName( $argv['group'] );
|
|
|
|
unset( $argv['group']);
|
|
|
|
--$cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $cnt == 0)
|
|
|
|
return array ($key,$group);
|
2005-12-24 18:03:21 +00:00
|
|
|
else
|
|
|
|
// Invalid key
|
2008-03-21 15:25:26 +00:00
|
|
|
return array(false);
|
|
|
|
}
|
2005-12-21 01:08:25 +00:00
|
|
|
else
|
|
|
|
// No key
|
2008-03-21 15:25:26 +00:00
|
|
|
return array(null,$group);
|
2005-12-21 01:08:25 +00:00
|
|
|
}
|
2007-12-16 18:45:23 +00:00
|
|
|
|
2006-05-26 11:54:38 +00:00
|
|
|
/**
|
|
|
|
* Since the key name is used in an XHTML id attribute, it must
|
|
|
|
* conform to the validity rules. The restriction to begin with
|
|
|
|
* a letter is lifted since references have their own prefix.
|
|
|
|
*
|
|
|
|
* @fixme merge this code with the various section name transformations
|
|
|
|
* @fixme double-check for complete validity
|
|
|
|
* @return string if valid, false if invalid
|
|
|
|
*/
|
|
|
|
function validateName( $name ) {
|
|
|
|
if( preg_match( '/^[A-Za-z0-9:_.-]*$/i', $name ) ) {
|
|
|
|
return $name;
|
|
|
|
} else {
|
|
|
|
// WARNING: CRAPPY CUT AND PASTE MAKES BABY JESUS CRY
|
|
|
|
$text = urlencode( str_replace( ' ', '_', $name ) );
|
|
|
|
$replacearray = array(
|
|
|
|
'%3A' => ':',
|
|
|
|
'%' => '.'
|
|
|
|
);
|
|
|
|
return str_replace(
|
|
|
|
array_keys( $replacearray ),
|
|
|
|
array_values( $replacearray ),
|
|
|
|
$text );
|
|
|
|
}
|
|
|
|
}
|
2005-12-21 01:08:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Populate $this->mRefs based on input and arguments to <ref>
|
|
|
|
*
|
|
|
|
* @param string $str Input from the <ref> tag
|
|
|
|
* @param mixed $key Argument to the <ref> tag as returned by $this->refArg()
|
2007-12-16 18:45:23 +00:00
|
|
|
* @return string
|
2005-12-21 01:08:25 +00:00
|
|
|
*/
|
2008-03-21 15:25:26 +00:00
|
|
|
function stack( $str, $key = null, $group ) {
|
|
|
|
if (! isset($this->mRefs[$group]))
|
|
|
|
$this->mRefs[$group]=array();
|
|
|
|
if (! isset($this->mGroupCnt[$group]))
|
|
|
|
$this->mGroupCnt[$group]=0;
|
|
|
|
|
2005-12-21 01:08:25 +00:00
|
|
|
if ( $key === null ) {
|
|
|
|
// No key
|
2008-03-21 15:25:26 +00:00
|
|
|
//$this->mRefs[$group][] = $str;
|
|
|
|
$this->mRefs[$group][] = array('count'=>-1, 'text'=>$str, 'key'=>++$this->mOutCnt);
|
|
|
|
|
|
|
|
return $this->linkRef( $group, $this->mInCnt++ );
|
|
|
|
} else if ( is_string( $key ) ) {
|
2005-12-21 01:08:25 +00:00
|
|
|
// Valid key
|
2008-03-21 15:25:26 +00:00
|
|
|
if ( ! isset( $this->mRefs[$group][$key] ) || ! is_array( $this->mRefs[$group][$key] ) ) {
|
|
|
|
// First occurance
|
|
|
|
$this->mRefs[$group][$key] = array(
|
2005-12-21 01:08:25 +00:00
|
|
|
'text' => $str,
|
2005-12-29 21:56:00 +00:00
|
|
|
'count' => 0,
|
2008-03-21 15:25:26 +00:00
|
|
|
'key' => ++$this->mOutCnt,
|
|
|
|
'number' => ++$this->mGroupCnt[$group]
|
2005-12-21 01:08:25 +00:00
|
|
|
);
|
2008-03-21 22:31:55 +00:00
|
|
|
$this->mInCnt++;
|
2005-12-29 21:56:00 +00:00
|
|
|
return
|
|
|
|
$this->linkRef(
|
2008-03-21 15:25:26 +00:00
|
|
|
$group,
|
2005-12-29 21:56:00 +00:00
|
|
|
$key,
|
2008-03-21 15:25:26 +00:00
|
|
|
$this->mRefs[$group][$key]['key']."-".$this->mRefs[$group][$key]['count'],
|
|
|
|
$this->mRefs[$group][$key]['number'],
|
|
|
|
"-".$this->mRefs[$group][$key]['key']
|
2005-12-29 21:56:00 +00:00
|
|
|
);
|
2006-11-03 14:08:35 +00:00
|
|
|
} else {
|
2005-12-21 01:08:25 +00:00
|
|
|
// We've been here before
|
2008-03-21 15:25:26 +00:00
|
|
|
if ( $this->mRefs[$group][$key]['text'] === null && $str !== '' ) {
|
2006-11-03 14:08:35 +00:00
|
|
|
// If no text found before, use this text
|
2008-03-21 15:25:26 +00:00
|
|
|
$this->mRefs[$group][$key]['text'] = $str;
|
2006-11-03 14:08:35 +00:00
|
|
|
};
|
2007-12-16 18:45:23 +00:00
|
|
|
return
|
2005-12-29 21:56:00 +00:00
|
|
|
$this->linkRef(
|
2008-03-21 15:25:26 +00:00
|
|
|
$group,
|
2005-12-29 21:56:00 +00:00
|
|
|
$key,
|
2008-03-21 15:25:26 +00:00
|
|
|
$this->mRefs[$group][$key]['key']."-".++$this->mRefs[$group][$key]['count'],
|
|
|
|
$this->mRefs[$group][$key]['number'],
|
|
|
|
"-".$this->mRefs[$group][$key]['key']
|
2006-11-03 14:08:35 +00:00
|
|
|
); }
|
2008-03-21 15:25:26 +00:00
|
|
|
}
|
|
|
|
|
2005-12-21 01:08:25 +00:00
|
|
|
else
|
2007-12-16 20:18:33 +00:00
|
|
|
$this->croak( 'cite_error_stack_invalid_input', serialize( array( $key, $str ) ) );
|
2005-12-21 01:08:25 +00:00
|
|
|
}
|
2007-12-16 18:45:23 +00:00
|
|
|
|
2005-12-21 01:08:25 +00:00
|
|
|
/**
|
2005-12-23 02:34:39 +00:00
|
|
|
* Callback function for <references>
|
|
|
|
*
|
|
|
|
* @param string $str Input
|
|
|
|
* @param array $argv Arguments
|
2005-12-21 01:08:25 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2006-02-21 19:56:09 +00:00
|
|
|
function references( $str, $argv, $parser ) {
|
2008-01-10 20:09:12 +00:00
|
|
|
wfLoadExtensionMessages( 'Cite' );
|
2006-04-16 06:22:42 +00:00
|
|
|
if ( $this->mInCite ) {
|
|
|
|
if ( is_null( $str ) ) {
|
|
|
|
return htmlspecialchars( "<references/>" );
|
|
|
|
} else {
|
|
|
|
return htmlspecialchars( "<references>$str</references>" );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->mInCite = true;
|
|
|
|
$ret = $this->guardedReferences( $str, $argv, $parser );
|
|
|
|
$this->mInCite = false;
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
}
|
2008-03-21 15:25:26 +00:00
|
|
|
|
|
|
|
function guardedReferences( $str, $argv, $parser, $group = CITE_DEFAULT_GROUP ) {
|
|
|
|
global $wgAllowCiteGroups;
|
|
|
|
|
2006-02-21 19:56:09 +00:00
|
|
|
$this->mParser = $parser;
|
2008-03-21 15:25:26 +00:00
|
|
|
|
2005-12-21 01:08:25 +00:00
|
|
|
if ( $str !== null )
|
2007-12-16 20:18:33 +00:00
|
|
|
return $this->error( 'cite_error_references_invalid_input' );
|
2008-03-21 15:25:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
if ( isset( $argv['group'] ) and $wgAllowCiteGroups) {
|
|
|
|
$group = $argv['group'];
|
|
|
|
unset ($argv['group']);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( count( $argv ) )
|
2007-12-16 20:18:33 +00:00
|
|
|
return $this->error( 'cite_error_references_invalid_parameters' );
|
2005-12-21 01:08:25 +00:00
|
|
|
else
|
2008-03-21 15:25:26 +00:00
|
|
|
return $this->referencesFormat($group);
|
2005-12-21 01:08:25 +00:00
|
|
|
}
|
|
|
|
|
2005-12-23 02:34:39 +00:00
|
|
|
/**
|
|
|
|
* Make output to be returned from the references() function
|
|
|
|
*
|
|
|
|
* @return string XHTML ready for output
|
|
|
|
*/
|
2008-03-21 15:25:26 +00:00
|
|
|
function referencesFormat($group) {
|
|
|
|
if (( count( $this->mRefs ) == 0 ) or (count( $this->mRefs[$group] ) == 0 ))
|
2007-05-08 21:20:48 +00:00
|
|
|
return '';
|
2007-12-16 18:45:23 +00:00
|
|
|
|
2008-01-29 01:48:21 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
|
|
|
wfProfileIn( __METHOD__ .'-entries' );
|
2007-05-08 21:20:48 +00:00
|
|
|
$ent = array();
|
2008-03-21 15:25:26 +00:00
|
|
|
foreach ( $this->mRefs[$group] as $k => $v )
|
2005-12-21 01:08:25 +00:00
|
|
|
$ent[] = $this->referencesFormatEntry( $k, $v );
|
2007-12-16 18:45:23 +00:00
|
|
|
|
2005-12-21 01:08:25 +00:00
|
|
|
$prefix = wfMsgForContentNoTrans( 'cite_references_prefix' );
|
|
|
|
$suffix = wfMsgForContentNoTrans( 'cite_references_suffix' );
|
|
|
|
$content = implode( "\n", $ent );
|
2007-12-16 18:45:23 +00:00
|
|
|
|
2008-01-29 01:48:21 +00:00
|
|
|
wfProfileOut( __METHOD__ .'-entries' );
|
|
|
|
wfProfileIn( __METHOD__ .'-parse' );
|
2006-04-02 03:43:47 +00:00
|
|
|
// Live hack: parse() adds two newlines on WM, can't reproduce it locally -ævar
|
2008-01-29 01:48:21 +00:00
|
|
|
$ret = rtrim( $this->parse( $prefix . $content . $suffix ), "\n" );
|
|
|
|
wfProfileOut( __METHOD__ .'-parse' );
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
|
2008-03-21 15:25:26 +00:00
|
|
|
//done, clean up so we can reuse the group
|
|
|
|
unset ($this->mRefs[$group]);
|
|
|
|
unset($this->mGroupCnt[$group]);
|
|
|
|
|
2008-01-29 01:48:21 +00:00
|
|
|
return $ret;
|
2005-12-21 01:08:25 +00:00
|
|
|
}
|
|
|
|
|
2005-12-23 02:34:39 +00:00
|
|
|
/**
|
|
|
|
* Format a single entry for the referencesFormat() function
|
|
|
|
*
|
|
|
|
* @param string $key The key of the reference
|
|
|
|
* @param mixed $val The value of the reference, string for anonymous
|
2007-01-13 22:04:57 +00:00
|
|
|
* references, array for user-suppplied
|
2005-12-23 02:34:39 +00:00
|
|
|
* @return string Wikitext
|
|
|
|
*/
|
2005-12-21 01:08:25 +00:00
|
|
|
function referencesFormatEntry( $key, $val ) {
|
2006-02-23 21:24:57 +00:00
|
|
|
// Anonymous reference
|
2005-12-21 01:08:25 +00:00
|
|
|
if ( ! is_array( $val ) )
|
|
|
|
return
|
|
|
|
wfMsgForContentNoTrans(
|
|
|
|
'cite_references_link_one',
|
2005-12-23 02:34:39 +00:00
|
|
|
$this->referencesKey( $key ),
|
|
|
|
$this->refKey( $key ),
|
2005-12-21 01:08:25 +00:00
|
|
|
$val
|
|
|
|
);
|
2007-09-23 23:38:12 +00:00
|
|
|
else if ($val['text']=='') return
|
|
|
|
wfMsgForContentNoTrans(
|
|
|
|
'cite_references_link_one',
|
|
|
|
$this->referencesKey( $key ),
|
|
|
|
$this->refKey( $key, $val['count'] ),
|
2008-01-04 13:58:40 +00:00
|
|
|
$this->error( 'cite_error_references_no_text', $key )
|
2007-09-23 23:38:12 +00:00
|
|
|
);
|
2008-03-21 15:25:26 +00:00
|
|
|
if ( $val['count'] < 0 )
|
|
|
|
return
|
|
|
|
wfMsgForContentNoTrans(
|
|
|
|
'cite_references_link_one',
|
|
|
|
$this->referencesKey( $val['key'] ),
|
|
|
|
#$this->refKey( $val['key'], $val['count'] ),
|
|
|
|
$this->refKey( $val['key'] ),
|
|
|
|
|
|
|
|
( $val['text'] != '' ? $val['text'] : $this->error( 'cite_error_references_no_text', $key ) )
|
|
|
|
);
|
2006-02-23 21:24:57 +00:00
|
|
|
// Standalone named reference, I want to format this like an
|
|
|
|
// anonymous reference because displaying "1. 1.1 Ref text" is
|
|
|
|
// overkill and users frequently use named references when they
|
|
|
|
// don't need them for convenience
|
|
|
|
else if ( $val['count'] === 0 )
|
|
|
|
return
|
|
|
|
wfMsgForContentNoTrans(
|
|
|
|
'cite_references_link_one',
|
2008-03-21 15:25:26 +00:00
|
|
|
$this->referencesKey( $key ."-" . $val['key'] ),
|
|
|
|
#$this->refKey( $key, $val['count'] ),
|
|
|
|
$this->refKey( $key, $val['key']."-".$val['count'] ),
|
2008-01-04 13:58:40 +00:00
|
|
|
( $val['text'] != '' ? $val['text'] : $this->error( 'cite_error_references_no_text', $key ) )
|
2006-02-23 21:24:57 +00:00
|
|
|
);
|
|
|
|
// Named references with >1 occurrences
|
2005-12-21 01:08:25 +00:00
|
|
|
else {
|
|
|
|
$links = array();
|
2008-03-21 15:25:26 +00:00
|
|
|
//for group handling, we have an extra key here.
|
2005-12-21 01:08:25 +00:00
|
|
|
for ( $i = 0; $i <= $val['count']; ++$i ) {
|
|
|
|
$links[] = wfMsgForContentNoTrans(
|
|
|
|
'cite_references_link_many_format',
|
2008-03-21 15:25:26 +00:00
|
|
|
$this->refKey( $key, $val['key']."-$i" ),
|
2006-05-02 17:02:23 +00:00
|
|
|
$this->referencesFormatEntryNumericBacklinkLabel( $val['number'], $i, $val['count'] ),
|
2005-12-29 21:56:00 +00:00
|
|
|
$this->referencesFormatEntryAlternateBacklinkLabel( $i )
|
2005-12-21 01:08:25 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$list = $this->listToText( $links );
|
|
|
|
|
|
|
|
return
|
|
|
|
wfMsgForContentNoTrans( 'cite_references_link_many',
|
2008-03-21 15:25:26 +00:00
|
|
|
$this->referencesKey( $key ."-" . $val['key'] ),
|
2005-12-21 01:08:25 +00:00
|
|
|
$list,
|
2008-01-04 13:58:40 +00:00
|
|
|
( $val['text'] != '' ? $val['text'] : $this->error( 'cite_error_references_no_text', $key ) )
|
2005-12-21 01:08:25 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-12-29 21:56:00 +00:00
|
|
|
/**
|
|
|
|
* Generate a numeric backlink given a base number and an
|
|
|
|
* offset, e.g. $base = 1, $offset = 2; = 1.2
|
2006-05-02 17:02:23 +00:00
|
|
|
* Since bug #5525, it correctly does 1.9 -> 1.10 as well as 1.099 -> 1.100
|
2005-12-29 21:56:00 +00:00
|
|
|
*
|
2006-01-07 09:24:48 +00:00
|
|
|
* @static
|
|
|
|
*
|
2005-12-29 21:56:00 +00:00
|
|
|
* @param int $base The base
|
|
|
|
* @param int $offset The offset
|
2006-05-02 17:02:23 +00:00
|
|
|
* @param int $max Maximum value expected.
|
2005-12-29 21:56:00 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2006-05-02 17:02:23 +00:00
|
|
|
function referencesFormatEntryNumericBacklinkLabel( $base, $offset, $max ) {
|
2005-12-29 21:56:00 +00:00
|
|
|
global $wgContLang;
|
2006-05-02 17:02:23 +00:00
|
|
|
$scope = strlen( $max );
|
|
|
|
$ret = $wgContLang->formatNum(
|
|
|
|
sprintf("%s.%0{$scope}s", $base, $offset)
|
|
|
|
);
|
|
|
|
return $ret;
|
2005-12-29 21:56:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a custom format backlink given an offset, e.g.
|
|
|
|
* $offset = 2; = c if $this->mBacklinkLabels = array( 'a',
|
|
|
|
* 'b', 'c', ...). Return an error if the offset > the # of
|
|
|
|
* array items
|
|
|
|
*
|
|
|
|
* @param int $offset The offset
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function referencesFormatEntryAlternateBacklinkLabel( $offset ) {
|
2006-07-02 14:19:45 +00:00
|
|
|
if ( !isset( $this->mBacklinkLabels ) ) {
|
|
|
|
$this->genBacklinkLabels();
|
|
|
|
}
|
|
|
|
if ( isset( $this->mBacklinkLabels[$offset] ) ) {
|
2005-12-29 21:56:00 +00:00
|
|
|
return $this->mBacklinkLabels[$offset];
|
2006-07-02 14:19:45 +00:00
|
|
|
} else {
|
2005-12-29 21:56:00 +00:00
|
|
|
// Feed me!
|
2007-12-16 20:18:33 +00:00
|
|
|
return $this->error( 'cite_error_references_no_backlink_label' );
|
2006-07-02 14:19:45 +00:00
|
|
|
}
|
2005-12-29 21:56:00 +00:00
|
|
|
}
|
|
|
|
|
2005-12-23 02:34:39 +00:00
|
|
|
/**
|
|
|
|
* Return an id for use in wikitext output based on a key and
|
2008-03-21 15:25:26 +00:00
|
|
|
* optionally the number of it, used in <references>, not <ref>
|
2005-12-23 02:34:39 +00:00
|
|
|
* (since otherwise it would link to itself)
|
|
|
|
*
|
2006-01-07 09:24:48 +00:00
|
|
|
* @static
|
|
|
|
*
|
2005-12-23 02:34:39 +00:00
|
|
|
* @param string $key The key
|
|
|
|
* @param int $num The number of the key
|
|
|
|
* @return string A key for use in wikitext
|
|
|
|
*/
|
|
|
|
function refKey( $key, $num = null ) {
|
|
|
|
$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 );
|
2007-12-16 18:45:23 +00:00
|
|
|
|
2005-12-23 02:34:39 +00:00
|
|
|
return $prefix . $key . $suffix;
|
|
|
|
}
|
2005-12-21 01:08:25 +00:00
|
|
|
|
2005-12-23 02:34:39 +00:00
|
|
|
/**
|
|
|
|
* Return an id for use in wikitext output based on a key and
|
2008-03-21 15:25:26 +00:00
|
|
|
* optionally the number of it, used in <ref>, not <references>
|
2005-12-23 02:34:39 +00:00
|
|
|
* (since otherwise it would link to itself)
|
|
|
|
*
|
2006-01-07 09:24:48 +00:00
|
|
|
* @static
|
|
|
|
*
|
2005-12-23 02:34:39 +00:00
|
|
|
* @param string $key The key
|
|
|
|
* @param int $num The number of the key
|
|
|
|
* @return string A key for use in wikitext
|
|
|
|
*/
|
|
|
|
function referencesKey( $key, $num = null ) {
|
|
|
|
$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 );
|
2007-12-16 18:45:23 +00:00
|
|
|
|
2005-12-21 01:08:25 +00:00
|
|
|
return $prefix . $key . $suffix;
|
|
|
|
}
|
|
|
|
|
2005-12-23 02:34:39 +00:00
|
|
|
/**
|
|
|
|
* Generate a link (<sup ...) for the <ref> element from a key
|
|
|
|
* and return XHTML ready for output
|
|
|
|
*
|
|
|
|
* @param string $key The key for the link
|
2008-03-21 15:25:26 +00:00
|
|
|
* @param int $count The index of the key, used for distinguishing
|
|
|
|
* multiple occurances of the same key
|
2005-12-29 21:56:00 +00:00
|
|
|
* @param int $label The label to use for the link, I want to
|
|
|
|
* use the same label for all occourances of
|
|
|
|
* the same named reference.
|
2005-12-23 02:34:39 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2008-03-21 15:25:26 +00:00
|
|
|
function linkRef( $group, $key, $count = null, $label = null, $subkey = '' ) {
|
2005-12-21 01:08:25 +00:00
|
|
|
global $wgContLang;
|
|
|
|
return
|
|
|
|
$this->parse(
|
|
|
|
wfMsgForContentNoTrans(
|
|
|
|
'cite_reference_link',
|
2005-12-29 21:56:00 +00:00
|
|
|
$this->refKey( $key, $count ),
|
2008-03-21 15:25:26 +00:00
|
|
|
$this->referencesKey( $key . $subkey ),
|
|
|
|
(($group == CITE_DEFAULT_GROUP)?'':"$group ").$wgContLang->formatNum( is_null( $label ) ? ++$this->mGroupCnt[$group] : $label )
|
2005-12-21 01:08:25 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This does approximately the same thing as
|
2008-03-01 02:12:15 +00:00
|
|
|
* Language::listToText() but due to this being used for a
|
2005-12-21 01:08:25 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2006-01-07 09:24:48 +00:00
|
|
|
* @static
|
|
|
|
*
|
2005-12-23 02:34:39 +00:00
|
|
|
* @param array $arr The array to format
|
2005-12-21 01:08:25 +00:00
|
|
|
* @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 )
|
2005-12-23 02:34:39 +00:00
|
|
|
// Enforce always returning a string
|
2005-12-21 01:08:25 +00:00
|
|
|
return (string)$arr[0];
|
|
|
|
else {
|
|
|
|
$t = array_slice( $arr, 0, $cnt - 1 );
|
2005-12-23 00:43:23 +00:00
|
|
|
return implode( $sep, $t ) . $and . $arr[$cnt - 1];
|
2005-12-21 01:08:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-12-23 02:34:39 +00:00
|
|
|
/**
|
|
|
|
* Parse a given fragment and fix up Tidy's trail of blood on
|
|
|
|
* it...
|
|
|
|
*
|
|
|
|
* @param string $in The text to parse
|
|
|
|
* @return string The parsed text
|
|
|
|
*/
|
2005-12-21 01:08:25 +00:00
|
|
|
function parse( $in ) {
|
2006-08-06 15:46:00 +00:00
|
|
|
if ( method_exists( $this->mParser, 'recursiveTagParse' ) ) {
|
|
|
|
// New fast method
|
|
|
|
return $this->mParser->recursiveTagParse( $in );
|
|
|
|
} else {
|
|
|
|
// Old method
|
|
|
|
$ret = $this->mParser->parse(
|
|
|
|
$in,
|
|
|
|
$this->mParser->mTitle,
|
|
|
|
$this->mParser->mOptions,
|
|
|
|
// Avoid whitespace buildup
|
|
|
|
false,
|
|
|
|
// Important, otherwise $this->clearState()
|
|
|
|
// would get run every time <ref> or
|
|
|
|
// <references> is called, fucking the whole
|
|
|
|
// thing up.
|
|
|
|
false
|
|
|
|
);
|
|
|
|
$text = $ret->getText();
|
2007-12-16 18:45:23 +00:00
|
|
|
|
2006-08-06 15:46:00 +00:00
|
|
|
return $this->fixTidy( $text );
|
|
|
|
}
|
2005-12-23 00:43:23 +00:00
|
|
|
}
|
|
|
|
|
2005-12-23 02:34:39 +00:00
|
|
|
/**
|
|
|
|
* Tidy treats all input as a block, it will e.g. wrap most
|
|
|
|
* input in <p> if it isn't already, fix that and return the fixed text
|
|
|
|
*
|
2006-01-07 09:24:48 +00:00
|
|
|
* @static
|
|
|
|
*
|
2005-12-23 02:34:39 +00:00
|
|
|
* @param string $text The text to fix
|
|
|
|
* @return string The fixed text
|
|
|
|
*/
|
2005-12-23 00:43:23 +00:00
|
|
|
function fixTidy( $text ) {
|
|
|
|
global $wgUseTidy;
|
|
|
|
|
|
|
|
if ( ! $wgUseTidy )
|
|
|
|
return $text;
|
|
|
|
else {
|
2006-01-07 09:24:48 +00:00
|
|
|
$text = preg_replace( '~^<p>\s*~', '', $text );
|
|
|
|
$text = preg_replace( '~\s*</p>\s*~', '', $text );
|
|
|
|
$text = preg_replace( '~\n$~', '', $text );
|
2007-12-16 18:45:23 +00:00
|
|
|
|
2005-12-23 00:43:23 +00:00
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-12-29 21:56:00 +00:00
|
|
|
/**
|
|
|
|
* Generate the labels to pass to the
|
|
|
|
* 'cite_references_link_many_format' message, the format is an
|
|
|
|
* arbitary number of tokens seperated by [\t\n ]
|
|
|
|
*/
|
|
|
|
function genBacklinkLabels() {
|
2006-07-02 14:19:45 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2005-12-29 21:56:00 +00:00
|
|
|
$text = wfMsgForContentNoTrans( 'cite_references_link_many_format_backlink_labels' );
|
|
|
|
$this->mBacklinkLabels = preg_split( '#[\n\t ]#', $text );
|
2006-07-02 14:19:45 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2005-12-29 21:56:00 +00:00
|
|
|
}
|
|
|
|
|
2005-12-23 06:06:57 +00:00
|
|
|
/**
|
|
|
|
* Gets run when Parser::clearState() gets run, since we don't
|
|
|
|
* want the counts to transcend pages and other instances
|
|
|
|
*/
|
|
|
|
function clearState() {
|
2008-03-21 15:25:26 +00:00
|
|
|
$this->mGroupCnt = array();
|
|
|
|
$this->mOutCnt = -1;
|
|
|
|
$this->mInCnt = 0;
|
2005-12-23 06:06:57 +00:00
|
|
|
$this->mRefs = array();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-12-21 01:08:25 +00:00
|
|
|
/**
|
|
|
|
* Initialize the parser hooks
|
|
|
|
*/
|
|
|
|
function setHooks() {
|
2005-12-23 06:06:57 +00:00
|
|
|
global $wgParser, $wgHooks;
|
2007-12-16 18:45:23 +00:00
|
|
|
|
2005-12-21 01:08:25 +00:00
|
|
|
$wgParser->setHook( 'ref' , array( &$this, 'ref' ) );
|
|
|
|
$wgParser->setHook( 'references' , array( &$this, 'references' ) );
|
2005-12-23 06:06:57 +00:00
|
|
|
|
|
|
|
$wgHooks['ParserClearState'][] = array( &$this, 'clearState' );
|
2005-12-21 01:08:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-12-23 02:34:39 +00:00
|
|
|
* Return an error message based on an error ID
|
|
|
|
*
|
2008-01-04 13:58:40 +00:00
|
|
|
* @param string $key Message name for the error
|
|
|
|
* @param string $param Parameter to pass to the message
|
2005-12-23 02:34:39 +00:00
|
|
|
* @return string XHTML ready for output
|
2005-12-21 01:08:25 +00:00
|
|
|
*/
|
2008-01-04 13:58:40 +00:00
|
|
|
function error( $key, $param=null ) {
|
|
|
|
# We rely on the fact that PHP is okay with passing unused argu-
|
|
|
|
# ments to functions. If $1 is not used in the message, wfMsg will
|
|
|
|
# just ignore the extra parameter.
|
2007-12-16 20:18:33 +00:00
|
|
|
return
|
|
|
|
$this->parse(
|
|
|
|
'<strong class="error">' .
|
2008-01-04 13:58:40 +00:00
|
|
|
wfMsg( 'cite_error', wfMsg( $key, $param ) ) .
|
2007-12-16 20:18:33 +00:00
|
|
|
'</strong>'
|
|
|
|
);
|
2005-12-21 01:08:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**#@-*/
|
|
|
|
}
|
|
|
|
|
2006-01-07 09:24:48 +00:00
|
|
|
new Cite;
|
2005-12-21 01:08:25 +00:00
|
|
|
}
|
2007-12-16 18:45:23 +00:00
|
|
|
|
|
|
|
/**#@-*/
|
|
|
|
|