globalfunctions.php

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

PHP
2,006
字号
		}	}	return $retVal;}/** * wfMerge attempts to merge differences between three texts. * Returns true for a clean merge and false for failure or a conflict. */function wfMerge( $old, $mine, $yours, &$result ){	global $wgDiff3;	# This check may also protect against code injection in	# case of broken installations.	if(! file_exists( $wgDiff3 ) ){		wfDebug( "diff3 not found\n" );		return false;	}	# Make temporary files	$td = wfTempDir();	$oldtextFile = fopen( $oldtextName = tempnam( $td, 'merge-old-' ), 'w' );	$mytextFile = fopen( $mytextName = tempnam( $td, 'merge-mine-' ), 'w' );	$yourtextFile = fopen( $yourtextName = tempnam( $td, 'merge-your-' ), 'w' );	fwrite( $oldtextFile, $old ); fclose( $oldtextFile );	fwrite( $mytextFile, $mine ); fclose( $mytextFile );	fwrite( $yourtextFile, $yours ); fclose( $yourtextFile );	# Check for a conflict	$cmd = $wgDiff3 . ' -a --overlap-only ' .	  wfEscapeShellArg( $mytextName ) . ' ' .	  wfEscapeShellArg( $oldtextName ) . ' ' .	  wfEscapeShellArg( $yourtextName );	$handle = popen( $cmd, 'r' );	if( fgets( $handle, 1024 ) ){		$conflict = true;	} else {		$conflict = false;	}	pclose( $handle );	# Merge differences	$cmd = $wgDiff3 . ' -a -e --merge ' .	  wfEscapeShellArg( $mytextName, $oldtextName, $yourtextName );	$handle = popen( $cmd, 'r' );	$result = '';	do {		$data = fread( $handle, 8192 );		if ( strlen( $data ) == 0 ) {			break;		}		$result .= $data;	} while ( true );	pclose( $handle );	unlink( $mytextName ); unlink( $oldtextName ); unlink( $yourtextName );	if ( $result === '' && $old !== '' && $conflict == false ) {		wfDebug( "Unexpected null result from diff3. Command: $cmd\n" );		$conflict = true;	}	return ! $conflict;}/** * @todo document */function wfVarDump( $var ) {	global $wgOut;	$s = str_replace("\n","<br />\n", var_export( $var, true ) . "\n");	if ( headers_sent() || !@is_object( $wgOut ) ) {		print $s;	} else {		$wgOut->addHTML( $s );	}}/** * Provide a simple HTTP error. */function wfHttpError( $code, $label, $desc ) {	global $wgOut;	$wgOut->disable();	header( "HTTP/1.0 $code $label" );	header( "Status: $code $label" );	$wgOut->sendCacheControl();	header( 'Content-type: text/html' );	print "<html><head><title>" .		htmlspecialchars( $label ) .		"</title></head><body><h1>" .		htmlspecialchars( $label ) .		"</h1><p>" .		htmlspecialchars( $desc ) .		"</p></body></html>\n";}/** * Converts an Accept-* header into an array mapping string values to quality * factors */function wfAcceptToPrefs( $accept, $def = '*/*' ) {	# No arg means accept anything (per HTTP spec)	if( !$accept ) {		return array( $def => 1 );	}	$prefs = array();	$parts = explode( ',', $accept );	foreach( $parts as $part ) {		# FIXME: doesn't deal with params like 'text/html; level=1'		@list( $value, $qpart ) = explode( ';', $part );		if( !isset( $qpart ) ) {			$prefs[$value] = 1;		} elseif( preg_match( '/q\s*=\s*(\d*\.\d+)/', $qpart, $match ) ) {			$prefs[$value] = $match[1];		}	}	return $prefs;}/** * Checks if a given MIME type matches any of the keys in the given * array. Basic wildcards are accepted in the array keys. * * Returns the matching MIME type (or wildcard) if a match, otherwise * NULL if no match. * * @param string $type * @param array $avail * @return string * @private */function mimeTypeMatch( $type, $avail ) {	if( array_key_exists($type, $avail) ) {		return $type;	} else {		$parts = explode( '/', $type );		if( array_key_exists( $parts[0] . '/*', $avail ) ) {			return $parts[0] . '/*';		} elseif( array_key_exists( '*/*', $avail ) ) {			return '*/*';		} else {			return NULL;		}	}}/** * Returns the 'best' match between a client's requested internet media types * and the server's list of available types. Each list should be an associative * array of type to preference (preference is a float between 0.0 and 1.0). * Wildcards in the types are acceptable. * * @param array $cprefs Client's acceptable type list * @param array $sprefs Server's offered types * @return string * * @todo FIXME: doesn't handle params like 'text/plain; charset=UTF-8' * XXX: generalize to negotiate other stuff */function wfNegotiateType( $cprefs, $sprefs ) {	$combine = array();	foreach( array_keys($sprefs) as $type ) {		$parts = explode( '/', $type );		if( $parts[1] != '*' ) {			$ckey = mimeTypeMatch( $type, $cprefs );			if( $ckey ) {				$combine[$type] = $sprefs[$type] * $cprefs[$ckey];			}		}	}	foreach( array_keys( $cprefs ) as $type ) {		$parts = explode( '/', $type );		if( $parts[1] != '*' && !array_key_exists( $type, $sprefs ) ) {			$skey = mimeTypeMatch( $type, $sprefs );			if( $skey ) {				$combine[$type] = $sprefs[$skey] * $cprefs[$type];			}		}	}	$bestq = 0;	$besttype = NULL;	foreach( array_keys( $combine ) as $type ) {		if( $combine[$type] > $bestq ) {			$besttype = $type;			$bestq = $combine[$type];		}	}	return $besttype;}/** * Array lookup * Returns an array where the values in the first array are replaced by the * values in the second array with the corresponding keys * * @return array */function wfArrayLookup( $a, $b ) {	return array_flip( array_intersect( array_flip( $a ), array_keys( $b ) ) );}/** * Convenience function; returns MediaWiki timestamp for the present time. * @return string */function wfTimestampNow() {	# return NOW	return wfTimestamp( TS_MW, time() );}/** * Reference-counted warning suppression */function wfSuppressWarnings( $end = false ) {	static $suppressCount = 0;	static $originalLevel = false;	if ( $end ) {		if ( $suppressCount ) {			--$suppressCount;			if ( !$suppressCount ) {				error_reporting( $originalLevel );			}		}	} else {		if ( !$suppressCount ) {			$originalLevel = error_reporting( E_ALL & ~( E_WARNING | E_NOTICE ) );		}		++$suppressCount;	}}/** * Restore error level to previous value */function wfRestoreWarnings() {	wfSuppressWarnings( true );}# Autodetect, convert and provide timestamps of various types/** * Unix time - the number of seconds since 1970-01-01 00:00:00 UTC */define('TS_UNIX', 0);/** * MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS) */define('TS_MW', 1);/** * MySQL DATETIME (YYYY-MM-DD HH:MM:SS) */define('TS_DB', 2);/** * RFC 2822 format, for E-mail and HTTP headers */define('TS_RFC2822', 3);/** * ISO 8601 format with no timezone: 1986-02-09T20:00:00Z * * This is used by Special:Export */define('TS_ISO_8601', 4);/** * An Exif timestamp (YYYY:MM:DD HH:MM:SS) * * @url http://exif.org/Exif2-2.PDF The Exif 2.2 spec, see page 28 for the *       DateTime tag and page 36 for the DateTimeOriginal and *       DateTimeDigitized tags. */define('TS_EXIF', 5);/** * Oracle format time. */define('TS_ORACLE', 6);/** * @param mixed $outputtype A timestamp in one of the supported formats, the *                          function will autodetect which format is supplied *                          and act accordingly. * @return string Time in the format specified in $outputtype */function wfTimestamp($outputtype=TS_UNIX,$ts=0) {	$uts = 0;	$da = array();	if ($ts==0) {		$uts=time();	} elseif (preg_match("/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)$/D",$ts,$da)) {		# TS_DB		$uts=gmmktime((int)$da[4],(int)$da[5],(int)$da[6],			    (int)$da[2],(int)$da[3],(int)$da[1]);	} elseif (preg_match("/^(\d{4}):(\d\d):(\d\d) (\d\d):(\d\d):(\d\d)$/D",$ts,$da)) {		# TS_EXIF		$uts=gmmktime((int)$da[4],(int)$da[5],(int)$da[6],			(int)$da[2],(int)$da[3],(int)$da[1]);	} elseif (preg_match("/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/D",$ts,$da)) {		# TS_MW		$uts=gmmktime((int)$da[4],(int)$da[5],(int)$da[6],			    (int)$da[2],(int)$da[3],(int)$da[1]);	} elseif (preg_match("/^(\d{1,13})$/D",$ts,$datearray)) {		# TS_UNIX		$uts = $ts;	} elseif (preg_match('/^(\d{1,2})-(...)-(\d\d(\d\d)?) (\d\d)\.(\d\d)\.(\d\d)/', $ts, $da)) {		# TS_ORACLE		$uts = strtotime(preg_replace('/(\d\d)\.(\d\d)\.(\d\d)(\.(\d+))?/', "$1:$2:$3",				str_replace("+00:00", "UTC", $ts)));	} elseif (preg_match('/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z$/', $ts, $da)) {		# TS_ISO_8601		$uts=gmmktime((int)$da[4],(int)$da[5],(int)$da[6],			(int)$da[2],(int)$da[3],(int)$da[1]);	} else {		# Bogus value; fall back to the epoch...		wfDebug("wfTimestamp() fed bogus time value: $outputtype; $ts\n");		$uts = 0;	} 	switch($outputtype) {		case TS_UNIX:			return $uts;		case TS_MW:			return gmdate( 'YmdHis', $uts );		case TS_DB:			return gmdate( 'Y-m-d H:i:s', $uts );		case TS_ISO_8601:			return gmdate( 'Y-m-d\TH:i:s\Z', $uts );		// This shouldn't ever be used, but is included for completeness		case TS_EXIF:			return gmdate(  'Y:m:d H:i:s', $uts );		case TS_RFC2822:			return gmdate( 'D, d M Y H:i:s', $uts ) . ' GMT';		case TS_ORACLE:			return gmdate( 'd-M-y h.i.s A', $uts) . ' +00:00';		default:			throw new MWException( 'wfTimestamp() called with illegal output type.');	}}/** * Return a formatted timestamp, or null if input is null. * For dealing with nullable timestamp columns in the database. * @param int $outputtype * @param string $ts * @return string */function wfTimestampOrNull( $outputtype = TS_UNIX, $ts = null ) {	if( is_null( $ts ) ) {		return null;	} else {		return wfTimestamp( $outputtype, $ts );	}}/** * Check if the operating system is Windows * * @return bool True if it's Windows, False otherwise. */function wfIsWindows() {	if (substr(php_uname(), 0, 7) == 'Windows') {		return true;	} else {		return false;	}}/** * Swap two variables */function swap( &$x, &$y ) {	$z = $x;	$x = $y;	$y = $z;}function wfGetCachedNotice( $name ) {	global $wgOut, $parserMemc, $wgDBname;	$fname = 'wfGetCachedNotice';	wfProfileIn( $fname );		$needParse = false;	$notice = wfMsgForContent( $name );	if( $notice == '&lt;'. $name . ';&gt' || $notice == '-' ) {		wfProfileOut( $fname );		return( false );	}		$cachedNotice = $parserMemc->get( $wgDBname . ':' . $name );	if( is_array( $cachedNotice ) ) {		if( md5( $notice ) == $cachedNotice['hash'] ) {			$notice = $cachedNotice['html'];		} else {			$needParse = true;		}	} else {		$needParse = true;	}		if( $needParse ) {		if( is_object( $wgOut ) ) {			$parsed = $wgOut->parse( $notice );			$parserMemc->set( $wgDBname . ':' . $name, array( 'html' => $parsed, 'hash' => md5( $notice ) ), 600 );			$notice = $parsed;		} else {			wfDebug( 'wfGetCachedNotice called for ' . $name . ' with no $wgOut available' );			$notice = '';		}	}		wfProfileOut( $fname );	return $notice;}function wfGetNamespaceNotice() {	global $wgTitle;		# Paranoia	if ( !isset( $wgTitle ) || !is_object( $wgTitle ) )		return "";	$fname = 'wfGetNamespaceNotice';	wfProfileIn( $fname );		$key = "namespacenotice-" . $wgTitle->getNsText();	$namespaceNotice = wfGetCachedNotice( $key );	if ( $namespaceNotice && substr ( $namespaceNotice , 0 ,7 ) != "<p>&lt;" ) {		 $namespaceNotice = '<div id="namespacebanner">' . $namespaceNotice . "</div>";	} else {		$namespaceNotice = "";	}	wfProfileOut( $fname );	return $namespaceNotice;}function wfGetSiteNotice() {	global $wgUser, $wgSiteNotice;	$fname = 'wfGetSiteNotice';	wfProfileIn( $fname );	$siteNotice = '';			if( wfRunHooks( 'SiteNoticeBefore', array( &$siteNotice ) ) ) {		if( is_object( $wgUser ) && $wgUser->isLoggedIn() ) {			$siteNotice = wfGetCachedNotice( 'sitenotice' );			$siteNotice = !$siteNotice ? $wgSiteNotice : $siteNotice;		} else {			$anonNotice = wfGetCachedNotice( 'anonnotice' );			if( !$anonNotice ) {				$siteNotice = wfGetCachedNotice( 'sitenotice' );				$siteNotice = !$siteNotice ? $wgSiteNotice : $siteNotice;			} else {				$siteNotice = $anonNotice;			}		}	}	wfRunHooks( 'SiteNoticeAfter', array( &$siteNotice ) );	wfProfileOut( $fname );	return $siteNotice;}/** Global singleton instance of MimeMagic. This is initialized on demand,* please always use the wfGetMimeMagic() function to get the instance.** @private*/$wgMimeMagic= NULL;/** Factory functions for the global MimeMagic object.* This function always returns the same singleton instance of MimeMagic.* That objects will be instantiated on the first call to this function.* If needed, the MimeMagic.php file is automatically included by this function.* @return MimeMagic the global MimeMagic objects.*/function &wfGetMimeMagic() {	global $wgMimeMagic;	if (!is_null($wgMimeMagic)) {		return $wgMimeMagic;	}	if (!class_exists("MimeMagic")) {		#include on demand		require_once("MimeMagic.php");	}

⌨️ 快捷键说明

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