<commit_before><?php
# Copyright (C) 2008	<NAME>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

require_once( 'MantisSourcePlugin.class.php' );

/**
 * General source control integration API.
 * @author <NAME>
 */

global $g_Source_cache_types;
$g_Source_cache_types = null;

function SourceType( $p_type ) {
	$t_types = SourceTypes();

	if ( isset( $t_types[$p_type] ) ) {
		return $t_types[$p_type];
	}

	return $p_type;
}

function SourceTypes() {
	global $g_Source_cache_types;

	if ( is_null( $g_Source_cache_types ) ) {
		$t_types = array();

		$t_raw_data = event_signal( 'EVENT_SOURCE_GET_TYPES' );
		foreach ( $t_raw_data as $t_plugin => $t_callbacks ) {
			foreach ( $t_callbacks as $t_callback => $t_data ) {
				foreach ( $t_data as $t_type => $t_name ) {
					$t_types[$t_type] = $t_name;
				}
			}
		}

		asort( $t_types );
		$g_Source_cache_types = $t_types;
	}

	return $g_Source_cache_types;
}

function Source_Parse_Buglinks( $p_string ) {
	$t_bugs = array();

	$t_regex1 = plugin_config_get( 'buglink_regex_1', null, 'Source' );
	$t_regex2 = plugin_config_get( 'buglink_regex_2', null, 'Source' );

	preg_match_all( $t_regex1, $p_string, $t_matches_all );

	foreach( $t_matches_all[0] as $t_substring ) {
		preg_match_all( $t_regex2, $t_substring, $t_matches );
		foreach ( $t_matches[1] as $t_match ) {
			if ( 0 < (int)$t_match ) {
				$t_bugs[$t_match] = true;
			}
		}
	}

	return array_keys( $t_bugs );
}

/**
 * Given a set of changeset objects, parse the bug links
 * and save the changes.
 * @param array Changeset objects
 */
function Source_Process_Buglinks( $p_changesets ) {
	if ( !is_array( $p_changesets ) ) {
		return;
	}

	# Parse normal bug links
	foreach( $p_changesets as $t_changeset ) {
		$t_changeset->bugs = Source_Parse_Buglinks( $t_changeset->message );<commit_msg>Issue #3: Duplicate the existing buglink parsing for bugfix links and add basic caching.<commit_after><?php
# Copyright (C) 2008	<NAME>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

require_once( 'MantisSourcePlugin.class.php' );

/**
 * General source control integration API.
 * @author <NAME>
 */

global $g_Source_cache_types;
$g_Source_cache_types = null;

function SourceType( $p_type ) {
	$t_types = SourceTypes();

	if ( isset( $t_types[$p_type] ) ) {
		return $t_types[$p_type];
	}

	return $p_type;
}

function SourceTypes() {
	global $g_Source_cache_types;

	if ( is_null( $g_Source_cache_types ) ) {
		$t_types = array();

		$t_raw_data = event_signal( 'EVENT_SOURCE_GET_TYPES' );
		foreach ( $t_raw_data as $t_plugin => $t_callbacks ) {
			foreach ( $t_callbacks as $t_callback => $t_data ) {
				foreach ( $t_data as $t_type => $t_name ) {
					$t_types[$t_type] = $t_name;
				}
			}
		}

		asort( $t_types );
		$g_Source_cache_types = $t_types;
	}

	return $g_Source_cache_types;
}

/**
 * Parse basic bug links from a changeset commit message
 * and return a list of referenced bug IDs.
 * @param string Changeset commit message
 * @return array Bug IDs
 */
function Source_Parse_Buglinks( $p_string ) {
	static $s_regex1, $s_regex2;

	$t_bugs = array();

	if ( is_null( $s_regex1 ) ) {
		$s_regex1 = plugin_config_get( 'buglink_regex_1', null, 'Source' );
		$s_regex2 = plugin_config_get( 'buglink_regex_2', null, 'Source' );
	}

	preg_match_all( $t_regex1, $p_string, $t_matches_all );

	foreach( $t_matches_all[0] as $t_substring ) {
		preg_match_all( $t_regex2, $t_substring, $t_matches );
		foreach ( $t_matches[1] as $t_match ) {
			if ( 0 < (int)$t_match ) {
				$t_bugs[$t_match] = true;
			}
		}
	}

	return array_keys( $t_bugs );
}

/**
 * Parse resolved bug fix links from a changeset commit message
 * and return a list of referenced bug IDs.
 * @param string Changeset commit message
 * @return array Bug IDs
 */
function Source_Parse_Bugfixes( $p_string ) {
	static $s_regex1, $s_regex2;

	$t_bugs = array();

	if ( is_null( $s_regex1 ) ) {
		$s_regex1 = plugin_config_get( 'bugfix_regex_1', null, 'Source' );
		$s_regex2 = plugin_config_get( 'bugfix_regex_2', null, 'Source' );
	}

	preg_match_all( $t_regex1, $p_string, $t_matches_all );

	foreach( $t_matches_all[0] as $t_substring ) {
		preg_match_all( $t_regex2, $t_substring, $t_matches );
		foreach ( $t_matches[1] as $t_match ) {
			if ( 0 < (int)$t_match ) {
				$t_bugs[$t_match] = true;
			}
		}
	}

	return array_keys( $t_bugs );
}

/**
 * Given a set of changeset objects, parse the bug links
 * and save the changes.
 * @param array Changeset objects
 */
function Source_Process_Buglinks( $p_changesets ) {
	if ( !is_array( $p_changesets ) ) {
		return;
	}

	# Parse normal bug links
	foreach( $p_changesets as $t_changeset ) {
		$t_changeset->bugs = Source_Parse_Buglinks( $t_changeset->message );
{'fraction_non_alphanumeric': 0.12053571428571429, 'fraction_numerical': 0.007857142857142858, 'mean_word_length': 4.2296918767507, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3909031', 'n_tokens_mistral': 2161, 'n_tokens_neox': 2016, 'n_words': 555}