Performance improvements for page_rows(). Props hailin. fixes #7286

git-svn-id: https://develop.svn.wordpress.org/trunk@8319 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ryan Boren 2008-07-12 15:40:57 +00:00
parent 3b6e23f787
commit 60f6e46f2f
1 changed files with 45 additions and 23 deletions

View File

@ -525,9 +525,8 @@ foreach ($posts_columns as $column_name=>$column_display_name) {
} }
/* /*
* displays pages in hierarchical order * displays pages in hierarchical order with paging support
*/ */
function page_rows($pages, $pagenum = 1, $per_page = 20) { function page_rows($pages, $pagenum = 1, $per_page = 20) {
$level = 0; $level = 0;
@ -538,14 +537,20 @@ function page_rows($pages, $pagenum = 1, $per_page = 20) {
return false; return false;
} }
// splice pages into two parts: those without parent and those with parent /*
$top_level_pages = array(); * arrange pages into two parts: top level pages and children_pages
$children_pages = array(); * children_pages is two dimensional array, eg.
* children_pages[10][] contains all sub-pages whose parent is 10.
// If searching, ignore hierarchy and treat everything as top level, otherwise split * It only takes O(N) to arrange this and it takes O(1) for subsequent lookup operations
// into top level and children * If searching, ignore hierarchy and treat everything as top level
*/
if ( empty($_GET['s']) ) { if ( empty($_GET['s']) ) {
$top_level_pages = array();
$children_pages = array();
foreach ( $pages as $page ) { foreach ( $pages as $page ) {
// catch and repair bad pages // catch and repair bad pages
if ( $page->post_parent == $page->ID ) { if ( $page->post_parent == $page->ID ) {
$page->post_parent = 0; $page->post_parent = 0;
@ -556,7 +561,7 @@ function page_rows($pages, $pagenum = 1, $per_page = 20) {
if ( 0 == $page->post_parent ) if ( 0 == $page->post_parent )
$top_level_pages[] = $page; $top_level_pages[] = $page;
else else
$children_pages[] = $page; $children_pages[ $page->post_parent ][] = $page;
} }
$pages = &$top_level_pages; $pages = &$top_level_pages;
@ -565,12 +570,11 @@ function page_rows($pages, $pagenum = 1, $per_page = 20) {
$count = 0; $count = 0;
$start = ($pagenum - 1) * $per_page; $start = ($pagenum - 1) * $per_page;
$end = $start + $per_page; $end = $start + $per_page;
foreach ( $pages as $page ) { foreach ( $pages as $page ) {
if ( $count >= $end ) if ( $count >= $end )
break; break;
$i++;
if ( $count >= $start ) if ( $count >= $start )
echo "\t" . display_page_row( $page, $level ); echo "\t" . display_page_row( $page, $level );
@ -579,21 +583,38 @@ function page_rows($pages, $pagenum = 1, $per_page = 20) {
if ( isset($children_pages) ) if ( isset($children_pages) )
_page_rows( $children_pages, $count, $page->ID, $level + 1, $pagenum, $per_page ); _page_rows( $children_pages, $count, $page->ID, $level + 1, $pagenum, $per_page );
} }
// if it is the last pagenum and there are orphaned pages, display them with paging as well
if ( isset($children_pages) && $count < $end ){
foreach( $children_pages as $orphans ){
foreach ( $orphans as $op ) {
if ( $count >= $end )
break;
if ( $count >= $start )
echo "\t" . display_page_row( $op, 0 );
$count++;
}
}
}
} }
function _page_rows( $pages, &$count, $parent, $level, $pagenum, $per_page ) { /*
* Given a top level page ID, display the nested hierarchy of sub-pages
* together with paging support
*/
function _page_rows( &$children_pages, &$count, $parent, $level, $pagenum, $per_page ) {
if ( ! isset( $children_pages[$parent] ) )
return;
$start = ($pagenum - 1) * $per_page; $start = ($pagenum - 1) * $per_page;
$end = $start + $per_page; $end = $start + $per_page;
$i = -1;
foreach ( $pages as $page ) { foreach ( $children_pages[$parent] as $page ) {
if ( $count >= $end ) if ( $count >= $end )
break; break;
$i++;
if ( $page->post_parent != $parent )
continue;
// If the page starts in a subtree, print the parents. // If the page starts in a subtree, print the parents.
if ( $count == $start && $page->post_parent > 0 ) { if ( $count == $start && $page->post_parent > 0 ) {
$my_parents = array(); $my_parents = array();
@ -614,12 +635,13 @@ function _page_rows( $pages, &$count, $parent, $level, $pagenum, $per_page ) {
if ( $count >= $start ) if ( $count >= $start )
echo "\t" . display_page_row( $page, $level ); echo "\t" . display_page_row( $page, $level );
unset($pages[$i]); // Prune the working set
$count++; $count++;
_page_rows( $pages, $count, $page->ID, $level + 1, $pagenum, $per_page ); _page_rows( $children_pages, $count, $page->ID, $level + 1, $pagenum, $per_page );
} }
unset( $children_pages[$parent] ); //required in order to keep track of orphans
} }
function user_row( $user_object, $style = '', $role = '' ) { function user_row( $user_object, $style = '', $role = '' ) {