Rewrite rule generation cleanup, including re-indent.

git-svn-id: http://svn.automattic.com/wordpress/trunk@1870 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
rboren 2004-11-20 04:28:36 +00:00
parent acbad8e480
commit 81bb103348
1 changed files with 158 additions and 156 deletions

View File

@ -1420,121 +1420,138 @@ function get_day_permastruct($permalink_structure = '') {
return get_date_permastruct($permalink_structure); return get_date_permastruct($permalink_structure);
} }
function generate_rewrite_rules($permalink_structure = '', $matches = '') { function generate_rewrite_rules($permalink_structure = '', $matches = '', $forcomments = false) {
$rewritecode = $rewritecode =
array( array(
'%year%', '%year%',
'%monthnum%', '%monthnum%',
'%day%', '%day%',
'%hour%', '%hour%',
'%minute%', '%minute%',
'%second%', '%second%',
'%postname%', '%postname%',
'%post_id%', '%post_id%',
'%category%', '%category%',
'%author%', '%author%',
'%pagename%', '%pagename%',
'%search%' '%search%'
); );
$rewritereplace = $rewritereplace =
array( array(
'([0-9]{4})', '([0-9]{4})',
'([0-9]{1,2})', '([0-9]{1,2})',
'([0-9]{1,2})', '([0-9]{1,2})',
'([0-9]{1,2})', '([0-9]{1,2})',
'([0-9]{1,2})', '([0-9]{1,2})',
'([0-9]{1,2})', '([0-9]{1,2})',
'([^/]+)', '([^/]+)',
'([0-9]+)', '([0-9]+)',
'(.+?)', '(.+?)',
'([^/]+)', '([^/]+)',
'([^/]+)', '([^/]+)',
'(.+)' '(.+)'
); );
$queryreplace = $queryreplace =
array ( array (
'year=', 'year=',
'monthnum=', 'monthnum=',
'day=', 'day=',
'hour=', 'hour=',
'minute=', 'minute=',
'second=', 'second=',
'name=', 'name=',
'p=', 'p=',
'category_name=', 'category_name=',
'author_name=', 'author_name=',
'pagename=', 'pagename=',
's=' 's='
); );
$feedregex = '(feed|rdf|rss|rss2|atom)/?$'; $feedregex2 = '(feed|rdf|rss|rss2|atom)/?$';
$trackbackregex = 'trackback/?$'; $feedregex = 'feed/' . $feedregex2;
$pageregex = 'page/?([0-9]{1,})/?$';
$front = substr($permalink_structure, 0, strpos($permalink_structure, '%')); $trackbackregex = 'trackback/?$';
preg_match_all('/%.+?%/', $permalink_structure, $tokens); $pageregex = 'page/?([0-9]{1,})/?$';
$num_tokens = count($tokens[0]); $front = substr($permalink_structure, 0, strpos($permalink_structure, '%'));
preg_match_all('/%.+?%/', $permalink_structure, $tokens);
$index = 'index.php'; $num_tokens = count($tokens[0]);
$feedindex = $index;
$trackbackindex = $index; $index = 'index.php';
for ($i = 0; $i < $num_tokens; ++$i) { $feedindex = $index;
if (0 < $i) { $trackbackindex = $index;
$queries[$i] = $queries[$i - 1] . '&'; for ($i = 0; $i < $num_tokens; ++$i) {
} if (0 < $i) {
$queries[$i] = $queries[$i - 1] . '&';
}
$query_token = str_replace($rewritecode, $queryreplace, $tokens[0][$i]) . preg_index($i+1, $matches); $query_token = str_replace($rewritecode, $queryreplace, $tokens[0][$i]) . preg_index($i+1, $matches);
$queries[$i] .= $query_token; $queries[$i] .= $query_token;
} }
$structure = $permalink_structure; $structure = $permalink_structure;
if ($front != '/') { if ($front != '/') {
$structure = str_replace($front, '', $structure); $structure = str_replace($front, '', $structure);
} }
$structure = trim($structure, '/'); $structure = trim($structure, '/');
$dirs = explode('/', $structure); $dirs = explode('/', $structure);
$num_dirs = count($dirs); $num_dirs = count($dirs);
$front = preg_replace('|^/+|', '', $front); $front = preg_replace('|^/+|', '', $front);
$post_rewrite = array(); $post_rewrite = array();
$struct = $front; $struct = $front;
for ($j = 0; $j < $num_dirs; ++$j) { for ($j = 0; $j < $num_dirs; ++$j) {
$struct .= $dirs[$j] . '/'; $struct .= $dirs[$j] . '/';
$match = str_replace($rewritecode, $rewritereplace, $struct); $struct = ltrim($struct, '/');
$num_toks = preg_match_all('/%.+?%/', $struct, $toks); $match = str_replace($rewritecode, $rewritereplace, $struct);
$query = $queries[$num_toks - 1]; $num_toks = preg_match_all('/%.+?%/', $struct, $toks);
$query = $queries[$num_toks - 1];
$pagematch = $match . $pageregex; $pagematch = $match . $pageregex;
$pagequery = $index . '?' . $query . '&paged=' . preg_index($num_toks + 1, $matches); $pagequery = $index . '?' . $query . '&paged=' . preg_index($num_toks + 1, $matches);
$feedmatch = $match . $feedregex; $feedmatch = $match . $feedregex;
$feedquery = $feedindex . '?' . $query . '&feed=' . preg_index($num_toks + 1, $matches); $feedquery = $feedindex . '?' . $query . '&feed=' . preg_index($num_toks + 1, $matches);
$post = 0; $feedmatch2 = $match . $feedregex2;
if (strstr($struct, '%postname%') || strstr($struct, '%post_id%') $feedquery2 = $feedindex . '?' . $query . '&feed=' . preg_index($num_toks + 1, $matches);
|| (strstr($struct, '%year%') && strstr($struct, '%monthnum%') && strstr($struct, '%day%') && strstr($struct, '%hour%') && strstr($struct, '%minute') && strstr($struct, '%second%'))) {
$post = 1;
$trackbackmatch = $match . $trackbackregex;
$trackbackquery = $trackbackindex . '?' . $query . '&tb=1';
$match = $match . '?([0-9]+)?/?$';
$query = $index . '?' . $query . '&page=' . preg_index($num_toks + 1, $matches);
} else {
$match .= '?$';
$query = $index . '?' . $query;
}
$post_rewrite = array($feedmatch => $feedquery, $pagematch => $pagequery, $match => $query) + $post_rewrite;
if ($post) { if ($forcomments) {
$post_rewrite = array($trackbackmatch => $trackbackquery) + $post_rewrite; $feedquery .= '&withcomments=1';
} $feedquery2 .= '&withcomments=1';
} }
$rewrite = array($feedmatch => $feedquery, $feedmatch2 => $feedquery2, $pagematch => $pagequery);
return $post_rewrite; if ($num_toks) {
$post = 0;
if (strstr($struct, '%postname%') || strstr($struct, '%post_id%')
|| (strstr($struct, '%year%') && strstr($struct, '%monthnum%') && strstr($struct, '%day%') && strstr($struct, '%hour%') && strstr($struct, '%minute') && strstr($struct, '%second%'))) {
$post = 1;
$trackbackmatch = $match . $trackbackregex;
$trackbackquery = $trackbackindex . '?' . $query . '&tb=1';
$match = $match . '?([0-9]+)?/?$';
$query = $index . '?' . $query . '&page=' . preg_index($num_toks + 1, $matches);
} else {
$match .= '?$';
$query = $index . '?' . $query;
}
$rewrite = $rewrite + array($match => $query);
if ($post) {
$rewrite = array($trackbackmatch => $trackbackquery) + $rewrite;
}
}
$post_rewrite = $rewrite + $post_rewrite;
}
return $post_rewrite;
} }
/* rewrite_rules /* rewrite_rules
@ -1544,79 +1561,64 @@ function generate_rewrite_rules($permalink_structure = '', $matches = '') {
* Returns an associate array of matches and queries. * Returns an associate array of matches and queries.
*/ */
function rewrite_rules($matches = '', $permalink_structure = '') { function rewrite_rules($matches = '', $permalink_structure = '') {
$rewrite = array(); $rewrite = array();
if (empty($permalink_structure)) { if (empty($permalink_structure)) {
$permalink_structure = get_settings('permalink_structure'); $permalink_structure = get_settings('permalink_structure');
if (empty($permalink_structure)) { if (empty($permalink_structure)) {
return $rewrite; return $rewrite;
} }
} }
$post_rewrite = generate_rewrite_rules($permalink_structure, $matches); $front = substr($permalink_structure, 0, strpos($permalink_structure, '%'));
$index = 'index.php';
$prefix = '';
if (using_index_permalinks($permalink_structure)) {
$prefix = $index . '/';
}
$feedregex = '(feed|rdf|rss|rss2|atom)/?$'; // Post
$pageregex = 'page/?([0-9]{1,})/?$'; $post_rewrite = generate_rewrite_rules($permalink_structure, $matches);
$front = substr($permalink_structure, 0, strpos($permalink_structure, '%'));
$index = 'index.php';
$prefix = '';
if (using_index_permalinks($permalink_structure)) {
$prefix = $index . '/';
}
// Generate date rules. // Date
$date_rewrite = generate_rewrite_rules(get_date_permastruct($permalink_structure), $matches); $date_rewrite = generate_rewrite_rules(get_date_permastruct($permalink_structure), $matches);
// Root
$root_rewrite = generate_rewrite_rules($prefix . '/', $matches);
// Site feed // Comments
$sitefeedmatch = $prefix . 'feed/?([_0-9a-z-]+)?/?$'; $comments_rewrite = generate_rewrite_rules($prefix . 'comments', $matches, true);
$sitefeedquery = 'index.php?feed=_' . preg_index(1, $matches);
// Site comment feed // Search
$sitecommentfeedmatch = $prefix . 'comments/feed/?([_0-9a-z-]+)?/?$'; $search_structure = $prefix . "search/%search%";
$sitecommentfeedquery = 'index.php?feed=_' . preg_index(1, $matches) . '&withcomments=1'; $search_rewrite = generate_rewrite_rules($search_structure, $matches);
// Site page // Categories
$sitepagematch = $prefix . $pageregex;
$sitepagequery = 'index.php?paged=' . preg_index(1, $matches);
$site_rewrite = array(
$sitefeedmatch => $sitefeedquery,
$sitecommentfeedmatch => $sitecommentfeedquery,
$sitepagematch => $sitepagequery,
);
// Search
$search_structure = $prefix . "search/%search%";
$search_rewrite = generate_rewrite_rules($search_structure, $matches);
// Categories
if ( '' == get_settings('category_base') ) if ( '' == get_settings('category_base') )
$category_structure = $front . 'category/'; $category_structure = $front . 'category/';
else else
$category_structure = get_settings('category_base') . '/'; $category_structure = get_settings('category_base') . '/';
$category_structure = $category_structure . '%category%'; $category_structure = $category_structure . '%category%';
$category_rewrite = generate_rewrite_rules($category_structure, $matches); $category_rewrite = generate_rewrite_rules($category_structure, $matches);
// Authors // Authors
$author_structure = $front . 'author/%author%'; $author_structure = $front . 'author/%author%';
$author_rewrite = generate_rewrite_rules($author_structure, $matches); $author_rewrite = generate_rewrite_rules($author_structure, $matches);
// Site static pages // Pages
$page_structure = $prefix . 'site/%pagename%'; $page_rewrite = page_rewrite_rules();
$page_rewrite = generate_rewrite_rules($page_structure, $matches);
// Pages // Deprecated style static pages
$pages_rewrite = page_rewrite_rules(); $page_structure = $prefix . 'site/%pagename%';
$old_page_rewrite = generate_rewrite_rules($page_structure, $matches);
// Put them together. // Put them together.
$rewrite = $pages_rewrite + $site_rewrite + $page_rewrite + $search_rewrite + $category_rewrite + $author_rewrite + $date_rewrite; $rewrite = $page_rewrite + $root_rewrite + $comments_rewrite + $old_page_rewrite + $search_rewrite + $category_rewrite + $author_rewrite + $date_rewrite + $post_rewrite;
$rewrite = $rewrite + $post_rewrite; $rewrite = apply_filters('rewrite_rules_array', $rewrite);
return $rewrite;
$rewrite = apply_filters('rewrite_rules_array', $rewrite);
return $rewrite;
} }
function mod_rewrite_rules ($permalink_structure) { function mod_rewrite_rules ($permalink_structure) {