From 7e700530b89e8dd6728cccc2e1a454b0742a1c24 Mon Sep 17 00:00:00 2001 From: Aryeh Gregor Date: Sun, 16 Dec 2007 18:49:10 +0000 Subject: [PATCH] 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. --- Cite.i18n.php | 80 ++++++++++++--------------------------------- Cite.php | 73 ++++++++++++++++++++++------------------- citeParserTests.txt | 35 ++++++++++++++++++++ 3 files changed, 95 insertions(+), 93 deletions(-) diff --git a/Cite.i18n.php b/Cite.i18n.php index f2059c67..ed2f15a5 100644 --- a/Cite.i18n.php +++ b/Cite.i18n.php @@ -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']; \ No newline at end of file +$wgCiteMessages['zh-yue'] = $wgCiteMessages['yue']; diff --git a/Cite.php b/Cite.php index 19cd62bb..9bfbe7e8 100644 --- a/Cite.php +++ b/Cite.php @@ -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 === '' ) { + # . This construct is always invalid: either + # it's a contentful ref, or it's a named duplicate and should + # be . + 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 ; 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" ) ); } /** diff --git a/citeParserTests.txt b/citeParserTests.txt index 4cb60e12..82e16fc6 100644 --- a/citeParserTests.txt +++ b/citeParserTests.txt @@ -187,3 +187,38 @@ Regression sanity check: non-blank ref "1" followed by ref with content
  1. 1.0 1.1 1
!! end + +!! test +Erroneous refs +!! input +Zero + +Also zero, but differently! (Normal ref) + + + + + + + +I'm a references with something to say! + + + + +!! result +

Cite error 1; Invalid <ref> tag; name cannot be a simple integer, use a descriptive title +

[1] +

Cite error 2; Invalid <ref> tag; refs with no content must have a name +

[2] +

[3] +

Cite error 5; Invalid <references> tag; no input is allowed, use +<references /> +

Cite error 6; Invalid <references> tag; no parameters are allowed, use <references /> +

+
  1. Also zero, but differently! (Normal ref)
  2. +
  3. Cite error 8; No text given.
  4. +
  5. Cite error 8; No text given.
+ +!! end +