2008-06-06 20:38:04 +00:00
|
|
|
<?php
|
|
|
|
|
2016-09-21 11:01:25 +00:00
|
|
|
/**
|
2008-06-06 20:38:04 +00:00
|
|
|
* A parser extension that adds two tags, <ref> and <references> for adding
|
|
|
|
* citations to pages
|
|
|
|
*
|
2010-06-06 15:12:22 +00:00
|
|
|
* @ingroup Extensions
|
2008-06-06 20:38:04 +00:00
|
|
|
*
|
2016-09-21 11:01:25 +00:00
|
|
|
* Documentation
|
|
|
|
* @link http://www.mediawiki.org/wiki/Extension:Cite/Cite.php
|
|
|
|
*
|
|
|
|
* <cite> definition in HTML
|
|
|
|
* @link http://www.w3.org/TR/html4/struct/text.html#edef-CITE
|
|
|
|
*
|
|
|
|
* <cite> definition in XHTML 2.0
|
|
|
|
* @link http://www.w3.org/TR/2005/WD-xhtml2-20050527/mod-text.html#edef_text_cite
|
2008-06-06 20:38:04 +00:00
|
|
|
*
|
2016-09-29 14:19:16 +00:00
|
|
|
* @bug https://phabricator.wikimedia.org/T6579
|
2008-06-06 20:38:04 +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
|
|
|
|
*/
|
|
|
|
|
2017-12-25 13:17:20 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2017-12-22 20:28:29 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
2017-12-29 19:58:09 +00:00
|
|
|
use Wikimedia\Rdbms\Database;
|
2017-12-22 20:28:29 +00:00
|
|
|
|
2015-04-30 18:41:06 +00:00
|
|
|
/**
|
|
|
|
* WARNING: MediaWiki core hardcodes this class name to check if the
|
|
|
|
* Cite extension is installed. See T89151.
|
|
|
|
*/
|
2008-08-08 19:11:31 +00:00
|
|
|
class Cite {
|
2015-04-30 18:34:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @todo document
|
|
|
|
*/
|
|
|
|
const DEFAULT_GROUP = '';
|
|
|
|
|
2016-01-25 16:52:46 +00:00
|
|
|
/**
|
|
|
|
* Maximum storage capacity for pp_value field of page_props table
|
|
|
|
* @todo Find a way to retrieve this information from the DBAL
|
|
|
|
*/
|
|
|
|
const MAX_STORAGE_LENGTH = 65535; // Size of MySQL 'blob' field
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Key used for storage in parser output's ExtensionData and ObjectCache
|
|
|
|
*/
|
|
|
|
const EXT_DATA_KEY = 'Cite:References';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Version number in case we change the data structure in the future
|
|
|
|
*/
|
|
|
|
const DATA_VERSION_NUMBER = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache duration set when parsing a page with references
|
|
|
|
*/
|
|
|
|
const CACHE_DURATION_ONPARSE = 3600; // 1 hour
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache duration set when fetching references from db
|
|
|
|
*/
|
|
|
|
const CACHE_DURATION_ONFETCH = 18000; // 5 hours
|
|
|
|
|
2008-06-06 20:38:04 +00:00
|
|
|
/**
|
|
|
|
* Datastructure representing <ref> input, in the format of:
|
|
|
|
* <code>
|
2017-01-07 09:08:35 +00:00
|
|
|
* [
|
|
|
|
* 'user supplied' => [
|
2008-06-06 20:38:04 +00:00
|
|
|
* 'text' => 'user supplied reference & key',
|
|
|
|
* 'count' => 1, // occurs twice
|
|
|
|
* 'number' => 1, // The first reference, we want
|
|
|
|
* // all occourances of it to
|
|
|
|
* // use the same number
|
2017-01-07 09:08:35 +00:00
|
|
|
* ],
|
2008-06-06 20:38:04 +00:00
|
|
|
* 0 => 'Anonymous reference',
|
|
|
|
* 1 => 'Another anonymous reference',
|
2017-01-07 09:08:35 +00:00
|
|
|
* 'some key' => [
|
2008-06-06 20:38:04 +00:00
|
|
|
* 'text' => 'this one occurs once'
|
|
|
|
* 'count' => 0,
|
|
|
|
* 'number' => 4
|
2017-01-07 09:08:35 +00:00
|
|
|
* ],
|
2008-06-06 20:38:04 +00:00
|
|
|
* 3 => 'more stuff'
|
2017-01-07 09:08:35 +00:00
|
|
|
* ];
|
2008-06-06 20:38:04 +00:00
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* This works because:
|
2011-02-26 22:39:53 +00:00
|
|
|
* * PHP's datastructures are guaranteed to be returned in the
|
2008-06-06 20:38:04 +00:00
|
|
|
* 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
|
|
|
|
*
|
2016-09-21 11:01:25 +00:00
|
|
|
* @var array[]
|
|
|
|
*/
|
|
|
|
private $mRefs = [];
|
2011-02-22 00:07:21 +00:00
|
|
|
|
2008-06-06 20:38:04 +00:00
|
|
|
/**
|
|
|
|
* Count for user displayed output (ref[1], ref[2], ...)
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
2016-09-21 11:01:25 +00:00
|
|
|
private $mOutCnt = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int[]
|
|
|
|
*/
|
|
|
|
private $mGroupCnt = [];
|
2008-06-06 20:38:04 +00:00
|
|
|
|
2011-03-04 11:45:22 +00:00
|
|
|
/**
|
|
|
|
* Counter to track the total number of (useful) calls to either the
|
|
|
|
* ref or references tag hook
|
2016-09-21 11:01:25 +00:00
|
|
|
*
|
|
|
|
* @var int
|
2011-03-04 11:45:22 +00:00
|
|
|
*/
|
2016-09-21 11:01:25 +00:00
|
|
|
private $mCallCnt = 0;
|
2008-06-06 20:38:04 +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
|
|
|
|
*
|
2016-09-21 11:01:25 +00:00
|
|
|
* @var string[]
|
2008-06-06 20:38:04 +00:00
|
|
|
*/
|
2016-09-21 11:01:25 +00:00
|
|
|
private $mBacklinkLabels;
|
2010-05-22 14:28:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The links to use per group, in order.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-09-21 11:01:25 +00:00
|
|
|
private $mLinkLabels = [];
|
2011-02-22 00:07:21 +00:00
|
|
|
|
2008-06-06 20:38:04 +00:00
|
|
|
/**
|
2011-05-28 20:44:24 +00:00
|
|
|
* @var Parser
|
2008-06-06 20:38:04 +00:00
|
|
|
*/
|
2016-09-21 11:01:25 +00:00
|
|
|
private $mParser;
|
2011-02-22 00:07:21 +00:00
|
|
|
|
2015-06-08 14:05:06 +00:00
|
|
|
/**
|
|
|
|
* True when the ParserAfterParse hook has been called.
|
|
|
|
* Used to avoid doing anything in ParserBeforeTidy.
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2016-09-21 11:01:25 +00:00
|
|
|
private $mHaveAfterParse = false;
|
2015-06-08 14:05:06 +00:00
|
|
|
|
2008-06-06 20:38:04 +00:00
|
|
|
/**
|
2009-07-26 22:15:13 +00:00
|
|
|
* True when a <ref> tag is being processed.
|
2008-06-06 20:38:04 +00:00
|
|
|
* Used to avoid infinite recursion
|
2011-02-22 00:07:21 +00:00
|
|
|
*
|
2008-06-06 20:38:04 +00:00
|
|
|
* @var boolean
|
|
|
|
*/
|
2014-08-17 21:04:15 +00:00
|
|
|
public $mInCite = false;
|
2009-07-26 22:15:13 +00:00
|
|
|
|
|
|
|
/**
|
2015-06-08 14:05:06 +00:00
|
|
|
* True when a <references> tag is being processed.
|
|
|
|
* Used to detect the use of <references> to define refs
|
2011-02-22 00:07:21 +00:00
|
|
|
*
|
2015-06-08 14:05:06 +00:00
|
|
|
* @var boolean
|
2009-07-26 22:15:13 +00:00
|
|
|
*/
|
2015-06-08 14:05:06 +00:00
|
|
|
public $mInReferences = false;
|
2009-07-26 22:15:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Error stack used when defining refs in <references>
|
2011-02-22 00:07:21 +00:00
|
|
|
*
|
2016-09-21 11:01:25 +00:00
|
|
|
* @var string[]
|
2009-07-26 22:15:13 +00:00
|
|
|
*/
|
2016-09-21 11:01:25 +00:00
|
|
|
private $mReferencesErrors = [];
|
2009-07-26 22:15:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Group used when in <references> block
|
2011-02-22 00:07:21 +00:00
|
|
|
*
|
2009-07-26 22:15:13 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
2016-09-21 11:01:25 +00:00
|
|
|
private $mReferencesGroup = '';
|
2009-07-26 22:15:13 +00:00
|
|
|
|
2015-06-08 14:05:06 +00:00
|
|
|
/**
|
|
|
|
* <ref> call stack
|
|
|
|
* Used to cleanup out of sequence ref calls created by #tag
|
|
|
|
* See description of function rollbackRef.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-09-21 11:01:25 +00:00
|
|
|
private $mRefCallStack = [];
|
2015-06-08 14:05:06 +00:00
|
|
|
|
2016-01-25 16:52:46 +00:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $mBumpRefData = false;
|
|
|
|
|
2008-06-06 20:38:04 +00:00
|
|
|
/**
|
2011-05-31 17:49:22 +00:00
|
|
|
* Did we install us into $wgHooks yet?
|
|
|
|
* @var Boolean
|
2008-06-06 20:38:04 +00:00
|
|
|
*/
|
2016-09-21 11:01:25 +00:00
|
|
|
private static $hooksInstalled = false;
|
2008-06-06 20:38:04 +00:00
|
|
|
|
|
|
|
/**
|
2015-06-08 14:05:06 +00:00
|
|
|
* Callback function for <ref>
|
2008-06-06 20:38:04 +00:00
|
|
|
*
|
2015-10-26 14:44:02 +00:00
|
|
|
* @param string|null $str Raw content of the <ref> tag.
|
|
|
|
* @param string[] $argv Arguments
|
|
|
|
* @param Parser $parser
|
|
|
|
* @param PPFrame $frame
|
2011-05-28 20:44:24 +00:00
|
|
|
*
|
2008-06-06 20:38:04 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-09-20 13:59:58 +00:00
|
|
|
public function ref( $str, array $argv, Parser $parser, PPFrame $frame ) {
|
2015-06-08 14:05:06 +00:00
|
|
|
if ( $this->mInCite ) {
|
2008-06-06 20:38:04 +00:00
|
|
|
return htmlspecialchars( "<ref>$str</ref>" );
|
|
|
|
}
|
2014-10-01 14:25:58 +00:00
|
|
|
|
|
|
|
$this->mCallCnt++;
|
|
|
|
$this->mInCite = true;
|
|
|
|
|
|
|
|
$ret = $this->guardedRef( $str, $argv, $parser );
|
|
|
|
|
|
|
|
$this->mInCite = false;
|
|
|
|
|
|
|
|
$parserOutput = $parser->getOutput();
|
2015-06-05 20:56:58 +00:00
|
|
|
$parserOutput->addModules( 'ext.cite.a11y' );
|
|
|
|
$parserOutput->addModuleStyles( 'ext.cite.styles' );
|
2014-10-01 14:25:58 +00:00
|
|
|
|
2016-05-09 23:36:49 +00:00
|
|
|
if ( is_callable( [ $frame, 'setVolatile' ] ) ) {
|
2015-06-08 14:05:06 +00:00
|
|
|
$frame->setVolatile();
|
|
|
|
}
|
2009-02-11 06:34:39 +00:00
|
|
|
|
2016-01-25 16:52:46 +00:00
|
|
|
// new <ref> tag, we may need to bump the ref data counter
|
|
|
|
// to avoid overwriting a previous group
|
|
|
|
$this->mBumpRefData = true;
|
|
|
|
|
2015-06-08 14:05:06 +00:00
|
|
|
return $ret;
|
2014-12-19 18:38:37 +00:00
|
|
|
}
|
|
|
|
|
2011-05-28 20:44:24 +00:00
|
|
|
/**
|
2015-10-26 14:44:02 +00:00
|
|
|
* @param string|null $str Raw content of the <ref> tag.
|
|
|
|
* @param string[] $argv Arguments
|
|
|
|
* @param Parser $parser
|
|
|
|
* @param string $default_group
|
|
|
|
*
|
|
|
|
* @throws Exception
|
2011-05-28 20:44:24 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-09-21 11:01:25 +00:00
|
|
|
private function guardedRef(
|
2016-09-20 13:59:58 +00:00
|
|
|
$str,
|
|
|
|
array $argv,
|
|
|
|
Parser $parser,
|
|
|
|
$default_group = self::DEFAULT_GROUP
|
|
|
|
) {
|
2008-06-06 20:38:04 +00:00
|
|
|
$this->mParser = $parser;
|
2011-02-22 00:07:21 +00:00
|
|
|
|
2008-06-06 20:38:04 +00:00
|
|
|
# The key here is the "name" attribute.
|
2010-08-16 12:05:57 +00:00
|
|
|
list( $key, $group, $follow ) = $this->refArg( $argv );
|
2011-02-22 00:07:21 +00:00
|
|
|
|
2009-07-26 22:15:13 +00:00
|
|
|
# Split these into groups.
|
2010-04-17 21:07:37 +00:00
|
|
|
if ( $group === null ) {
|
2015-06-08 14:05:06 +00:00
|
|
|
if ( $this->mInReferences ) {
|
2009-07-26 22:15:13 +00:00
|
|
|
$group = $this->mReferencesGroup;
|
|
|
|
} else {
|
|
|
|
$group = $default_group;
|
|
|
|
}
|
|
|
|
}
|
2011-02-22 00:07:21 +00:00
|
|
|
|
2015-12-16 13:03:54 +00:00
|
|
|
/*
|
|
|
|
* This section deals with constructions of the form
|
|
|
|
*
|
|
|
|
* <references>
|
|
|
|
* <ref name="foo"> BAR </ref>
|
|
|
|
* </references>
|
|
|
|
*/
|
2015-06-08 14:05:06 +00:00
|
|
|
if ( $this->mInReferences ) {
|
2016-02-24 23:54:17 +00:00
|
|
|
$isSectionPreview = $parser->getOptions()->getIsSectionPreview();
|
2010-04-17 21:07:37 +00:00
|
|
|
if ( $group != $this->mReferencesGroup ) {
|
2009-07-26 22:15:13 +00:00
|
|
|
# <ref> and <references> have conflicting group attributes.
|
2010-04-17 21:07:37 +00:00
|
|
|
$this->mReferencesErrors[] =
|
2017-09-18 19:17:06 +00:00
|
|
|
$this->error(
|
|
|
|
'cite_error_references_group_mismatch',
|
|
|
|
Sanitizer::safeEncodeAttribute( $group )
|
|
|
|
);
|
2010-04-17 21:07:37 +00:00
|
|
|
} elseif ( $str !== '' ) {
|
2016-02-24 23:54:17 +00:00
|
|
|
if ( !$isSectionPreview && !isset( $this->mRefs[$group] ) ) {
|
2009-07-26 22:15:13 +00:00
|
|
|
# Called with group attribute not defined in text.
|
2010-04-17 21:07:37 +00:00
|
|
|
$this->mReferencesErrors[] =
|
2017-09-18 19:17:06 +00:00
|
|
|
$this->error(
|
|
|
|
'cite_error_references_missing_group',
|
|
|
|
Sanitizer::safeEncodeAttribute( $group )
|
|
|
|
);
|
2010-04-17 21:07:37 +00:00
|
|
|
} elseif ( $key === null || $key === '' ) {
|
2009-07-26 22:15:13 +00:00
|
|
|
# <ref> calls inside <references> must be named
|
2010-04-17 21:07:37 +00:00
|
|
|
$this->mReferencesErrors[] =
|
2009-07-26 22:15:13 +00:00
|
|
|
$this->error( 'cite_error_references_no_key' );
|
2016-02-24 23:54:17 +00:00
|
|
|
} elseif ( !$isSectionPreview && !isset( $this->mRefs[$group][$key] ) ) {
|
2009-07-26 22:15:13 +00:00
|
|
|
# Called with name attribute not defined in text.
|
2010-04-17 21:07:37 +00:00
|
|
|
$this->mReferencesErrors[] =
|
2017-09-18 19:17:06 +00:00
|
|
|
$this->error( 'cite_error_references_missing_key', Sanitizer::safeEncodeAttribute( $key ) );
|
2009-07-26 22:15:13 +00:00
|
|
|
} else {
|
2015-12-16 13:03:54 +00:00
|
|
|
if (
|
|
|
|
isset( $this->mRefs[$group][$key]['text'] ) &&
|
|
|
|
$str !== $this->mRefs[$group][$key]['text']
|
|
|
|
) {
|
2015-10-28 14:15:48 +00:00
|
|
|
// two refs with same key and different content
|
|
|
|
// add error message to the original ref
|
2015-12-16 13:03:54 +00:00
|
|
|
$this->mRefs[$group][$key]['text'] .= ' ' . $this->error(
|
|
|
|
'cite_error_references_duplicate_key', $key, 'noparse'
|
|
|
|
);
|
2015-10-28 14:15:48 +00:00
|
|
|
} else {
|
|
|
|
# Assign the text to corresponding ref
|
|
|
|
$this->mRefs[$group][$key]['text'] = $str;
|
|
|
|
}
|
2009-07-26 22:15:13 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
# <ref> called in <references> has no content.
|
2010-04-17 21:07:37 +00:00
|
|
|
$this->mReferencesErrors[] =
|
2017-09-18 19:17:06 +00:00
|
|
|
$this->error( 'cite_error_empty_references_define', Sanitizer::safeEncodeAttribute( $key ) );
|
2009-07-26 22:15:13 +00:00
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2010-04-17 21:07:37 +00:00
|
|
|
if ( $str === '' ) {
|
2008-10-04 22:26:45 +00:00
|
|
|
# <ref ...></ref>. This construct is invalid if
|
|
|
|
# it's a contentful ref, but OK if it's a named duplicate and should
|
|
|
|
# be equivalent <ref ... />, for compatability with #tag.
|
2015-07-07 12:26:35 +00:00
|
|
|
if ( is_string( $key ) && $key !== '' ) {
|
|
|
|
$str = null;
|
|
|
|
} else {
|
2015-06-08 14:05:06 +00:00
|
|
|
$this->mRefCallStack[] = false;
|
2015-07-05 12:32:44 +00:00
|
|
|
|
2008-10-01 19:56:40 +00:00
|
|
|
return $this->error( 'cite_error_ref_no_input' );
|
2009-07-26 22:15:13 +00:00
|
|
|
}
|
2008-06-06 20:38:04 +00:00
|
|
|
}
|
2011-02-22 00:07:21 +00:00
|
|
|
|
2010-04-17 21:07:37 +00:00
|
|
|
if ( $key === false ) {
|
2008-06-06 20:38:04 +00:00
|
|
|
# TODO: Comment this case; what does this condition mean?
|
2015-06-08 14:05:06 +00:00
|
|
|
$this->mRefCallStack[] = false;
|
2008-06-06 20:38:04 +00:00
|
|
|
return $this->error( 'cite_error_ref_too_many_keys' );
|
|
|
|
}
|
|
|
|
|
2011-04-27 11:11:54 +00:00
|
|
|
if ( $str === null && $key === null ) {
|
2008-06-06 20:38:04 +00:00
|
|
|
# Something like <ref />; this makes no sense.
|
2015-06-08 14:05:06 +00:00
|
|
|
$this->mRefCallStack[] = false;
|
2008-06-06 20:38:04 +00:00
|
|
|
return $this->error( 'cite_error_ref_no_key' );
|
|
|
|
}
|
2011-02-22 00:07:21 +00:00
|
|
|
|
2017-09-30 23:15:21 +00:00
|
|
|
if ( is_string( $key ) && preg_match( '/^[0-9]+$/', $key ) ||
|
|
|
|
is_string( $follow ) && preg_match( '/^[0-9]+$/', $follow )
|
|
|
|
) {
|
2008-06-06 20:38:04 +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).
|
2009-07-26 22:15:13 +00:00
|
|
|
|
2015-06-08 14:05:06 +00:00
|
|
|
$this->mRefCallStack[] = false;
|
2008-06-06 20:38:04 +00:00
|
|
|
return $this->error( 'cite_error_ref_numeric_key' );
|
|
|
|
}
|
|
|
|
|
2010-04-17 21:07:37 +00:00
|
|
|
if ( preg_match(
|
2008-09-18 17:21:13 +00:00
|
|
|
'/<ref\b[^<]*?>/',
|
|
|
|
preg_replace( '#<([^ ]+?).*?>.*?</\\1 *>|<!--.*?-->#', '', $str )
|
|
|
|
) ) {
|
2015-12-16 13:03:54 +00:00
|
|
|
# (bug T8199) This most likely implies that someone left off the
|
2008-09-18 17:16:10 +00:00
|
|
|
# closing </ref> tag, which will cause the entire article to be
|
|
|
|
# eaten up until the next <ref>. So we bail out early instead.
|
|
|
|
# The fancy regex above first tries chopping out anything that
|
|
|
|
# looks like a comment or SGML tag, which is a crude way to avoid
|
|
|
|
# false alarms for <nowiki>, <pre>, etc.
|
2015-12-16 13:03:54 +00:00
|
|
|
|
2008-09-18 17:16:10 +00:00
|
|
|
# Possible improvement: print the warning, followed by the contents
|
|
|
|
# of the <ref> tag. This way no part of the article will be eaten
|
|
|
|
# even temporarily.
|
2009-07-26 22:15:13 +00:00
|
|
|
|
2015-06-08 14:05:06 +00:00
|
|
|
$this->mRefCallStack[] = false;
|
2008-09-18 17:16:10 +00:00
|
|
|
return $this->error( 'cite_error_included_ref' );
|
|
|
|
}
|
|
|
|
|
2011-04-27 11:11:54 +00:00
|
|
|
if ( is_string( $key ) || is_string( $str ) ) {
|
2008-06-06 20:38:04 +00:00
|
|
|
# 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.
|
2009-07-26 22:15:13 +00:00
|
|
|
|
2010-08-16 12:05:57 +00:00
|
|
|
return $this->stack( $str, $key, $group, $follow, $argv );
|
2008-06-06 20:38:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Not clear how we could get here, but something is probably
|
|
|
|
# wrong with the types. Let's fail fast.
|
2016-05-09 23:36:49 +00:00
|
|
|
throw new Exception( 'Invalid $str and/or $key: ' . serialize( [ $str, $key ] ) );
|
2008-06-06 20:38:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the arguments to the <ref> tag
|
2011-02-22 00:07:21 +00:00
|
|
|
*
|
2010-08-16 12:05:57 +00:00
|
|
|
* "name" : Key of the reference.
|
|
|
|
* "group" : Group to which it belongs. Needs to be passed to <references /> too.
|
|
|
|
* "follow" : If the current reference is the continuation of another, key of that reference.
|
2008-06-06 20:38:04 +00:00
|
|
|
*
|
2015-10-26 14:44:02 +00:00
|
|
|
* @param string[] $argv The argument vector
|
2008-06-06 20:38:04 +00:00
|
|
|
* @return mixed false on invalid input, a string on valid
|
|
|
|
* input and null on no input
|
|
|
|
*/
|
2016-09-21 11:01:25 +00:00
|
|
|
private function refArg( array $argv ) {
|
2008-06-06 20:38:04 +00:00
|
|
|
$cnt = count( $argv );
|
|
|
|
$group = null;
|
|
|
|
$key = null;
|
2010-08-16 12:05:57 +00:00
|
|
|
$follow = null;
|
2008-06-06 20:38:04 +00:00
|
|
|
|
2011-02-22 00:07:21 +00:00
|
|
|
if ( $cnt > 2 ) {
|
2010-08-16 12:05:57 +00:00
|
|
|
// There should only be one key or follow parameter, and one group parameter
|
2011-02-22 00:07:21 +00:00
|
|
|
// FIXME : this looks inconsistent, it should probably return a tuple
|
2008-06-06 20:38:04 +00:00
|
|
|
return false;
|
2011-02-22 00:07:21 +00:00
|
|
|
} elseif ( $cnt >= 1 ) {
|
2010-08-16 12:05:57 +00:00
|
|
|
if ( isset( $argv['name'] ) && isset( $argv['follow'] ) ) {
|
2016-05-09 23:36:49 +00:00
|
|
|
return [ false, false, false ];
|
2010-08-16 12:05:57 +00:00
|
|
|
}
|
2008-06-06 20:38:04 +00:00
|
|
|
if ( isset( $argv['name'] ) ) {
|
|
|
|
// Key given.
|
2017-11-10 01:21:54 +00:00
|
|
|
$key = trim( $argv['name'] );
|
2010-04-17 21:07:37 +00:00
|
|
|
unset( $argv['name'] );
|
2008-06-06 20:38:04 +00:00
|
|
|
--$cnt;
|
|
|
|
}
|
2010-08-16 12:05:57 +00:00
|
|
|
if ( isset( $argv['follow'] ) ) {
|
|
|
|
// Follow given.
|
2017-11-10 01:21:54 +00:00
|
|
|
$follow = trim( $argv['follow'] );
|
2010-08-16 12:05:57 +00:00
|
|
|
unset( $argv['follow'] );
|
|
|
|
--$cnt;
|
|
|
|
}
|
2010-04-17 21:07:37 +00:00
|
|
|
if ( isset( $argv['group'] ) ) {
|
2008-06-06 20:38:04 +00:00
|
|
|
// Group given.
|
2008-07-30 21:23:43 +00:00
|
|
|
$group = $argv['group'];
|
2010-04-17 21:07:37 +00:00
|
|
|
unset( $argv['group'] );
|
2008-06-06 20:38:04 +00:00
|
|
|
--$cnt;
|
|
|
|
}
|
|
|
|
|
2015-07-07 12:20:53 +00:00
|
|
|
if ( $cnt === 0 ) {
|
2016-05-09 23:36:49 +00:00
|
|
|
return [ $key, $group, $follow ];
|
2011-02-22 00:07:21 +00:00
|
|
|
} else {
|
2008-06-06 20:38:04 +00:00
|
|
|
// Invalid key
|
2016-05-09 23:36:49 +00:00
|
|
|
return [ false, false, false ];
|
2011-02-22 00:07:21 +00:00
|
|
|
}
|
|
|
|
} else {
|
2008-06-06 20:38:04 +00:00
|
|
|
// No key
|
2016-05-09 23:36:49 +00:00
|
|
|
return [ null, $group, false ];
|
2011-02-22 00:07:21 +00:00
|
|
|
}
|
2008-06-06 20:38:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Populate $this->mRefs based on input and arguments to <ref>
|
|
|
|
*
|
2015-10-26 14:44:02 +00:00
|
|
|
* @param string $str Input from the <ref> tag
|
|
|
|
* @param string|null $key Argument to the <ref> tag as returned by $this->refArg()
|
|
|
|
* @param string $group
|
|
|
|
* @param string|null $follow
|
|
|
|
* @param string[] $call
|
2011-05-28 20:44:24 +00:00
|
|
|
*
|
2015-10-26 14:44:02 +00:00
|
|
|
* @throws Exception
|
2011-02-22 00:07:21 +00:00
|
|
|
* @return string
|
2008-06-06 20:38:04 +00:00
|
|
|
*/
|
2017-12-25 13:17:20 +00:00
|
|
|
private function stack( $str, $key, $group, $follow, array $call ) {
|
2011-02-22 00:07:21 +00:00
|
|
|
if ( !isset( $this->mRefs[$group] ) ) {
|
2016-05-09 23:36:49 +00:00
|
|
|
$this->mRefs[$group] = [];
|
2011-02-22 00:07:21 +00:00
|
|
|
}
|
|
|
|
if ( !isset( $this->mGroupCnt[$group] ) ) {
|
2010-04-17 21:07:37 +00:00
|
|
|
$this->mGroupCnt[$group] = 0;
|
2011-02-22 00:07:21 +00:00
|
|
|
}
|
2010-08-16 12:05:57 +00:00
|
|
|
if ( $follow != null ) {
|
|
|
|
if ( isset( $this->mRefs[$group][$follow] ) && is_array( $this->mRefs[$group][$follow] ) ) {
|
|
|
|
// add text to the note that is being followed
|
2015-10-07 15:32:28 +00:00
|
|
|
$this->mRefs[$group][$follow]['text'] .= ' ' . $str;
|
2010-08-16 12:05:57 +00:00
|
|
|
} else {
|
|
|
|
// insert part of note at the beginning of the group
|
2014-09-27 16:49:28 +00:00
|
|
|
$groupsCount = count( $this->mRefs[$group] );
|
|
|
|
for ( $k = 0; $k < $groupsCount; $k++ ) {
|
2015-01-09 00:05:13 +00:00
|
|
|
if ( !isset( $this->mRefs[$group][$k]['follow'] ) ) {
|
2011-02-22 00:07:21 +00:00
|
|
|
break;
|
|
|
|
}
|
2010-08-16 12:05:57 +00:00
|
|
|
}
|
2016-05-09 23:36:49 +00:00
|
|
|
array_splice( $this->mRefs[$group], $k, 0, [ [
|
2015-10-26 14:44:02 +00:00
|
|
|
'count' => -1,
|
|
|
|
'text' => $str,
|
|
|
|
'key' => ++$this->mOutCnt,
|
|
|
|
'follow' => $follow
|
2016-05-09 23:36:49 +00:00
|
|
|
] ] );
|
2015-06-08 14:05:06 +00:00
|
|
|
array_splice( $this->mRefCallStack, $k, 0,
|
2016-05-09 23:36:49 +00:00
|
|
|
[ [ 'new', $call, $str, $key, $group, $this->mOutCnt ] ] );
|
2010-08-16 12:05:57 +00:00
|
|
|
}
|
|
|
|
// return an empty string : this is not a reference
|
|
|
|
return '';
|
|
|
|
}
|
2015-07-18 20:55:32 +00:00
|
|
|
|
2008-06-06 20:38:04 +00:00
|
|
|
if ( $key === null ) {
|
|
|
|
// No key
|
2010-04-17 21:07:37 +00:00
|
|
|
// $this->mRefs[$group][] = $str;
|
2016-05-09 23:36:49 +00:00
|
|
|
$this->mRefs[$group][] = [
|
2015-10-26 14:44:02 +00:00
|
|
|
'count' => -1,
|
|
|
|
'text' => $str,
|
|
|
|
'key' => ++$this->mOutCnt
|
2016-05-09 23:36:49 +00:00
|
|
|
];
|
|
|
|
$this->mRefCallStack[] = [ 'new', $call, $str, $key, $group, $this->mOutCnt ];
|
2008-06-06 20:38:04 +00:00
|
|
|
|
2012-10-17 22:58:55 +00:00
|
|
|
return $this->linkRef( $group, $this->mOutCnt );
|
2015-07-18 20:55:32 +00:00
|
|
|
}
|
|
|
|
if ( !is_string( $key ) ) {
|
2015-01-10 01:47:47 +00:00
|
|
|
throw new Exception( 'Invalid stack key: ' . serialize( $key ) );
|
2011-02-22 00:07:21 +00:00
|
|
|
}
|
2015-07-18 20:55:32 +00:00
|
|
|
|
|
|
|
// Valid key
|
|
|
|
if ( !isset( $this->mRefs[$group][$key] ) || !is_array( $this->mRefs[$group][$key] ) ) {
|
|
|
|
// First occurrence
|
2016-05-09 23:36:49 +00:00
|
|
|
$this->mRefs[$group][$key] = [
|
2015-07-18 20:55:32 +00:00
|
|
|
'text' => $str,
|
|
|
|
'count' => 0,
|
|
|
|
'key' => ++$this->mOutCnt,
|
|
|
|
'number' => ++$this->mGroupCnt[$group]
|
2016-05-09 23:36:49 +00:00
|
|
|
];
|
|
|
|
$this->mRefCallStack[] = [ 'new', $call, $str, $key, $group, $this->mOutCnt ];
|
2015-07-18 20:55:32 +00:00
|
|
|
|
|
|
|
return $this->linkRef(
|
|
|
|
$group,
|
|
|
|
$key,
|
|
|
|
$this->mRefs[$group][$key]['key'] . "-" . $this->mRefs[$group][$key]['count'],
|
|
|
|
$this->mRefs[$group][$key]['number'],
|
|
|
|
"-" . $this->mRefs[$group][$key]['key']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We've been here before
|
|
|
|
if ( $this->mRefs[$group][$key]['text'] === null && $str !== '' ) {
|
|
|
|
// If no text found before, use this text
|
|
|
|
$this->mRefs[$group][$key]['text'] = $str;
|
2016-05-09 23:36:49 +00:00
|
|
|
$this->mRefCallStack[] = [ 'assign', $call, $str, $key, $group,
|
|
|
|
$this->mRefs[$group][$key]['key'] ];
|
2015-07-18 20:55:32 +00:00
|
|
|
} else {
|
2015-07-23 01:57:34 +00:00
|
|
|
if ( $str != null && $str !== '' && $str !== $this->mRefs[$group][$key]['text'] ) {
|
|
|
|
// two refs with same key and different content
|
2015-10-07 15:32:28 +00:00
|
|
|
// add error message to the original ref
|
2015-12-16 13:03:54 +00:00
|
|
|
$this->mRefs[$group][$key]['text'] .= ' ' . $this->error(
|
|
|
|
'cite_error_references_duplicate_key', $key, 'noparse'
|
|
|
|
);
|
2015-07-23 01:57:34 +00:00
|
|
|
}
|
2016-05-09 23:36:49 +00:00
|
|
|
$this->mRefCallStack[] = [ 'increment', $call, $str, $key, $group,
|
|
|
|
$this->mRefs[$group][$key]['key'] ];
|
2015-07-18 20:55:32 +00:00
|
|
|
}
|
|
|
|
return $this->linkRef(
|
|
|
|
$group,
|
|
|
|
$key,
|
|
|
|
$this->mRefs[$group][$key]['key'] . "-" . ++$this->mRefs[$group][$key]['count'],
|
|
|
|
$this->mRefs[$group][$key]['number'],
|
|
|
|
"-" . $this->mRefs[$group][$key]['key']
|
|
|
|
);
|
2008-06-06 20:38:04 +00:00
|
|
|
}
|
2011-02-22 00:07:21 +00:00
|
|
|
|
2009-07-26 22:15:13 +00:00
|
|
|
/**
|
2015-06-08 14:05:06 +00:00
|
|
|
* Partially undoes the effect of calls to stack()
|
|
|
|
*
|
|
|
|
* Called by guardedReferences()
|
|
|
|
*
|
|
|
|
* The option to define <ref> within <references> makes the
|
|
|
|
* behavior of <ref> context dependent. This is normally fine
|
|
|
|
* but certain operations (especially #tag) lead to out-of-order
|
|
|
|
* parser evaluation with the <ref> tags being processed before
|
|
|
|
* their containing <reference> element is read. This leads to
|
|
|
|
* stack corruption that this function works to fix.
|
|
|
|
*
|
|
|
|
* This function is not a total rollback since some internal
|
|
|
|
* counters remain incremented. Doing so prevents accidentally
|
|
|
|
* corrupting certain links.
|
|
|
|
*
|
2015-10-26 14:44:02 +00:00
|
|
|
* @param string $type
|
|
|
|
* @param string|null $key
|
|
|
|
* @param string $group
|
|
|
|
* @param int $index
|
2015-06-08 14:05:06 +00:00
|
|
|
*/
|
2016-09-21 11:01:25 +00:00
|
|
|
private function rollbackRef( $type, $key, $group, $index ) {
|
2015-06-08 14:05:06 +00:00
|
|
|
if ( !isset( $this->mRefs[$group] ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $key === null ) {
|
|
|
|
foreach ( $this->mRefs[$group] as $k => $v ) {
|
|
|
|
if ( $this->mRefs[$group][$k]['key'] === $index ) {
|
|
|
|
$key = $k;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-18 20:55:32 +00:00
|
|
|
// Sanity checks that specified element exists.
|
2015-06-08 14:05:06 +00:00
|
|
|
if ( $key === null ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( !isset( $this->mRefs[$group][$key] ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( $this->mRefs[$group][$key]['key'] != $index ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ( $type ) {
|
|
|
|
case 'new':
|
|
|
|
# Rollback the addition of new elements to the stack.
|
|
|
|
unset( $this->mRefs[$group][$key] );
|
2016-05-09 23:36:49 +00:00
|
|
|
if ( $this->mRefs[$group] === [] ) {
|
2015-06-08 14:05:06 +00:00
|
|
|
unset( $this->mRefs[$group] );
|
|
|
|
unset( $this->mGroupCnt[$group] );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'assign':
|
|
|
|
# Rollback assignment of text to pre-existing elements.
|
|
|
|
$this->mRefs[$group][$key]['text'] = null;
|
|
|
|
# continue without break
|
|
|
|
case 'increment':
|
|
|
|
# Rollback increase in named ref occurrences.
|
|
|
|
$this->mRefs[$group][$key]['count']--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback function for <references>
|
2008-06-06 20:38:04 +00:00
|
|
|
*
|
2015-10-26 14:44:02 +00:00
|
|
|
* @param string|null $str Raw content of the <references> tag.
|
|
|
|
* @param string[] $argv Arguments
|
|
|
|
* @param Parser $parser
|
|
|
|
* @param PPFrame $frame
|
2011-05-28 20:44:24 +00:00
|
|
|
*
|
2008-06-06 20:38:04 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-09-20 13:59:58 +00:00
|
|
|
public function references( $str, array $argv, Parser $parser, PPFrame $frame ) {
|
2015-06-08 14:05:06 +00:00
|
|
|
if ( $this->mInCite || $this->mInReferences ) {
|
2008-06-06 20:38:04 +00:00
|
|
|
if ( is_null( $str ) ) {
|
|
|
|
return htmlspecialchars( "<references/>" );
|
2015-06-08 14:05:06 +00:00
|
|
|
}
|
2015-07-18 20:55:32 +00:00
|
|
|
return htmlspecialchars( "<references>$str</references>" );
|
|
|
|
}
|
|
|
|
$this->mCallCnt++;
|
|
|
|
$this->mInReferences = true;
|
|
|
|
$ret = $this->guardedReferences( $str, $argv, $parser );
|
|
|
|
$this->mInReferences = false;
|
2016-05-09 23:36:49 +00:00
|
|
|
if ( is_callable( [ $frame, 'setVolatile' ] ) ) {
|
2015-07-18 20:55:32 +00:00
|
|
|
$frame->setVolatile();
|
2008-06-06 20:38:04 +00:00
|
|
|
}
|
2015-07-18 20:55:32 +00:00
|
|
|
return $ret;
|
2008-06-06 20:38:04 +00:00
|
|
|
}
|
|
|
|
|
2011-05-28 20:44:24 +00:00
|
|
|
/**
|
2015-10-26 14:44:02 +00:00
|
|
|
* @param string|null $str Raw content of the <references> tag.
|
|
|
|
* @param string[] $argv
|
|
|
|
* @param Parser $parser
|
|
|
|
* @param string $group
|
|
|
|
*
|
2011-05-28 20:44:24 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-09-21 11:01:25 +00:00
|
|
|
private function guardedReferences(
|
2016-09-20 13:59:58 +00:00
|
|
|
$str,
|
|
|
|
array $argv,
|
|
|
|
Parser $parser,
|
|
|
|
$group = self::DEFAULT_GROUP
|
|
|
|
) {
|
2017-03-22 19:13:22 +00:00
|
|
|
global $wgCiteResponsiveReferences;
|
2008-06-06 20:38:04 +00:00
|
|
|
|
|
|
|
$this->mParser = $parser;
|
2011-02-22 00:07:21 +00:00
|
|
|
|
2017-03-22 19:13:22 +00:00
|
|
|
if ( isset( $argv['group'] ) ) {
|
2008-06-06 20:38:04 +00:00
|
|
|
$group = $argv['group'];
|
2015-08-25 15:56:42 +00:00
|
|
|
unset( $argv['group'] );
|
2008-06-06 20:38:04 +00:00
|
|
|
}
|
2011-02-22 00:07:21 +00:00
|
|
|
|
2009-07-26 22:15:13 +00:00
|
|
|
if ( strval( $str ) !== '' ) {
|
|
|
|
$this->mReferencesGroup = $group;
|
2011-02-22 00:07:21 +00:00
|
|
|
|
Rollback all, then redo all, when fixing out-of-order tags; not one-by-one
Imagine the following wikitext:
<ref name=r/>
<references>
<ref name=r>A</ref>
<ref name=r>B</ref>
</references>
This is simple. Cite would see these as the following operations, in
order:
1. Use only: <ref name=r/>
2. References block
3. Define only: <ref name=r>A</ref>
4. Define only: <ref name=r>B</ref>
<ref name=r> is defined twice with different content and we get an
error message.
Now, imagine the following wikitext:
<ref name=r/>
{{#tag:references|
<ref name=r>A</ref>
<ref name=r>B</ref>
}}
Cite would see these as the following operations, in order:
1. Use only: <ref name=r/>
2. Use and define: <ref name=r>A</ref>
3. Use and define: <ref name=r>B</ref>
4. References block
When the 'references' block appears, Cite notices that the tag has
parsed content, and deduces that it was called with #tag. We need to
undo the last operations to update internal bookkeeping, as the last
two 'ref' tags do not actually represent ref usages, as we assumed,
but only definitions.
5. Undo: <ref name=r> reused
6. Define only: <ref name=r>B</ref>
7. Undo: <ref name=r> defined
(Right now, it appears to Cite that <ref name=r> was never defined!)
8. Define only: <ref name=r>A</ref>
Thus we get no errors, although we should.
This patch changes the order of the rollback operations:
5. Undo: <ref name=r> reused
6. Undo: <ref name=r> defined
7. Define only: <ref name=r>A</ref>
8. Define only: <ref name=r>B</ref>
Aha! <ref name=r> is defined twice with different content! We get an
error correctly.
Bug: T124227
Change-Id: I61766c4104856323987cca9a5e4ff85a76b3618b
2016-01-20 20:38:59 +00:00
|
|
|
# Detect whether we were sent already rendered <ref>s.
|
|
|
|
# Mostly a side effect of using #tag to call references.
|
|
|
|
# The following assumes that the parsed <ref>s sent within
|
|
|
|
# the <references> block were the most recent calls to
|
|
|
|
# <ref>. This assumption is true for all known use cases,
|
|
|
|
# but not strictly enforced by the parser. It is possible
|
|
|
|
# that some unusual combination of #tag, <references> and
|
|
|
|
# conditional parser functions could be created that would
|
|
|
|
# lead to malformed references here.
|
2015-06-08 14:05:06 +00:00
|
|
|
$count = substr_count( $str, Parser::MARKER_PREFIX . "-ref-" );
|
2016-05-09 23:36:49 +00:00
|
|
|
$redoStack = [];
|
Rollback all, then redo all, when fixing out-of-order tags; not one-by-one
Imagine the following wikitext:
<ref name=r/>
<references>
<ref name=r>A</ref>
<ref name=r>B</ref>
</references>
This is simple. Cite would see these as the following operations, in
order:
1. Use only: <ref name=r/>
2. References block
3. Define only: <ref name=r>A</ref>
4. Define only: <ref name=r>B</ref>
<ref name=r> is defined twice with different content and we get an
error message.
Now, imagine the following wikitext:
<ref name=r/>
{{#tag:references|
<ref name=r>A</ref>
<ref name=r>B</ref>
}}
Cite would see these as the following operations, in order:
1. Use only: <ref name=r/>
2. Use and define: <ref name=r>A</ref>
3. Use and define: <ref name=r>B</ref>
4. References block
When the 'references' block appears, Cite notices that the tag has
parsed content, and deduces that it was called with #tag. We need to
undo the last operations to update internal bookkeeping, as the last
two 'ref' tags do not actually represent ref usages, as we assumed,
but only definitions.
5. Undo: <ref name=r> reused
6. Define only: <ref name=r>B</ref>
7. Undo: <ref name=r> defined
(Right now, it appears to Cite that <ref name=r> was never defined!)
8. Define only: <ref name=r>A</ref>
Thus we get no errors, although we should.
This patch changes the order of the rollback operations:
5. Undo: <ref name=r> reused
6. Undo: <ref name=r> defined
7. Define only: <ref name=r>A</ref>
8. Define only: <ref name=r>B</ref>
Aha! <ref name=r> is defined twice with different content! We get an
error correctly.
Bug: T124227
Change-Id: I61766c4104856323987cca9a5e4ff85a76b3618b
2016-01-20 20:38:59 +00:00
|
|
|
|
|
|
|
# Undo effects of calling <ref> while unaware of containing <references>
|
2015-06-08 14:05:06 +00:00
|
|
|
for ( $i = 1; $i <= $count; $i++ ) {
|
2015-07-07 07:37:14 +00:00
|
|
|
if ( !$this->mRefCallStack ) {
|
2015-06-08 14:05:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$call = array_pop( $this->mRefCallStack );
|
Rollback all, then redo all, when fixing out-of-order tags; not one-by-one
Imagine the following wikitext:
<ref name=r/>
<references>
<ref name=r>A</ref>
<ref name=r>B</ref>
</references>
This is simple. Cite would see these as the following operations, in
order:
1. Use only: <ref name=r/>
2. References block
3. Define only: <ref name=r>A</ref>
4. Define only: <ref name=r>B</ref>
<ref name=r> is defined twice with different content and we get an
error message.
Now, imagine the following wikitext:
<ref name=r/>
{{#tag:references|
<ref name=r>A</ref>
<ref name=r>B</ref>
}}
Cite would see these as the following operations, in order:
1. Use only: <ref name=r/>
2. Use and define: <ref name=r>A</ref>
3. Use and define: <ref name=r>B</ref>
4. References block
When the 'references' block appears, Cite notices that the tag has
parsed content, and deduces that it was called with #tag. We need to
undo the last operations to update internal bookkeeping, as the last
two 'ref' tags do not actually represent ref usages, as we assumed,
but only definitions.
5. Undo: <ref name=r> reused
6. Define only: <ref name=r>B</ref>
7. Undo: <ref name=r> defined
(Right now, it appears to Cite that <ref name=r> was never defined!)
8. Define only: <ref name=r>A</ref>
Thus we get no errors, although we should.
This patch changes the order of the rollback operations:
5. Undo: <ref name=r> reused
6. Undo: <ref name=r> defined
7. Define only: <ref name=r>A</ref>
8. Define only: <ref name=r>B</ref>
Aha! <ref name=r> is defined twice with different content! We get an
error correctly.
Bug: T124227
Change-Id: I61766c4104856323987cca9a5e4ff85a76b3618b
2016-01-20 20:38:59 +00:00
|
|
|
$redoStack[] = $call;
|
2015-06-08 14:05:06 +00:00
|
|
|
if ( $call !== false ) {
|
|
|
|
list( $type, $ref_argv, $ref_str,
|
|
|
|
$ref_key, $ref_group, $ref_index ) = $call;
|
|
|
|
$this->rollbackRef( $type, $ref_key, $ref_group, $ref_index );
|
Rollback all, then redo all, when fixing out-of-order tags; not one-by-one
Imagine the following wikitext:
<ref name=r/>
<references>
<ref name=r>A</ref>
<ref name=r>B</ref>
</references>
This is simple. Cite would see these as the following operations, in
order:
1. Use only: <ref name=r/>
2. References block
3. Define only: <ref name=r>A</ref>
4. Define only: <ref name=r>B</ref>
<ref name=r> is defined twice with different content and we get an
error message.
Now, imagine the following wikitext:
<ref name=r/>
{{#tag:references|
<ref name=r>A</ref>
<ref name=r>B</ref>
}}
Cite would see these as the following operations, in order:
1. Use only: <ref name=r/>
2. Use and define: <ref name=r>A</ref>
3. Use and define: <ref name=r>B</ref>
4. References block
When the 'references' block appears, Cite notices that the tag has
parsed content, and deduces that it was called with #tag. We need to
undo the last operations to update internal bookkeeping, as the last
two 'ref' tags do not actually represent ref usages, as we assumed,
but only definitions.
5. Undo: <ref name=r> reused
6. Define only: <ref name=r>B</ref>
7. Undo: <ref name=r> defined
(Right now, it appears to Cite that <ref name=r> was never defined!)
8. Define only: <ref name=r>A</ref>
Thus we get no errors, although we should.
This patch changes the order of the rollback operations:
5. Undo: <ref name=r> reused
6. Undo: <ref name=r> defined
7. Define only: <ref name=r>A</ref>
8. Define only: <ref name=r>B</ref>
Aha! <ref name=r> is defined twice with different content! We get an
error correctly.
Bug: T124227
Change-Id: I61766c4104856323987cca9a5e4ff85a76b3618b
2016-01-20 20:38:59 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-08 14:05:06 +00:00
|
|
|
|
Rollback all, then redo all, when fixing out-of-order tags; not one-by-one
Imagine the following wikitext:
<ref name=r/>
<references>
<ref name=r>A</ref>
<ref name=r>B</ref>
</references>
This is simple. Cite would see these as the following operations, in
order:
1. Use only: <ref name=r/>
2. References block
3. Define only: <ref name=r>A</ref>
4. Define only: <ref name=r>B</ref>
<ref name=r> is defined twice with different content and we get an
error message.
Now, imagine the following wikitext:
<ref name=r/>
{{#tag:references|
<ref name=r>A</ref>
<ref name=r>B</ref>
}}
Cite would see these as the following operations, in order:
1. Use only: <ref name=r/>
2. Use and define: <ref name=r>A</ref>
3. Use and define: <ref name=r>B</ref>
4. References block
When the 'references' block appears, Cite notices that the tag has
parsed content, and deduces that it was called with #tag. We need to
undo the last operations to update internal bookkeeping, as the last
two 'ref' tags do not actually represent ref usages, as we assumed,
but only definitions.
5. Undo: <ref name=r> reused
6. Define only: <ref name=r>B</ref>
7. Undo: <ref name=r> defined
(Right now, it appears to Cite that <ref name=r> was never defined!)
8. Define only: <ref name=r>A</ref>
Thus we get no errors, although we should.
This patch changes the order of the rollback operations:
5. Undo: <ref name=r> reused
6. Undo: <ref name=r> defined
7. Define only: <ref name=r>A</ref>
8. Define only: <ref name=r>B</ref>
Aha! <ref name=r> is defined twice with different content! We get an
error correctly.
Bug: T124227
Change-Id: I61766c4104856323987cca9a5e4ff85a76b3618b
2016-01-20 20:38:59 +00:00
|
|
|
# Rerun <ref> call now that mInReferences is set.
|
|
|
|
for ( $i = count( $redoStack ) - 1; $i >= 0; $i-- ) {
|
|
|
|
$call = $redoStack[$i];
|
|
|
|
if ( $call !== false ) {
|
|
|
|
list( $type, $ref_argv, $ref_str,
|
|
|
|
$ref_key, $ref_group, $ref_index ) = $call;
|
2015-06-08 14:05:06 +00:00
|
|
|
$this->guardedRef( $ref_str, $ref_argv, $parser );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-26 22:15:13 +00:00
|
|
|
# Parse $str to process any unparsed <ref> tags.
|
2015-06-08 14:05:06 +00:00
|
|
|
$parser->recursiveTagParse( $str );
|
|
|
|
|
|
|
|
# Reset call stack
|
2016-05-09 23:36:49 +00:00
|
|
|
$this->mRefCallStack = [];
|
2009-07-26 22:15:13 +00:00
|
|
|
}
|
|
|
|
|
Implement responsive columns for reference lists
This is based on the popular 'count' parameter from Template:Reflist on
English Wikipedia, which has also been adopted by many other wikis.
That template's 'count' parameter allows maximum flexibility on a per-
page basis. This was important because the template can't know how many
references the list will contain. Users typically manually add (and
later, increment) the 'count' parameter when the list exceeds a certain
threshold.
The template currently sets an exact column count (via the CSS3
property `column-count`).
This patch improves on that by instead using the closely related CSS3
`column-width` property. This automatically derives the column count
based on the available space in the browser window. It will thus create
two or three columns on a typical desktop screen, and two or no columns
on a mobile device.
The specified width is the minimum width of a column. This ensures that
the list is not split when rendered on a narrow screen or mobile device.
It also hooks into the raw list before parsing and adds the class only
when the list will contain more than a certain number of items. This
prevents very short lists from being split into multiple columns.
Templates like Template:Reflist on English Wikipedia currently are not
able to set inline styles on the list element directly, which is why
they set it on a `<div>` wrapping the `<references />` output. Because
of this, the feature of the Cite extension must not be enabled at the
same time, as that would result in both the template's wrapper and the
references list being split. The end result would involve sitations with
three columns split in four sub-columns, creating a complicated mess of
nine intermixed columns.
To provide a smooth migration for wikis, this feature can be disabled by
default using `$wgCiteResponsiveReferences = false`. Each individual
template createing reference list can then be migrated, by removing the
wrapper column styles and instead settting the new "responsive"
attribute, like so: `<references responsive />`.
Once any conflicting templates have been migrated, the default for the
wiki can be swapped by setting `$wgCiteResponsiveReferences = true`.
If wikis wish for some templates to keep their custom column splitting
behaviour, templates can also opt-out by setting `responsive="0"`, which
will make sure that it will keep behaving the current way even after the
feature becomes enabled by default for the wiki.
In summary, when disabled by default, pages can opt into this system
with `<references responsive />`. When enabled by default, pages can opt
out of the system with `<references responsive=0 />`.
* Deprecate cite_references_prefix/cite_references_suffix.
This message is rarely used and opens up compatibility hazards.
It was already removed by Parsoid, but the PHP implementation
still had it. It's typically used to add inline styles to the
wrapper which is more appropiately done in Common.css (or
obsoleted as part of the skin or Cite extenion itself nowadays
depending on what style in question).
It was also a HTML-style message with separated open and close
segments, which is an anti-pattern in itself.
* Declare module target explicitly and include mobile. The absence of
this stylesheet caused subtle BiDi/RTL bugs on mobile.
Bug: T33597
Change-Id: Ia535f9b722e825e71e792b36356febc3bd444387
2015-07-21 02:33:50 +00:00
|
|
|
if ( isset( $argv['responsive'] ) ) {
|
|
|
|
$responsive = $argv['responsive'] !== '0';
|
|
|
|
unset( $argv['responsive'] );
|
|
|
|
} else {
|
|
|
|
$responsive = $wgCiteResponsiveReferences;
|
|
|
|
}
|
|
|
|
|
2017-03-22 19:05:27 +00:00
|
|
|
// There are remaining parameters we don't recognise
|
2015-07-07 07:37:14 +00:00
|
|
|
if ( $argv ) {
|
2017-03-22 19:13:22 +00:00
|
|
|
return $this->error( 'cite_error_references_invalid_parameters' );
|
2015-07-18 20:55:32 +00:00
|
|
|
}
|
2011-02-22 00:07:21 +00:00
|
|
|
|
Implement responsive columns for reference lists
This is based on the popular 'count' parameter from Template:Reflist on
English Wikipedia, which has also been adopted by many other wikis.
That template's 'count' parameter allows maximum flexibility on a per-
page basis. This was important because the template can't know how many
references the list will contain. Users typically manually add (and
later, increment) the 'count' parameter when the list exceeds a certain
threshold.
The template currently sets an exact column count (via the CSS3
property `column-count`).
This patch improves on that by instead using the closely related CSS3
`column-width` property. This automatically derives the column count
based on the available space in the browser window. It will thus create
two or three columns on a typical desktop screen, and two or no columns
on a mobile device.
The specified width is the minimum width of a column. This ensures that
the list is not split when rendered on a narrow screen or mobile device.
It also hooks into the raw list before parsing and adds the class only
when the list will contain more than a certain number of items. This
prevents very short lists from being split into multiple columns.
Templates like Template:Reflist on English Wikipedia currently are not
able to set inline styles on the list element directly, which is why
they set it on a `<div>` wrapping the `<references />` output. Because
of this, the feature of the Cite extension must not be enabled at the
same time, as that would result in both the template's wrapper and the
references list being split. The end result would involve sitations with
three columns split in four sub-columns, creating a complicated mess of
nine intermixed columns.
To provide a smooth migration for wikis, this feature can be disabled by
default using `$wgCiteResponsiveReferences = false`. Each individual
template createing reference list can then be migrated, by removing the
wrapper column styles and instead settting the new "responsive"
attribute, like so: `<references responsive />`.
Once any conflicting templates have been migrated, the default for the
wiki can be swapped by setting `$wgCiteResponsiveReferences = true`.
If wikis wish for some templates to keep their custom column splitting
behaviour, templates can also opt-out by setting `responsive="0"`, which
will make sure that it will keep behaving the current way even after the
feature becomes enabled by default for the wiki.
In summary, when disabled by default, pages can opt into this system
with `<references responsive />`. When enabled by default, pages can opt
out of the system with `<references responsive=0 />`.
* Deprecate cite_references_prefix/cite_references_suffix.
This message is rarely used and opens up compatibility hazards.
It was already removed by Parsoid, but the PHP implementation
still had it. It's typically used to add inline styles to the
wrapper which is more appropiately done in Common.css (or
obsoleted as part of the skin or Cite extenion itself nowadays
depending on what style in question).
It was also a HTML-style message with separated open and close
segments, which is an anti-pattern in itself.
* Declare module target explicitly and include mobile. The absence of
this stylesheet caused subtle BiDi/RTL bugs on mobile.
Bug: T33597
Change-Id: Ia535f9b722e825e71e792b36356febc3bd444387
2015-07-21 02:33:50 +00:00
|
|
|
$s = $this->referencesFormat( $group, $responsive );
|
2015-07-18 20:55:32 +00:00
|
|
|
|
|
|
|
# Append errors generated while processing <references>
|
2015-07-07 07:37:14 +00:00
|
|
|
if ( $this->mReferencesErrors ) {
|
2015-07-18 20:55:32 +00:00
|
|
|
$s .= "\n" . implode( "<br />\n", $this->mReferencesErrors );
|
2016-05-09 23:36:49 +00:00
|
|
|
$this->mReferencesErrors = [];
|
2015-07-18 20:55:32 +00:00
|
|
|
}
|
|
|
|
return $s;
|
2008-06-06 20:38:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make output to be returned from the references() function
|
2011-09-14 15:07:20 +00:00
|
|
|
*
|
2015-10-26 14:44:02 +00:00
|
|
|
* @param string $group
|
Implement responsive columns for reference lists
This is based on the popular 'count' parameter from Template:Reflist on
English Wikipedia, which has also been adopted by many other wikis.
That template's 'count' parameter allows maximum flexibility on a per-
page basis. This was important because the template can't know how many
references the list will contain. Users typically manually add (and
later, increment) the 'count' parameter when the list exceeds a certain
threshold.
The template currently sets an exact column count (via the CSS3
property `column-count`).
This patch improves on that by instead using the closely related CSS3
`column-width` property. This automatically derives the column count
based on the available space in the browser window. It will thus create
two or three columns on a typical desktop screen, and two or no columns
on a mobile device.
The specified width is the minimum width of a column. This ensures that
the list is not split when rendered on a narrow screen or mobile device.
It also hooks into the raw list before parsing and adds the class only
when the list will contain more than a certain number of items. This
prevents very short lists from being split into multiple columns.
Templates like Template:Reflist on English Wikipedia currently are not
able to set inline styles on the list element directly, which is why
they set it on a `<div>` wrapping the `<references />` output. Because
of this, the feature of the Cite extension must not be enabled at the
same time, as that would result in both the template's wrapper and the
references list being split. The end result would involve sitations with
three columns split in four sub-columns, creating a complicated mess of
nine intermixed columns.
To provide a smooth migration for wikis, this feature can be disabled by
default using `$wgCiteResponsiveReferences = false`. Each individual
template createing reference list can then be migrated, by removing the
wrapper column styles and instead settting the new "responsive"
attribute, like so: `<references responsive />`.
Once any conflicting templates have been migrated, the default for the
wiki can be swapped by setting `$wgCiteResponsiveReferences = true`.
If wikis wish for some templates to keep their custom column splitting
behaviour, templates can also opt-out by setting `responsive="0"`, which
will make sure that it will keep behaving the current way even after the
feature becomes enabled by default for the wiki.
In summary, when disabled by default, pages can opt into this system
with `<references responsive />`. When enabled by default, pages can opt
out of the system with `<references responsive=0 />`.
* Deprecate cite_references_prefix/cite_references_suffix.
This message is rarely used and opens up compatibility hazards.
It was already removed by Parsoid, but the PHP implementation
still had it. It's typically used to add inline styles to the
wrapper which is more appropiately done in Common.css (or
obsoleted as part of the skin or Cite extenion itself nowadays
depending on what style in question).
It was also a HTML-style message with separated open and close
segments, which is an anti-pattern in itself.
* Declare module target explicitly and include mobile. The absence of
this stylesheet caused subtle BiDi/RTL bugs on mobile.
Bug: T33597
Change-Id: Ia535f9b722e825e71e792b36356febc3bd444387
2015-07-21 02:33:50 +00:00
|
|
|
* @param bool $responsive
|
|
|
|
* @return string HTML ready for output
|
2008-06-06 20:38:04 +00:00
|
|
|
*/
|
Implement responsive columns for reference lists
This is based on the popular 'count' parameter from Template:Reflist on
English Wikipedia, which has also been adopted by many other wikis.
That template's 'count' parameter allows maximum flexibility on a per-
page basis. This was important because the template can't know how many
references the list will contain. Users typically manually add (and
later, increment) the 'count' parameter when the list exceeds a certain
threshold.
The template currently sets an exact column count (via the CSS3
property `column-count`).
This patch improves on that by instead using the closely related CSS3
`column-width` property. This automatically derives the column count
based on the available space in the browser window. It will thus create
two or three columns on a typical desktop screen, and two or no columns
on a mobile device.
The specified width is the minimum width of a column. This ensures that
the list is not split when rendered on a narrow screen or mobile device.
It also hooks into the raw list before parsing and adds the class only
when the list will contain more than a certain number of items. This
prevents very short lists from being split into multiple columns.
Templates like Template:Reflist on English Wikipedia currently are not
able to set inline styles on the list element directly, which is why
they set it on a `<div>` wrapping the `<references />` output. Because
of this, the feature of the Cite extension must not be enabled at the
same time, as that would result in both the template's wrapper and the
references list being split. The end result would involve sitations with
three columns split in four sub-columns, creating a complicated mess of
nine intermixed columns.
To provide a smooth migration for wikis, this feature can be disabled by
default using `$wgCiteResponsiveReferences = false`. Each individual
template createing reference list can then be migrated, by removing the
wrapper column styles and instead settting the new "responsive"
attribute, like so: `<references responsive />`.
Once any conflicting templates have been migrated, the default for the
wiki can be swapped by setting `$wgCiteResponsiveReferences = true`.
If wikis wish for some templates to keep their custom column splitting
behaviour, templates can also opt-out by setting `responsive="0"`, which
will make sure that it will keep behaving the current way even after the
feature becomes enabled by default for the wiki.
In summary, when disabled by default, pages can opt into this system
with `<references responsive />`. When enabled by default, pages can opt
out of the system with `<references responsive=0 />`.
* Deprecate cite_references_prefix/cite_references_suffix.
This message is rarely used and opens up compatibility hazards.
It was already removed by Parsoid, but the PHP implementation
still had it. It's typically used to add inline styles to the
wrapper which is more appropiately done in Common.css (or
obsoleted as part of the skin or Cite extenion itself nowadays
depending on what style in question).
It was also a HTML-style message with separated open and close
segments, which is an anti-pattern in itself.
* Declare module target explicitly and include mobile. The absence of
this stylesheet caused subtle BiDi/RTL bugs on mobile.
Bug: T33597
Change-Id: Ia535f9b722e825e71e792b36356febc3bd444387
2015-07-21 02:33:50 +00:00
|
|
|
private function referencesFormat( $group, $responsive ) {
|
2015-10-29 21:27:05 +00:00
|
|
|
if ( !$this->mRefs || !isset( $this->mRefs[$group] ) ) {
|
2008-06-06 20:38:04 +00:00
|
|
|
return '';
|
2011-02-22 00:07:21 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 23:36:49 +00:00
|
|
|
$ent = [];
|
2011-02-22 00:07:21 +00:00
|
|
|
foreach ( $this->mRefs[$group] as $k => $v ) {
|
2008-06-06 20:38:04 +00:00
|
|
|
$ent[] = $this->referencesFormatEntry( $k, $v );
|
2011-02-22 00:07:21 +00:00
|
|
|
}
|
|
|
|
|
Implement responsive columns for reference lists
This is based on the popular 'count' parameter from Template:Reflist on
English Wikipedia, which has also been adopted by many other wikis.
That template's 'count' parameter allows maximum flexibility on a per-
page basis. This was important because the template can't know how many
references the list will contain. Users typically manually add (and
later, increment) the 'count' parameter when the list exceeds a certain
threshold.
The template currently sets an exact column count (via the CSS3
property `column-count`).
This patch improves on that by instead using the closely related CSS3
`column-width` property. This automatically derives the column count
based on the available space in the browser window. It will thus create
two or three columns on a typical desktop screen, and two or no columns
on a mobile device.
The specified width is the minimum width of a column. This ensures that
the list is not split when rendered on a narrow screen or mobile device.
It also hooks into the raw list before parsing and adds the class only
when the list will contain more than a certain number of items. This
prevents very short lists from being split into multiple columns.
Templates like Template:Reflist on English Wikipedia currently are not
able to set inline styles on the list element directly, which is why
they set it on a `<div>` wrapping the `<references />` output. Because
of this, the feature of the Cite extension must not be enabled at the
same time, as that would result in both the template's wrapper and the
references list being split. The end result would involve sitations with
three columns split in four sub-columns, creating a complicated mess of
nine intermixed columns.
To provide a smooth migration for wikis, this feature can be disabled by
default using `$wgCiteResponsiveReferences = false`. Each individual
template createing reference list can then be migrated, by removing the
wrapper column styles and instead settting the new "responsive"
attribute, like so: `<references responsive />`.
Once any conflicting templates have been migrated, the default for the
wiki can be swapped by setting `$wgCiteResponsiveReferences = true`.
If wikis wish for some templates to keep their custom column splitting
behaviour, templates can also opt-out by setting `responsive="0"`, which
will make sure that it will keep behaving the current way even after the
feature becomes enabled by default for the wiki.
In summary, when disabled by default, pages can opt into this system
with `<references responsive />`. When enabled by default, pages can opt
out of the system with `<references responsive=0 />`.
* Deprecate cite_references_prefix/cite_references_suffix.
This message is rarely used and opens up compatibility hazards.
It was already removed by Parsoid, but the PHP implementation
still had it. It's typically used to add inline styles to the
wrapper which is more appropiately done in Common.css (or
obsoleted as part of the skin or Cite extenion itself nowadays
depending on what style in question).
It was also a HTML-style message with separated open and close
segments, which is an anti-pattern in itself.
* Declare module target explicitly and include mobile. The absence of
this stylesheet caused subtle BiDi/RTL bugs on mobile.
Bug: T33597
Change-Id: Ia535f9b722e825e71e792b36356febc3bd444387
2015-07-21 02:33:50 +00:00
|
|
|
// Add new lines between the list items (ref entires) to avoid confusing tidy (bug 13073).
|
|
|
|
// Note: This builds a string of wikitext, not html.
|
|
|
|
$parserInput = Html::rawElement( 'ol', [ 'class' => [ 'references' ] ],
|
|
|
|
"\n" . implode( "\n", $ent ) . "\n"
|
|
|
|
);
|
2012-10-30 16:22:47 +00:00
|
|
|
|
2018-02-28 09:59:50 +00:00
|
|
|
// Live hack: parse() adds two newlines on WM, can't reproduce it locally -ævar
|
|
|
|
$ret = rtrim( $this->mParser->recursiveTagParse( $parserInput ), "\n" );
|
2009-02-03 04:57:28 +00:00
|
|
|
|
Implement responsive columns for reference lists
This is based on the popular 'count' parameter from Template:Reflist on
English Wikipedia, which has also been adopted by many other wikis.
That template's 'count' parameter allows maximum flexibility on a per-
page basis. This was important because the template can't know how many
references the list will contain. Users typically manually add (and
later, increment) the 'count' parameter when the list exceeds a certain
threshold.
The template currently sets an exact column count (via the CSS3
property `column-count`).
This patch improves on that by instead using the closely related CSS3
`column-width` property. This automatically derives the column count
based on the available space in the browser window. It will thus create
two or three columns on a typical desktop screen, and two or no columns
on a mobile device.
The specified width is the minimum width of a column. This ensures that
the list is not split when rendered on a narrow screen or mobile device.
It also hooks into the raw list before parsing and adds the class only
when the list will contain more than a certain number of items. This
prevents very short lists from being split into multiple columns.
Templates like Template:Reflist on English Wikipedia currently are not
able to set inline styles on the list element directly, which is why
they set it on a `<div>` wrapping the `<references />` output. Because
of this, the feature of the Cite extension must not be enabled at the
same time, as that would result in both the template's wrapper and the
references list being split. The end result would involve sitations with
three columns split in four sub-columns, creating a complicated mess of
nine intermixed columns.
To provide a smooth migration for wikis, this feature can be disabled by
default using `$wgCiteResponsiveReferences = false`. Each individual
template createing reference list can then be migrated, by removing the
wrapper column styles and instead settting the new "responsive"
attribute, like so: `<references responsive />`.
Once any conflicting templates have been migrated, the default for the
wiki can be swapped by setting `$wgCiteResponsiveReferences = true`.
If wikis wish for some templates to keep their custom column splitting
behaviour, templates can also opt-out by setting `responsive="0"`, which
will make sure that it will keep behaving the current way even after the
feature becomes enabled by default for the wiki.
In summary, when disabled by default, pages can opt into this system
with `<references responsive />`. When enabled by default, pages can opt
out of the system with `<references responsive=0 />`.
* Deprecate cite_references_prefix/cite_references_suffix.
This message is rarely used and opens up compatibility hazards.
It was already removed by Parsoid, but the PHP implementation
still had it. It's typically used to add inline styles to the
wrapper which is more appropiately done in Common.css (or
obsoleted as part of the skin or Cite extenion itself nowadays
depending on what style in question).
It was also a HTML-style message with separated open and close
segments, which is an anti-pattern in itself.
* Declare module target explicitly and include mobile. The absence of
this stylesheet caused subtle BiDi/RTL bugs on mobile.
Bug: T33597
Change-Id: Ia535f9b722e825e71e792b36356febc3bd444387
2015-07-21 02:33:50 +00:00
|
|
|
if ( $responsive ) {
|
|
|
|
// Use a DIV wrap because column-count on a list directly is broken in Chrome.
|
|
|
|
// See https://bugs.chromium.org/p/chromium/issues/detail?id=498730.
|
|
|
|
$wrapClasses = [ 'mw-references-wrap' ];
|
|
|
|
if ( count( $this->mRefs[$group] ) > 10 ) {
|
|
|
|
$wrapClasses[] = 'mw-references-columns';
|
|
|
|
}
|
|
|
|
$ret = Html::rawElement( 'div', [ 'class' => $wrapClasses ], $ret );
|
|
|
|
}
|
|
|
|
|
2016-01-25 16:52:46 +00:00
|
|
|
if ( !$this->mParser->getOptions()->getIsPreview() ) {
|
|
|
|
// save references data for later use by LinksUpdate hooks
|
|
|
|
$this->saveReferencesData( $group );
|
|
|
|
}
|
|
|
|
|
2010-04-17 21:07:37 +00:00
|
|
|
// done, clean up so we can reuse the group
|
2011-02-22 00:07:21 +00:00
|
|
|
unset( $this->mRefs[$group] );
|
2010-04-17 21:07:37 +00:00
|
|
|
unset( $this->mGroupCnt[$group] );
|
2011-02-22 00:07:21 +00:00
|
|
|
|
2008-06-06 20:38:04 +00:00
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* references, array for user-suppplied
|
|
|
|
* @return string Wikitext
|
|
|
|
*/
|
2016-09-21 11:01:25 +00:00
|
|
|
private function referencesFormatEntry( $key, $val ) {
|
2008-06-06 20:38:04 +00:00
|
|
|
// Anonymous reference
|
2011-02-03 21:40:55 +00:00
|
|
|
if ( !is_array( $val ) ) {
|
2012-08-30 08:41:27 +00:00
|
|
|
return wfMessage(
|
2008-06-06 20:38:04 +00:00
|
|
|
'cite_references_link_one',
|
2017-11-10 01:21:54 +00:00
|
|
|
$this->normalizeKey(
|
2017-09-18 19:17:06 +00:00
|
|
|
self::getReferencesKey( $key )
|
|
|
|
),
|
2017-11-10 01:21:54 +00:00
|
|
|
$this->normalizeKey(
|
2017-09-18 19:17:06 +00:00
|
|
|
$this->refKey( $key )
|
|
|
|
),
|
2012-01-04 15:00:01 +00:00
|
|
|
$this->referenceText( $key, $val )
|
2012-08-30 08:41:27 +00:00
|
|
|
)->inContentLanguage()->plain();
|
2012-01-04 15:00:01 +00:00
|
|
|
}
|
|
|
|
$text = $this->referenceText( $key, $val['text'] );
|
|
|
|
if ( isset( $val['follow'] ) ) {
|
2012-08-30 08:41:27 +00:00
|
|
|
return wfMessage(
|
2010-08-16 12:05:57 +00:00
|
|
|
'cite_references_no_link',
|
2017-11-10 01:21:54 +00:00
|
|
|
$this->normalizeKey(
|
2017-09-18 19:17:06 +00:00
|
|
|
self::getReferencesKey( $val['follow'] )
|
|
|
|
),
|
2012-01-04 15:00:01 +00:00
|
|
|
$text
|
2012-08-30 08:41:27 +00:00
|
|
|
)->inContentLanguage()->plain();
|
2011-02-03 21:40:55 +00:00
|
|
|
}
|
2016-05-05 19:09:33 +00:00
|
|
|
if ( !isset( $val['count'] ) ) {
|
|
|
|
// this handles the case of section preview for list-defined references
|
|
|
|
return wfMessage( 'cite_references_link_many',
|
2017-11-10 01:21:54 +00:00
|
|
|
$this->normalizeKey(
|
2017-09-18 19:17:06 +00:00
|
|
|
self::getReferencesKey( $key . "-" . ( isset( $val['key'] ) ? $val['key'] : '' ) )
|
|
|
|
),
|
2016-05-05 19:09:33 +00:00
|
|
|
'',
|
|
|
|
$text
|
|
|
|
)->inContentLanguage()->plain();
|
|
|
|
}
|
2011-02-03 21:40:55 +00:00
|
|
|
if ( $val['count'] < 0 ) {
|
2012-08-30 08:41:27 +00:00
|
|
|
return wfMessage(
|
2008-06-06 20:38:04 +00:00
|
|
|
'cite_references_link_one',
|
2017-11-10 01:21:54 +00:00
|
|
|
$this->normalizeKey(
|
2017-09-18 19:17:06 +00:00
|
|
|
self::getReferencesKey( $val['key'] )
|
|
|
|
),
|
2017-11-10 01:21:54 +00:00
|
|
|
$this->normalizeKey(
|
2017-09-18 19:17:06 +00:00
|
|
|
# $this->refKey( $val['key'], $val['count'] )
|
|
|
|
$this->refKey( $val['key'] )
|
|
|
|
),
|
2012-01-04 15:00:01 +00:00
|
|
|
$text
|
2012-08-30 08:41:27 +00:00
|
|
|
)->inContentLanguage()->plain();
|
2011-02-03 21:40:55 +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
|
2016-02-24 23:54:17 +00:00
|
|
|
}
|
|
|
|
if ( $val['count'] === 0 ) {
|
2012-08-30 08:41:27 +00:00
|
|
|
return wfMessage(
|
2008-06-06 20:38:04 +00:00
|
|
|
'cite_references_link_one',
|
2017-11-10 01:21:54 +00:00
|
|
|
$this->normalizeKey(
|
2017-09-18 19:17:06 +00:00
|
|
|
self::getReferencesKey( $key . "-" . $val['key'] )
|
|
|
|
),
|
2017-11-10 01:21:54 +00:00
|
|
|
$this->normalizeKey(
|
2017-09-18 19:17:06 +00:00
|
|
|
# $this->refKey( $key, $val['count'] ),
|
|
|
|
$this->refKey( $key, $val['key'] . "-" . $val['count'] )
|
|
|
|
),
|
2012-01-04 15:00:01 +00:00
|
|
|
$text
|
2012-08-30 08:41:27 +00:00
|
|
|
)->inContentLanguage()->plain();
|
2008-06-06 20:38:04 +00:00
|
|
|
// Named references with >1 occurrences
|
2016-02-24 23:54:17 +00:00
|
|
|
}
|
2016-05-09 23:36:49 +00:00
|
|
|
$links = [];
|
2016-02-24 23:54:17 +00:00
|
|
|
// for group handling, we have an extra key here.
|
|
|
|
for ( $i = 0; $i <= $val['count']; ++$i ) {
|
|
|
|
$links[] = wfMessage(
|
|
|
|
'cite_references_link_many_format',
|
2017-11-10 01:21:54 +00:00
|
|
|
$this->normalizeKey(
|
2017-09-18 19:17:06 +00:00
|
|
|
$this->refKey( $key, $val['key'] . "-$i" )
|
|
|
|
),
|
2016-02-24 23:54:17 +00:00
|
|
|
$this->referencesFormatEntryNumericBacklinkLabel( $val['number'], $i, $val['count'] ),
|
|
|
|
$this->referencesFormatEntryAlternateBacklinkLabel( $i )
|
|
|
|
)->inContentLanguage()->plain();
|
|
|
|
}
|
|
|
|
|
|
|
|
$list = $this->listToText( $links );
|
|
|
|
|
|
|
|
return wfMessage( 'cite_references_link_many',
|
2017-11-10 01:21:54 +00:00
|
|
|
$this->normalizeKey(
|
2017-09-18 19:17:06 +00:00
|
|
|
self::getReferencesKey( $key . "-" . $val['key'] )
|
|
|
|
),
|
2016-02-24 23:54:17 +00:00
|
|
|
$list,
|
|
|
|
$text
|
|
|
|
)->inContentLanguage()->plain();
|
2008-06-06 20:38:04 +00:00
|
|
|
}
|
|
|
|
|
2012-01-04 15:00:01 +00:00
|
|
|
/**
|
|
|
|
* Returns formatted reference text
|
|
|
|
* @param String $key
|
|
|
|
* @param String $text
|
|
|
|
* @return String
|
|
|
|
*/
|
2016-09-21 11:01:25 +00:00
|
|
|
private function referenceText( $key, $text ) {
|
2014-10-01 15:30:34 +00:00
|
|
|
if ( !isset( $text ) || $text === '' ) {
|
2016-02-18 06:50:04 +00:00
|
|
|
if ( $this->mParser->getOptions()->getIsSectionPreview() ) {
|
|
|
|
return $this->warning( 'cite_warning_sectionpreview_no_text', $key, 'noparse' );
|
|
|
|
}
|
2012-01-04 15:00:01 +00:00
|
|
|
return $this->error( 'cite_error_references_no_text', $key, 'noparse' );
|
|
|
|
}
|
|
|
|
return '<span class="reference-text">' . rtrim( $text, "\n" ) . "</span>\n";
|
|
|
|
}
|
|
|
|
|
2008-06-06 20:38:04 +00:00
|
|
|
/**
|
|
|
|
* Generate a numeric backlink given a base number and an
|
|
|
|
* offset, e.g. $base = 1, $offset = 2; = 1.2
|
|
|
|
* Since bug #5525, it correctly does 1.9 -> 1.10 as well as 1.099 -> 1.100
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
*
|
2017-12-28 14:55:49 +00:00
|
|
|
* @param int $base
|
|
|
|
* @param int $offset
|
2008-06-06 20:38:04 +00:00
|
|
|
* @param int $max Maximum value expected.
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-09-21 11:01:25 +00:00
|
|
|
private function referencesFormatEntryNumericBacklinkLabel( $base, $offset, $max ) {
|
2008-06-06 20:38:04 +00:00
|
|
|
global $wgContLang;
|
|
|
|
$scope = strlen( $max );
|
|
|
|
$ret = $wgContLang->formatNum(
|
2010-04-17 21:07:37 +00:00
|
|
|
sprintf( "%s.%0{$scope}s", $base, $offset )
|
2008-06-06 20:38:04 +00:00
|
|
|
);
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a custom format backlink given an offset, e.g.
|
2017-01-07 09:08:35 +00:00
|
|
|
* $offset = 2; = c if $this->mBacklinkLabels = [ 'a',
|
|
|
|
* 'b', 'c', ...]. Return an error if the offset > the # of
|
2008-06-06 20:38:04 +00:00
|
|
|
* array items
|
|
|
|
*
|
2017-12-28 14:55:49 +00:00
|
|
|
* @param int $offset
|
2008-06-06 20:38:04 +00:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-09-21 11:01:25 +00:00
|
|
|
private function referencesFormatEntryAlternateBacklinkLabel( $offset ) {
|
2008-06-06 20:38:04 +00:00
|
|
|
if ( !isset( $this->mBacklinkLabels ) ) {
|
|
|
|
$this->genBacklinkLabels();
|
|
|
|
}
|
|
|
|
if ( isset( $this->mBacklinkLabels[$offset] ) ) {
|
|
|
|
return $this->mBacklinkLabels[$offset];
|
|
|
|
} else {
|
|
|
|
// Feed me!
|
2011-11-14 12:22:07 +00:00
|
|
|
return $this->error( 'cite_error_references_no_backlink_label', null, 'noparse' );
|
2008-06-06 20:38:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-22 14:28:48 +00:00
|
|
|
/**
|
|
|
|
* Generate a custom format link for a group given an offset, e.g.
|
2011-02-22 00:07:21 +00:00
|
|
|
* the second <ref group="foo"> is b if $this->mLinkLabels["foo"] =
|
2017-01-07 09:08:35 +00:00
|
|
|
* [ 'a', 'b', 'c', ...].
|
2010-05-22 14:28:48 +00:00
|
|
|
* Return an error if the offset > the # of array items
|
|
|
|
*
|
2017-12-28 14:55:49 +00:00
|
|
|
* @param int $offset
|
2010-05-22 14:28:48 +00:00
|
|
|
* @param string $group The group name
|
|
|
|
* @param string $label The text to use if there's no message for them.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-09-21 11:01:25 +00:00
|
|
|
private function getLinkLabel( $offset, $group, $label ) {
|
2010-05-22 14:28:48 +00:00
|
|
|
$message = "cite_link_label_group-$group";
|
|
|
|
if ( !isset( $this->mLinkLabels[$group] ) ) {
|
2011-02-22 00:07:21 +00:00
|
|
|
$this->genLinkLabels( $group, $message );
|
2010-05-22 14:28:48 +00:00
|
|
|
}
|
2011-02-22 00:07:21 +00:00
|
|
|
if ( $this->mLinkLabels[$group] === false ) {
|
2010-05-22 14:28:48 +00:00
|
|
|
// Use normal representation, ie. "$group 1", "$group 2"...
|
|
|
|
return $label;
|
|
|
|
}
|
2011-02-22 00:07:21 +00:00
|
|
|
|
2010-05-22 14:28:48 +00:00
|
|
|
if ( isset( $this->mLinkLabels[$group][$offset - 1] ) ) {
|
|
|
|
return $this->mLinkLabels[$group][$offset - 1];
|
|
|
|
} else {
|
|
|
|
// Feed me!
|
2016-05-09 23:36:49 +00:00
|
|
|
return $this->error( 'cite_error_no_link_label_group', [ $group, $message ], 'noparse' );
|
2010-05-22 14:28:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-06 20:38:04 +00:00
|
|
|
/**
|
|
|
|
* Return an id for use in wikitext output based on a key and
|
|
|
|
* optionally the number of it, used in <references>, not <ref>
|
|
|
|
* (since otherwise it would link to itself)
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
*
|
2017-12-28 14:55:49 +00:00
|
|
|
* @param string $key
|
2008-06-06 20:38:04 +00:00
|
|
|
* @param int $num The number of the key
|
|
|
|
* @return string A key for use in wikitext
|
|
|
|
*/
|
2016-09-21 11:01:25 +00:00
|
|
|
private function refKey( $key, $num = null ) {
|
2012-08-30 08:41:27 +00:00
|
|
|
$prefix = wfMessage( 'cite_reference_link_prefix' )->inContentLanguage()->text();
|
|
|
|
$suffix = wfMessage( 'cite_reference_link_suffix' )->inContentLanguage()->text();
|
2011-02-22 00:07:21 +00:00
|
|
|
if ( isset( $num ) ) {
|
2012-08-30 08:41:27 +00:00
|
|
|
$key = wfMessage( 'cite_reference_link_key_with_num', $key, $num )
|
|
|
|
->inContentLanguage()->plain();
|
2011-02-22 00:07:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return "$prefix$key$suffix";
|
2008-06-06 20:38:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an id for use in wikitext output based on a key and
|
|
|
|
* optionally the number of it, used in <ref>, not <references>
|
|
|
|
* (since otherwise it would link to itself)
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
*
|
2017-12-28 14:55:49 +00:00
|
|
|
* @param string $key
|
2008-06-06 20:38:04 +00:00
|
|
|
* @return string A key for use in wikitext
|
|
|
|
*/
|
2016-02-24 21:14:32 +00:00
|
|
|
public static function getReferencesKey( $key ) {
|
2012-08-30 08:41:27 +00:00
|
|
|
$prefix = wfMessage( 'cite_references_link_prefix' )->inContentLanguage()->text();
|
|
|
|
$suffix = wfMessage( 'cite_references_link_suffix' )->inContentLanguage()->text();
|
2011-02-22 00:07:21 +00:00
|
|
|
|
|
|
|
return "$prefix$key$suffix";
|
2008-06-06 20:38:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a link (<sup ...) for the <ref> element from a key
|
|
|
|
* and return XHTML ready for output
|
|
|
|
*
|
2015-10-26 14:44:02 +00:00
|
|
|
* @param string $group
|
|
|
|
* @param string $key The key for the link
|
|
|
|
* @param int $count The index of the key, used for distinguishing
|
2012-03-19 14:59:29 +00:00
|
|
|
* multiple occurrences of the same key
|
2015-10-26 14:44:02 +00:00
|
|
|
* @param int $label The label to use for the link, I want to
|
2008-06-06 20:38:04 +00:00
|
|
|
* use the same label for all occourances of
|
|
|
|
* the same named reference.
|
2015-10-26 14:44:02 +00:00
|
|
|
* @param string $subkey
|
2011-05-28 20:44:24 +00:00
|
|
|
*
|
2008-06-06 20:38:04 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-09-21 11:01:25 +00:00
|
|
|
private function linkRef( $group, $key, $count = null, $label = null, $subkey = '' ) {
|
2008-06-06 20:38:04 +00:00
|
|
|
global $wgContLang;
|
2010-05-22 14:28:48 +00:00
|
|
|
$label = is_null( $label ) ? ++$this->mGroupCnt[$group] : $label;
|
2011-02-22 00:07:21 +00:00
|
|
|
|
2017-12-29 23:28:16 +00:00
|
|
|
return $this->mParser->recursiveTagParse(
|
2012-08-30 08:41:27 +00:00
|
|
|
wfMessage(
|
2008-06-06 20:38:04 +00:00
|
|
|
'cite_reference_link',
|
2017-11-10 01:21:54 +00:00
|
|
|
$this->normalizeKey(
|
2017-09-18 19:17:06 +00:00
|
|
|
$this->refKey( $key, $count )
|
|
|
|
),
|
2017-11-10 01:21:54 +00:00
|
|
|
$this->normalizeKey(
|
2017-09-18 19:17:06 +00:00
|
|
|
self::getReferencesKey( $key . $subkey )
|
|
|
|
),
|
|
|
|
Sanitizer::safeEncodeAttribute(
|
|
|
|
$this->getLinkLabel( $label, $group,
|
|
|
|
( ( $group === self::DEFAULT_GROUP ) ? '' : "$group " ) . $wgContLang->formatNum( $label ) )
|
|
|
|
)
|
2012-08-30 08:41:27 +00:00
|
|
|
)->inContentLanguage()->plain()
|
2008-06-06 20:38:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-11-10 01:21:54 +00:00
|
|
|
/**
|
|
|
|
* Normalizes and sanitizes a reference key
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function normalizeKey( $key ) {
|
|
|
|
$key = Sanitizer::escapeIdForAttribute( $key );
|
|
|
|
$key = Sanitizer::safeEncodeAttribute( $key );
|
|
|
|
|
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
|
2008-06-06 20:38:04 +00:00
|
|
|
/**
|
|
|
|
* This does approximately the same thing as
|
|
|
|
* Language::listToText() but due to this being used for a
|
|
|
|
* slightly different purpose (people might not want , as the
|
2008-09-15 20:28:25 +00:00
|
|
|
* first separator and not 'and' as the second, and this has to
|
2008-06-06 20:38:04 +00:00
|
|
|
* use messages from the content language) I'm rolling my own.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
*
|
|
|
|
* @param array $arr The array to format
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-09-21 11:01:25 +00:00
|
|
|
private function listToText( $arr ) {
|
2008-06-06 20:38:04 +00:00
|
|
|
$cnt = count( $arr );
|
|
|
|
|
2012-08-30 08:41:27 +00:00
|
|
|
$sep = wfMessage( 'cite_references_link_many_sep' )->inContentLanguage()->plain();
|
|
|
|
$and = wfMessage( 'cite_references_link_many_and' )->inContentLanguage()->plain();
|
2008-06-06 20:38:04 +00:00
|
|
|
|
2015-07-07 12:20:53 +00:00
|
|
|
if ( $cnt === 1 ) {
|
2008-06-06 20:38:04 +00:00
|
|
|
// Enforce always returning a string
|
|
|
|
return (string)$arr[0];
|
2011-02-22 00:07:21 +00:00
|
|
|
} else {
|
2008-06-06 20:38:04 +00:00
|
|
|
$t = array_slice( $arr, 0, $cnt - 1 );
|
|
|
|
return implode( $sep, $t ) . $and . $arr[$cnt - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate the labels to pass to the
|
|
|
|
* 'cite_references_link_many_format' message, the format is an
|
2010-05-22 14:28:48 +00:00
|
|
|
* arbitrary number of tokens separated by [\t\n ]
|
2008-06-06 20:38:04 +00:00
|
|
|
*/
|
2016-09-21 11:01:25 +00:00
|
|
|
private function genBacklinkLabels() {
|
2012-08-30 08:41:27 +00:00
|
|
|
$text = wfMessage( 'cite_references_link_many_format_backlink_labels' )
|
|
|
|
->inContentLanguage()->plain();
|
2008-06-06 20:38:04 +00:00
|
|
|
$this->mBacklinkLabels = preg_split( '#[\n\t ]#', $text );
|
|
|
|
}
|
|
|
|
|
2010-05-22 14:28:48 +00:00
|
|
|
/**
|
|
|
|
* Generate the labels to pass to the
|
|
|
|
* 'cite_reference_link' message instead of numbers, the format is an
|
|
|
|
* arbitrary number of tokens separated by [\t\n ]
|
2011-05-28 20:44:24 +00:00
|
|
|
*
|
2015-10-26 14:44:02 +00:00
|
|
|
* @param string $group
|
|
|
|
* @param string $message
|
2010-05-22 14:28:48 +00:00
|
|
|
*/
|
2016-09-21 11:01:25 +00:00
|
|
|
private function genLinkLabels( $group, $message ) {
|
2010-05-22 14:28:48 +00:00
|
|
|
$text = false;
|
2011-01-17 08:04:14 +00:00
|
|
|
$msg = wfMessage( $message )->inContentLanguage();
|
2011-02-22 00:07:21 +00:00
|
|
|
if ( $msg->exists() ) {
|
2011-01-17 08:04:14 +00:00
|
|
|
$text = $msg->plain();
|
2011-02-22 00:07:21 +00:00
|
|
|
}
|
2014-10-01 14:42:18 +00:00
|
|
|
$this->mLinkLabels[$group] = ( !$text ) ? false : preg_split( '#[\n\t ]#', $text );
|
2010-05-22 14:28:48 +00:00
|
|
|
}
|
|
|
|
|
2008-06-06 20:38:04 +00:00
|
|
|
/**
|
|
|
|
* Gets run when Parser::clearState() gets run, since we don't
|
|
|
|
* want the counts to transcend pages and other instances
|
2011-05-28 20:44:24 +00:00
|
|
|
*
|
2017-10-06 19:22:30 +00:00
|
|
|
* @param Parser &$parser
|
2011-05-28 20:44:24 +00:00
|
|
|
*
|
|
|
|
* @return bool
|
2008-06-06 20:38:04 +00:00
|
|
|
*/
|
2016-09-20 13:59:58 +00:00
|
|
|
public function clearState( Parser &$parser ) {
|
2011-05-31 17:49:22 +00:00
|
|
|
if ( $parser->extCite !== $this ) {
|
|
|
|
return $parser->extCite->clearState( $parser );
|
|
|
|
}
|
2011-09-14 15:07:20 +00:00
|
|
|
|
2008-06-16 14:04:39 +00:00
|
|
|
# Don't clear state when we're in the middle of parsing
|
2011-05-31 17:51:30 +00:00
|
|
|
# a <ref> tag
|
2011-05-31 17:49:22 +00:00
|
|
|
if ( $this->mInCite || $this->mInReferences ) {
|
2008-06-16 14:04:39 +00:00
|
|
|
return true;
|
2011-02-22 00:07:21 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 23:36:49 +00:00
|
|
|
$this->mGroupCnt = [];
|
2012-10-17 22:58:55 +00:00
|
|
|
$this->mOutCnt = 0;
|
2011-03-04 11:45:22 +00:00
|
|
|
$this->mCallCnt = 0;
|
2016-05-09 23:36:49 +00:00
|
|
|
$this->mRefs = [];
|
|
|
|
$this->mReferencesErrors = [];
|
|
|
|
$this->mRefCallStack = [];
|
2008-06-06 20:38:04 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-15 00:11:20 +00:00
|
|
|
/**
|
|
|
|
* Gets run when the parser is cloned.
|
|
|
|
*
|
2015-10-26 14:44:02 +00:00
|
|
|
* @param Parser $parser
|
2012-11-15 00:11:20 +00:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-09-20 13:59:58 +00:00
|
|
|
public function cloneState( Parser $parser ) {
|
2012-11-15 00:11:20 +00:00
|
|
|
if ( $parser->extCite !== $this ) {
|
|
|
|
return $parser->extCite->cloneState( $parser );
|
|
|
|
}
|
|
|
|
|
|
|
|
$parser->extCite = clone $this;
|
2016-05-09 23:36:49 +00:00
|
|
|
$parser->setHook( 'ref', [ $parser->extCite, 'ref' ] );
|
|
|
|
$parser->setHook( 'references', [ $parser->extCite, 'references' ] );
|
2013-04-16 19:38:05 +00:00
|
|
|
|
|
|
|
// Clear the state, making sure it will actually work.
|
|
|
|
$parser->extCite->mInCite = false;
|
2015-06-08 14:05:06 +00:00
|
|
|
$parser->extCite->mInReferences = false;
|
2012-11-15 00:11:20 +00:00
|
|
|
$parser->extCite->clearState( $parser );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-01-26 18:02:28 +00:00
|
|
|
/**
|
2015-08-06 17:33:01 +00:00
|
|
|
* Called at the end of page processing to append a default references
|
|
|
|
* section, if refs were used without a main references tag. If there are references
|
|
|
|
* in a custom group, and there is no references tag for it, show an error
|
|
|
|
* message for that group.
|
2014-05-18 23:06:44 +00:00
|
|
|
* If we are processing a section preview, this adds the missing
|
|
|
|
* references tags and does not add the errors.
|
2011-05-28 20:44:24 +00:00
|
|
|
*
|
2015-10-26 14:44:02 +00:00
|
|
|
* @param bool $afterParse True if called from the ParserAfterParse hook
|
2017-10-06 19:22:30 +00:00
|
|
|
* @param Parser &$parser
|
|
|
|
* @param string &$text
|
2011-05-28 20:44:24 +00:00
|
|
|
*
|
|
|
|
* @return bool
|
2009-01-26 18:02:28 +00:00
|
|
|
*/
|
2016-09-20 13:59:58 +00:00
|
|
|
public function checkRefsNoReferences( $afterParse, &$parser, &$text ) {
|
Implement responsive columns for reference lists
This is based on the popular 'count' parameter from Template:Reflist on
English Wikipedia, which has also been adopted by many other wikis.
That template's 'count' parameter allows maximum flexibility on a per-
page basis. This was important because the template can't know how many
references the list will contain. Users typically manually add (and
later, increment) the 'count' parameter when the list exceeds a certain
threshold.
The template currently sets an exact column count (via the CSS3
property `column-count`).
This patch improves on that by instead using the closely related CSS3
`column-width` property. This automatically derives the column count
based on the available space in the browser window. It will thus create
two or three columns on a typical desktop screen, and two or no columns
on a mobile device.
The specified width is the minimum width of a column. This ensures that
the list is not split when rendered on a narrow screen or mobile device.
It also hooks into the raw list before parsing and adds the class only
when the list will contain more than a certain number of items. This
prevents very short lists from being split into multiple columns.
Templates like Template:Reflist on English Wikipedia currently are not
able to set inline styles on the list element directly, which is why
they set it on a `<div>` wrapping the `<references />` output. Because
of this, the feature of the Cite extension must not be enabled at the
same time, as that would result in both the template's wrapper and the
references list being split. The end result would involve sitations with
three columns split in four sub-columns, creating a complicated mess of
nine intermixed columns.
To provide a smooth migration for wikis, this feature can be disabled by
default using `$wgCiteResponsiveReferences = false`. Each individual
template createing reference list can then be migrated, by removing the
wrapper column styles and instead settting the new "responsive"
attribute, like so: `<references responsive />`.
Once any conflicting templates have been migrated, the default for the
wiki can be swapped by setting `$wgCiteResponsiveReferences = true`.
If wikis wish for some templates to keep their custom column splitting
behaviour, templates can also opt-out by setting `responsive="0"`, which
will make sure that it will keep behaving the current way even after the
feature becomes enabled by default for the wiki.
In summary, when disabled by default, pages can opt into this system
with `<references responsive />`. When enabled by default, pages can opt
out of the system with `<references responsive=0 />`.
* Deprecate cite_references_prefix/cite_references_suffix.
This message is rarely used and opens up compatibility hazards.
It was already removed by Parsoid, but the PHP implementation
still had it. It's typically used to add inline styles to the
wrapper which is more appropiately done in Common.css (or
obsoleted as part of the skin or Cite extenion itself nowadays
depending on what style in question).
It was also a HTML-style message with separated open and close
segments, which is an anti-pattern in itself.
* Declare module target explicitly and include mobile. The absence of
this stylesheet caused subtle BiDi/RTL bugs on mobile.
Bug: T33597
Change-Id: Ia535f9b722e825e71e792b36356febc3bd444387
2015-07-21 02:33:50 +00:00
|
|
|
global $wgCiteResponsiveReferences;
|
2015-07-22 14:40:14 +00:00
|
|
|
if ( is_null( $parser->extCite ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
2011-05-31 17:49:22 +00:00
|
|
|
if ( $parser->extCite !== $this ) {
|
2015-06-08 14:05:06 +00:00
|
|
|
return $parser->extCite->checkRefsNoReferences( $afterParse, $parser, $text );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $afterParse ) {
|
|
|
|
$this->mHaveAfterParse = true;
|
|
|
|
} elseif ( $this->mHaveAfterParse ) {
|
|
|
|
return true;
|
2011-05-31 17:49:22 +00:00
|
|
|
}
|
2011-09-14 15:07:20 +00:00
|
|
|
|
2016-01-25 16:52:46 +00:00
|
|
|
if ( !$parser->getOptions()->getIsPreview() ) {
|
|
|
|
// save references data for later use by LinksUpdate hooks
|
|
|
|
if ( $this->mRefs && isset( $this->mRefs[self::DEFAULT_GROUP] ) ) {
|
|
|
|
$this->saveReferencesData();
|
|
|
|
}
|
|
|
|
$isSectionPreview = false;
|
|
|
|
} else {
|
|
|
|
$isSectionPreview = $parser->getOptions()->getIsSectionPreview();
|
|
|
|
}
|
2016-02-06 12:24:58 +00:00
|
|
|
|
|
|
|
$s = '';
|
2009-01-26 18:02:28 +00:00
|
|
|
foreach ( $this->mRefs as $group => $refs ) {
|
2015-07-07 07:37:14 +00:00
|
|
|
if ( !$refs ) {
|
2011-02-22 00:07:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-05-18 23:06:44 +00:00
|
|
|
if ( $group === self::DEFAULT_GROUP || $isSectionPreview ) {
|
Implement responsive columns for reference lists
This is based on the popular 'count' parameter from Template:Reflist on
English Wikipedia, which has also been adopted by many other wikis.
That template's 'count' parameter allows maximum flexibility on a per-
page basis. This was important because the template can't know how many
references the list will contain. Users typically manually add (and
later, increment) the 'count' parameter when the list exceeds a certain
threshold.
The template currently sets an exact column count (via the CSS3
property `column-count`).
This patch improves on that by instead using the closely related CSS3
`column-width` property. This automatically derives the column count
based on the available space in the browser window. It will thus create
two or three columns on a typical desktop screen, and two or no columns
on a mobile device.
The specified width is the minimum width of a column. This ensures that
the list is not split when rendered on a narrow screen or mobile device.
It also hooks into the raw list before parsing and adds the class only
when the list will contain more than a certain number of items. This
prevents very short lists from being split into multiple columns.
Templates like Template:Reflist on English Wikipedia currently are not
able to set inline styles on the list element directly, which is why
they set it on a `<div>` wrapping the `<references />` output. Because
of this, the feature of the Cite extension must not be enabled at the
same time, as that would result in both the template's wrapper and the
references list being split. The end result would involve sitations with
three columns split in four sub-columns, creating a complicated mess of
nine intermixed columns.
To provide a smooth migration for wikis, this feature can be disabled by
default using `$wgCiteResponsiveReferences = false`. Each individual
template createing reference list can then be migrated, by removing the
wrapper column styles and instead settting the new "responsive"
attribute, like so: `<references responsive />`.
Once any conflicting templates have been migrated, the default for the
wiki can be swapped by setting `$wgCiteResponsiveReferences = true`.
If wikis wish for some templates to keep their custom column splitting
behaviour, templates can also opt-out by setting `responsive="0"`, which
will make sure that it will keep behaving the current way even after the
feature becomes enabled by default for the wiki.
In summary, when disabled by default, pages can opt into this system
with `<references responsive />`. When enabled by default, pages can opt
out of the system with `<references responsive=0 />`.
* Deprecate cite_references_prefix/cite_references_suffix.
This message is rarely used and opens up compatibility hazards.
It was already removed by Parsoid, but the PHP implementation
still had it. It's typically used to add inline styles to the
wrapper which is more appropiately done in Common.css (or
obsoleted as part of the skin or Cite extenion itself nowadays
depending on what style in question).
It was also a HTML-style message with separated open and close
segments, which is an anti-pattern in itself.
* Declare module target explicitly and include mobile. The absence of
this stylesheet caused subtle BiDi/RTL bugs on mobile.
Bug: T33597
Change-Id: Ia535f9b722e825e71e792b36356febc3bd444387
2015-07-21 02:33:50 +00:00
|
|
|
$s .= $this->referencesFormat( $group, $wgCiteResponsiveReferences );
|
2009-01-26 18:02:28 +00:00
|
|
|
} else {
|
2016-02-06 12:24:58 +00:00
|
|
|
$s .= "\n<br />" .
|
2017-09-18 19:17:06 +00:00
|
|
|
$this->error(
|
|
|
|
'cite_error_group_refs_without_references',
|
|
|
|
Sanitizer::safeEncodeAttribute( $group )
|
|
|
|
);
|
2009-01-26 18:02:28 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-06 12:24:58 +00:00
|
|
|
if ( $isSectionPreview && $s !== '' ) {
|
2016-02-08 16:02:47 +00:00
|
|
|
// provide a preview of references in its own section
|
|
|
|
$text .= "\n" . '<div class="mw-ext-cite-cite_section_preview_references" >';
|
|
|
|
$headerMsg = wfMessage( 'cite_section_preview_references' );
|
|
|
|
if ( !$headerMsg->isDisabled() ) {
|
|
|
|
$text .= '<h2 id="mw-ext-cite-cite_section_preview_references_header" >'
|
|
|
|
. $headerMsg->escaped()
|
|
|
|
. '</h2>';
|
|
|
|
}
|
|
|
|
$text .= $s . '</div>';
|
2016-02-06 12:24:58 +00:00
|
|
|
} else {
|
|
|
|
$text .= $s;
|
|
|
|
}
|
2009-01-26 18:02:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
2011-09-14 15:07:20 +00:00
|
|
|
|
2016-01-25 16:52:46 +00:00
|
|
|
/**
|
|
|
|
* Saves references in parser extension data
|
|
|
|
* This is called by each <references/> tag, and by checkRefsNoReferences
|
|
|
|
* Assumes $this->mRefs[$group] is set
|
|
|
|
*
|
2017-10-06 19:22:30 +00:00
|
|
|
* @param string $group
|
2016-01-25 16:52:46 +00:00
|
|
|
*/
|
|
|
|
private function saveReferencesData( $group = self::DEFAULT_GROUP ) {
|
|
|
|
global $wgCiteStoreReferencesData;
|
|
|
|
if ( !$wgCiteStoreReferencesData ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$savedRefs = $this->mParser->getOutput()->getExtensionData( self::EXT_DATA_KEY );
|
|
|
|
if ( $savedRefs === null ) {
|
|
|
|
// Initialize array structure
|
2016-05-09 23:36:49 +00:00
|
|
|
$savedRefs = [
|
|
|
|
'refs' => [],
|
2016-01-25 16:52:46 +00:00
|
|
|
'version' => self::DATA_VERSION_NUMBER,
|
2016-05-09 23:36:49 +00:00
|
|
|
];
|
2016-01-25 16:52:46 +00:00
|
|
|
}
|
|
|
|
if ( $this->mBumpRefData ) {
|
|
|
|
// This handles pages with multiple <references/> tags with <ref> tags in between.
|
|
|
|
// On those, a group can appear several times, so we need to avoid overwriting
|
|
|
|
// a previous appearance.
|
2016-05-09 23:36:49 +00:00
|
|
|
$savedRefs['refs'][] = [];
|
2016-01-25 16:52:46 +00:00
|
|
|
$this->mBumpRefData = false;
|
|
|
|
}
|
|
|
|
$n = count( $savedRefs['refs'] ) - 1;
|
|
|
|
// save group
|
|
|
|
$savedRefs['refs'][$n][$group] = $this->mRefs[$group];
|
|
|
|
|
|
|
|
$this->mParser->getOutput()->setExtensionData( self::EXT_DATA_KEY, $savedRefs );
|
|
|
|
}
|
|
|
|
|
2011-02-27 14:50:56 +00:00
|
|
|
/**
|
2014-09-27 16:49:28 +00:00
|
|
|
* Hook for the InlineEditor extension.
|
|
|
|
* If any ref or reference reference tag is in the text,
|
|
|
|
* the entire page should be reparsed, so we return false in that case.
|
2011-05-28 20:44:24 +00:00
|
|
|
*
|
2017-10-06 19:22:30 +00:00
|
|
|
* @param string &$output
|
2011-09-14 15:07:20 +00:00
|
|
|
*
|
2011-05-28 20:44:24 +00:00
|
|
|
* @return bool
|
2011-02-27 14:50:56 +00:00
|
|
|
*/
|
2016-09-20 13:59:58 +00:00
|
|
|
public function checkAnyCalls( &$output ) {
|
2011-05-31 17:49:22 +00:00
|
|
|
global $wgParser;
|
|
|
|
/* InlineEditor always uses $wgParser */
|
|
|
|
return ( $wgParser->extCite->mCallCnt <= 0 );
|
2011-02-27 14:50:56 +00:00
|
|
|
}
|
2009-01-26 18:02:28 +00:00
|
|
|
|
2008-06-06 20:38:04 +00:00
|
|
|
/**
|
|
|
|
* Initialize the parser hooks
|
2011-05-28 20:44:24 +00:00
|
|
|
*
|
2015-10-26 14:44:02 +00:00
|
|
|
* @param Parser $parser
|
2011-05-28 20:44:24 +00:00
|
|
|
*
|
|
|
|
* @return bool
|
2008-06-06 20:38:04 +00:00
|
|
|
*/
|
2016-09-20 13:59:58 +00:00
|
|
|
public static function setHooks( Parser $parser ) {
|
2011-02-26 22:39:53 +00:00
|
|
|
global $wgHooks;
|
2009-02-11 06:34:39 +00:00
|
|
|
|
2011-05-31 17:49:22 +00:00
|
|
|
$parser->extCite = new self();
|
2011-09-14 15:07:20 +00:00
|
|
|
|
2017-07-23 07:10:18 +00:00
|
|
|
if ( !self::$hooksInstalled ) {
|
2016-05-09 23:36:49 +00:00
|
|
|
$wgHooks['ParserClearState'][] = [ $parser->extCite, 'clearState' ];
|
|
|
|
$wgHooks['ParserCloned'][] = [ $parser->extCite, 'cloneState' ];
|
|
|
|
$wgHooks['ParserAfterParse'][] = [ $parser->extCite, 'checkRefsNoReferences', true ];
|
|
|
|
$wgHooks['ParserBeforeTidy'][] = [ $parser->extCite, 'checkRefsNoReferences', false ];
|
|
|
|
$wgHooks['InlineEditorPartialAfterParse'][] = [ $parser->extCite, 'checkAnyCalls' ];
|
2017-07-23 07:10:18 +00:00
|
|
|
self::$hooksInstalled = true;
|
2011-02-26 22:39:53 +00:00
|
|
|
}
|
2016-05-09 23:36:49 +00:00
|
|
|
$parser->setHook( 'ref', [ $parser->extCite, 'ref' ] );
|
|
|
|
$parser->setHook( 'references', [ $parser->extCite, 'references' ] );
|
2011-02-26 22:39:53 +00:00
|
|
|
|
|
|
|
return true;
|
2008-06-06 20:38:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an error message based on an error ID
|
|
|
|
*
|
|
|
|
* @param string $key Message name for the error
|
2017-12-25 13:17:20 +00:00
|
|
|
* @param string[]|string|null $param Parameter to pass to the message
|
2011-11-14 12:22:07 +00:00
|
|
|
* @param string $parse Whether to parse the message ('parse') or not ('noparse')
|
|
|
|
* @return string XHTML or wikitext ready for output
|
2008-06-06 20:38:04 +00:00
|
|
|
*/
|
2016-09-21 11:01:25 +00:00
|
|
|
private function error( $key, $param = null, $parse = 'parse' ) {
|
2016-01-06 20:48:33 +00:00
|
|
|
# For ease of debugging and because errors are rare, we
|
|
|
|
# use the user language and split the parser cache.
|
|
|
|
$lang = $this->mParser->getOptions()->getUserLangObj();
|
2015-10-08 20:38:01 +00:00
|
|
|
$dir = $lang->getDir();
|
|
|
|
|
|
|
|
# We rely on the fact that PHP is okay with passing unused argu-
|
|
|
|
# ments to functions. If $1 is not used in the message, wfMessage will
|
|
|
|
# just ignore the extra parameter.
|
2016-01-06 20:48:33 +00:00
|
|
|
$msg = wfMessage(
|
2016-02-18 06:50:04 +00:00
|
|
|
'cite_error',
|
2016-01-06 20:48:33 +00:00
|
|
|
wfMessage( $key, $param )->inLanguage( $lang )->plain()
|
|
|
|
)
|
|
|
|
->inLanguage( $lang )
|
2014-09-27 16:49:28 +00:00
|
|
|
->plain();
|
|
|
|
|
2016-02-18 06:50:04 +00:00
|
|
|
$this->mParser->addTrackingCategory( 'cite-tracking-category-cite-error' );
|
|
|
|
|
2015-10-08 20:38:01 +00:00
|
|
|
$ret = Html::rawElement(
|
|
|
|
'span',
|
2016-05-09 23:36:49 +00:00
|
|
|
[
|
2015-10-08 20:38:01 +00:00
|
|
|
'class' => 'error mw-ext-cite-error',
|
|
|
|
'lang' => $lang->getHtmlCode(),
|
|
|
|
'dir' => $dir,
|
2016-05-09 23:36:49 +00:00
|
|
|
],
|
2015-10-08 20:38:01 +00:00
|
|
|
$msg
|
|
|
|
);
|
2015-07-05 12:32:44 +00:00
|
|
|
|
2016-02-18 06:50:04 +00:00
|
|
|
if ( $parse === 'parse' ) {
|
|
|
|
$ret = $this->mParser->recursiveTagParse( $ret );
|
2014-05-18 23:06:44 +00:00
|
|
|
}
|
2014-09-27 16:49:28 +00:00
|
|
|
|
2016-02-18 06:50:04 +00:00
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a warning message based on a warning ID
|
|
|
|
*
|
|
|
|
* @param string $key Message name for the warning. Name should start with cite_warning_
|
|
|
|
* @param string|null $param Parameter to pass to the message
|
|
|
|
* @param string $parse Whether to parse the message ('parse') or not ('noparse')
|
|
|
|
* @return string XHTML or wikitext ready for output
|
|
|
|
*/
|
2016-09-21 11:01:25 +00:00
|
|
|
private function warning( $key, $param = null, $parse = 'parse' ) {
|
2016-02-18 06:50:04 +00:00
|
|
|
# For ease of debugging and because errors are rare, we
|
|
|
|
# use the user language and split the parser cache.
|
|
|
|
$lang = $this->mParser->getOptions()->getUserLangObj();
|
2015-10-08 20:38:01 +00:00
|
|
|
$dir = $lang->getDir();
|
|
|
|
|
|
|
|
# We rely on the fact that PHP is okay with passing unused argu-
|
|
|
|
# ments to functions. If $1 is not used in the message, wfMessage will
|
|
|
|
# just ignore the extra parameter.
|
2016-02-18 06:50:04 +00:00
|
|
|
$msg = wfMessage(
|
|
|
|
'cite_warning',
|
|
|
|
wfMessage( $key, $param )->inLanguage( $lang )->plain()
|
|
|
|
)
|
|
|
|
->inLanguage( $lang )
|
|
|
|
->plain();
|
|
|
|
|
|
|
|
$key = preg_replace( '/^cite_warning_/', '', $key ) . '';
|
2015-10-08 20:38:01 +00:00
|
|
|
$ret = Html::rawElement(
|
|
|
|
'span',
|
2016-05-09 23:36:49 +00:00
|
|
|
[
|
2015-10-08 20:38:01 +00:00
|
|
|
'class' => 'warning mw-ext-cite-warning mw-ext-cite-warning-' .
|
|
|
|
Sanitizer::escapeClass( $key ),
|
|
|
|
'lang' => $lang->getHtmlCode(),
|
|
|
|
'dir' => $dir,
|
2016-05-09 23:36:49 +00:00
|
|
|
],
|
2015-10-08 20:38:01 +00:00
|
|
|
$msg
|
|
|
|
);
|
2016-02-18 06:50:04 +00:00
|
|
|
|
2015-07-07 12:20:53 +00:00
|
|
|
if ( $parse === 'parse' ) {
|
2014-06-05 17:51:48 +00:00
|
|
|
$ret = $this->mParser->recursiveTagParse( $ret );
|
2011-11-14 12:22:07 +00:00
|
|
|
}
|
2014-09-27 16:49:28 +00:00
|
|
|
|
2011-11-14 12:22:07 +00:00
|
|
|
return $ret;
|
2008-06-06 20:38:04 +00:00
|
|
|
}
|
|
|
|
|
2016-01-25 16:52:46 +00:00
|
|
|
/**
|
|
|
|
* Fetch references stored for the given title in page_props
|
|
|
|
* For performance, results are cached
|
|
|
|
*
|
|
|
|
* @param Title $title
|
|
|
|
* @return array|false
|
|
|
|
*/
|
|
|
|
public static function getStoredReferences( Title $title ) {
|
|
|
|
global $wgCiteStoreReferencesData;
|
|
|
|
if ( !$wgCiteStoreReferencesData ) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-12-25 13:17:20 +00:00
|
|
|
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
|
2016-01-25 16:52:46 +00:00
|
|
|
$key = $cache->makeKey( self::EXT_DATA_KEY, $title->getArticleID() );
|
|
|
|
return $cache->getWithSetCallback(
|
|
|
|
$key,
|
|
|
|
self::CACHE_DURATION_ONFETCH,
|
|
|
|
function ( $oldValue, &$ttl, array &$setOpts ) use ( $title ) {
|
2017-09-02 00:06:37 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
2016-01-25 16:52:46 +00:00
|
|
|
$setOpts += Database::getCacheSetOptions( $dbr );
|
|
|
|
return self::recursiveFetchRefsFromDB( $title, $dbr );
|
|
|
|
},
|
2016-05-09 23:36:49 +00:00
|
|
|
[
|
|
|
|
'checkKeys' => [ $key ],
|
2016-01-25 16:52:46 +00:00
|
|
|
'lockTSE' => 30,
|
2016-05-09 23:36:49 +00:00
|
|
|
]
|
2016-01-25 16:52:46 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reconstructs compressed json by successively retrieving the properties references-1, -2, etc
|
|
|
|
* It attempts the next step when a decoding error occurs.
|
|
|
|
* Returns json_decoded uncompressed string, with validation of json
|
|
|
|
*
|
|
|
|
* @param Title $title
|
2017-12-22 20:28:29 +00:00
|
|
|
* @param IDatabase $dbr
|
2016-01-25 16:52:46 +00:00
|
|
|
* @param string $string
|
|
|
|
* @param int $i
|
|
|
|
* @return array|false
|
|
|
|
*/
|
2017-12-22 20:28:29 +00:00
|
|
|
private static function recursiveFetchRefsFromDB( Title $title, IDatabase $dbr,
|
2016-01-25 16:52:46 +00:00
|
|
|
$string = '', $i = 1 ) {
|
|
|
|
$id = $title->getArticleID();
|
|
|
|
$result = $dbr->selectField(
|
|
|
|
'page_props',
|
|
|
|
'pp_value',
|
2016-05-09 23:36:49 +00:00
|
|
|
[
|
2016-01-25 16:52:46 +00:00
|
|
|
'pp_page' => $id,
|
|
|
|
'pp_propname' => 'references-' . $i
|
2016-05-09 23:36:49 +00:00
|
|
|
],
|
2016-01-25 16:52:46 +00:00
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
if ( $result !== false ) {
|
|
|
|
$string .= $result;
|
|
|
|
$decodedString = gzdecode( $string );
|
|
|
|
if ( $decodedString !== false ) {
|
|
|
|
$json = json_decode( $decodedString, true );
|
|
|
|
if ( json_last_error() === JSON_ERROR_NONE ) {
|
|
|
|
return $json;
|
|
|
|
}
|
|
|
|
// corrupted json ?
|
|
|
|
// shouldn't happen since when string is truncated, gzdecode should fail
|
|
|
|
wfDebug( "Corrupted json detected when retrieving stored references for title id $id" );
|
|
|
|
}
|
|
|
|
// if gzdecode fails, try to fetch next references- property value
|
|
|
|
return self::recursiveFetchRefsFromDB( $title, $dbr, $string, ++$i );
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// no refs stored in page_props at this index
|
|
|
|
if ( $i > 1 ) {
|
|
|
|
// shouldn't happen
|
|
|
|
wfDebug( "Failed to retrieve stored references for title id $id" );
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-06 20:38:04 +00:00
|
|
|
}
|