<commit_before><?php
/**
 * Moor is a URL Routing/Linking/Controller library for PHP 5
 *
 * @copyright  Copyright (c) 2010 <NAME>, others
 * @author     <NAME> [jt] <<EMAIL>>
 * @author     <NAME> [wb] <<EMAIL>>
 * @license    MIT (see LICENSE or bottom of this file)
 * @package    Moor
 * @link       http://github.com/jeffturcotte/moor
 * @version    1.0.0b7
 */
class Moor {
	/**
	 * The currently running callback
	 *
	 * @var string
	 */
	private static $active_callback = NULL;
	
	/**
	 * The currently running namespace
	 *
	 * @var string
	 */
	private static $active_namespace = NULL;

	/**
	 * The currently running class (w/ namespace)
	 *
	 * @var string
	 */
	private static $active_class = NULL;

	/**
	 * The currently running class (w/o namespace)
	 *
	 * @var string
	 */
	private static $active_short_class = NULL;

	/**
	 * The currently running method (w/ namespace & class)
	 *
	 * @var string
	 */
	private static $active_method = NULL;
	
	/**
	 * The currently running method (w/o namespace or class)
	 *
	 * @var string
	 */
	private static $active_short_method = NULL;
	
	/**
	 * The currently running function
	 *
	 * @var string
	 */
	private static $active_function = NULL;

	/**
	 * The camelize() cache
	 *
	 * @var array
	 */
	private static $camelize = array();

	/**
	 * Default pattern to match :id params in incoming urls
	 *
	 * @var string
	 */
	private static $default_request_param_pattern = '[A-Za-z0-9_]+';

	/**
	 * Whether or not to show debug messages on default 404 page
	 *
	 * @var boolean
	 **/
	private static $debug = FALSE;

	/**
	 * Wether or not to cache with APC
	 *
	 * @var boolean
	 **/
	private static $enable_cache = FALSE;
	
	/**
	 * The current more instance, only used for chaining
	 *
	 * @var object Moor
	 */ 
	private static $instance = NULL;

	/**
	 * The linkTo() cache
	 *
	 * @var array
	 */
	private static $link_to = array();

	/**
	 * Debug messages
	 *
	 * @var array
	 **/
	private static $messages = array();

	/**
	 * The 404 callback to run upon no route matches
	 *
	 * @var string
	 **/
	private static $not_found_callback = 'Moor::routeNotFoundCallback';

	/**
	 * The paramsTo() cache
	 *
	 * @var array
	 */
	private static $params_to = array();

	/**
	 * The request path
	 *
	 * @var string
	 **/
	private static $request_path = NULL;

	/**
	 * All routes in their compiled form
	 *
	 * @var array
	 **/
	private static $routes = array();

	/**
	 * Whether or not the router is running
	 *
	 * @var boolean
	 */
	private static $running = FALSE;

	/**
	 * The underscore() cache
	 *
	 * @var array
	 */
	private static $underscorize = array();
	
	/**
	 * The prefix to add to $url_string when calling ::route()
	 * 
	 * @var string
	 */
	private static $url_prefix = '';


	// ==============
	// = Public API =
	// ==============

	/**
	 * Enable or disable the debugging
	 *
	 * @return object  The Moor instance for chaining
	 **/
	public static function enableDebug()
	{
		self::$debug = TRUE;
		return self::getInstance();
		
	}
	
	/**
	 * Find the URL where a particular callback lives
	 *
	 * @param  string $callback_string The callback to search for
	 * @param  string $params          The GET params to send
	 * @return string The URL
	 */
	public static function linkTo($key)
	{
		$param_values    = func_get_args();
		$callback_string = array_shift($param_values);
		$callback_string = trim($callback_string);
		$callback_string = self::expandCallback($callback_string);
		
		if (!isset(self::$link_to[$key])) {
			//self::$cache_for_linkTo[$key] = FALSE;
			$best_route = NULL;
			$param_names = preg_split('/(\s+:)|(\s+)|((?<!:):(?!:))/', $callback_string);
			$callback_string = array_shift($param_names);
			$param_names_flipped = array_flip($param_names);
			$callback_params = array();
			$param_matches = NULL;
			$low_dist = NULL;
			$top_accu = NULL;
			$low_diff = NULL;
			$top_sect = NULL;
			
			if (!$best_route) {
				foreach(self::$routes as $route) {
					$callback = $route->callback;
					
					if (!preg_match($callback->pattern, $callback_string, $callback_param_matches)) {
						continue;
					}
					
					foreach($callback_param_matches as $name => $param_match) {
						if (is_string($name)) { $callback_params[$name] = $param_match; }
					}
					
					$dist = levenshtein($callback->finder, $callback_string);
					$accu = strrpos($callback->finder, '*');
					$diff = abs(count($route->url->request_params) - count($param_names));
					$sect = count(array_intersect_key(array_flip(array_keys($route->url->request_params)), $param_names_flipped));
					
					$is_best = (
						$best_route === NULL ||
						$low_dist > $dist ||
						$low_dist == $dist && $top_accu < $accu ||
						$low_dist == $dist && $top_accu == $accu && $top_sect < $sect ||
						$low_dist == $dist && $top_accu == $accu && $top_sect <= $sect && $top_diff > $diff 
					);
					
					if ($is_best) {
						$best_route = $route;
						$low_dist = $dist;
						$top_accu = $accu;
						$top_sect = $sect;
						$top_diff = $diff;
					}
				}
			}
			
			if (!$best_route) {
				throw new MoorProgrammerException('No link could be found for the callback ' . $callback_string);
			}
			
			if ($best_route) {
				$cache = (object) $best_route->url->shorthand;
				$cache->param_names = $param_names;<commit_msg>Fixed diff handling in linkTo<commit_after><?php
/**
 * Moor is a URL Routing/Linking/Controller library for PHP 5
 *
 * @copyright  Copyright (c) 2010 <NAME>, others
 * @author     <NAME> [jt] <<EMAIL>>
 * @author     <NAME> [wb] <<EMAIL>>
 * @license    MIT (see LICENSE or bottom of this file)
 * @package    Moor
 * @link       http://github.com/jeffturcotte/moor
 * @version    1.0.0b7
 */
class Moor {
	/**
	 * The currently running callback
	 *
	 * @var string
	 */
	private static $active_callback = NULL;
	
	/**
	 * The currently running namespace
	 *
	 * @var string
	 */
	private static $active_namespace = NULL;

	/**
	 * The currently running class (w/ namespace)
	 *
	 * @var string
	 */
	private static $active_class = NULL;

	/**
	 * The currently running class (w/o namespace)
	 *
	 * @var string
	 */
	private static $active_short_class = NULL;

	/**
	 * The currently running method (w/ namespace & class)
	 *
	 * @var string
	 */
	private static $active_method = NULL;
	
	/**
	 * The currently running method (w/o namespace or class)
	 *
	 * @var string
	 */
	private static $active_short_method = NULL;
	
	/**
	 * The currently running function
	 *
	 * @var string
	 */
	private static $active_function = NULL;

	/**
	 * The camelize() cache
	 *
	 * @var array
	 */
	private static $camelize = array();

	/**
	 * Default pattern to match :id params in incoming urls
	 *
	 * @var string
	 */
	private static $default_request_param_pattern = '[A-Za-z0-9_]+';

	/**
	 * Whether or not to show debug messages on default 404 page
	 *
	 * @var boolean
	 **/
	private static $debug = FALSE;

	/**
	 * Wether or not to cache with APC
	 *
	 * @var boolean
	 **/
	private static $enable_cache = FALSE;
	
	/**
	 * The current more instance, only used for chaining
	 *
	 * @var object Moor
	 */ 
	private static $instance = NULL;

	/**
	 * The linkTo() cache
	 *
	 * @var array
	 */
	private static $link_to = array();

	/**
	 * Debug messages
	 *
	 * @var array
	 **/
	private static $messages = array();

	/**
	 * The 404 callback to run upon no route matches
	 *
	 * @var string
	 **/
	private static $not_found_callback = 'Moor::routeNotFoundCallback';

	/**
	 * The paramsTo() cache
	 *
	 * @var array
	 */
	private static $params_to = array();

	/**
	 * The request path
	 *
	 * @var string
	 **/
	private static $request_path = NULL;

	/**
	 * All routes in their compiled form
	 *
	 * @var array
	 **/
	private static $routes = array();

	/**
	 * Whether or not the router is running
	 *
	 * @var boolean
	 */
	private static $running = FALSE;

	/**
	 * The underscore() cache
	 *
	 * @var array
	 */
	private static $underscorize = array();
	
	/**
	 * The prefix to add to $url_string when calling ::route()
	 * 
	 * @var string
	 */
	private static $url_prefix = '';


	// ==============
	// = Public API =
	// ==============

	/**
	 * Enable or disable the debugging
	 *
	 * @return object  The Moor instance for chaining
	 **/
	public static function enableDebug()
	{
		self::$debug = TRUE;
		return self::getInstance();
		
	}
	
	/**
	 * Find the URL where a particular callback lives
	 *
	 * @param  string $callback_string The callback to search for
	 * @param  string $params          The GET params to send
	 * @return string The URL
	 */
	public static function linkTo($key)
	{
		$param_values    = func_get_args();
		$callback_string = array_shift($param_values);
		$callback_string = trim($callback_string);
		$callback_string = self::expandCallback($callback_string);
		
		if (!isset(self::$link_to[$key])) {
			//self::$cache_for_linkTo[$key] = FALSE;
			$best_route = NULL;
			$param_names = preg_split('/(\s+:)|(\s+)|((?<!:):(?!:))/', $callback_string);
			$callback_string = array_shift($param_names);
			$param_names_flipped = array_flip($param_names);
			$callback_params = array();
			$param_matches = NULL;
			$low_dist = NULL;
			$top_accu = NULL;
			$low_diff = NULL;
			$top_sect = NULL;
			
			if (!$best_route) {
				foreach(self::$routes as $route) {
					$callback = $route->callback;
					
					if (!preg_match($callback->pattern, $callback_string, $callback_param_matches)) {
						continue;
					}
					
					foreach($callback_param_matches as $name => $param_match) {
						if (is_string($name)) { $callback_params[$name] = $param_match; }
					}
					
					$dist = levenshtein($callback->finder, $callback_string);
					$accu = strrpos($callback->finder, '*');
					$diff = count(array_diff($route->url->request_params, $param_names));
					$sect = count(array_intersect_key(array_flip(array_keys($route->url->request_params)), $param_names_flipped));
					
					$is_best = (
						$best_route === NULL ||
						$low_dist > $dist ||
						$low_dist == $dist && $top_accu < $accu ||
						$low_dist == $dist && $top_accu == $accu && $top_sect < $sect ||
						$low_dist == $dist && $top_accu == $accu && $top_sect <= $sect && $low_diff > $diff 
					);
					
					if ($is_best) {
						$best_route = $route;
						$low_dist = $dist;
						$top_accu = $accu;
						$top_sect = $sect;
						$low_diff = $diff;
					}
				}
			}
			
			if (!$best_route) {
				throw new MoorProgrammerException('No link could be found for the callback ' . $callback_string);
			}
			
			if ($best_route) {
				$cache = (object) $best_route->url->shorthand;
				$cache->param_names = $param_names;
{'fraction_non_alphanumeric': 0.15141540487162608, 'fraction_numerical': 0.003197592401015706, 'mean_word_length': 2.862695241554668, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 45, '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': '6751298', 'n_tokens_mistral': 4080, 'n_tokens_neox': 3376, 'n_words': 927}