require_once 'session.ppp' ;
require_once 'local.ppp' ;
require_once 'conf.ppp' ;
require_once 'baselib.ppp' ;
require_once 'wwwsec.ppp' ;
require_once 'librepo.ppp' ;
# $DEBUG = 2 ;
get_config () ;
$conn = db_connect_or_die () ;
assert_login () ;
if ( $ADM = GETVAR ( 'ADM' ) ) { switch_actv ( $ADM ) ; }
$TIT = REPO_TIT ( 'stats and checks' ) ;
html_start ( $TIT ) ;
if ( ! $USER ) { html_exit ( "stats : not authorised" ) ; }
if ( ! isroot () ) { html_exit ( "stats : no admin" ) ; }
$USER_GROUPS = user_group_ids () ;
$ACT = GETVAR ( 'ACT' ) ;
if ( $ACT and ! is_adm () )
{ html_exit ( "stats : you are not an admin ; go away" ) ; }
if ( $ACT == 'DEL' )
{ if ( ! ( $RIDS = GETVAR ( 'RIDS' ) ) )
{ html_exit ( "stats : no RIDS" ) ; }
$res = '' ;
foreach ( explode ( ',', $RIDS ) as $rid )
{ $repo = get_repo ( $rid ) ;
$res .= $repo -> delete () ;
}
echo H3 ( 'update results' ) ;
echo SPN ( $res, 'red' ) ;
}
elseif ( $ACT == 'DEL_RRS' )
{ if ( ! ( $ID = GETVAR ( 'ID' ) ) )
{ html_exit ( "stats : no ID" ) ; }
$xuser = get_repo_xuser ( $ID ) ; # or die
$res = $xuser -> delete_rrs_mems () ;
echo H3 ( 'update results' ) ;
if ( ! $res ) { $res = ULmk ( 'no change' ) ; }
echo SPN ( $res, 'red' ) ;
}
elseif ( $ACT )
{ html_exit ( "stats : unknow ACT ($ACT)" ) ; }
$Repos = TBL ( 'repos' ) ;
$Paths = TBL ( 'paths' ) ;
$subrec = "SELECT id FROM $Repos ORDER BY creat DESC LIMIT 6" ;
$repos = get_repos ( '', '', 'side,ownr,name' ) ;
$recent = get_repos ( '', "$Repos.id IN ( $subrec )", 'creat DESC' ) ;
$groups = get_repo_groups ( '', '', 'name' ) ;
$mis_sub = "select pid from {$TBL(xusers)}" ;
$mis_qwe = "ownr in ( $mis_sub )" ;
$missing = get_repos ( '', $mis_qwe, 'side,ownr,name' ) ;
$xusers = get_repo_xusers ( '', '', 'pid' ) ;
$xrepos = get_repos_xusers ( 'side,ownr,name' ) ;
$xgroups = get_groups_xusers ( 'name' ) ;
$r0_sub
= "SELECT distinct {$TBL(repos)}.id "
. "FROM {$TBL(repos)} "
. "LEFT JOIN {$TBL(stats)} ON {$TBL(repos)}.id = rid "
. "GROUP BY {$TBL(repos)}.id HAVING max(rev) = 0 "
;
$r0_qwe = "{$TBL(repos)}.id IN ( $r0_sub )" ;
$rev0s = get_repos ( '', $r0_qwe, 'creat' ) ;
# foreach ( get_repos ( '', $r0_qwe, 'creat' ) as $repo )
# { if ( $repo -> revision () == 0 )
# { $rev0s [] = $repo ; }
# }
$tab = array () ;
$tab [ 'side' ] = array () ;
foreach ( $repos as $repo )
{ $side = $repo -> side ;
$tab [ 'side' ] [ $side ] [] = $repo ;
}
function gen_tab_sides ( $tab )
{ $res = TR ( TH ( 'side' ) . TH ( 'cnt' ) ) ;
$tot = 0 ;
$sides = $tab [ 'side' ] ;
foreach ( $sides as $side => $reps )
{ $cnt = count ( $reps ) ;
$res .= TR
( TD ( $side )
. TDr ( $cnt )
) ;
$tot += $cnt ;
}
$res .= TR
( TH ( 'total' )
. THr ( $tot )
) ;
return TAB ( $res ) ;
}
function gen_tab_repos ( $repos )
{ if ( ! count ( $repos ) ) { return 'none' ; }
$res = repos_info_head () ;
foreach ( $repos as $repo )
{ $res .= $repo -> info_row () ; }
return TAB ( $res ) ;
}
function gen_tab_unused_guests ()
{ global $TBL ;
$logins_with_rights
= "SELECT distinct pid FROM {$TBL(rights)} "
;
$used_groups
= "SELECT distinct gid FROM {$TBL(groups)} "
. "WHERE name IN ( $logins_with_rights )"
;
$logins_in_used_group
= "SELECT distinct pid FROM {$TBL(gr_mem)} "
. "WHERE gid IN ( $used_groups )"
;
$users_with_rights
= "SELECT distinct pid FROM {$TBL(rights)}"
;
$unused_guests
= "login NOT IN ( $logins_in_used_group ) AND "
. "login NOT IN ( $users_with_rights )"
;
$guests = get_repo_guests ( '', $unused_guests, '' ) ;
if ( ! count ( $guests ) ) { return 'none' ; }
$res = repo_guests_info_head () ;
foreach ( $guests as $guest )
{ $res .= $guest -> info_row () ; }
return TAB ( $res ) ;
}
function gen_tab_unused_groups ()
{ global $TBL ;
$groups_with_rights =
"SELECT distinct pid FROM {$TBL(rights)} WHERE pid LIKE '@%'" ;
$unused_groups = "name NOT IN ( $groups_with_rights )" ;
$groups = get_repo_groups ( '', $unused_groups, '' ) ;
if ( ! count ( $groups ) ) { return 'none' ; }
$res = repo_groups_info_head () ;
foreach ( $groups as $group )
{ $res .= $group -> info_row () ; }
return TAB ( $res ) ;
}
function gen_xuser ( $xuser, $xrepos, $xgroups, $missing, $all_groups )
{ $pid = $xuser -> pid ;
$res = H4 ( "ex-user $pid" ) ;
$lis = '' ;
$incl_txt = array () ;
$repos = array () ;
foreach ( $missing as $idx => $repo )
{ if ( $repo -> ownr == $pid )
{ $repos [] = $repo ; }
}
$repo_cnt = count ( $repos ) ;
if ( $repo_cnt )
{ $tab = repos_info_head () ;
foreach ( $repos as $idx => $repo )
{ $tab .= $repo -> info_row () ; }
$repo_txt =
$repo_cnt == 1 ? '1 repository' : "$repo_cnt repositories" ;
$incl_txt [] = $repo_txt ;
$lis .= LI
( "$pid owns $repo_txt :"
. BQ ( TAB ( $tab ) )
) ;
}
$repos = array () ;
foreach ( $xrepos as $idx => $repo )
{ foreach ( $repo -> subs as $sub )
{ if ( $sub -> rights [ $pid ] ) { $repos [] = $repo ; } }
}
$cnt = count ( $repos ) ;
$cnt_rrs = $cnt ;
if ( $cnt )
{ $tab = repos_info_head () ;
foreach ( $repos as $idx => $repo )
{ $tab .= $repo -> info_row () ; }
$txt = $cnt == 1 ? '1 repository' : "$cnt repositories" ;
$lis .= LI
( "$pid has access rights in $txt :"
. BQ ( TAB ( $tab ) )
) ;
}
$groups = array () ;
foreach ( $xgroups as $idx => $group )
{ if ( $group -> mems [ $pid ] ) { $groups [] = $group ; } }
$cnt = count ( $groups ) ;
$cnt_mems = $cnt ;
if ( $cnt )
{ $tab = repo_groups_info_head () ;
foreach ( $groups as $idx => $group )
{ $tab .= $group -> info_row () ; }
$txt = $cnt == 1 ? '1 group' : "$cnt groups" ;
$lis .= LI
( "$pid is a member of $txt : "
. BQ ( TAB ( $tab ) )
) ;
}
$groups = array () ;
foreach ( $all_groups as $idx => $group )
{ if ( $group -> ownr == $pid ) { $groups [] = $group ; } }
$cnt = count ( $groups ) ;
$cnt_grps = $cnt ;
if ( $cnt )
{ $tab = repo_groups_info_head () ;
foreach ( $groups as $idx => $group )
{ $tab .= $group -> info_row () ; }
$grps_txt = $cnt == 1 ? '1 group' : "$cnt groups" ;
$incl_txt [] = $grps_txt ;
$lis .= LI
( "$pid is the owner of $grps_txt : "
. BQ ( TAB ( $tab ) )
) ;
}
if ( ! $lis )
{ $lis = LI ( "no dangling repo rights or group memberships" ) ; }
if ( is_adm () )
{ $lis .=
( $cnt_rrs + $cnt_mems
? LI ( $xuser -> delete_rrs_mems_url () )
: ''
) ;
}
return $res . UL ( $lis ) ;
}
function gen_xusers ( $xusers, $xrepos, $xgroups, $missing, $groups )
{ $res = '' ;
foreach ( $xusers as $idx => $xuser )
{ $res .= gen_xuser ( $xuser, $xrepos, $xgroups, $missing, $groups ) ; }
return ( $res ? $res : BQ ( 'none' ) ) ;
}
function del_url ( $rids )
{ $cnt = count ( $rids ) ;
$txt =
( $cnt == 1
? 'delete this repository (unforgiving ; are you sure?)'
: "delete these $cnt repositories (unforgiving ; are you sure?)"
) ;
$RIDS = implode ( ',', $rids ) ;
return URL ( "stats.php?ACT=DEL&RIDS=$RIDS", SPN ( $txt, 'red' ) ) ;
}
function gen_no_rights ()
{ $Repos = TBL ( 'repos' ) ;
$Paths = TBL ( 'paths' ) ;
$Rights = TBL ( 'rights' ) ;
$sub = << info_row () ;
$lis [] = del_url ( array ( $repo -> id ) )
. ' ← ' . $repo -> url () ;
$dels [] = $repo -> id ;
}
if ( count ( $dels ) > 1 )
{ $lis [] = del_url ( $dels ) ; }
return TAB ( $res ) . ( is_adm () ? ULmk ( $lis ) : '' ) ;
}
function gen_with_paths ()
{ $Repos = TBL ( 'repos' ) ;
$Paths = TBL ( 'paths' ) ;
$sub = << info_row () ; }
return TAB ( $res ) ;
}
echo H3 ( 'repositories' ) ;
echo BQ ( gen_tab_sides ( $tab ) ) ;
echo H3 ( 'recent additions' ) ;
echo BQ ( gen_tab_repos ( $recent ) ) ;
echo H3 ( 'on revision zero ; ordered by date' ) ;
echo BQ ( gen_tab_repos ( $rev0s ) ) ;
echo H3 ( 'un-used guests' ) ;
echo BQ ( gen_tab_unused_guests () ) ;
echo H3 ( 'un-used groups' ) ;
echo BQ ( gen_tab_unused_groups () ) ;
echo H3 ( 'ex-users' ) ;
echo gen_xusers ( $xusers, $xrepos, $xgroups, $missing, $groups ) ;
echo H3 ( 'repos without rights' ) ;
echo BQ ( gen_no_rights () ) ;
echo H3 ( 'repos with paths' ) ;
echo BQ ( gen_with_paths () ) ;
html_end () ;
# phpinfo() ;
?>