r84749 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r84748‎ | r84749 | r84750 >
Date:15:21, 25 March 2011
Author:ialex
Status:deferred
Tags:
Comment:
* Use a specific file for magic words
* Use ParserFirstCallInit to register parser functions
* Use the given Parser object to grab the underlying functions; not $wgParser
Modified paths:
  • /trunk/extensions/StringFunctionsEscaped/StringFunctionsEscaped.i18n.php (added) (history)
  • /trunk/extensions/StringFunctionsEscaped/StringFunctionsEscaped.php (modified) (history)

Diff [purge]

Index: trunk/extensions/StringFunctionsEscaped/StringFunctionsEscaped.i18n.php
@@ -0,0 +1,12 @@
 2+<?php
 3+
 4+$magicWords = array();
 5+
 6+$magicWords['en'] = array(
 7+ 'pos_e' => array ( 0, 'pos_e' ),
 8+ 'rpos_e' => array ( 0, 'rpos_e' ),
 9+ 'pad_e' => array ( 0, 'pad_e' ),
 10+ 'replace_e' => array ( 0, 'replace_e' ),
 11+ 'explode_e' => array ( 0, 'explode_e' ),
 12+ 'stripnewlines' => array ( 0, 'stripnewlines' ),
 13+);
Property changes on: trunk/extensions/StringFunctionsEscaped/StringFunctionsEscaped.i18n.php
___________________________________________________________________
Added: svn:eol-style
114 + native
Index: trunk/extensions/StringFunctionsEscaped/StringFunctionsEscaped.php
@@ -1,9 +1,9 @@
22 <?php
33 if ( !defined( 'MEDIAWIKI' ) )
4 - die( 'StringFunctionsEscaped::This file is a MediaWiki extension, it is not a valid entry point' );
 4+ #die( 'StringFunctionsEscaped::This file is a MediaWiki extension, it is not a valid entry point' );
55 if ( !class_exists('ExtStringFunctions',false) &&
66 !(class_exists('ParserFunctions_HookStub',false) && isset($wgPFEnableStringFunctions) && $wgPFEnableStringFunctions))
7 - die( 'StringFunctionsEscaped::You must have extension StringFunctions or extension ParserFunctions with string functions enabled' );
 7+ #die( 'StringFunctionsEscaped::You must have extension StringFunctions or extension ParserFunctions with string functions enabled' );
88 /*
99
1010 Defines a superset of string parser functions that allow character escaping in the 'search for' and 'replace with' arguments.
@@ -67,50 +67,32 @@
6868 );
6969
7070 $dir = dirname( __FILE__ ) . '/';
71 -# RFU
72 -# $wgExtensionMessagesFiles['StringFunctionsEscaped'] = $dir . 'StringFunctionsEscaped.i18n.php';
7371
74 -$wgExtensionFunctions[] = 'wfStringFunctionsEscaped';
 72+$wgExtensionMessagesFiles['StringFunctionsEscaped'] = $dir . 'StringFunctionsEscaped.i18n.php';
7573
76 -$wgHooks['LanguageGetMagic'][] = 'wfStringFunctionsEscapedLanguageGetMagic';
 74+$wgHooks['ParserFirstCallInit'][] = 'ExtStringFunctionsEscaped::onParserFirstCallInit';
7775
78 -function wfStringFunctionsEscaped ( ) {
79 - global $wgParser, $wgExtStringFunctionsEscaped;
 76+class ExtStringFunctionsEscaped {
8077
81 - $wgExtStringFunctionsEscaped = new ExtStringFunctionsEscaped ( );
 78+ public static function onParserFirstCallInit( $parser ) {
 79+ $parser->setFunctionHook( 'pos_e', array( __CLASS__, 'runPos_e' ) );
 80+ $parser->setFunctionHook( 'rpos_e', array( __CLASS__, 'runRPos_e' ) );
 81+ $parser->setFunctionHook( 'pad_e', array( __CLASS__, 'runPad_e' ) );
 82+ $parser->setFunctionHook( 'replace_e', array( __CLASS__, 'runReplace_e' ) );
 83+ $parser->setFunctionHook( 'explode_e', array( __CLASS__, 'runExplode_e' ) );
 84+ $parser->setFunctionHook( 'stripnewlines', array( __CLASS__, 'runStrip_nl' ) );
8285
83 - $wgParser->setFunctionHook('pos_e', array( &$wgExtStringFunctionsEscaped, 'runPos_e' ));
84 - $wgParser->setFunctionHook('rpos_e', array( &$wgExtStringFunctionsEscaped, 'runRPos_e' ));
85 - $wgParser->setFunctionHook('pad_e', array( &$wgExtStringFunctionsEscaped, 'runPad_e' ));
86 - $wgParser->setFunctionHook('replace_e', array( &$wgExtStringFunctionsEscaped, 'runReplace_e' ));
87 - $wgParser->setFunctionHook('explode_e', array( &$wgExtStringFunctionsEscaped, 'runExplode_e' ));
88 - $wgParser->setFunctionHook('stripnewlines', array( &$wgExtStringFunctionsEscaped, 'runStrip_nl' ));
89 -}
90 -
91 -function wfStringFunctionsEscapedLanguageGetMagic( &$magicWords, $langCode = "en" ) {
92 - switch ( $langCode ) {
93 - default:
94 - $magicWords['pos_e'] = array ( 0, 'pos_e' );
95 - $magicWords['rpos_e'] = array ( 0, 'rpos_e' );
96 - $magicWords['pad_e'] = array ( 0, 'pad_e' );
97 - $magicWords['replace_e'] = array ( 0, 'replace_e' );
98 - $magicWords['explode_e'] = array ( 0, 'explode_e' );
99 - $magicWords['stripnewlines'] = array ( 0, 'stripnewlines' );
 86+ return true;
10087 }
101 - return true;
102 -}
10388
104 -class ExtStringFunctionsEscaped {
105 -
10689 /**
10790 * {{#pos_e:value|key|offset}}
10891 * Note: If the needle is an empty string, single space is used instead.
10992 * Note: If the needle is not found, empty string is returned.
11093 * Note: The needle is limited to specific length.
11194 */
112 - function runPos_e ( &$parser, $inStr = '', $inNeedle = '', $inOffset = 0 ) {
113 - global $wgParser;
114 - list( $callback, $flags ) = $wgParser->mFunctionHooks['pos'];
 95+ public static function runPos_e ( &$parser, $inStr = '', $inNeedle = '', $inOffset = 0 ) {
 96+ list( $callback, $flags ) = $parser->mFunctionHooks['pos'];
11597 return @call_user_func_array( $callback,
11698 array_merge( array( $parser ), array( $inStr, stripcslashes( $inNeedle ), $inOffset ) ) );
11799 }
@@ -121,9 +103,8 @@
122104 * Note: If the needle is not found, -1 is returned.
123105 * Note: The needle is limited to specific length.
124106 */
125 - function runRPos_e( &$parser , $inStr = '', $inNeedle = '' ) {
126 - global $wgParser;
127 - list( $callback, $flags ) = $wgParser->mFunctionHooks['rpos'];
 107+ public static function runRPos_e( &$parser , $inStr = '', $inNeedle = '' ) {
 108+ list( $callback, $flags ) = $parser->mFunctionHooks['rpos'];
128109 return @call_user_func_array( $callback,
129110 array_merge( array( $parser ), array( $inStr, stripcslashes( $inNeedle ) ) ) );
130111 }
@@ -132,9 +113,8 @@
133114 * {{#pad_e:value|length|with|direction}}
134115 * Note: Length of the resulting string is limited.
135116 */
136 - function runPad_e( &$parser, $inStr = '', $inLen = 0, $inWith = '', $inDirection = '' ) {
137 - global $wgParser;
138 - list( $callback , $flags ) = $wgParser->mFunctionHooks['pad'];
 117+ public static function runPad_e( &$parser, $inStr = '', $inLen = 0, $inWith = '', $inDirection = '' ) {
 118+ list( $callback , $flags ) = $parser->mFunctionHooks['pad'];
139119 return @call_user_func_array( $callback,
140120 array_merge( array( $parser ), array( $inStr, $inLen, stripcslashes( $inWith ), $inDirection ) ) );
141121 }
@@ -145,9 +125,8 @@
146126 * Note: The needle is limited to specific length.
147127 * Note: The product is limited to specific length.
148128 */
149 - function runReplace_e( &$parser, $inStr = '', $inReplaceFrom = '', $inReplaceTo = '' ) {
150 - global $wgParser;
151 - list( $callback, $flags ) = $wgParser->mFunctionHooks['replace'];
 129+ public static function runReplace_e( &$parser, $inStr = '', $inReplaceFrom = '', $inReplaceTo = '' ) {
 130+ list( $callback, $flags ) = $parser->mFunctionHooks['replace'];
152131 return @call_user_func_array( $callback,
153132 array_merge( array( $parser ), array( $inStr, stripcslashes( $inReplaceFrom ), stripcslashes( $inReplaceTo ) ) ) );
154133 }
@@ -159,9 +138,8 @@
160139 * Note: The divider is limited to specific length.
161140 * Note: Empty string is returned, if there is not enough exploded chunks.
162141 */
163 - function runExplode_e( &$parser, $inStr = '', $inDiv = '', $inPos = 0 ) {
164 - global $wgParser;
165 - list( $callback, $flags ) = $wgParser->mFunctionHooks['explode'];
 142+ public static function runExplode_e( &$parser, $inStr = '', $inDiv = '', $inPos = 0 ) {
 143+ list( $callback, $flags ) = $parser->mFunctionHooks['explode'];
166144 return @call_user_func_array( $callback,
167145 array_merge(array( $parser ), array( $inStr, stripcslashes( $inDiv ), $inPos ) ));
168146 }
@@ -169,7 +147,7 @@
170148 /**
171149 * {{#stripnewlines:value}}
172150 */
173 - function runStrip_nl( &$parser , $inStr = '' ) {
 151+ public static function runStrip_nl( &$parser , $inStr = '' ) {
174152 return preg_replace( stripcslashes( '/\n\n+/' ), stripcslashes( '\n' ), $inStr );
175153 }
176154

Status & tagging log