/**
 * {@internal Missing Short Description}}
 *
 * @since unknown
 *
 * @param unknown_type $children_pages
 * @param unknown_type $count
 * @param unknown_type $parent
 * @param unknown_type $level
 * @param unknown_type $pagenum
 * @param unknown_type $per_page
 */
function _page_rows( &$children_pages, &$count, $parent, $level, $pagenum, $per_page ) {

	if ( ! isset( $children_pages[$parent] ) )
		return;

	$start = ($pagenum - 1) * $per_page;
	$end = $start + $per_page;

	foreach ( $children_pages[$parent] as $page ) {

		if ( $count >= $end )
			break;

		// If the page starts in a subtree, print the parents.
		if ( $count == $start && $page->post_parent > 0 ) {
			$my_parents = array();
			$my_parent = $page->post_parent;
			while ( $my_parent) {
				$my_parent = get_post($my_parent);
				$my_parents[] = $my_parent;
				if ( !$my_parent->post_parent )
					break;
				$my_parent = $my_parent->post_parent;
			}
			$num_parents = count($my_parents);
			while( $my_parent = array_pop($my_parents) ) {
				echo "\t" . display_page_row( $my_parent, $level - $num_parents );
				$num_parents--;
			}
		}

		if ( $count >= $start )
			echo "\t" . display_page_row( $page, $level );

		$count++;

		_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 page_rows( $pages ) {
	if ( ! $pages )
		$pages = get_pages( 'sort_column=menu_order' );

	if ( ! $pages )
		return false;

	// splice pages into two parts: those without parent and those with parent

	$top_level_pages = array();
	$children_pages  = array();

	foreach ( $pages as $page ) {

		// catch and repair bad pages
		if ( $page->post_parent == $page->ID ) {
			$page->post_parent = 0;
			$wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_parent = '0' WHERE ID = %d", $page->ID) );
			clean_page_cache( $page->ID );
		}

		if ( 0 == $page->post_parent )
			$top_level_pages[] = $page;
		else
			$children_pages[] = $page;
	}

	foreach ( $top_level_pages as $page )
		display_page_row($page, $children_pages, 0);

	/*
	 * display the remaining children_pages which are orphans
	 * having orphan requires parental attention
	 */
	 if ( count($children_pages) > 0 ) {
	 	$empty_array = array();
	 	foreach ( $children_pages as $orphan_page ) {
			clean_page_cache( $orphan_page->ID);
			display_page_row( $orphan_page, $empty_array, 0 );
		}
	 }
}