Revamp rewrite rule generation. Add clean page links. Credit to Jaykul for get_pagenum_link().

git-svn-id: http://svn.automattic.com/wordpress/trunk@1373 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
rboren 2004-05-31 17:22:25 +00:00
parent 38293cef03
commit e4bf283bc6
4 changed files with 213 additions and 95 deletions

View File

@ -125,12 +125,15 @@ RewriteBase <?php echo $home_root; ?>
$rewrite = rewrite_rules('', $permalink_structure); $rewrite = rewrite_rules('', $permalink_structure);
$rules = ''; $rules = '';
foreach ($rewrite as $match => $query) { foreach ($rewrite as $match => $query) {
if (strstr($query, 'index.php')) $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA]\n"; if (strstr($query, 'index.php')) {
$rules .= 'RewriteRule ^' . $match . ' ' . $site_root . $query . " [QSA]\n"; $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA]\n";
} else {
$rules .= 'RewriteRule ^' . $match . ' ' . $site_root . $query . " [QSA]\n";
}
} }
echo apply_filters('rewrite_rules', $rules); echo apply_filters('rewrite_rules', $rules);
?> ?>
</textarea> </textarea>
</p> </p>
<?php printf(__('<p>If your <code>.htaccess</code> file is writable by WordPress, you can <a href="%s">edit it through your template interface</a>.</p>'), 'templates.php?file=.htaccess') ?> <?php printf(__('<p>If your <code>.htaccess</code> file is writable by WordPress, you can <a href="%s">edit it through your template interface</a>.</p>'), 'templates.php?file=.htaccess') ?>
</form> </form>

View File

@ -1206,14 +1206,14 @@ function rewrite_rules($matches = '', $permalink_structure = '') {
$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-9a-z-]+)?', '([_0-9a-z-]+)',
'([0-9]+)?' '([0-9]+)'
); );
$queryreplace = $queryreplace =
@ -1228,58 +1228,89 @@ function rewrite_rules($matches = '', $permalink_structure = '') {
'p=' 'p='
); );
$feedregex = '(feed|rdf|rss|rss2|atom)/?$';
$trackbackregex = 'trackback/?$';
$pageregex = 'page/?([0-9]{1,})/?$';
$match = str_replace('/', '/?', $permalink_structure); $front = substr($permalink_structure, 0, strpos($permalink_structure, '%'));
$match = preg_replace('|/[?]|', '', $match, 1);
$match = str_replace($rewritecode, $rewritereplace, $match);
$match = preg_replace('|[?]|', '', $match, 1);
$feedmatch = trailingslashit(str_replace('?/?', '/', $match));
$trackbackmatch = $feedmatch;
preg_match_all('/%.+?%/', $permalink_structure, $tokens); preg_match_all('/%.+?%/', $permalink_structure, $tokens);
$query = 'index.php?'; $num_tokens = count($tokens[0]);
$feedquery = 'wp-feed.php?';
$trackbackquery = 'wp-trackback.php?'; $index = 'index.php';
for ($i = 0; $i < count($tokens[0]); ++$i) { $feedindex = 'wp-feed.php';
$trackbackindex = 'wp-trackback.php';
for ($i = 0; $i < $num_tokens; ++$i) {
if (0 < $i) { if (0 < $i) {
$query .= '&'; $queries[$i] = $queries[$i - 1] . '&';
$feedquery .= '&';
$trackbackquery .= '&';
} }
$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);
$query .= $query_token; $queries[$i] .= $query_token;
$feedquery .= $query_token;
$trackbackquery .= $query_token;
} }
++$i;
// Add post paged stuff $structure = str_replace($front, '', $permalink_structure);
$match .= '([0-9]+)?/?$'; $structure = trim($structure, '/');
$query .= '&page=' . preg_index($i, $matches); $dirs = explode('/', $structure);
$num_dirs = count($dirs);
// Add post feed stuff $front = preg_replace('|^/+|', '', $front);
$feedregex = '(feed|rdf|rss|rss2|atom)/?$';
$feedmatch .= $feedregex;
$feedquery .= '&feed=' . preg_index($i, $matches);
// Add post trackback stuff $post_rewrite = array();
$trackbackregex = 'trackback/?$'; $struct = $front;
$trackbackmatch .= $trackbackregex; for ($j = 0; $j < $num_dirs; ++$j) {
$struct .= $dirs[$j] . '/';
$match = str_replace($rewritecode, $rewritereplace, $struct);
$num_toks = preg_match_all('/%.+?%/', $struct, $toks);
$query = $queries[$num_toks - 1];
$pagematch = $match . $pageregex;
$pagequery = $index . '?' . $query . '&paged=' . preg_index($num_toks + 1, $matches);
$feedmatch = $match . $feedregex;
$feedquery = $feedindex . '?' . $query . '&feed=' . preg_index($num_toks + 1, $matches);
$post = 0;
if (strstr($struct, '%postname%') || strstr($struct, '%post_id%')) {
$post = 1;
$trackbackmatch = $match . $trackbackregex;
$trackbackquery = $trackbackindex . '?' . $query;
$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) {
$post_rewrite = array($trackbackmatch => $trackbackquery) + $post_rewrite;
}
}
// If the permalink does not have year, month, and day, we need to create a
// separate archive rule.
// TODO: Need to write separate rules for each component of the permalink.
$doarchive = false;
if (! (strstr($permalink_structure, '%year') && strstr($permalink_structure, '%monthnum') && strstr($permalink_structure, '%day')) ) {
$doarchive = true;
$archivematch = $front . '([0-9]{4})/?([0-9]{1,2})?/?([0-9]{1,2})?/?$';
$archivequery = 'index.php?year=' . preg_index(1, $matches) . '&monthnum=' . preg_index(2, $matches) . '&day=' . preg_index(3, $matches) ;
}
// Site feed // Site feed
$sitefeedmatch = 'feed/?([_0-9a-z-]+)?/?$'; $sitefeedmatch = 'feed/?([_0-9a-z-]+)?/?$';
$sitefeedquery = 'wp-feed.php?feed=' . preg_index(1, $matches); $sitefeedquery = 'wp-feed.php?feed=' . preg_index(1, $matches);
$sitepagematch = $pageregex;
$sitepagequery = 'index.php?paged=' . preg_index(1, $matches);
// Site comment feed // Site comment feed
$sitecommentfeedmatch = 'comments/feed/?([_0-9a-z-]+)?/?$'; $sitecommentfeedmatch = 'comments/feed/?([_0-9a-z-]+)?/?$';
$sitecommentfeedquery = 'wp-feed.php?feed=' . preg_index(1, $matches) . '&withcomments=1'; $sitecommentfeedquery = 'wp-feed.php?feed=' . preg_index(1, $matches) . '&withcomments=1';
// Code for nice categories and authors, currently not very flexible // Code for nice categories and authors.
$front = substr($permalink_structure, 0, strpos($permalink_structure, '%'));
if ( '' == get_settings('category_base') ) if ( '' == get_settings('category_base') )
$catmatch = $front . 'category/'; $catmatch = $front . 'category/';
else else
@ -1289,6 +1320,9 @@ function rewrite_rules($matches = '', $permalink_structure = '') {
$catfeedmatch = $catmatch . '(.*)/' . $feedregex; $catfeedmatch = $catmatch . '(.*)/' . $feedregex;
$catfeedquery = 'wp-feed.php?category_name=' . preg_index(1, $matches) . '&feed=' . preg_index(2, $matches); $catfeedquery = 'wp-feed.php?category_name=' . preg_index(1, $matches) . '&feed=' . preg_index(2, $matches);
$catpagematch = $catmatch . '(.*)/' . $pageregex;
$catpagequery = 'index.php?category_name=' . preg_index(1, $matches) . '&paged=' . preg_index(2, $matches);
$catmatch = $catmatch . '?(.*)'; $catmatch = $catmatch . '?(.*)';
$catquery = 'index.php?category_name=' . preg_index(1, $matches); $catquery = 'index.php?category_name=' . preg_index(1, $matches);
@ -1298,21 +1332,30 @@ function rewrite_rules($matches = '', $permalink_structure = '') {
$authorfeedmatch = $authormatch . '(.*)/' . $feedregex; $authorfeedmatch = $authormatch . '(.*)/' . $feedregex;
$authorfeedquery = 'wp-feed.php?author_name=' . preg_index(1, $matches) . '&feed=' . preg_index(2, $matches); $authorfeedquery = 'wp-feed.php?author_name=' . preg_index(1, $matches) . '&feed=' . preg_index(2, $matches);
$authorpagematch = $authormatch . '(.*)/' . $pageregex;
$authorpagequery = 'index.php?author_name=' . preg_index(1, $matches) . '&paged=' . preg_index(2, $matches);
$authormatch = $authormatch . '?(.*)'; $authormatch = $authormatch . '?(.*)';
$authorquery = 'index.php?author_name=' . preg_index(1, $matches); $authorquery = 'index.php?author_name=' . preg_index(1, $matches);
$rewrite = array( $rewrite = array(
$sitefeedmatch => $sitefeedquery,
$sitecommentfeedmatch => $sitecommentfeedquery,
$sitepagematch => $sitepagequery,
$catfeedmatch => $catfeedquery, $catfeedmatch => $catfeedquery,
$catpagematch => $catpagequery,
$catmatch => $catquery, $catmatch => $catquery,
$authorfeedmatch => $authorfeedquery, $authorfeedmatch => $authorfeedquery,
$authormatch => $authorquery, $authorpagematch => $authorpagequery,
$match => $query, $authormatch => $authorquery
$feedmatch => $feedquery,
$trackbackmatch => $trackbackquery,
$sitefeedmatch => $sitefeedquery,
$sitecommentfeedmatch => $sitecommentfeedquery
); );
$rewrite = $rewrite + $post_rewrite;
if ($doarchive) {
$rewrite = $rewrite + array($archivematch => $archivequery);
}
return $rewrite; return $rewrite;
} }

View File

@ -83,9 +83,18 @@ function get_month_link($year, $month) {
if (!$year) $year = gmdate('Y', time()+(get_settings('gmt_offset') * 3600)); if (!$year) $year = gmdate('Y', time()+(get_settings('gmt_offset') * 3600));
if (!$month) $month = gmdate('m', time()+(get_settings('gmt_offset') * 3600)); if (!$month) $month = gmdate('m', time()+(get_settings('gmt_offset') * 3600));
if ('' != get_settings('permalink_structure')) { if ('' != get_settings('permalink_structure')) {
$off = strpos(get_settings('permalink_structure'), '%monthnum%'); $permalink = get_settings('permalink_structure');
// If the permalink structure does not contain year and month, make
// one that does.
if (! (strstr($permalink, '%year') && strstr($permalink, '%monthnum')) ) {
$front = substr($permalink, 0, strpos($permalink, '%'));
$permalink = $front . '%year%/%monthnum%/';
}
$off = strpos($permalink, '%monthnum%');
$offset = $off + 11; $offset = $off + 11;
$monthlink = substr(get_settings('permalink_structure'), 0, $offset); $monthlink = substr($permalink, 0, $offset);
if ('/' != substr($monthlink, -1)) $monthlink = substr($monthlink, 0, -1); if ('/' != substr($monthlink, -1)) $monthlink = substr($monthlink, 0, -1);
$monthlink = str_replace('%year%', $year, $monthlink); $monthlink = str_replace('%year%', $year, $monthlink);
$monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink); $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
@ -102,9 +111,18 @@ function get_day_link($year, $month, $day) {
if (!$month) $month = gmdate('m', time()+(get_settings('gmt_offset') * 3600)); if (!$month) $month = gmdate('m', time()+(get_settings('gmt_offset') * 3600));
if (!$day) $day = gmdate('j', time()+(get_settings('gmt_offset') * 3600)); if (!$day) $day = gmdate('j', time()+(get_settings('gmt_offset') * 3600));
if ('' != get_settings('permalink_structure')) { if ('' != get_settings('permalink_structure')) {
$off = strpos(get_settings('permalink_structure'), '%day%'); $permalink = get_settings('permalink_structure');
// If the permalink structure does not contain year, month, and day,
// make one that does.
if (! (strstr($permalink, '%year') && strstr($permalink, '%monthnum')) ) {
$front = substr($permalink, 0, strpos($permalink, '%'));
$permalink = $front . '%year%/%monthnum%/%day%/';
}
$off = strpos($permalink, '%day%');
$offset = $off + 6; $offset = $off + 6;
$daylink = substr(get_settings('permalink_structure'), 0, $offset); $daylink = substr($permalink, 0, $offset);
if ('/' != substr($daylink, -1)) $daylink = substr($daylink, 0, -1); if ('/' != substr($daylink, -1)) $daylink = substr($daylink, 0, -1);
$daylink = str_replace('%year%', $year, $daylink); $daylink = str_replace('%year%', $year, $daylink);
$daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink); $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);

View File

@ -350,31 +350,79 @@ function next_post($format='%', $next='next post: ', $title='yes', $in_same_cat=
} }
} }
function get_pagenum_link($pagenum = 1){
$qstr = $_SERVER['REQUEST_URI'];
$page_querystring = "paged";
$page_modstring = "page/";
$page_modregex = "page/?";
// if we already have a QUERY style page string
if( stristr( $qstr, $page_querystring ) ) {
$replacement = "$page_querystring=$pagenum";
$qstr = preg_replace("/".$page_querystring."[^\d]+\d+/", $replacement, $qstr);
// if we already have a mod_rewrite style page string
} elseif ( preg_match( '|'.$page_modregex.'\d+|', $qstr ) ){
$qstr = preg_replace('|'.$page_modregex.'\d+|',"$page_modstring$pagenum",$qstr);
// if we don't have a page string at all ...
// lets see what sort of URL we have...
} else {
// we need to know the way queries are being written
global $querystring_start, $querystring_equal, $querystring_separator;
// if there's a querystring_start (a "?" usually), it's deffinitely not mod_rewritten
if ( stristr( $qstr, $querystring_start ) ){
// so append the query string (using &, since we already have ?)
$qstr .= $querystring_separator.$page_querystring.$querystring_equal.$pagenum;
// otherwise, it could be rewritten, OR just the default index ...
} elseif( '' != get_settings('permalink_structure')) {
$qstr = preg_replace('|(.*)/[^/]*|', '$1/', $qstr).$page_modstring.$pagenum;
} else {
$qstr = get_settings('blogfilename') . $querystring_start.$page_querystring.$querystring_equal.$pagenum;
}
}
$home_root = str_replace('http://', '', trim(get_settings('home')));
$home_root = preg_replace('|([^/]*)(.*)|i', '$2', $home_root);
if ('/' != substr($home_root, -1)) $home_root = $home_root . '/';
$qstr = str_replace($home_root, '', $qstr);
return trailingslashit(get_settings('home')).$qstr;
}
function next_posts($max_page = 0) { // original by cfactor at cooltux.org function next_posts($max_page = 0) { // original by cfactor at cooltux.org
global $p, $paged, $what_to_show, $pagenow; global $p, $paged, $what_to_show, $pagenow;
global $querystring_start, $querystring_equal, $querystring_separator; global $querystring_start, $querystring_equal, $querystring_separator;
if (empty($p) && ($what_to_show == 'paged')) { // if (empty($p) && ($what_to_show == 'paged')) {
$qstr = $_SERVER['QUERY_STRING']; // $qstr = $_SERVER['QUERY_STRING'];
if (!empty($qstr)) { // if (!empty($qstr)) {
$qstr = preg_replace('/&paged=\d{0,}/', '', $qstr); // $qstr = preg_replace('/&paged=\d{0,}/', '', $qstr);
$qstr = preg_replace('/paged=\d{0,}/', '', $qstr); // $qstr = preg_replace('/paged=\d{0,}/', '', $qstr);
} elseif (stristr($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME'] )) { // } elseif (stristr($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME'] )) {
if ('' != $qstr = str_replace($_SERVER['SCRIPT_NAME'], '', // if ('' != $qstr = str_replace($_SERVER['SCRIPT_NAME'], '',
$_SERVER['REQUEST_URI']) ) { // $_SERVER['REQUEST_URI']) ) {
$qstr = preg_replace('/^\//', '', $qstr); // $qstr = preg_replace('/^\//', '', $qstr);
$qstr = preg_replace('/paged\/\d{0,}\//', '', $qstr); // $qstr = preg_replace('/paged\/\d{0,}\//', '', $qstr);
$qstr = preg_replace('/paged\/\d{0,}/', '', $qstr); // $qstr = preg_replace('/paged\/\d{0,}/', '', $qstr);
$qstr = preg_replace('/\/$/', '', $qstr); // $qstr = preg_replace('/\/$/', '', $qstr);
} // }
} // }
if (!$paged) $paged = 1; // if (!$paged) $paged = 1;
$nextpage = intval($paged) + 1; // $nextpage = intval($paged) + 1;
if (!$max_page || $max_page >= $nextpage) { // if (!$max_page || $max_page >= $nextpage) {
echo get_settings('home') .'/'.$pagenow.$querystring_start. // echo get_settings('home') .'/'.$pagenow.$querystring_start.
($qstr == '' ? '' : $qstr.$querystring_separator) . // ($qstr == '' ? '' : $qstr.$querystring_separator) .
'paged'.$querystring_equal.$nextpage; // 'paged'.$querystring_equal.$nextpage;
} // }
} // }
if (empty($p) && ($what_to_show == 'paged')) {
if (!$paged) $paged = 1;
$nextpage = intval($paged) + 1;
if (!$max_page || $max_page >= $nextpage) {
echo get_pagenum_link($nextpage);
}
}
} }
function next_posts_link($label='Next Page &raquo;', $max_page=0) { function next_posts_link($label='Next Page &raquo;', $max_page=0) {
@ -406,26 +454,32 @@ function next_posts_link($label='Next Page &raquo;', $max_page=0) {
function previous_posts() { // original by cfactor at cooltux.org function previous_posts() { // original by cfactor at cooltux.org
global $_SERVER, $p, $paged, $what_to_show, $pagenow; global $_SERVER, $p, $paged, $what_to_show, $pagenow;
global $querystring_start, $querystring_equal, $querystring_separator; global $querystring_start, $querystring_equal, $querystring_separator;
if (empty($p) && ($what_to_show == 'paged')) { // if (empty($p) && ($what_to_show == 'paged')) {
$qstr = $_SERVER['QUERY_STRING']; // $qstr = $_SERVER['QUERY_STRING'];
if (!empty($qstr)) { // if (!empty($qstr)) {
$qstr = preg_replace('/&paged=\d{0,}/', '', $qstr); // $qstr = preg_replace('/&paged=\d{0,}/', '', $qstr);
$qstr = preg_replace('/paged=\d{0,}/', '', $qstr); // $qstr = preg_replace('/paged=\d{0,}/', '', $qstr);
} elseif (stristr($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME'] )) { // } elseif (stristr($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME'] )) {
if ('' != $qstr = str_replace($_SERVER['SCRIPT_NAME'], '', // if ('' != $qstr = str_replace($_SERVER['SCRIPT_NAME'], '',
$_SERVER['REQUEST_URI']) ) { // $_SERVER['REQUEST_URI']) ) {
$qstr = preg_replace('/^\//', '', $qstr); // $qstr = preg_replace('/^\//', '', $qstr);
$qstr = preg_replace("/paged\/\d{0,}\//", '', $qstr); // $qstr = preg_replace("/paged\/\d{0,}\//", '', $qstr);
$qstr = preg_replace('/paged\/\d{0,}/', '', $qstr); // $qstr = preg_replace('/paged\/\d{0,}/', '', $qstr);
$qstr = preg_replace('/\/$/', '', $qstr); // $qstr = preg_replace('/\/$/', '', $qstr);
} // }
} // }
$nextpage = intval($paged) - 1; // $nextpage = intval($paged) - 1;
if ($nextpage < 1) $nextpage = 1; // if ($nextpage < 1) $nextpage = 1;
echo get_settings('home') .'/'.$pagenow.$querystring_start. // echo get_settings('home') .'/'.$pagenow.$querystring_start.
($qstr == '' ? '' : $qstr.$querystring_separator) . // ($qstr == '' ? '' : $qstr.$querystring_separator) .
'paged'.$querystring_equal.$nextpage; // 'paged'.$querystring_equal.$nextpage;
} // }
if (empty($p) && ($what_to_show == 'paged')) {
$nextpage = intval($paged) - 1;
if ($nextpage < 1) $nextpage = 1;
echo get_pagenum_link($nextpage);
}
} }
function previous_posts_link($label='&laquo; Previous Page') { function previous_posts_link($label='&laquo; Previous Page') {