public function check_node(midgardmvc_core_node $node) { if (!$node->up && !is_null(self::$root_node)) { $node->up = midgardmvc_core::get_instance()->context->get_request()->get_node()->get_object()->id; } if (!$node->name) { if (midgardmvc_core::get_instance()->component->is_installed('midgardmvc_helper_urlize')) { $node->name = midgardmvc_helper_urlize::string($node->title); } else { $node->name = strtolower(str_replace(' ', '_', $node->title)); } } }
/** * Helper function for generating "clean" URL names from titles, etc. * * @param string $string String to edit. * @param string $replacer The replacement for invalid characters. * @return string Normalized name. */ public static function generate_urlname_from_string($string, $replacer = "-", $r = 0) { if ($r > 5) { debug_add('$r > 5, aborting', MIDCOM_LOG_ERROR); return $string; } if (empty($string)) { debug_add('$string was empty(), aborting', MIDCOM_LOG_ERROR); return ''; } // TODO: sanity-check $replacer ? $orig_string = $string; // Try to transliterate non-latin strings to URL-safe format $string = midgardmvc_helper_urlize::string($string, $replacer); $string = trim(str_replace('[?]', '', $string)); // Ultimate fall-back, if we couldn't get anything out of the transliteration we use the UTF-8 character hexes as the name string to have *something* if (empty($string) || preg_match("/^{$replacer}+\$/", $string)) { $i = 0; // make sure this is not mb_strlen (ie mb automatic overloading off) $len = strlen($orig_string); $string = ''; while ($i < $len) { $byte = $orig_string[$i]; $string .= str_pad(dechex(ord($byte)), '0', STR_PAD_LEFT); $i++; } } // Rest of spaces to underscores $string = preg_replace('/\\s+/', '_', $string); // Regular expression for characters to replace (the ^ means an inverted character class, ie characters *not* in this class are replaced) $regexp = '/[^a-zA-Z0-9_-]/'; // Replace the unsafe characters with the given replacer (which is supposed to be safe...) $safe = preg_replace($regexp, $replacer, $string); // Strip trailing {$replacer}s and underscores from start and end of string $safe = preg_replace("/^[{$replacer}_]+|[{$replacer}_]+\$/", '', $safe); // Clean underscores around $replacer $safe = preg_replace("/_{$replacer}|{$replacer}_/", $replacer, $safe); // Any other cleanup routines ? // We're done here, return $string lowercased $safe = strtolower($safe); /** * Quick and dirty workaround for http://trac.midgard-project.org/ticket/1530 by recursing */ // Recurse until we make no changes to the string if ($string === $safe) { return $string; } return self::generate_urlname_from_string($safe, $replacer, $r + 1); }