r79479 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r79478‎ | r79479 | r79480 >
Date:06:48, 2 January 2011
Author:soxred93
Status:ok (Comments)
Tags:
Comment:
Per my comment on r68760: Make MWfunction class, complete with call_user_func helper functions that automatically
make the callback PHP 5.1 compatible with the Class::Method syntax. Add Unit tests to supplement it.
Modified paths:
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/MWFunction.php (added) (history)
  • /trunk/phase3/includes/WebStart.php (modified) (history)
  • /trunk/phase3/maintenance/doMaintenance.php (modified) (history)
  • /trunk/phase3/tests/phpunit/includes/MWFunctionTest.php (added) (history)

Diff [purge]

Index: trunk/phase3/maintenance/doMaintenance.php
@@ -65,12 +65,7 @@
6666
6767 if ( defined( 'MW_CONFIG_CALLBACK' ) ) {
6868 # Use a callback function to configure MediaWiki
69 - $callback = MW_CONFIG_CALLBACK;
70 - # PHP 5.1 doesn't support "class::method" for call_user_func, so split it
71 - if ( strpos( $callback, '::' ) !== false ) {
72 - $callback = explode( '::', $callback, 2 );
73 - }
74 - call_user_func( $callback );
 69+ MWFunction::call( MW_CONFIG_CALLBACK );
7570 } elseif ( file_exists( "$IP/wmf-config/wikimedia-mode" ) ) {
7671 // Load settings, using wikimedia-mode if needed
7772 // Fixme: replace this hack with general farm-friendly code
Index: trunk/phase3/tests/phpunit/includes/MWFunctionTest.php
@@ -0,0 +1,34 @@
 2+<?php
 3+
 4+class MWFunctionTest extends MediaWikiTestCase {
 5+
 6+ function testCallUserFuncWorkarounds() {
 7+
 8+ $this->assertEquals(
 9+ MWFunction::call( 'MWFunctionTest::someMethod' ),
 10+ call_user_func( array( 'MWFunctionTest', 'someMethod' ) )
 11+ );
 12+ $this->assertEquals(
 13+ MWFunction::call( 'MWFunctionTest::someMethod', 'foo', 'bar', 'baz' ),
 14+ call_user_func( array( 'MWFunctionTest', 'someMethod' ), 'foo', 'bar', 'baz' )
 15+ );
 16+
 17+
 18+
 19+ $this->assertEquals(
 20+ MWFunction::callArray( 'MWFunctionTest::someMethod', array() ),
 21+ call_user_func_array( array( 'MWFunctionTest', 'someMethod' ), array() )
 22+ );
 23+ $this->assertEquals(
 24+ MWFunction::callArray( 'MWFunctionTest::someMethod', array( 'foo', 'bar', 'baz' ) ),
 25+ call_user_func_array( array( 'MWFunctionTest', 'someMethod' ), array( 'foo', 'bar', 'baz' ) )
 26+ );
 27+
 28+ }
 29+
 30+ public static function someMethod() {
 31+ return func_get_args();
 32+ }
 33+
 34+}
 35+
Property changes on: trunk/phase3/tests/phpunit/includes/MWFunctionTest.php
___________________________________________________________________
Added: svn:eol-style
136 + native
Index: trunk/phase3/includes/MWFunction.php
@@ -0,0 +1,52 @@
 2+<?php
 3+
 4+/**
 5+ * This program is free software; you can redistribute it and/or modify
 6+ * it under the terms of the GNU General Public License as published by
 7+ * the Free Software Foundation; either version 2 of the License, or
 8+ * (at your option) any later version.
 9+ *
 10+ * This program is distributed in the hope that it will be useful,
 11+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 13+ * GNU General Public License for more details.
 14+ *
 15+ * You should have received a copy of the GNU General Public License along
 16+ * with this program; if not, write to the Free Software Foundation, Inc.,
 17+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 18+ * http://www.gnu.org/copyleft/gpl.html
 19+ *
 20+ */
 21+
 22+class MWFunction {
 23+
 24+ protected static function cleanCallback( $callback ) {
 25+
 26+ if( is_string( $callback ) ) {
 27+ if ( in_string( '::', $callback ) ) {
 28+ //PHP 5.1 cannot use call_user_func( 'Class::Method' )
 29+ //It can only handle only call_user_func( array( 'Class', 'Method' ) )
 30+ $callback = explode( '::', $callback, 2);
 31+ }
 32+ }
 33+
 34+ return $callback;
 35+ }
 36+
 37+ public static function call( $callback ) {
 38+ $callback = self::cleanCallback( $callback );
 39+
 40+ $args = func_get_args();
 41+
 42+ return call_user_func_array( 'call_user_func', $args );
 43+
 44+ }
 45+
 46+ public static function callArray( $callback, $params ) {
 47+
 48+ $callback = self::cleanCallback( $callback );
 49+ return call_user_func_array( $callback, $params );
 50+
 51+ }
 52+
 53+}
Property changes on: trunk/phase3/includes/MWFunction.php
___________________________________________________________________
Added: svn:eol-style
154 + native
Index: trunk/phase3/includes/WebStart.php
@@ -96,12 +96,8 @@
9797
9898 if ( defined( 'MW_CONFIG_CALLBACK' ) ) {
9999 # Use a callback function to configure MediaWiki
100 - $callback = MW_CONFIG_CALLBACK;
101 - # PHP 5.1 doesn't support "class::method" for call_user_func, so split it
102 - if ( strpos( $callback, '::' ) !== false ) {
103 - $callback = explode( '::', $callback, 2);
104 - }
105 - call_user_func( $callback );
 100+ MWFunction::call( MW_CONFIG_CALLBACK );
 101+
106102 } else {
107103 if ( !defined('MW_CONFIG_FILE') )
108104 define('MW_CONFIG_FILE', "$IP/LocalSettings.php");
Index: trunk/phase3/includes/AutoLoader.php
@@ -166,6 +166,7 @@
167167 'MessageCache' => 'includes/MessageCache.php',
168168 'MimeMagic' => 'includes/MimeMagic.php',
169169 'MWException' => 'includes/Exception.php',
 170+ 'MWFunction' => 'includes/MWFunction.php',
170171 'MWHttpRequest' => 'includes/HttpFunctions.php',
171172 'MWMemcached' => 'includes/memcached-client.php',
172173 'MWNamespace' => 'includes/Namespace.php',

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r68760r68756 - Make the use of MW_CONFIG_CALLBACK compatible with PHP 5.1mah04:25, 30 June 2010

Comments

#Comment by 😂 (talk | contribs)   00:07, 1 June 2011

Can we please not use in_string() and get rid of the silly thing? Just use strpos().

Status & tagging log