From f54597796263daa981602cb31542bc2fef677cfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=86var=20Arnfj=C3=B6r=C3=B0=20Bjarmason?= Date: Mon, 23 Jan 2006 14:55:26 +0000 Subject: [PATCH] * Optimizing: Only make the parser and backlink labels when we need to * sprintf( '%d', $key ) === (string)$key ==> ctype_digit( $key ) * die() => die( -1 ) * Moved the message cache initialization into the class --- Cite.php | 145 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 84 insertions(+), 61 deletions(-) diff --git a/Cite.php b/Cite.php index 0feec2db5..b6f508a4b 100644 --- a/Cite.php +++ b/Cite.php @@ -1,6 +1,6 @@ and for adding * citations to pages @@ -55,61 +55,7 @@ for ( $i = 0; $i < count( $wgCiteErrors['user'] ); ++$i ) // User errors are positive integers define( $wgCiteErrors['user'][$i], $i + 1 ); -function wfCite() { - global $wgMessageCache; - - $wgMessageCache->addMessages( - array( - /* - Debug & errors - */ - - // 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', - - // User errors - 'cite_error' => 'Cite error $1; $2', - - 'cite_error_' . CITE_ERROR_REF_NUMERIC_KEY => 'Invalid call; expecting a non-integer key', - 'cite_error_' . CITE_ERROR_REF_NO_KEY => 'Invalid call; no key specified', - 'cite_error_' . CITE_ERROR_REF_TOO_MANY_KEYS => 'Invalid call; invalid keys, e.g. too many or wrong key specified', - 'cite_error_' . CITE_ERROR_REF_NO_INPUT => 'Invalid call; no input specified', - 'cite_error_' . CITE_ERROR_REFERENCES_INVALID_INPUT => 'Invalid input; expecting none', - 'cite_error_' . CITE_ERROR_REFERENCES_INVALID_PARAMETERS => 'Invalid parameters; expecting none', - 'cite_error_' . CITE_ERROR_REFERENCES_NO_BACKLINK_LABEL => "Ran out of custom backlink labels, define more in the \"''cite_references_link_many_format_backlink_labels''\" message", - - /* - Output formatting - */ - 'cite_reference_link_key_with_num' => '$1_$2', - // Ids produced by - 'cite_reference_link_prefix' => '_ref-', - 'cite_reference_link_suffix' => '', - // Ids produced by - 'cite_references_link_prefix' => '_note-', - 'cite_references_link_suffix' => '', - - 'cite_reference_link' => '[[#$2|[$3]]]', - 'cite_references_link_one' => '
  • [[#$2|^]] $3
  • ', - 'cite_references_link_many' => '
  • ^ $2 $3
  • ', - 'cite_references_link_many_format' => '[[#$1|$2]]', - // An item from this set is passed as $3 in the message above - 'cite_references_link_many_format_backlink_labels' => 'a b c d e f g h i j k l m n o p q r s t u v w x y z', - 'cite_references_link_many_sep' => "\xc2\xa0", //   - 'cite_references_link_many_and' => "\xc2\xa0", // &nbps; - - // Although I could just use # instead of
  • above and nothing here that - // will break on input that contains linebreaks - 'cite_references_prefix' => '
      ', - 'cite_references_suffix' => '
    ', - ) - ); - +function wfCite() { class Cite { /**#@+ * @access private @@ -177,7 +123,18 @@ function wfCite() { * @var object */ var $mParser, $mParserOptions; - + + /**#@+ + * Whether or not the variables above have been loaded, doing so in a + * request where they're not going to be used (like when thumb.php gets + * loaded) is redundant and expensive + * + * @var bool + */ + var $mLoadedBacklinkLabels = false; + var $mLoadedParser = false; + /**#@-*/ + /**#@-*/ /** @@ -185,8 +142,7 @@ function wfCite() { */ function Cite() { $this->setHooks(); - $this->genParser(); - $this->genBacklinkLabels(); + $this->setMessages(); } /**#@+ @access private */ @@ -208,7 +164,7 @@ function wfCite() { // 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 ) + if ( ctype_digit( $key ) ) return $this->error( CITE_ERROR_REF_NUMERIC_KEY ); else return $this->stack( $str, $key ); @@ -220,7 +176,7 @@ function wfCite() { $this->croak( CITE_ERROR_KEY_INVALID_1, serialize( $key ) ); } else if ( $str === null ) { if ( is_string( $key ) ) - if ( sprintf( '%d', $key ) === (string)$key ) + if ( ctype_digit( $key ) ) return $this->error( CITE_ERROR_REF_NUMERIC_KEY ); else return $this->stack( $str, $key ); @@ -403,6 +359,7 @@ function wfCite() { * @return string */ function referencesFormatEntryAlternateBacklinkLabel( $offset ) { + $this->genBacklinkLabels(); if ( isset( $this->mBacklinkLabels[$offset] ) ) return $this->mBacklinkLabels[$offset]; else @@ -511,6 +468,9 @@ function wfCite() { * @return string The parsed text */ function parse( $in ) { + if ( ! $this->mLoadedParser ) + $this->genParser(); + global $wgTitle; $ret = $this->mParser->parse( @@ -561,6 +521,7 @@ function wfCite() { function genParser() { $this->mParser = new Parser; $this->mParserOptions = new ParserOptions; + $this->mLoadedParser = true; } /** @@ -569,8 +530,11 @@ function wfCite() { * arbitary number of tokens seperated by [\t\n ] */ function genBacklinkLabels() { + if ( $this->mLoadedBacklinkLabels ) + return; $text = wfMsgForContentNoTrans( 'cite_references_link_many_format_backlink_labels' ); $this->mBacklinkLabels = preg_split( '#[\n\t ]#', $text ); + $this->mLoadedBacklinkLabels = true; } /** @@ -596,6 +560,65 @@ function wfCite() { $wgHooks['ParserClearState'][] = array( &$this, 'clearState' ); } + /** + * Initialize the messages + */ + function setMessages() { + global $wgMessageCache; + + $wgMessageCache->addMessages( + array( + /* + Debug & errors + */ + + // 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', + + // User errors + 'cite_error' => 'Cite error $1; $2', + + 'cite_error_' . CITE_ERROR_REF_NUMERIC_KEY => 'Invalid call; expecting a non-integer key', + 'cite_error_' . CITE_ERROR_REF_NO_KEY => 'Invalid call; no key specified', + 'cite_error_' . CITE_ERROR_REF_TOO_MANY_KEYS => 'Invalid call; invalid keys, e.g. too many or wrong key specified', + 'cite_error_' . CITE_ERROR_REF_NO_INPUT => 'Invalid call; no input specified', + 'cite_error_' . CITE_ERROR_REFERENCES_INVALID_INPUT => 'Invalid input; expecting none', + 'cite_error_' . CITE_ERROR_REFERENCES_INVALID_PARAMETERS => 'Invalid parameters; expecting none', + 'cite_error_' . CITE_ERROR_REFERENCES_NO_BACKLINK_LABEL => "Ran out of custom backlink labels, define more in the \"''cite_references_link_many_format_backlink_labels''\" message", + + /* + Output formatting + */ + 'cite_reference_link_key_with_num' => '$1_$2', + // Ids produced by + 'cite_reference_link_prefix' => '_ref-', + 'cite_reference_link_suffix' => '', + // Ids produced by + 'cite_references_link_prefix' => '_note-', + 'cite_references_link_suffix' => '', + + 'cite_reference_link' => '[[#$2|[$3]]]', + 'cite_references_link_one' => '
  • [[#$2|^]] $3
  • ', + 'cite_references_link_many' => '
  • ^ $2 $3
  • ', + 'cite_references_link_many_format' => '[[#$1|$2]]', + // An item from this set is passed as $3 in the message above + 'cite_references_link_many_format_backlink_labels' => 'a b c d e f g h i j k l m n o p q r s t u v w x y z', + 'cite_references_link_many_sep' => "\xc2\xa0", //   + 'cite_references_link_many_and' => "\xc2\xa0", // &nbps; + + // Although I could just use # instead of
  • above and nothing here that + // will break on input that contains linebreaks + 'cite_references_prefix' => '
      ', + 'cite_references_suffix' => '
    ', + ) + ); + } + /** * Return an error message based on an error ID *