Index: trunk/extensions/Cite/Cite.php |
— | — | @@ -30,6 +30,13 @@ |
31 | 31 | $wgParserTestFiles[] = dirname( __FILE__ ) . "/citeParserTests.txt"; |
32 | 32 | $wgExtensionMessagesFiles['Cite'] = dirname( __FILE__ ) . "/Cite.i18n.php"; |
33 | 33 | |
| 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 | + |
34 | 41 | function wfCite() { |
35 | 42 | class Cite { |
36 | 43 | /**#@+ |
— | — | @@ -75,6 +82,7 @@ |
76 | 83 | * @var int |
77 | 84 | */ |
78 | 85 | var $mOutCnt = 0; |
| 86 | + var $mGroupCnt = array(); |
79 | 87 | |
80 | 88 | /** |
81 | 89 | * Internal counter for anonymous references, seperate from |
— | — | @@ -137,11 +145,11 @@ |
138 | 146 | } |
139 | 147 | } |
140 | 148 | |
141 | | - function guardedRef( $str, $argv, $parser ) { |
| 149 | + function guardedRef( $str, $argv, $parser, $default_group=CITE_DEFAULT_GROUP ) { |
142 | 150 | $this->mParser = $parser; |
143 | 151 | |
144 | 152 | # The key here is the "name" attribute. |
145 | | - $key = $this->refArg( $argv ); |
| 153 | + list($key,$group) = $this->refArg( $argv ); |
146 | 154 | |
147 | 155 | if( $str === '' ) { |
148 | 156 | # <ref ...></ref>. This construct is always invalid: either |
— | — | @@ -167,6 +175,11 @@ |
168 | 176 | # (and would produce weird id's anyway). |
169 | 177 | return $this->error( 'cite_error_ref_numeric_key' ); |
170 | 178 | } |
| 179 | + |
| 180 | + #Split these into groups. |
| 181 | + if( $group === null ) { |
| 182 | + $group = $default_group; |
| 183 | + } |
171 | 184 | |
172 | 185 | if( is_string( $key ) or is_string( $str ) ) { |
173 | 186 | # We don't care about the content: if the key exists, the ref |
— | — | @@ -174,7 +187,7 @@ |
175 | 188 | # fers to an existing one. If it refers to a nonexistent ref, |
176 | 189 | # we'll figure that out later. Likewise it's definitely valid |
177 | 190 | # if there's any content, regardless of key. |
178 | | - return $this->stack( $str, $key ); |
| 191 | + return $this->stack( $str, $key, $group ); |
179 | 192 | } |
180 | 193 | |
181 | 194 | # Not clear how we could get here, but something is probably |
— | — | @@ -192,21 +205,36 @@ |
193 | 206 | * input and null on no input |
194 | 207 | */ |
195 | 208 | function refArg( $argv ) { |
| 209 | + global $wgAllowCiteGroups; |
196 | 210 | $cnt = count( $argv ); |
197 | 211 | |
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 |
200 | 214 | return false; |
201 | | - else if ( $cnt == 1 ) |
202 | | - if ( isset( $argv['name'] ) ) |
| 215 | + else if ( $cnt >= 1 ) { |
| 216 | + if ( isset( $argv['name'] ) ) { |
203 | 217 | // 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); |
205 | 232 | else |
206 | 233 | // Invalid key |
207 | | - return false; |
| 234 | + return array(false); |
| 235 | + } |
208 | 236 | else |
209 | 237 | // No key |
210 | | - return null; |
| 238 | + return array(null,$group); |
211 | 239 | } |
212 | 240 | |
213 | 241 | /** |
— | — | @@ -242,38 +270,47 @@ |
243 | 271 | * @param mixed $key Argument to the <ref> tag as returned by $this->refArg() |
244 | 272 | * @return string |
245 | 273 | */ |
246 | | - function stack( $str, $key = null ) { |
| 274 | + function stack( $str, $key = null, $group ) { |
247 | 275 | if ( $key === null ) { |
248 | 276 | // 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 ) ) { |
252 | 282 | // 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( |
256 | 286 | 'text' => $str, |
257 | 287 | 'count' => 0, |
258 | | - 'number' => ++$this->mOutCnt |
| 288 | + 'key' => ++$this->mOutCnt, |
| 289 | + 'number' => ++$this->mGroupCnt[$group] |
259 | 290 | ); |
260 | 291 | return |
261 | 292 | $this->linkRef( |
| 293 | + $group, |
262 | 294 | $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'] |
265 | 298 | ); |
266 | 299 | } else { |
267 | 300 | // We've been here before |
268 | | - if ( $this->mRefs[$key]['text'] === null && $str !== '' ) { |
| 301 | + if ( $this->mRefs[$group][$key]['text'] === null && $str !== '' ) { |
269 | 302 | // If no text found before, use this text |
270 | | - $this->mRefs[$key]['text'] = $str; |
| 303 | + $this->mRefs[$group][$key]['text'] = $str; |
271 | 304 | }; |
272 | 305 | return |
273 | 306 | $this->linkRef( |
| 307 | + $group, |
274 | 308 | $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'] |
277 | 312 | ); } |
| 313 | + } |
| 314 | + |
278 | 315 | else |
279 | 316 | $this->croak( 'cite_error_stack_invalid_input', serialize( array( $key, $str ) ) ); |
280 | 317 | } |
— | — | @@ -300,15 +337,26 @@ |
301 | 338 | return $ret; |
302 | 339 | } |
303 | 340 | } |
304 | | - |
305 | | - function guardedReferences( $str, $argv, $parser ) { |
| 341 | + |
| 342 | + function guardedReferences( $str, $argv, $parser, $group = CITE_DEFAULT_GROUP ) { |
| 343 | + global $wgAllowCiteGroups; |
| 344 | + |
306 | 345 | $this->mParser = $parser; |
| 346 | + |
307 | 347 | if ( $str !== null ) |
308 | 348 | 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 ) ) |
310 | 358 | return $this->error( 'cite_error_references_invalid_parameters' ); |
311 | 359 | else |
312 | | - return $this->referencesFormat(); |
| 360 | + return $this->referencesFormat($group); |
313 | 361 | } |
314 | 362 | |
315 | 363 | /** |
— | — | @@ -316,14 +364,14 @@ |
317 | 365 | * |
318 | 366 | * @return string XHTML ready for output |
319 | 367 | */ |
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 )) |
322 | 370 | return ''; |
323 | 371 | |
324 | 372 | wfProfileIn( __METHOD__ ); |
325 | 373 | wfProfileIn( __METHOD__ .'-entries' ); |
326 | 374 | $ent = array(); |
327 | | - foreach ( $this->mRefs as $k => $v ) |
| 375 | + foreach ( $this->mRefs[$group] as $k => $v ) |
328 | 376 | $ent[] = $this->referencesFormatEntry( $k, $v ); |
329 | 377 | |
330 | 378 | $prefix = wfMsgForContentNoTrans( 'cite_references_prefix' ); |
— | — | @@ -365,6 +413,16 @@ |
366 | 414 | $this->refKey( $key, $val['count'] ), |
367 | 415 | $this->error( 'cite_error_references_no_text', $key ) |
368 | 416 | ); |
| 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 | + ); |
369 | 427 | // Standalone named reference, I want to format this like an |
370 | 428 | // anonymous reference because displaying "1. 1.1 Ref text" is |
371 | 429 | // overkill and users frequently use named references when they |
— | — | @@ -373,18 +431,19 @@ |
374 | 432 | return |
375 | 433 | wfMsgForContentNoTrans( |
376 | 434 | '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'] ), |
379 | 438 | ( $val['text'] != '' ? $val['text'] : $this->error( 'cite_error_references_no_text', $key ) ) |
380 | 439 | ); |
381 | 440 | // Named references with >1 occurrences |
382 | 441 | else { |
383 | 442 | $links = array(); |
384 | | - |
| 443 | +//for group handling, we have an extra key here. |
385 | 444 | for ( $i = 0; $i <= $val['count']; ++$i ) { |
386 | 445 | $links[] = wfMsgForContentNoTrans( |
387 | 446 | 'cite_references_link_many_format', |
388 | | - $this->refKey( $key, $i ), |
| 447 | + $this->refKey( $key, $val['key']."-$i" ), |
389 | 448 | $this->referencesFormatEntryNumericBacklinkLabel( $val['number'], $i, $val['count'] ), |
390 | 449 | $this->referencesFormatEntryAlternateBacklinkLabel( $i ) |
391 | 450 | ); |
— | — | @@ -394,7 +453,7 @@ |
395 | 454 | |
396 | 455 | return |
397 | 456 | wfMsgForContentNoTrans( 'cite_references_link_many', |
398 | | - $this->referencesKey( $key ), |
| 457 | + $this->referencesKey( $key ."-" . $val['key'] ), |
399 | 458 | $list, |
400 | 459 | ( $val['text'] != '' ? $val['text'] : $this->error( 'cite_error_references_no_text', $key ) ) |
401 | 460 | ); |
— | — | @@ -446,7 +505,7 @@ |
447 | 506 | |
448 | 507 | /** |
449 | 508 | * 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> |
451 | 510 | * (since otherwise it would link to itself) |
452 | 511 | * |
453 | 512 | * @static |
— | — | @@ -466,7 +525,7 @@ |
467 | 526 | |
468 | 527 | /** |
469 | 528 | * 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> |
471 | 530 | * (since otherwise it would link to itself) |
472 | 531 | * |
473 | 532 | * @static |
— | — | @@ -489,23 +548,22 @@ |
490 | 549 | * and return XHTML ready for output |
491 | 550 | * |
492 | 551 | * @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 |
495 | 554 | * @param int $label The label to use for the link, I want to |
496 | 555 | * use the same label for all occourances of |
497 | 556 | * the same named reference. |
498 | 557 | * @return string |
499 | 558 | */ |
500 | | - function linkRef( $key, $count = null, $label = null ) { |
| 559 | + function linkRef( $group, $key, $count = null, $label = null, $subkey = '' ) { |
501 | 560 | global $wgContLang; |
502 | | - |
503 | 561 | return |
504 | 562 | $this->parse( |
505 | 563 | wfMsgForContentNoTrans( |
506 | 564 | 'cite_reference_link', |
507 | 565 | $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 ) |
510 | 568 | ) |
511 | 569 | ); |
512 | 570 | } |
— | — | @@ -608,7 +666,9 @@ |
609 | 667 | * want the counts to transcend pages and other instances |
610 | 668 | */ |
611 | 669 | function clearState() { |
612 | | - $this->mOutCnt = $this->mInCnt = 0; |
| 670 | + $this->mGroupCnt = array(); |
| 671 | + $this->mOutCnt = -1; |
| 672 | + $this->mInCnt = 0; |
613 | 673 | $this->mRefs = array(); |
614 | 674 | |
615 | 675 | return true; |
Index: trunk/extensions/Cite/citeParserTests.txt |
— | — | @@ -149,10 +149,10 @@ |
150 | 150 | |
151 | 151 | <references/> |
152 | 152 | !! 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> |
155 | 155 | </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> |
157 | 157 | |
158 | 158 | !! end |
159 | 159 | |
— | — | @@ -165,10 +165,10 @@ |
166 | 166 | |
167 | 167 | <references/> |
168 | 168 | !! 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> |
171 | 171 | </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> |
173 | 173 | |
174 | 174 | !! end |
175 | 175 | |
— | — | @@ -181,10 +181,10 @@ |
182 | 182 | |
183 | 183 | <references/> |
184 | 184 | !! 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> |
187 | 187 | </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> |
189 | 189 | |
190 | 190 | !! end |
191 | 191 | |
— | — | @@ -197,13 +197,13 @@ |
198 | 198 | |
199 | 199 | <references /> |
200 | 200 | !! 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> |
204 | 204 | </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> |
208 | 208 | |
209 | 209 | !! end |
210 | 210 | |
— | — | @@ -229,8 +229,8 @@ |
230 | 230 | <p><strong class="error">Cite error: Invalid <code><ref></code> tag; name cannot be a simple integer, use a descriptive title</strong> |
231 | 231 | </p><p><sup id="cite_ref-0" class="reference"><a href="#cite_note-0" title="">[1]</a></sup> |
232 | 232 | </p><p><strong class="error">Cite error: Invalid <code><ref></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> |
235 | 235 | </p><p><strong class="error">Cite error: Invalid <code><references></code> tag; no input is allowed, use |
236 | 236 | <code><references /></code></strong> |
237 | 237 | </p><p><strong class="error">Cite error: Invalid <code><references></code> tag; no parameters are allowed, use <code><references /></code></strong> |
— | — | @@ -242,3 +242,19 @@ |
243 | 243 | !! end |
244 | 244 | |
245 | 245 | |
| 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 |