Jackmcbarn has uploaded a new change for review.
https://gerrit.wikimedia.org/r/214116
Change subject: Parse references as we go
......................................................................
Parse references as we go
Parse each <ref> tag as we process it, rather than saving them all up
for when the <references> tag is processed. This is necessary to make
nested references work correctly.
Bug: T100477
Change-Id: I020ece804b37c93c91b3aaefa7dc14656ee093d6
---
M Cite_body.php
M extension.json
2 files changed, 63 insertions(+), 72 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Cite
refs/changes/16/214116/1
diff --git a/Cite_body.php b/Cite_body.php
index e5f0a5a..662f51f 100644
--- a/Cite_body.php
+++ b/Cite_body.php
@@ -101,12 +101,12 @@
public $mParser;
/**
- * True when a <ref> tag is being processed.
+ * The number of <ref> tags we are deep
* Used to avoid infinite recursion
*
- * @var boolean
+ * @var int
*/
- public $mInCite = false;
+ public $mInCite = 0;
const NOT_IN_REFERENCES = 0;
@@ -146,6 +146,20 @@
/**#@+ @access private */
/**
+ * Convenience function for parsing and unstripping a string.
+ *
+ * @param string $str
+ *
+ * @return string
+ */
+ function parseAndUnstrip( $str ) {
+ if ( $str === null ) {
+ return null;
+ }
+ return $this->mParser->mStripState->unstripGeneral(
$this->mParser->recursiveTagParse( $str ) );
+ }
+
+ /**
* Callback function for <ref> that actually does the work.
*
* @param $str string Input
@@ -159,16 +173,16 @@
// However, the parser might be set to HTML when we're called,
but then
// not set that way when we're unstripped. If this happens,
treat it as
// if we were already in a reference, and bail out.
- if ( $this->mInCite || !$parser->ot['html'] ) {
+ if ( !$parser->ot['html'] ) {
return htmlspecialchars( "<ref>$str</ref>" );
}
$this->mCallCnt++;
- $this->mInCite = true;
+ $this->mInCite++;
$ret = $this->guardedRef( $str, $argv, $parser );
- $this->mInCite = false;
+ $this->mInCite--;
$parserOutput = $parser->getOutput();
$parserOutput->addModules( 'ext.cite' );
@@ -241,7 +255,7 @@
$this->error(
'cite_error_references_missing_key', $key );
} else {
# Assign the text to corresponding ref
- $this->mRefs[$group][$key]['text'] =
$str;
+ $this->mRefs[$group][$key]['text'] =
$this->parseAndUnstrip( $str );
}
} else {
# <ref> called in <references> has no content.
@@ -398,7 +412,7 @@
if ( $follow != null ) {
if ( isset( $this->mRefs[$group][$follow] ) &&
is_array( $this->mRefs[$group][$follow] ) ) {
// add text to the note that is being followed
- $this->mRefs[$group][$follow]['text'] =
$this->mRefs[$group][$follow]['text'] . ' ' . $str;
+ $this->mRefs[$group][$follow]['text'] =
$this->mRefs[$group][$follow]['text'] . ' ' . $this->parseAndUnstrip( $str );
} else {
// insert part of note at the beginning of the
group
$groupsCount = count( $this->mRefs[$group] );
@@ -409,9 +423,9 @@
}
array_splice( $this->mRefs[$group], $k, 0,
array( array( 'count' => - 1,
- 'text' => $str,
'key' => ++$this->mOutCnt ,
'follow' => $follow ) ) );
+ $this->mRefs[$group][$k]['text'] =
$this->parseAndUnstrip( $str );
}
// return an empty string : this is not a reference
return '';
@@ -419,19 +433,23 @@
if ( $key === null ) {
// No key
// $this->mRefs[$group][] = $str;
- $this->mRefs[$group][] = array( 'count' => - 1, 'text'
=> $str, 'key' => ++$this->mOutCnt );
-
- return $this->linkRef( $group, $this->mOutCnt );
+ $arr = array( 'count' => -1, 'key' => ++$this->mOutCnt
);
+ $this->mRefs[$group][] =& $arr;
+ // When linkRef is called with less than 4 parameters,
it has side effects,
+ // so make sure we do it before we call parseAndUnstrip
+ $ret = $this->linkRef( $group, $this->mOutCnt );
+ $arr['text'] = $this->parseAndUnstrip( $str );
+ return $ret;
} elseif ( is_string( $key ) ) {
// Valid key
if ( !isset( $this->mRefs[$group][$key] ) || !is_array(
$this->mRefs[$group][$key] ) ) {
// First occurrence
$this->mRefs[$group][$key] = array(
- 'text' => $str,
'count' => 0,
'key' => ++$this->mOutCnt,
'number' => ++$this->mGroupCnt[$group]
);
+ $this->mRefs[$group][$key]['text'] =
$this->parseAndUnstrip( $str );
return
$this->linkRef(
@@ -445,7 +463,7 @@
// 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;
+ $this->mRefs[$group][$key]['text'] =
$this->parseAndUnstrip( $str );
}
return
$this->linkRef(
@@ -528,7 +546,7 @@
# Parse $str to process any unparsed <ref> tags.
$this->mInReferences = Cite::PARSING_REFERENCES;
- $parser->mStripState->unstripGeneral(
$parser->recursiveTagParse( $str ) );
+ $this->parseAndUnstrip( $str );
$this->mInReferences = Cite::RUNNING_REFERENCES;
}
@@ -563,55 +581,23 @@
return '';
}
- wfProfileIn( __METHOD__ . '-entries' );
$ent = array();
foreach ( $this->mRefs[$group] as $k => $v ) {
$ent[] = $this->referencesFormatEntry( $k, $v );
}
- $prefix = wfMessage( 'cite_references_prefix'
)->inContentLanguage()->plain();
- $suffix = wfMessage( 'cite_references_suffix'
)->inContentLanguage()->plain();
- $content = implode( "\n", $ent );
-
- // Prepare the parser input.
- // We add new lines between the pieces to avoid a confused tidy
(bug 13073).
- $parserInput = $prefix . "\n" . $content . "\n" . $suffix;
-
- // Let's try to cache it.
- global $wgMemc;
- $cacheKey = wfMemcKey( 'citeref', md5( $parserInput ),
$this->mParser->Title()->getArticleID() );
-
- wfProfileOut( __METHOD__ . '-entries' );
-
- global $wgCiteCacheReferences;
- $data = false;
- if ( $wgCiteCacheReferences ) {
- wfProfileIn( __METHOD__ . '-cache-get' );
- $data = $wgMemc->get( $cacheKey );
- wfProfileOut( __METHOD__ . '-cache-get' );
- }
-
- if ( !$data || !$this->mParser->isValidHalfParsedText( $data )
) {
- wfProfileIn( __METHOD__ . '-parse' );
-
- // Live hack: parse() adds two newlines on WM, can't
reproduce it locally -ævar
- $ret = rtrim( $this->mParser->recursiveTagParse(
$parserInput ), "\n" );
-
- if ( $wgCiteCacheReferences ) {
- $serData =
$this->mParser->serializeHalfParsedText( $ret );
- $wgMemc->set( $cacheKey, $serData, 86400 );
- }
-
- wfProfileOut( __METHOD__ . '-parse' );
- } else {
- $ret = $this->mParser->unserializeHalfParsedText( $data
);
- }
-
- // done, clean up so we can reuse the group
+ // clean up so we can reuse the group
unset( $this->mRefs[$group] );
unset( $this->mGroupCnt[$group] );
- return $ret;
+ $prefix = wfMessage( 'cite_references_prefix'
)->inContentLanguage()->plain();
+ $suffix = wfMessage( 'cite_references_suffix'
)->inContentLanguage()->plain();
+
+ // We add new lines between the pieces to avoid a confused tidy
(bug 13073).
+ $m = new RawMessage( "$1\n$2\n$3" );
+
+ // Live hack: parse() adds two newlines on WM, can't reproduce
it locally -ævar
+ return rtrim( $m->params( $prefix )->rawParams( implode( "\n",
$ent ) )->params( $suffix )->parse(), "\n" );
}
/**
@@ -628,24 +614,27 @@
return wfMessage(
'cite_references_link_one',
$this->referencesKey( $key ),
- $this->refKey( $key ),
+ $this->refKey( $key )
+ )->rawParams(
$this->referenceText( $key, $val )
- )->inContentLanguage()->plain();
+ )->inContentLanguage()->parse();
}
$text = $this->referenceText( $key, $val['text'] );
if ( isset( $val['follow'] ) ) {
return wfMessage(
'cite_references_no_link',
- $this->referencesKey( $val['follow'] ),
+ $this->referencesKey( $val['follow'] )
+ )->rawParams(
$text
- )->inContentLanguage()->plain();
+ )->inContentLanguage()->parse();
} elseif ( !isset( $val['text'] ) ) {
return wfMessage(
'cite_references_link_one',
$this->referencesKey( $key ),
- $this->refKey( $key,
$val['count'] ),
+ $this->refKey( $key,
$val['count'] )
+ )->rawParams(
$text
- )->inContentLanguage()->plain();
+ )->inContentLanguage()->parse();
}
if ( $val['count'] < 0 ) {
@@ -653,9 +642,10 @@
'cite_references_link_one',
$this->referencesKey( $val['key'] ),
# $this->refKey( $val['key'],
$val['count'] ),
- $this->refKey( $val['key'] ),
+ $this->refKey( $val['key'] )
+ )->rawParams(
$text
- )->inContentLanguage()->plain();
+ )->inContentLanguage()->parse();
// 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
@@ -665,9 +655,10 @@
'cite_references_link_one',
$this->referencesKey( $key . "-" .
$val['key'] ),
# $this->refKey( $key, $val['count'] ),
- $this->refKey( $key, $val['key'] . "-"
. $val['count'] ),
+ $this->refKey( $key, $val['key'] . "-"
. $val['count'] )
+ )->rawParams(
$text
- )->inContentLanguage()->plain();
+ )->inContentLanguage()->parse();
// Named references with >1 occurrences
} else {
$links = array();
@@ -685,9 +676,10 @@
return wfMessage( 'cite_references_link_many',
$this->referencesKey( $key . "-" .
$val['key'] ),
- $list,
+ $list
+ )->rawParams(
$text
- )->inContentLanguage()->plain();
+ )->inContentLanguage()->parse();
}
}
@@ -699,7 +691,7 @@
*/
function referenceText( $key, $text ) {
if ( !isset( $text ) || $text === '' ) {
- return $this->error( 'cite_error_references_no_text',
$key, 'noparse' );
+ return $this->error( 'cite_error_references_no_text',
$key );
}
return '<span class="reference-text">' . rtrim( $text, "\n" ) .
"</span>\n";
}
@@ -952,7 +944,7 @@
$parser->setHook( 'references', array( $parser->extCite,
'references' ) );
// Clear the state, making sure it will actually work.
- $parser->extCite->mInCite = false;
+ $parser->extCite->mInCite = 0;
$parser->extCite->mInReferences = Cite::NOT_IN_REFERENCES;
$parser->extCite->clearState( $parser );
diff --git a/extension.json b/extension.json
index ff1d444..aacb24a 100644
--- a/extension.json
+++ b/extension.json
@@ -40,8 +40,7 @@
"remoteExtPath": "Cite/modules"
},
"config": {
- "AllowCiteGroups": true,
- "CiteCacheReferences": false
+ "AllowCiteGroups": true
},
"AutoloadClasses": {
"Cite": "Cite_body.php"
--
To view, visit https://gerrit.wikimedia.org/r/214116
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I020ece804b37c93c91b3aaefa7dc14656ee093d6
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Cite
Gerrit-Branch: master
Gerrit-Owner: Jackmcbarn <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits