<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[18541] trunk/wp-includes: Eliminate verbose rewrite rules for ambiguous rewrite structures, resulting in massive performance gains.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg > ul, #logmsg > ol { margin-left: 0; margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/18541">18541</a></dd>
<dt>Author</dt> <dd>markjaquith</dd>
<dt>Date</dt> <dd>2011-08-12 01:55:08 +0000 (Fri, 12 Aug 2011)</dd>
</dl>

<h3>Log Message</h3>
<pre>Eliminate verbose rewrite rules for ambiguous rewrite structures, resulting in massive performance gains. props andy, otto42, duck_. Nice work everyone! see <a href="http://core.trac.wordpress.org/ticket/16687">#16687</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludesclasswpphp">trunk/wp-includes/class-wp.php</a></li>
<li><a href="#trunkwpincludespostphp">trunk/wp-includes/post.php</a></li>
<li><a href="#trunkwpincludesrewritephp">trunk/wp-includes/rewrite.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludesclasswpphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/class-wp.php (18540 => 18541)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/class-wp.php        2011-08-12 01:41:23 UTC (rev 18540)
+++ trunk/wp-includes/class-wp.php        2011-08-12 01:55:08 UTC (rev 18541)
</span><span class="lines">@@ -200,6 +200,13 @@
</span><span class="cx"> 
</span><span class="cx">                                         if ( preg_match(&quot;#^$match#&quot;, $request_match, $matches) ||
</span><span class="cx">                                                 preg_match(&quot;#^$match#&quot;, urldecode($request_match), $matches) ) {
</span><ins>+
+                                                if ( $wp_rewrite-&gt;use_verbose_page_rules &amp;&amp; preg_match( '/pagename=\$([^&amp;\[]+)\[([0-9]+)\]/', $query, $varmatch ) ) {
+                                                        // this is a verbose page match, lets check to be sure about it
+                                                        if ( ! get_page_by_path( ${$varmatch[1]}[$varmatch[2]] ) )
+                                                                 continue;
+                                                }
+
</ins><span class="cx">                                                 // Got a match.
</span><span class="cx">                                                 $this-&gt;matched_rule = $match;
</span><span class="cx">                                                 break;
</span></span></pre></div>
<a id="trunkwpincludespostphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/post.php (18540 => 18541)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/post.php        2011-08-12 01:41:23 UTC (rev 18540)
+++ trunk/wp-includes/post.php        2011-08-12 01:55:08 UTC (rev 18541)
</span><span class="lines">@@ -3146,38 +3146,49 @@
</span><span class="cx">  */
</span><span class="cx"> function get_page_by_path($page_path, $output = OBJECT, $post_type = 'page') {
</span><span class="cx">         global $wpdb;
</span><del>-        $null = null;
</del><ins>+
</ins><span class="cx">         $page_path = rawurlencode(urldecode($page_path));
</span><span class="cx">         $page_path = str_replace('%2F', '/', $page_path);
</span><span class="cx">         $page_path = str_replace('%20', ' ', $page_path);
</span><del>-        $page_paths = '/' . trim($page_path, '/');
-        $leaf_path  = sanitize_title(basename($page_paths));
-        $page_paths = explode('/', $page_paths);
-        $full_path = '';
-        foreach ( (array) $page_paths as $pathdir )
-                $full_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title($pathdir);
</del><ins>+        $parts = explode( '/', trim( $page_path, '/' ) );
+        $parts = array_map( 'esc_sql', $parts );
+        $parts = array_map( 'sanitize_title', $parts );
</ins><span class="cx"> 
</span><del>-        $pages = $wpdb-&gt;get_results( $wpdb-&gt;prepare( &quot;SELECT ID, post_name, post_parent FROM $wpdb-&gt;posts WHERE post_name = %s AND (post_type = %s OR post_type = 'attachment')&quot;, $leaf_path, $post_type ));
</del><ins>+        $in_string = &quot;'&quot;. implode( &quot;','&quot;, $parts ) . &quot;'&quot;;
+        $pages = $wpdb-&gt;get_results( $wpdb-&gt;prepare( &quot;SELECT ID, post_name, post_parent FROM $wpdb-&gt;posts WHERE post_name IN ({$in_string}) AND (post_type = %s OR post_type = 'attachment')&quot;, $post_type ), OBJECT_K );
</ins><span class="cx"> 
</span><del>-        if ( empty($pages) )
-                return $null;
</del><ins>+        $revparts = array_reverse( $parts );
</ins><span class="cx"> 
</span><ins>+        $foundid = 0;
</ins><span class="cx">         foreach ( $pages as $page ) {
</span><del>-                $path = '/' . $leaf_path;
-                $curpage = $page;
-                while ( $curpage-&gt;post_parent != 0 ) {
-                        $post_parent = $curpage-&gt;post_parent;
-                        $curpage = wp_cache_get( $post_parent, 'posts' );
-                        if ( false === $curpage )
-                                $curpage = $wpdb-&gt;get_row( $wpdb-&gt;prepare( &quot;SELECT ID, post_name, post_parent FROM $wpdb-&gt;posts WHERE ID = %d and post_type = %s&quot;, $post_parent, $post_type ) );
-                        $path = '/' . $curpage-&gt;post_name . $path;
</del><ins>+                if ( $page-&gt;post_name == $revparts[0] ) {
+                        $count = 0;
+                        if ( $page-&gt;post_parent != 0 ) {
+                                if ( null === ( $parent_page = $pages[ $page-&gt;post_parent ] ) )
+                                        continue;
+
+                                while ( $parent_page-&gt;ID != 0 ) {
+                                        $count++;
+                                        if ( $parent_page-&gt;post_name != $revparts[ $count ] )
+                                                break;
+                                        $parent_page = $pages[ $parent_page-&gt;post_parent ];
+                                }
+
+                                if ( $parent_page-&gt;ID == 0 &amp;&amp; $count+1 == count($revparts) ) {
+                                        $foundid = $page-&gt;ID;
+                                        break;
+                                }
+                        } else if ( count($revparts) == 1 ) {
+                                $foundid = $page-&gt;ID;
+                                break;
+                        }
</ins><span class="cx">                 }
</span><del>-
-                if ( $path == $full_path )
-                        return get_page($page-&gt;ID, $output, $post_type);
</del><span class="cx">         }
</span><span class="cx"> 
</span><del>-        return $null;
</del><ins>+        if ( $foundid )
+                return get_page($foundid, $output, $post_type);
+
+        return null;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span></span></pre></div>
<a id="trunkwpincludesrewritephp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/rewrite.php (18540 => 18541)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/rewrite.php        2011-08-12 01:41:23 UTC (rev 18540)
+++ trunk/wp-includes/rewrite.php        2011-08-12 01:55:08 UTC (rev 18541)
</span><span class="lines">@@ -306,12 +306,20 @@
</span><span class="cx">         // Look for matches.
</span><span class="cx">         $request_match = $request;
</span><span class="cx">         foreach ( (array)$rewrite as $match =&gt; $query) {
</span><ins>+
</ins><span class="cx">                 // If the requesting file is the anchor of the match, prepend it
</span><span class="cx">                 // to the path info.
</span><span class="cx">                 if ( !empty($url) &amp;&amp; ($url != $request) &amp;&amp; (strpos($match, $url) === 0) )
</span><span class="cx">                         $request_match = $url . '/' . $request;
</span><span class="cx"> 
</span><span class="cx">                 if ( preg_match(&quot;!^$match!&quot;, $request_match, $matches) ) {
</span><ins>+
+                        if ( $wp_rewrite-&gt;use_verbose_page_rules &amp;&amp; preg_match( '/pagename=\$([^&amp;\[]+)\[([0-9]+)\]/', $query, $varmatch ) ) {
+                                // this is a verbose page match, lets check to be sure about it
+                                if ( ! get_page_by_path( ${$varmatch[1]}[$varmatch[2]] ) )
+                                        continue;
+                        }
+
</ins><span class="cx">                         // Got a match.
</span><span class="cx">                         // Trim the query of everything up to the '?'.
</span><span class="cx">                         $query = preg_replace(&quot;!^.+\?!&quot;, '', $query);
</span><span class="lines">@@ -813,29 +821,9 @@
</span><span class="cx">                 $rewrite_rules = array();
</span><span class="cx">                 $page_structure = $this-&gt;get_page_permastruct();
</span><span class="cx"> 
</span><del>-                if ( ! $this-&gt;use_verbose_page_rules ) {
-                        $this-&gt;add_rewrite_tag('%pagename%', &quot;(.+?)&quot;, 'pagename=');
-                        $rewrite_rules = array_merge($rewrite_rules, $this-&gt;generate_rewrite_rules($page_structure, EP_PAGES));
-                        return $rewrite_rules;
-                }
-
-                $page_uris = $this-&gt;page_uri_index();
-                $uris = $page_uris[0];
-                $attachment_uris = $page_uris[1];
-
-                if ( is_array( $attachment_uris ) ) {
-                        foreach ( $attachment_uris as $uri =&gt; $pagename ) {
-                                $this-&gt;add_rewrite_tag('%pagename%', &quot;($uri)&quot;, 'attachment=');
-                                $rewrite_rules = array_merge($rewrite_rules, $this-&gt;generate_rewrite_rules($page_structure, EP_PAGES));
-                        }
-                }
-                if ( is_array( $uris ) ) {
-                        foreach ( $uris as $uri =&gt; $pagename ) {
-                                $this-&gt;add_rewrite_tag('%pagename%', &quot;($uri)&quot;, 'pagename=');
-                                $rewrite_rules = array_merge($rewrite_rules, $this-&gt;generate_rewrite_rules($page_structure, EP_PAGES));
-                        }
-                }
-
</del><ins>+                // the extra .? at the beginning prevents clashes with other regex's in thie structure
+                $this-&gt;add_rewrite_tag('%pagename%', &quot;(.?.+?)&quot;, 'pagename=');
+                $rewrite_rules = array_merge($rewrite_rules, $this-&gt;generate_rewrite_rules($page_structure, EP_PAGES));
</ins><span class="cx">                 return $rewrite_rules;
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -1553,7 +1541,7 @@
</span><span class="cx"> 
</span><span class="cx">                 // Put them together.
</span><span class="cx">                 if ( $this-&gt;use_verbose_page_rules )
</span><del>-                        $this-&gt;rules = array_merge($this-&gt;extra_rules_top, $robots_rewrite, $old_feed_files, $registration_pages, $page_rewrite, $root_rewrite, $comments_rewrite, $search_rewrite,  $author_rewrite, $date_rewrite, $post_rewrite, $this-&gt;extra_rules);
</del><ins>+                        $this-&gt;rules = array_merge($this-&gt;extra_rules_top, $robots_rewrite, $old_feed_files, $registration_pages, $root_rewrite, $comments_rewrite, $search_rewrite,  $author_rewrite, $date_rewrite, $page_rewrite, $post_rewrite, $this-&gt;extra_rules);
</ins><span class="cx">                 else
</span><span class="cx">                         $this-&gt;rules = array_merge($this-&gt;extra_rules_top, $robots_rewrite, $old_feed_files, $registration_pages, $root_rewrite, $comments_rewrite, $search_rewrite,  $author_rewrite, $date_rewrite, $post_rewrite, $page_rewrite, $this-&gt;extra_rules);
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>