globalfunctions.php

来自「php 开发的内容管理系统」· PHP 代码 · 共 2,006 行 · 第 1/4 页

PHP
2,006
字号
<?php/** * Global functions used everywhere * @package MediaWiki *//** * Some globals and requires needed *//** * Total number of articles * @global integer $wgNumberOfArticles */$wgNumberOfArticles = -1; # Unset/** * Total number of views * @global integer $wgTotalViews */$wgTotalViews = -1;/** * Total number of edits * @global integer $wgTotalEdits */$wgTotalEdits = -1;require_once( 'DatabaseFunctions.php' );require_once( 'LogPage.php' );require_once( 'normal/UtfNormalUtil.php' );require_once( 'XmlFunctions.php' );/** * Compatibility functions * PHP <4.3.x is not actively supported; 4.1.x and 4.2.x might or might not work. * <4.1.x will not work, as we use a number of features introduced in 4.1.0 * such as the new autoglobals. */if( !function_exists('iconv') ) {	# iconv support is not in the default configuration and so may not be present.	# Assume will only ever use utf-8 and iso-8859-1.	# This will *not* work in all circumstances.	function iconv( $from, $to, $string ) {		if(strcasecmp( $from, $to ) == 0) return $string;		if(strcasecmp( $from, 'utf-8' ) == 0) return utf8_decode( $string );		if(strcasecmp( $to, 'utf-8' ) == 0) return utf8_encode( $string );		return $string;	}}if( !function_exists('file_get_contents') ) {	# Exists in PHP 4.3.0+	function file_get_contents( $filename ) {		return implode( '', file( $filename ) );	}}if( !function_exists('is_a') ) {	# Exists in PHP 4.2.0+	function is_a( $object, $class_name ) {		return			(strcasecmp( get_class( $object ), $class_name ) == 0) ||			 is_subclass_of( $object, $class_name );	}}# UTF-8 substr function based on a PHP manual commentif ( !function_exists( 'mb_substr' ) ) {	function mb_substr( $str, $start ) {		preg_match_all( '/./us', $str, $ar );		if( func_num_args() >= 3 ) {			$end = func_get_arg( 2 );			return join( '', array_slice( $ar[0], $start, $end ) );		} else {			return join( '', array_slice( $ar[0], $start ) );		}	}}if( !function_exists( 'floatval' ) ) {	/**	 * First defined in PHP 4.2.0	 * @param mixed $var;	 * @return float	 */	function floatval( $var ) {		return (float)$var;	}}if ( !function_exists( 'array_diff_key' ) ) {	/**	 * Exists in PHP 5.1.0+	 * Not quite compatible, two-argument version only	 * Null values will cause problems due to this use of isset()	 */	function array_diff_key( $left, $right ) {		$result = $left;		foreach ( $left as $key => $value ) {			if ( isset( $right[$key] ) ) {				unset( $result[$key] );			}		}		return $result;	}}/** * Wrapper for clone() for PHP 4, for the moment. * PHP 5 won't let you declare a 'clone' function, even conditionally, * so it has to be a wrapper with a different name. */function wfClone( $object ) {	// WARNING: clone() is not a function in PHP 5, so function_exists fails.	if( version_compare( PHP_VERSION, '5.0' ) < 0 ) {		return $object;	} else {		return clone( $object );	}}/** * Where as we got a random seed * @var bool $wgTotalViews */$wgRandomSeeded = false;/** * Seed Mersenne Twister * Only necessary in PHP < 4.2.0 * * @return bool */function wfSeedRandom() {	global $wgRandomSeeded;	if ( ! $wgRandomSeeded && version_compare( phpversion(), '4.2.0' ) < 0 ) {		$seed = hexdec(substr(md5(microtime()),-8)) & 0x7fffffff;		mt_srand( $seed );		$wgRandomSeeded = true;	}}/** * Get a random decimal value between 0 and 1, in a way * not likely to give duplicate values for any realistic * number of articles. * * @return string */function wfRandom() {	# The maximum random value is "only" 2^31-1, so get two random	# values to reduce the chance of dupes	$max = mt_getrandmax();	$rand = number_format( (mt_rand() * $max + mt_rand())		/ $max / $max, 12, '.', '' );	return $rand;}/** * We want / and : to be included as literal characters in our title URLs. * %2F in the page titles seems to fatally break for some reason. * * @param $s String: * @return string*/function wfUrlencode ( $s ) {	$s = urlencode( $s );	$s = preg_replace( '/%3[Aa]/', ':', $s );	$s = preg_replace( '/%2[Ff]/', '/', $s );	return $s;}/** * Sends a line to the debug log if enabled or, optionally, to a comment in output. * In normal operation this is a NOP. * * Controlling globals: * $wgDebugLogFile - points to the log file * $wgProfileOnly - if set, normal debug messages will not be recorded. * $wgDebugRawPage - if false, 'action=raw' hits will not result in debug output. * $wgDebugComments - if on, some debug items may appear in comments in the HTML output. * * @param $text String * @param $logonly Bool: set true to avoid appearing in HTML when $wgDebugComments is set */function wfDebug( $text, $logonly = false ) {	global $wgOut, $wgDebugLogFile, $wgDebugComments, $wgProfileOnly, $wgDebugRawPage;	# Check for raw action using $_GET not $wgRequest, since the latter might not be initialised yet	if ( isset( $_GET['action'] ) && $_GET['action'] == 'raw' && !$wgDebugRawPage ) {		return;	}	if ( isset( $wgOut ) && $wgDebugComments && !$logonly ) {		$wgOut->debug( $text );	}	if ( '' != $wgDebugLogFile && !$wgProfileOnly ) {		# Strip unprintables; they can switch terminal modes when binary data		# gets dumped, which is pretty annoying.		$text = preg_replace( '![\x00-\x08\x0b\x0c\x0e-\x1f]!', ' ', $text );		@error_log( $text, 3, $wgDebugLogFile );	}}/** * Send a line to a supplementary debug log file, if configured, or main debug log if not. * $wgDebugLogGroups[$logGroup] should be set to a filename to send to a separate log. * * @param $logGroup String * @param $text String * @param $public Bool: whether to log the event in the public log if no private *                     log file is specified, (default true) */function wfDebugLog( $logGroup, $text, $public = true ) {	global $wgDebugLogGroups, $wgDBname;	if( $text{strlen( $text ) - 1} != "\n" ) $text .= "\n";	if( isset( $wgDebugLogGroups[$logGroup] ) ) {		$time = wfTimestamp( TS_DB );		@error_log( "$time $wgDBname: $text", 3, $wgDebugLogGroups[$logGroup] );	} else if ( $public === true ) {		wfDebug( $text, true );	}}/** * Log for database errors * @param $text String: database error message. */function wfLogDBError( $text ) {	global $wgDBerrorLog;	if ( $wgDBerrorLog ) {		$host = trim(`hostname`);		$text = date('D M j G:i:s T Y') . "\t$host\t".$text;		error_log( $text, 3, $wgDBerrorLog );	}}/** * @todo document */function logProfilingData() {	global $wgRequestTime, $wgDebugLogFile, $wgDebugRawPage, $wgRequest;	global $wgProfiling, $wgUser;	$now = wfTime();	$elapsed = $now - $wgRequestTime;	if ( $wgProfiling ) {		$prof = wfGetProfilingOutput( $wgRequestTime, $elapsed );		$forward = '';		if( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )			$forward = ' forwarded for ' . $_SERVER['HTTP_X_FORWARDED_FOR'];		if( !empty( $_SERVER['HTTP_CLIENT_IP'] ) )			$forward .= ' client IP ' . $_SERVER['HTTP_CLIENT_IP'];		if( !empty( $_SERVER['HTTP_FROM'] ) )			$forward .= ' from ' . $_SERVER['HTTP_FROM'];		if( $forward )			$forward = "\t(proxied via {$_SERVER['REMOTE_ADDR']}{$forward})";		if( is_object($wgUser) && $wgUser->isAnon() )			$forward .= ' anon';		$log = sprintf( "%s\t%04.3f\t%s\n",		  gmdate( 'YmdHis' ), $elapsed,		  urldecode( $_SERVER['REQUEST_URI'] . $forward ) );		if ( '' != $wgDebugLogFile && ( $wgRequest->getVal('action') != 'raw' || $wgDebugRawPage ) ) {			error_log( $log . $prof, 3, $wgDebugLogFile );		}	}}/** * Check if the wiki read-only lock file is present. This can be used to lock * off editing functions, but doesn't guarantee that the database will not be * modified. * @return bool */function wfReadOnly() {	global $wgReadOnlyFile, $wgReadOnly;	if ( !is_null( $wgReadOnly ) ) {		return (bool)$wgReadOnly;	}	if ( '' == $wgReadOnlyFile ) {		return false;	}	// Set $wgReadOnly for faster access next time	if ( is_file( $wgReadOnlyFile ) ) {		$wgReadOnly = file_get_contents( $wgReadOnlyFile );	} else {		$wgReadOnly = false;	}	return (bool)$wgReadOnly;}/** * Get a message from anywhere, for the current user language. * * Use wfMsgForContent() instead if the message should NOT * change depending on the user preferences. * * Note that the message may contain HTML, and is therefore * not safe for insertion anywhere. Some functions such as * addWikiText will do the escaping for you. Use wfMsgHtml() * if you need an escaped message. * * @param $key String: lookup key for the message, usually *    defined in languages/Language.php */function wfMsg( $key ) {	$args = func_get_args();	array_shift( $args );	return wfMsgReal( $key, $args, true );}/** * Same as above except doesn't transform the message */function wfMsgNoTrans( $key ) {	$args = func_get_args();	array_shift( $args );	return wfMsgReal( $key, $args, true, false );}/** * Get a message from anywhere, for the current global language * set with $wgLanguageCode. * * Use this if the message should NOT change  dependent on the * language set in the user's preferences. This is the case for * most text written into logs, as well as link targets (such as * the name of the copyright policy page). Link titles, on the * other hand, should be shown in the UI language. * * Note that MediaWiki allows users to change the user interface * language in their preferences, but a single installation * typically only contains content in one language. * * Be wary of this distinction: If you use wfMsg() where you should * use wfMsgForContent(), a user of the software may have to * customize over 70 messages in order to, e.g., fix a link in every * possible language. * * @param $key String: lookup key for the message, usually *    defined in languages/Language.php */function wfMsgForContent( $key ) {	global $wgForceUIMsgAsContentMsg;	$args = func_get_args();	array_shift( $args );	$forcontent = true;	if( is_array( $wgForceUIMsgAsContentMsg ) &&		in_array( $key, $wgForceUIMsgAsContentMsg ) )		$forcontent = false;	return wfMsgReal( $key, $args, true, $forcontent );}/** * Same as above except doesn't transform the message */function wfMsgForContentNoTrans( $key ) {	global $wgForceUIMsgAsContentMsg;	$args = func_get_args();	array_shift( $args );	$forcontent = true;	if( is_array( $wgForceUIMsgAsContentMsg ) &&		in_array( $key, $wgForceUIMsgAsContentMsg ) )		$forcontent = false;	return wfMsgReal( $key, $args, true, $forcontent, false );}/** * Get a message from the language file, for the UI elements */function wfMsgNoDB( $key ) {	$args = func_get_args();	array_shift( $args );	return wfMsgReal( $key, $args, false );}/** * Get a message from the language file, for the content */function wfMsgNoDBForContent( $key ) {	global $wgForceUIMsgAsContentMsg;	$args = func_get_args();	array_shift( $args );	$forcontent = true;	if( is_array( $wgForceUIMsgAsContentMsg ) &&		in_array( $key,	$wgForceUIMsgAsContentMsg ) )		$forcontent = false;	return wfMsgReal( $key, $args, false, $forcontent );}/** * Really get a message * @return $key String: key to get. * @return $args * @return $useDB Boolean * @return String: the requested message. */function wfMsgReal( $key, $args, $useDB = true, $forContent=false, $transform = true ) {	$fname = 'wfMsgReal';	$message = wfMsgGetKey( $key, $useDB, $forContent, $transform );	$message = wfMsgReplaceArgs( $message, $args );	return $message;}/** * This function provides the message source for messages to be edited which are *not* stored in the database. * @param $key String: */function wfMsgWeirdKey ( $key ) {	$subsource = str_replace ( ' ' , '_' , $key ) ;	$source = wfMsgForContentNoTrans( $subsource ) ;	if ( $source == "&lt;{$subsource}&gt;" ) {		# Try again with first char lower case		$subsource = strtolower ( substr ( $subsource , 0 , 1 ) ) . substr ( $subsource , 1 ) ;		$source = wfMsgForContentNoTrans( $subsource ) ;	}	if ( $source == "&lt;{$subsource}&gt;" ) {		# Didn't work either, return blank text		$source = "" ;	}	return $source ;}/** * Fetch a message string value, but don't replace any keys yet. * @param string $key * @param bool $useDB * @param bool $forContent * @return string * @private */function wfMsgGetKey( $key, $useDB, $forContent = false, $transform = true ) {	global $wgParser, $wgMsgParserOptions, $wgContLang, $wgMessageCache, $wgLang;	if ( is_object( $wgMessageCache ) )		$transstat = $wgMessageCache->getTransform();	if( is_object( $wgMessageCache ) ) {		if ( ! $transform )			$wgMessageCache->disableTransform();		$message = $wgMessageCache->get( $key, $useDB, $forContent );	} else {		if( $forContent ) {			$lang = &$wgContLang;		} else {			$lang = &$wgLang;		}		wfSuppressWarnings();		if( is_object( $lang ) ) {			$message = $lang->getMessage( $key );		} else {			$message = false;		}		wfRestoreWarnings();		if($message === false)			$message = Language::getMessage($key);		if ( $transform && strstr( $message, '{{' ) !== false ) {			$message = $wgParser->transformMsg($message, $wgMsgParserOptions);		}	}	if ( is_object( $wgMessageCache ) && ! $transform )		$wgMessageCache->setTransform( $transstat );	return $message;}/** * Replace message parameter keys on the given formatted output. * * @param string $message * @param array $args * @return string * @private */function wfMsgReplaceArgs( $message, $args ) {	# Fix windows line-endings	# Some messages are split with explode("\n", $msg)	$message = str_replace( "\r", '', $message );	// Replace arguments	if ( count( $args ) ) {		if ( is_array( $args[0] ) ) {			foreach ( $args[0] as $key => $val ) {				$message = str_replace( '$' . $key, $val, $message );			}		} else {			foreach( $args as $n => $param ) {				$replacementKeys['$' . ($n + 1)] = $param;			}			$message = strtr( $message, $replacementKeys );

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?