<?php
$wgExtensionCredits['parserhook'][] = array(
        'name' => 'SectionVariables',
        'url' => 'http://meta.wikimedia.org/wiki/SectionVariables',
        'author' => 'Kinsey Moore',
        'description' => 'Enhance parser with ability to grab section headings and numbering.',
);
$wgHooks['ParserAfterStrip'][] = 'doSourceSectionVars';
$wgHooks['InternalParseBeforeLinks'][] = 'doSectionVars';
function doSourceSectionVars(&$parser, &$text, &$strip_state) {
	global $action; // Access the global "action" variable
	// Only do the replacement if the action is not edit or history
	if(
		$action !== 'edit'
		&& $action !== 'history'
		&& $action !== 'delete'
		&& $action !== 'watch'
		&& strpos( $parser->mTitle->mPrefixedText, 'Special:' ) === false
		&& $parser->mTitle->mNamespace !== 8
	) {
		$text = doSectVars($text,true);
	}
	return true;
}

function doSectionVars(&$parser, &$text, &$strip_state) {
	global $action; // Access the global "action" variable
	// Only do the replacement if the action is not edit or history
	if(
		$action !== 'edit'
		&& $action !== 'history'
		&& $action !== 'delete'
		&& $action !== 'watch'
		&& strpos( $parser->mTitle->mPrefixedText, 'Special:' ) === false
		&& $parser->mTitle->mNamespace !== 8
	) {
		$text = doSectVars($text);
	}
	return true;
}

function doSectVars( $text, $doSrc = false, $prefix = "") {
	$srctext = "";
	if ($doSrc == true) {
		$srctext = "source";
	}
	# this function should probably take on the functions of formatHeadings
	# split on header openings and on section specifiers 
	$blocks = preg_split( "/(^={1,6}.*={1,6}\\s*$|\\[\\[section::.*?\\]\\]|<H[1-6].*?>.*?<\/H[1-6] *>)/mi", $text, -1, PREG_SPLIT_DELIM_CAPTURE );
	$outtext = '';
	$hcurrdepth = -1;
	$htext = '';
	$hlengths = array();
	$hlevels = array();
	foreach ($blocks as $block) {
		# take care of header generation
		if (preg_match("/^(={1,6})(.*)\\1\\s*$/",$block,$matches)) {
			$hdepth = strlen($matches[1]);
			$htext = $matches[2];
			$outtext .= $block;
			$updatestatus = true;
		# no modification takes place here
		} elseif ($doSrc == false && preg_match("/^<H([1-6]).*?>(.*?)<\/H[1-6] *>$/i",$block,$matches)) {
			$hdepth = $matches[1];
			$htext = $matches[2];
			$outtext .= $block;
			$updatestatus = true;
		# take care of context sensitive variable substitution
		} elseif ($doSrc == false && preg_match("/^\\[\\[section::name\\]\\]$/i",$block,$matches)) {
			$outtext .= $htext;
		} elseif (preg_match("/^\\[\\[section::".$srctext."number\\]\\]$/i",$block,$matches)) {
			$tmp = implode('.',$hlengths);
			if ($tmp == "") {
				$tmp = 0;
			}
			$outtext .= $prefix.$tmp;
		} else {
			$outtext .= $block;
		}

		# update status if necessary
		if ($updatestatus == true) {
			$updatestatus = false;
			# starting a new sublevel
			if ($hcurrdepth == -1 || $hdepth > $hlevels[$hcurrdepth]) {
				$hcurrdepth++;
				$hlevels[$hcurrdepth] = $hdepth;
				$hlengths[$hcurrdepth] = 1;
			# remaining in the current sublevel
			} elseif ($hdepth <= $hlevels[$hcurrdepth] && ($hcurrdepth == 0 || $hdepth > $hlevels[$hcurrdepth-1])) {
				$hlengths[$hcurrdepth]++;
				$hlevels[$hcurrdepth] = $hdepth;
			# jumping up sublevels
			} else {
				# in this case, look for the proper sublevel to jump out to
				for($i = $hcurrdepth-1;$i>=0;$i--) {
					if ($hdepth <= $hlevels[$i] && ($i == 0 || $hdepth > $hlevels[$i-1])) {
						$hcurrdepth = $i;
						break;
					}
				}
				$hlengths[$hcurrdepth]++;
				$hlevels[$hcurrdepth] = $hdepth;
				# truncate sublevels
				$hlengths = array_slice($hlengths,0,$hcurrdepth+1);
				$hlevels = array_slice($hlevels,0,$hcurrdepth+1);
			}
		}
	}
	return $outtext;
}
?>
