/** * Delivers all ancestors of a given subpage. * * @param Title $page The page to get the ancestors from. * @param int $depth Maximum depth back to the most distant ancestor relative from the given * subpage. If negative, that many elements from the top-level parent will be returend. * null means no limit. * * @return Title[] All ancestor pages of the given subpage in order from the top-level ancestor * to the direct parent. */ static function getAncestorPages( Title $page, $depth = null ) { $parts = preg_split( "/\//", $page->getPrefixedText(), 2 ); $rootPage = Title::newFromText( $parts[0] ); $pageFamily = SubpageInfo::getSubpages( $rootPage ); $pages = array(); //A page can't be it's own parent AND only a existing page can be a parent: if( !( $rootPage->equals( $page ) ) && $rootPage->exists() ) { $pages[] = $rootPage; } if( ! empty( $pageFamily ) ) { // order is top-level parent to direct parent foreach( $pageFamily as &$relativePage ) { if( SubpageInfo::isAncestorOf( $relativePage, $page ) ) { $pages[] = $relativePage; } } } if( $depth !== null ) { if( $depth <= 0 ) { $pages = array_slice( $pages, 0, -$depth ); } else { $pages = array_slice( $pages, -$depth ); } } return $pages; }