r32256 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r32255‎ | r32256 | r32257 >
Date:21:26, 20 March 2008
Author:sanbeg
Status:old
Tags:
Comment:
add "group" parameter to allow multiple reference/footnote groups
Modified paths:
  • /trunk/extensions/Cite/Cite.php (modified) (history)
  • /trunk/extensions/Cite/citeParserTests.txt (modified) (history)

Diff [purge]

Index: trunk/extensions/Cite/Cite.php
@@ -30,6 +30,13 @@
3131 $wgParserTestFiles[] = dirname( __FILE__ ) . "/citeParserTests.txt";
3232 $wgExtensionMessagesFiles['Cite'] = dirname( __FILE__ ) . "/Cite.i18n.php";
3333
 34+define( 'CITE_DEFAULT_GROUP', '');
 35+/**
 36+ * The emergency shut-off switch. Override in local settings to disable
 37+ * groups; or remove all references from this file to enable unconditionally
 38+ */
 39+$wgAllowCiteGroups = true;
 40+
3441 function wfCite() {
3542 class Cite {
3643 /**#@+
@@ -75,6 +82,7 @@
7683 * @var int
7784 */
7885 var $mOutCnt = 0;
 86+ var $mGroupCnt = array();
7987
8088 /**
8189 * Internal counter for anonymous references, seperate from
@@ -137,11 +145,11 @@
138146 }
139147 }
140148
141 - function guardedRef( $str, $argv, $parser ) {
 149+ function guardedRef( $str, $argv, $parser, $default_group=CITE_DEFAULT_GROUP ) {
142150 $this->mParser = $parser;
143151
144152 # The key here is the "name" attribute.
145 - $key = $this->refArg( $argv );
 153+ list($key,$group) = $this->refArg( $argv );
146154
147155 if( $str === '' ) {
148156 # <ref ...></ref>. This construct is always invalid: either
@@ -167,6 +175,11 @@
168176 # (and would produce weird id's anyway).
169177 return $this->error( 'cite_error_ref_numeric_key' );
170178 }
 179+
 180+ #Split these into groups.
 181+ if( $group === null ) {
 182+ $group = $default_group;
 183+ }
171184
172185 if( is_string( $key ) or is_string( $str ) ) {
173186 # We don't care about the content: if the key exists, the ref
@@ -174,7 +187,7 @@
175188 # fers to an existing one. If it refers to a nonexistent ref,
176189 # we'll figure that out later. Likewise it's definitely valid
177190 # if there's any content, regardless of key.
178 - return $this->stack( $str, $key );
 191+ return $this->stack( $str, $key, $group );
179192 }
180193
181194 # Not clear how we could get here, but something is probably
@@ -192,21 +205,36 @@
193206 * input and null on no input
194207 */
195208 function refArg( $argv ) {
 209+ global $wgAllowCiteGroups;
196210 $cnt = count( $argv );
197211
198 - if ( $cnt > 1 )
199 - // There should only be one key
 212+ if ( $cnt > 2 )
 213+ // There should only be one key and one group
200214 return false;
201 - else if ( $cnt == 1 )
202 - if ( isset( $argv['name'] ) )
 215+ else if ( $cnt >= 1 ) {
 216+ if ( isset( $argv['name'] ) ) {
203217 // Key given.
204 - return $this->validateName( array_shift( $argv ) );
 218+ $key = $this->validateName( $argv['name'] );
 219+ unset( $argv['name']);
 220+ --$cnt;
 221+ }
 222+ if ( isset( $argv['group'] ) ){
 223+ if (! $wgAllowCiteGroups ) return array(false); //remove when groups are fully tested.
 224+ // Group given.
 225+ $group = $this->validateName( $argv['group'] );
 226+ unset( $argv['group']);
 227+ --$cnt;
 228+ }
 229+
 230+ if ( $cnt == 0)
 231+ return array ($key,$group);
205232 else
206233 // Invalid key
207 - return false;
 234+ return array(false);
 235+ }
208236 else
209237 // No key
210 - return null;
 238+ return array(null,$group);
211239 }
212240
213241 /**
@@ -242,38 +270,47 @@
243271 * @param mixed $key Argument to the <ref> tag as returned by $this->refArg()
244272 * @return string
245273 */
246 - function stack( $str, $key = null ) {
 274+ function stack( $str, $key = null, $group ) {
247275 if ( $key === null ) {
248276 // No key
249 - $this->mRefs[] = $str;
250 - return $this->linkRef( $this->mInCnt++ );
251 - } else if ( is_string( $key ) )
 277+ //$this->mRefs[$group][] = $str;
 278+ $this->mRefs[$group][] = array('count'=>-1, 'text'=>$str, 'key'=>++$this->mOutCnt);
 279+
 280+ return $this->linkRef( $group, $this->mInCnt++ );
 281+ } else if ( is_string( $key ) ) {
252282 // Valid key
253 - if ( ! isset( $this->mRefs[$key] ) || ! is_array( $this->mRefs[$key] ) ) {
254 - // First occourance
255 - $this->mRefs[$key] = array(
 283+ if ( ! isset( $this->mRefs[$group][$key] ) || ! is_array( $this->mRefs[$group][$key] ) ) {
 284+ // First occurance
 285+ $this->mRefs[$group][$key] = array(
256286 'text' => $str,
257287 'count' => 0,
258 - 'number' => ++$this->mOutCnt
 288+ 'key' => ++$this->mOutCnt,
 289+ 'number' => ++$this->mGroupCnt[$group]
259290 );
260291 return
261292 $this->linkRef(
 293+ $group,
262294 $key,
263 - $this->mRefs[$key]['count'],
264 - $this->mRefs[$key]['number']
 295+ $this->mRefs[$group][$key]['key']."-".$this->mRefs[$group][$key]['count'],
 296+ $this->mRefs[$group][$key]['number'],
 297+ "-".$this->mRefs[$group][$key]['key']
265298 );
266299 } else {
267300 // We've been here before
268 - if ( $this->mRefs[$key]['text'] === null && $str !== '' ) {
 301+ if ( $this->mRefs[$group][$key]['text'] === null && $str !== '' ) {
269302 // If no text found before, use this text
270 - $this->mRefs[$key]['text'] = $str;
 303+ $this->mRefs[$group][$key]['text'] = $str;
271304 };
272305 return
273306 $this->linkRef(
 307+ $group,
274308 $key,
275 - ++$this->mRefs[$key]['count'],
276 - $this->mRefs[$key]['number']
 309+ $this->mRefs[$group][$key]['key']."-".++$this->mRefs[$group][$key]['count'],
 310+ $this->mRefs[$group][$key]['number'],
 311+ "-".$this->mRefs[$group][$key]['key']
277312 ); }
 313+ }
 314+
278315 else
279316 $this->croak( 'cite_error_stack_invalid_input', serialize( array( $key, $str ) ) );
280317 }
@@ -300,15 +337,26 @@
301338 return $ret;
302339 }
303340 }
304 -
305 - function guardedReferences( $str, $argv, $parser ) {
 341+
 342+ function guardedReferences( $str, $argv, $parser, $group = CITE_DEFAULT_GROUP ) {
 343+ global $wgAllowCiteGroups;
 344+
306345 $this->mParser = $parser;
 346+
307347 if ( $str !== null )
308348 return $this->error( 'cite_error_references_invalid_input' );
309 - else if ( count( $argv ) )
 349+
 350+
 351+ if ( isset( $argv['group'] ) and $wgAllowCiteGroups) {
 352+ $group = $argv['group'];
 353+ unset ($argv['group']);
 354+
 355+ }
 356+
 357+ if ( count( $argv ) )
310358 return $this->error( 'cite_error_references_invalid_parameters' );
311359 else
312 - return $this->referencesFormat();
 360+ return $this->referencesFormat($group);
313361 }
314362
315363 /**
@@ -316,14 +364,14 @@
317365 *
318366 * @return string XHTML ready for output
319367 */
320 - function referencesFormat() {
321 - if ( count( $this->mRefs ) == 0 )
 368+ function referencesFormat($group) {
 369+ if (( count( $this->mRefs ) == 0 ) or (count( $this->mRefs[$group] ) == 0 ))
322370 return '';
323371
324372 wfProfileIn( __METHOD__ );
325373 wfProfileIn( __METHOD__ .'-entries' );
326374 $ent = array();
327 - foreach ( $this->mRefs as $k => $v )
 375+ foreach ( $this->mRefs[$group] as $k => $v )
328376 $ent[] = $this->referencesFormatEntry( $k, $v );
329377
330378 $prefix = wfMsgForContentNoTrans( 'cite_references_prefix' );
@@ -365,6 +413,16 @@
366414 $this->refKey( $key, $val['count'] ),
367415 $this->error( 'cite_error_references_no_text', $key )
368416 );
 417+ if ( $val['count'] < 0 )
 418+ return
 419+ wfMsgForContentNoTrans(
 420+ 'cite_references_link_one',
 421+ $this->referencesKey( $val['key'] ),
 422+ #$this->refKey( $val['key'], $val['count'] ),
 423+ $this->refKey( $val['key'] ),
 424+
 425+ ( $val['text'] != '' ? $val['text'] : $this->error( 'cite_error_references_no_text', $key ) )
 426+ );
369427 // Standalone named reference, I want to format this like an
370428 // anonymous reference because displaying "1. 1.1 Ref text" is
371429 // overkill and users frequently use named references when they
@@ -373,18 +431,19 @@
374432 return
375433 wfMsgForContentNoTrans(
376434 'cite_references_link_one',
377 - $this->referencesKey( $key ),
378 - $this->refKey( $key, $val['count'] ),
 435+ $this->referencesKey( $key ."-" . $val['key'] ),
 436+ #$this->refKey( $key, $val['count'] ),
 437+ $this->refKey( $key, $val['key']."-".$val['count'] ),
379438 ( $val['text'] != '' ? $val['text'] : $this->error( 'cite_error_references_no_text', $key ) )
380439 );
381440 // Named references with >1 occurrences
382441 else {
383442 $links = array();
384 -
 443+//for group handling, we have an extra key here.
385444 for ( $i = 0; $i <= $val['count']; ++$i ) {
386445 $links[] = wfMsgForContentNoTrans(
387446 'cite_references_link_many_format',
388 - $this->refKey( $key, $i ),
 447+ $this->refKey( $key, $val['key']."-$i" ),
389448 $this->referencesFormatEntryNumericBacklinkLabel( $val['number'], $i, $val['count'] ),
390449 $this->referencesFormatEntryAlternateBacklinkLabel( $i )
391450 );
@@ -394,7 +453,7 @@
395454
396455 return
397456 wfMsgForContentNoTrans( 'cite_references_link_many',
398 - $this->referencesKey( $key ),
 457+ $this->referencesKey( $key ."-" . $val['key'] ),
399458 $list,
400459 ( $val['text'] != '' ? $val['text'] : $this->error( 'cite_error_references_no_text', $key ) )
401460 );
@@ -446,7 +505,7 @@
447506
448507 /**
449508 * Return an id for use in wikitext output based on a key and
450 - * optionally the # of it, used in <references>, not <ref>
 509+ * optionally the number of it, used in <references>, not <ref>
451510 * (since otherwise it would link to itself)
452511 *
453512 * @static
@@ -466,7 +525,7 @@
467526
468527 /**
469528 * Return an id for use in wikitext output based on a key and
470 - * optionally the # of it, used in <ref>, not <references>
 529+ * optionally the number of it, used in <ref>, not <references>
471530 * (since otherwise it would link to itself)
472531 *
473532 * @static
@@ -489,23 +548,22 @@
490549 * and return XHTML ready for output
491550 *
492551 * @param string $key The key for the link
493 - * @param int $count The # of the key, used for distinguishing
494 - * multiple occourances of the same key
 552+ * @param int $count The index of the key, used for distinguishing
 553+ * multiple occurances of the same key
495554 * @param int $label The label to use for the link, I want to
496555 * use the same label for all occourances of
497556 * the same named reference.
498557 * @return string
499558 */
500 - function linkRef( $key, $count = null, $label = null ) {
 559+ function linkRef( $group, $key, $count = null, $label = null, $subkey = '' ) {
501560 global $wgContLang;
502 -
503561 return
504562 $this->parse(
505563 wfMsgForContentNoTrans(
506564 'cite_reference_link',
507565 $this->refKey( $key, $count ),
508 - $this->referencesKey( $key ),
509 - $wgContLang->formatNum( is_null( $label ) ? ++$this->mOutCnt : $label )
 566+ $this->referencesKey( $key . $subkey ),
 567+ (($group == CITE_DEFAULT_GROUP)?'':"$group ").$wgContLang->formatNum( is_null( $label ) ? ++$this->mGroupCnt[$group] : $label )
510568 )
511569 );
512570 }
@@ -608,7 +666,9 @@
609667 * want the counts to transcend pages and other instances
610668 */
611669 function clearState() {
612 - $this->mOutCnt = $this->mInCnt = 0;
 670+ $this->mGroupCnt = array();
 671+ $this->mOutCnt = -1;
 672+ $this->mInCnt = 0;
613673 $this->mRefs = array();
614674
615675 return true;
Index: trunk/extensions/Cite/citeParserTests.txt
@@ -149,10 +149,10 @@
150150
151151 <references/>
152152 !! result
153 -<p><sup id="cite_ref-blank_0" class="reference"><a href="#cite_note-blank" title="">[1]</a></sup>
154 -</p><p><sup id="cite_ref-blank_1" class="reference"><a href="#cite_note-blank" title="">[1]</a></sup>
 153+<p><sup id="cite_ref-blank_0-0" class="reference"><a href="#cite_note-blank-0" title="">[1]</a></sup>
 154+</p><p><sup id="cite_ref-blank_0-1" class="reference"><a href="#cite_note-blank-0" title="">[1]</a></sup>
155155 </p>
156 -<ol class="references"><li id="cite_note-blank">↑ <sup><a href="#cite_ref-blank_0" title="">1.0</a></sup> <sup><a href="#cite_ref-blank_1" title="">1.1</a></sup> content</li></ol>
 156+<ol class="references"><li id="cite_note-blank-0">↑ <sup><a href="#cite_ref-blank_0-0" title="">1.0</a></sup> <sup><a href="#cite_ref-blank_0-1" title="">1.1</a></sup> content</li></ol>
157157
158158 !! end
159159
@@ -165,10 +165,10 @@
166166
167167 <references/>
168168 !! result
169 -<p><sup id="cite_ref-blank_0" class="reference"><a href="#cite_note-blank" title="">[1]</a></sup>
170 -</p><p><sup id="cite_ref-blank_1" class="reference"><a href="#cite_note-blank" title="">[1]</a></sup>
 169+<p><sup id="cite_ref-blank_0-0" class="reference"><a href="#cite_note-blank-0" title="">[1]</a></sup>
 170+</p><p><sup id="cite_ref-blank_0-1" class="reference"><a href="#cite_note-blank-0" title="">[1]</a></sup>
171171 </p>
172 -<ol class="references"><li id="cite_note-blank">↑ <sup><a href="#cite_ref-blank_0" title="">1.0</a></sup> <sup><a href="#cite_ref-blank_1" title="">1.1</a></sup> 0</li></ol>
 172+<ol class="references"><li id="cite_note-blank-0">↑ <sup><a href="#cite_ref-blank_0-0" title="">1.0</a></sup> <sup><a href="#cite_ref-blank_0-1" title="">1.1</a></sup> 0</li></ol>
173173
174174 !! end
175175
@@ -181,10 +181,10 @@
182182
183183 <references/>
184184 !! result
185 -<p><sup id="cite_ref-blank_0" class="reference"><a href="#cite_note-blank" title="">[1]</a></sup>
186 -</p><p><sup id="cite_ref-blank_1" class="reference"><a href="#cite_note-blank" title="">[1]</a></sup>
 185+<p><sup id="cite_ref-blank_0-0" class="reference"><a href="#cite_note-blank-0" title="">[1]</a></sup>
 186+</p><p><sup id="cite_ref-blank_0-1" class="reference"><a href="#cite_note-blank-0" title="">[1]</a></sup>
187187 </p>
188 -<ol class="references"><li id="cite_note-blank">↑ <sup><a href="#cite_ref-blank_0" title="">1.0</a></sup> <sup><a href="#cite_ref-blank_1" title="">1.1</a></sup> 1</li></ol>
 188+<ol class="references"><li id="cite_note-blank-0">↑ <sup><a href="#cite_ref-blank_0-0" title="">1.0</a></sup> <sup><a href="#cite_ref-blank_0-1" title="">1.1</a></sup> 1</li></ol>
189189
190190 !! end
191191
@@ -197,13 +197,13 @@
198198
199199 <references />
200200 !! result
201 -<p><sup id="cite_ref-test123test_0" class="reference"><a href="#cite_note-test123test" title="">[1]</a></sup>
202 -<sup id="cite_ref-123test_0" class="reference"><a href="#cite_note-123test" title="">[2]</a></sup>
203 -<sup id="cite_ref-test123_0" class="reference"><a href="#cite_note-test123" title="">[3]</a></sup>
 201+<p><sup id="cite_ref-test123test_0-0" class="reference"><a href="#cite_note-test123test-0" title="">[1]</a></sup>
 202+<sup id="cite_ref-123test_1-0" class="reference"><a href="#cite_note-123test-1" title="">[2]</a></sup>
 203+<sup id="cite_ref-test123_2-0" class="reference"><a href="#cite_note-test123-2" title="">[3]</a></sup>
204204 </p>
205 -<ol class="references"><li id="cite_note-test123test"><a href="#cite_ref-test123test_0" title="">↑</a> One</li>
206 -<li id="cite_note-123test"><a href="#cite_ref-123test_0" title="">↑</a> Two</li>
207 -<li id="cite_note-test123"><a href="#cite_ref-test123_0" title="">↑</a> Three</li></ol>
 205+<ol class="references"><li id="cite_note-test123test-0"><a href="#cite_ref-test123test_0-0" title="">↑</a> One</li>
 206+<li id="cite_note-123test-1"><a href="#cite_ref-123test_1-0" title="">↑</a> Two</li>
 207+<li id="cite_note-test123-2"><a href="#cite_ref-test123_2-0" title="">↑</a> Three</li></ol>
208208
209209 !! end
210210
@@ -229,8 +229,8 @@
230230 <p><strong class="error">Cite error: Invalid <code>&lt;ref&gt;</code> tag; name cannot be a simple integer, use a descriptive title</strong>
231231 </p><p><sup id="cite_ref-0" class="reference"><a href="#cite_note-0" title="">[1]</a></sup>
232232 </p><p><strong class="error">Cite error: Invalid <code>&lt;ref&gt;</code> tag; refs with no content must have a name</strong>
233 -</p><p><sup id="cite_ref-bar_0" class="reference"><a href="#cite_note-bar" title="">[2]</a></sup>
234 -</p><p><sup id="cite_ref-blankwithnoreference_0" class="reference"><a href="#cite_note-blankwithnoreference" title="">[3]</a></sup>
 233+</p><p><sup id="cite_ref-bar_1-0" class="reference"><a href="#cite_note-bar-1" title="">[2]</a></sup>
 234+</p><p><sup id="cite_ref-blankwithnoreference_2-0" class="reference"><a href="#cite_note-blankwithnoreference-2" title="">[3]</a></sup>
235235 </p><p><strong class="error">Cite error: Invalid <code>&lt;references&gt;</code> tag; no input is allowed, use
236236 <code>&lt;references /&gt;</code></strong>
237237 </p><p><strong class="error">Cite error: Invalid <code>&lt;references&gt;</code> tag; no parameters are allowed, use <code>&lt;references /&gt;</code></strong>
@@ -242,3 +242,19 @@
243243 !! end
244244
245245
 246+!! test
 247+Simple <ref>, with <references/> in group
 248+!! input
 249+Wikipedia rocks!<ref>Proceeds of Rockology, vol. XXI</ref>
 250+Wikipedia rocks!<ref group=note>Proceeds of Rockology, vol. XXI</ref>
 251+
 252+<references/>
 253+<references group=note/>
 254+!! result
 255+<p>Wikipedia rocks!<sup id="cite_ref-0" class="reference"><a href="#cite_note-0" title="">[1]</a></sup>
 256+Wikipedia rocks!<sup id="cite_ref-1" class="reference"><a href="#cite_note-1" title="">[note 1]</a></sup>
 257+</p>
 258+<ol class="references"><li id="cite_note-0"><a href="#cite_ref-0" title="">↑</a> Proceeds of Rockology, vol. XXI</li></ol>
 259+<ol class="references"><li id="cite_note-1"><a href="#cite_ref-1" title="">↑</a> Proceeds of Rockology, vol. XXI</li></ol>
 260+
 261+!! end

Follow-up revisions

RevisionCommit summaryAuthorDate
r32273Revert r32256 -- lots of notice errors about undefined index & such seen whil...brion00:19, 21 March 2008

Status & tagging log