Reduce nesting and code duplication in guardedRef; add comments. This is preparatory to possibly adding new attributes, like group or HTML attributes. Also, add a new parser test to verify failure on errors, improve should-never-occur messages slightly.

This commit is contained in:
Aryeh Gregor 2007-12-16 18:49:10 +00:00
parent 0ffcab3539
commit 7e700530b8
3 changed files with 95 additions and 93 deletions

View file

@ -15,10 +15,8 @@ $wgCiteMessages['en'] = array(
# Internal errors
'cite_croak' => 'Cite croaked; $1: $2',
'cite_error_' . CITE_ERROR_STR_INVALID => 'Internal error; invalid $str',
'cite_error_' . CITE_ERROR_KEY_INVALID_1 => 'Internal error; invalid key',
'cite_error_' . CITE_ERROR_KEY_INVALID_2 => 'Internal error; invalid key',
'cite_error_' . CITE_ERROR_STACK_INVALID_INPUT => 'Internal error; invalid stack key',
'cite_error_' . CITE_ERROR_KEY_STR_INVALID => 'Internal error; invalid $str and/or $key. This should never occur.',
'cite_error_' . CITE_ERROR_STACK_INVALID_INPUT => 'Internal error; invalid stack key. This should never occur.',
# User errors
'cite_error' => 'Cite error $1; $2',
@ -66,9 +64,7 @@ $wgCiteMessages['cs'] = array(
# Internal errors
'cite_croak' => 'Nefunkční citace; $1: $2',
'cite_error_' . CITE_ERROR_STR_INVALID => 'Vnitřní chyba; neplatný $str',
'cite_error_' . CITE_ERROR_KEY_INVALID_1 => 'Vnitřní chyba; neplatný klíč',
'cite_error_' . CITE_ERROR_KEY_INVALID_2 => 'Vnitřní chyba; neplatný klíč',
'cite_error_' . CITE_ERROR_KEY_STR_INVALID => 'Vnitřní chyba; neplatný $str',
'cite_error_' . CITE_ERROR_STACK_INVALID_INPUT => 'Vnitřní chyba; neplatný klíč zásobníku',
# User errors
@ -87,9 +83,7 @@ $wgCiteMessages['de'] = array(
# Internal errors
'cite_croak' => 'Fehler im Referenz-System. $1: $2',
'cite_error' => 'Referenz-Fehler $1: $2',
'cite_error_' . CITE_ERROR_STR_INVALID => 'Interner Fehler: ungültiger $str',
'cite_error_' . CITE_ERROR_KEY_INVALID_1 => 'Interner Fehler: Ungültiger „name“',
'cite_error_' . CITE_ERROR_KEY_INVALID_2 => 'Interner Fehler: ungültiger „name“',
'cite_error_' . CITE_ERROR_KEY_STR_INVALID => 'Interner Fehler: ungültiger $str',
'cite_error_' . CITE_ERROR_STACK_INVALID_INPUT => 'Interner Fehler: ungültiger „name“-stack',
# User errors
@ -111,9 +105,7 @@ $wgCiteMessages['de'] = array(
$wgCiteMessages['fr'] = array(
# Internal errors
'cite_croak' => 'Citation corrompue ; $1 : $2',
'cite_error_' . CITE_ERROR_STR_INVALID => 'Erreur interne ; $str attendue',
'cite_error_' . CITE_ERROR_KEY_INVALID_1 => 'Erreur interne ; clé invalide',
'cite_error_' . CITE_ERROR_KEY_INVALID_2 => 'Erreur interne ; clé invalide ',
'cite_error_' . CITE_ERROR_KEY_STR_INVALID => 'Erreur interne ; $str attendue',
'cite_error_' . CITE_ERROR_STACK_INVALID_INPUT => 'Erreur interne ; clé de pile invalide',
# User errors
@ -135,9 +127,7 @@ $wgCiteMessages['he'] = array(
# Internal errors
'cite_croak' => 'בהערה יש שגיאה; $1: $2',
'cite_error_' . CITE_ERROR_STR_INVALID => 'שגיאה פנימית; $str שגוי',
'cite_error_' . CITE_ERROR_KEY_INVALID_1 => 'שגיאה פנימית; מפתח שגוי',
'cite_error_' . CITE_ERROR_KEY_INVALID_2 => 'שגיאה פנימית; מפתח שגוי',
'cite_error_' . CITE_ERROR_KEY_STR_INVALID => 'שגיאה פנימית; $str שגוי',
'cite_error_' . CITE_ERROR_STACK_INVALID_INPUT => 'שגיאה פנימית; מפתח שגוי בערימה',
# User errors
@ -155,9 +145,7 @@ $wgCiteMessages['he'] = array(
$wgCiteMessages['id'] = array(
# Internal errors
'cite_croak' => 'Kegagalan pengutipan; $1: $2',
'cite_error_' . CITE_ERROR_STR_INVALID => 'Kesalahan internal; $str tak sah',
'cite_error_' . CITE_ERROR_KEY_INVALID_1 => 'Kesalahan internal; kunci tak sah',
'cite_error_' . CITE_ERROR_KEY_INVALID_2 => 'Kesalahan internal; kunci tak sah',
'cite_error_' . CITE_ERROR_KEY_STR_INVALID => 'Kesalahan internal; $str tak sah',
'cite_error_' . CITE_ERROR_STACK_INVALID_INPUT => 'Kesalahan internal; kunci stack tak sah',
# User errors
@ -174,9 +162,7 @@ $wgCiteMessages['it'] = array(
# Internal errors
'cite_croak' => 'Errore nella citazione: $1: $2',
'cite_error_' . CITE_ERROR_STR_INVALID => 'Errore interno: $str errato',
'cite_error_' . CITE_ERROR_KEY_INVALID_1 => 'Errore interno: chiave errata',
'cite_error_' . CITE_ERROR_KEY_INVALID_2 => 'Errore interno: chiave errata',
'cite_error_' . CITE_ERROR_KEY_STR_INVALID => 'Errore interno: $str errato',
'cite_error_' . CITE_ERROR_STACK_INVALID_INPUT => 'Errore interno: chiave di stack errata',
# User errors
@ -197,9 +183,7 @@ $wgCiteMessages['ja'] = array(
# Internal errors
'cite_croak' => '引用タグ機能の重大なエラー; $1: $2',
'cite_error_' . CITE_ERROR_STR_INVALID => '内部エラー; 無効な $str',
'cite_error_' . CITE_ERROR_KEY_INVALID_1 => '内部エラー; 無効なキー',
'cite_error_' . CITE_ERROR_KEY_INVALID_2 => '内部エラー; 無効なキー',
'cite_error_' . CITE_ERROR_KEY_STR_INVALID => '内部エラー; 無効な $str',
'cite_error_' . CITE_ERROR_STACK_INVALID_INPUT => '内部エラー; 無効なスタックキー',
# User errors
@ -222,9 +206,7 @@ $wgCiteMessages['kk-kz'] = array(
# Internal errors
'cite_croak' => 'Дәйексөз алу сәтсіз бітті; $1: $2 ',
'cite_error_' . CITE_ERROR_STR_INVALID => 'Ішкі қате; жарамсыз $str ',
'cite_error_' . CITE_ERROR_KEY_INVALID_1 => 'Ішкі қате; жарамсыз кілт',
'cite_error_' . CITE_ERROR_KEY_INVALID_2 => 'Ішкі қате; жарамсыз кілт',
'cite_error_' . CITE_ERROR_KEY_STR_INVALID => 'Ішкі қате; жарамсыз $str ',
'cite_error_' . CITE_ERROR_STACK_INVALID_INPUT => 'Ішкі қате; жарамсыз стек кілті',
# User errors
@ -246,9 +228,7 @@ $wgCiteMessages['kk-tr'] = array(
# Internal errors
'cite_croak' => 'Däýeksöz alw sätsiz bitti; $1: $2 ',
'cite_error_' . CITE_ERROR_STR_INVALID => 'İşki qate; jaramsız $str ',
'cite_error_' . CITE_ERROR_KEY_INVALID_1 => 'İşki qate; jaramsız kilt',
'cite_error_' . CITE_ERROR_KEY_INVALID_2 => 'İşki qate; jaramsız kilt',
'cite_error_' . CITE_ERROR_KEY_STR_INVALID => 'İşki qate; jaramsız $str ',
'cite_error_' . CITE_ERROR_STACK_INVALID_INPUT => 'İşki qate; jaramsız stek kilti',
# User errors
@ -270,9 +250,7 @@ $wgCiteMessages['kk-cn'] = array(
# Internal errors
'cite_croak' => 'دٵيەكسٶز الۋ سٵتسٸز بٸتتٸ; $1: $2 ',
'cite_error_' . CITE_ERROR_STR_INVALID => 'ٸشكٸ قاتە; جارامسىز $str ',
'cite_error_' . CITE_ERROR_KEY_INVALID_1 => 'ٸشكٸ قاتە; جارامسىز كٸلت',
'cite_error_' . CITE_ERROR_KEY_INVALID_2 => 'ٸشكٸ قاتە; جارامسىز كٸلت',
'cite_error_' . CITE_ERROR_KEY_STR_INVALID => 'ٸشكٸ قاتە; جارامسىز $str ',
'cite_error_' . CITE_ERROR_STACK_INVALID_INPUT => 'ٸشكٸ قاتە; جارامسىز ستەك كٸلتٸ',
# User errors
@ -290,9 +268,7 @@ $wgCiteMessages['kk'] = $wgCiteMessages['kk-kz'];
$wgCiteMessages['lt'] = array(
# Internal errors
'cite_croak' => 'Cituoti nepavyko; $1: $2',
'cite_error_' . CITE_ERROR_STR_INVALID => 'Vidinė klaida; neleistinas $str',
'cite_error_' . CITE_ERROR_KEY_INVALID_1 => 'Vidinė klaida; neleistinas raktas',
'cite_error_' . CITE_ERROR_KEY_INVALID_2 => 'Vidinė klaida; neleistinas raktas',
'cite_error_' . CITE_ERROR_KEY_STR_INVALID => 'Vidinė klaida; neleistinas $str',
'cite_error_' . CITE_ERROR_STACK_INVALID_INPUT => 'Vidinė klaida; neleistinas steko raktas',
# User errors
@ -313,9 +289,7 @@ $wgCiteMessages['nl'] = array(
# Internal errors
'cite_croak' => 'Probleem met Cite; $1: $2',
'cite_error_' . CITE_ERROR_STR_INVALID => 'Interne fout; onjuiste $str',
'cite_error_' . CITE_ERROR_KEY_INVALID_1 => 'Interne fout; onjuiste sleutel',
'cite_error_' . CITE_ERROR_KEY_INVALID_2 => 'Interne fout; onjuiste sleutel',
'cite_error_' . CITE_ERROR_KEY_STR_INVALID => 'Interne fout; onjuiste $str',
'cite_error_' . CITE_ERROR_STACK_INVALID_INPUT => 'Interne fout; onjuiste stacksleutel',
# User errors
@ -337,9 +311,7 @@ $wgCiteMessages['pt'] = array(
# Internal errors
'cite_croak' => 'Citação com problemas; $1: $2',
'cite_error_' . CITE_ERROR_STR_INVALID => 'Erro interno; $str inválido',
'cite_error_' . CITE_ERROR_KEY_INVALID_1 => 'Erro interno; chave inválida',
'cite_error_' . CITE_ERROR_KEY_INVALID_2 => 'Erro interno; chave inválida',
'cite_error_' . CITE_ERROR_KEY_STR_INVALID => 'Erro interno; $str inválido',
'cite_error_' . CITE_ERROR_STACK_INVALID_INPUT => 'Erro interno; chave fixa inválida',
# User errors
@ -362,9 +334,7 @@ $wgCiteMessages['ru'] = array(
# Internal errors
'cite_croak' => 'Цитата сдохла; $1: $2',
'cite_error_' . CITE_ERROR_STR_INVALID => 'Внутренняя ошибка: неверный $str',
'cite_error_' . CITE_ERROR_KEY_INVALID_1 => 'Внутренняя ошибка: неверный ключ',
'cite_error_' . CITE_ERROR_KEY_INVALID_2 => 'Внутренняя ошибка: неверный ключ',
'cite_error_' . CITE_ERROR_KEY_STR_INVALID => 'Внутренняя ошибка: неверный $str',
'cite_error_' . CITE_ERROR_STACK_INVALID_INPUT => 'Внутренняя ошибка: неверный ключ стека ',
# User errors
@ -391,9 +361,7 @@ $wgCiteMessages['sk'] = array(
# Internal errors
'cite_croak' => 'Citát je už neaktuálny; $1: $2',
'cite_error_' . CITE_ERROR_STR_INVALID => 'Vnútorná chyba; neplatný $str',
'cite_error_' . CITE_ERROR_KEY_INVALID_1 => 'Vnútorná chyba; neplatný kľúč',
'cite_error_' . CITE_ERROR_KEY_INVALID_2 => 'Vnútorná chyba; neplatný kľúč',
'cite_error_' . CITE_ERROR_KEY_STR_INVALID => 'Vnútorná chyba; neplatný $str',
'cite_error_' . CITE_ERROR_STACK_INVALID_INPUT => 'Vnútorná chyba; neplatný kľúč zásobníka',
# User errors
@ -415,9 +383,7 @@ $wgCiteMessages['yue'] = array(
# Internal errors
'cite_croak' => '引用阻塞咗; $1: $2',
'cite_error_' . CITE_ERROR_STR_INVALID => '內部錯誤; 無效嘅 $str',
'cite_error_' . CITE_ERROR_KEY_INVALID_1 => '內部錯誤; 無效嘅匙',
'cite_error_' . CITE_ERROR_KEY_INVALID_2 => '內部錯誤; 無效嘅匙',
'cite_error_' . CITE_ERROR_KEY_STR_INVALID => '內部錯誤; 無效嘅 $str',
'cite_error_' . CITE_ERROR_STACK_INVALID_INPUT => '內部錯誤; 無效嘅堆疊匙',
# User errors
@ -439,9 +405,7 @@ $wgCiteMessages['zh-hans'] = array(
# Internal errors
'cite_croak' => '引用阻塞; $1: $2',
'cite_error_' . CITE_ERROR_STR_INVALID => '内部错误;非法的 $str',
'cite_error_' . CITE_ERROR_KEY_INVALID_1 => '内部错误;非法键值',
'cite_error_' . CITE_ERROR_KEY_INVALID_2 => '内部错误;非法键值',
'cite_error_' . CITE_ERROR_KEY_STR_INVALID => '内部错误;非法的 $str',
'cite_error_' . CITE_ERROR_STACK_INVALID_INPUT => '内部错误;非法堆栈键值',
# User errors
@ -463,9 +427,7 @@ $wgCiteMessages['zh-hant'] = array(
# Internal errors
'cite_croak' => '引用阻塞; $1: $2',
'cite_error_' . CITE_ERROR_STR_INVALID => '內部錯誤;非法的 $str',
'cite_error_' . CITE_ERROR_KEY_INVALID_1 => '內部錯誤;非法鍵',
'cite_error_' . CITE_ERROR_KEY_INVALID_2 => '內部錯誤;非法鍵',
'cite_error_' . CITE_ERROR_KEY_STR_INVALID => '內部錯誤;非法的 $str',
'cite_error_' . CITE_ERROR_STACK_INVALID_INPUT => '內部錯誤;非法堆疊鍵值',
# User errors
@ -484,4 +446,4 @@ $wgCiteMessages['zh-cn'] = $wgCiteMessages['zh-hans'];
$wgCiteMessages['zh-hk'] = $wgCiteMessages['zh-hant'];
$wgCiteMessages['zh-sg'] = $wgCiteMessages['zh-hans'];
$wgCiteMessages['zh-tw'] = $wgCiteMessages['zh-hant'];
$wgCiteMessages['zh-yue'] = $wgCiteMessages['yue'];
$wgCiteMessages['zh-yue'] = $wgCiteMessages['yue'];

View file

@ -32,9 +32,7 @@ $wgParserTestFiles[] = dirname( __FILE__ ) . "/citeParserTests.txt";
*/
$wgCiteErrors = array(
'system' => array(
'CITE_ERROR_STR_INVALID',
'CITE_ERROR_KEY_INVALID_1',
'CITE_ERROR_KEY_INVALID_2',
'CITE_ERROR_KEY_STR_INVALID',
'CITE_ERROR_STACK_INVALID_INPUT'
),
'user' => array(
@ -173,40 +171,47 @@ function wfCite() {
function guardedRef( $str, $argv, $parser ) {
$this->mParser = $parser;
# The key here is the "name" attribute.
$key = $this->refArg( $argv );
if ( $str !== null ) {
if ( $str === '' )
return $this->error( CITE_ERROR_REF_NO_INPUT );
if ( is_string( $key ) )
// I don't want keys in the form of /^[0-9]+$/ because they would
// conflict with the php datastructure I'm using, besides, why specify
// a manual key if it's just going to be any old integer?
if ( sprintf( '%d', $key ) === (string)$key )
return $this->error( CITE_ERROR_REF_NUMERIC_KEY );
else
return $this->stack( $str, $key );
else if ( $key === null )
return $this->stack( $str );
else if ( $key === false )
return $this->error( CITE_ERROR_REF_TOO_MANY_KEYS );
else
$this->croak( CITE_ERROR_KEY_INVALID_1, serialize( $key ) );
} else if ( $str === null ) {
if ( is_string( $key ) )
if ( sprintf( '%d', $key ) === (string)$key )
return $this->error( CITE_ERROR_REF_NUMERIC_KEY );
else
return $this->stack( $str, $key );
else if ( $key === false )
return $this->error( CITE_ERROR_REF_TOO_MANY_KEYS );
else if ( $key === null )
return $this->error( CITE_ERROR_REF_NO_KEY );
else
$this->croak( CITE_ERROR_KEY_INVALID_2, serialize( $key ) );
if( $str === '' ) {
# <ref ...></ref>. This construct is always invalid: either
# it's a contentful ref, or it's a named duplicate and should
# be <ref ... />.
return $this->error( CITE_ERROR_REF_NO_INPUT );
}
} else
$this->croak( CITE_ERROR_STR_INVALID, serialize( $str ) );
if( $key === false ) {
# TODO: Comment this case; what does this condition mean?
return $this->error( CITE_ERROR_REF_TOO_MANY_KEYS );
}
if( $str === null and $key === null ) {
# Something like <ref />; this makes no sense.
return $this->error( CITE_ERROR_REF_NO_KEY );
}
if( preg_match( '/[0-9]+/', $key ) ) {
# 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).
return $this->error( CITE_ERROR_REF_NUMERIC_KEY );
}
if( is_string( $key ) or is_string( $str ) ) {
# We don't care about the content: if the key exists, the ref
# is presumptively valid. Either it stores a new ref, or re-
# fers to an existing one. If it refers to a nonexistent ref,
# we'll figure that out later. Likewise it's definitely valid
# if there's any content, regardless of key.
return $this->stack( $str, $key );
}
# Not clear how we could get here, but something is probably
# wrong with the types. Let's fail fast.
$this->croak( CITE_ERROR_KEY_STR_INVALID, serialize( "$str; $key" ) );
}
/**

View file

@ -187,3 +187,38 @@ Regression sanity check: non-blank ref "1" followed by ref with content
<ol class="references"><li id="_note-blank">↑ <sup><a href="#_ref-blank_0" title="">1.0</a></sup> <sup><a href="#_ref-blank_1" title="">1.1</a></sup> 1</li></ol>
!! end
!! test
Erroneous refs
!! input
<ref name="0">Zero</ref>
<ref>Also zero, but differently! (Normal ref)</ref>
<ref />
<ref name="foo" name="bar" />
<ref name="blankwithnoreference" />
<references>I'm a references with something to say!</references>
<references name="quasit" />
<references />
!! result
<p><strong class="error">Cite error 1; Invalid <code>&lt;ref&gt;</code> tag; name cannot be a simple integer, use a descriptive title</strong>
</p><p><sup id="_ref-0" class="reference"><a href="#_note-0" title="">[1]</a></sup>
</p><p><strong class="error">Cite error 2; Invalid <code>&lt;ref&gt;</code> tag; refs with no content must have a name</strong>
</p><p><sup id="_ref-bar_0" class="reference"><a href="#_note-bar" title="">[2]</a></sup>
</p><p><sup id="_ref-blankwithnoreference_0" class="reference"><a href="#_note-blankwithnoreference" title="">[3]</a></sup>
</p><p><strong class="error">Cite error 5; Invalid <code>&lt;references&gt;</code> tag; no input is allowed, use
<code>&lt;references /&gt;</code></strong>
</p><p><strong class="error">Cite error 6; Invalid <code>&lt;references&gt;</code> tag; no parameters are allowed, use <code>&lt;references /&gt;</code></strong>
</p>
<ol class="references"><li id="_note-0"><a href="#_ref-0" title="">↑</a> Also zero, but differently! (Normal ref)</li>
<li id="_note-bar"><a href="#_ref-bar_0" title="">↑</a> <strong class="error">Cite error 8; No text given.</strong></li>
<li id="_note-blankwithnoreference"><a href="#_ref-blankwithnoreference_0" title="">↑</a> <strong class="error">Cite error 8; No text given.</strong></li></ol>
!! end