|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * File: class-boldgrid-backup-admin-filelist-analyzer.php |
| 4 | + * |
| 5 | + * @link https://www.boldgrid.com |
| 6 | + * @since SINCEVERSION |
| 7 | + * |
| 8 | + * @package Boldgrid_Backup |
| 9 | + * @subpackage Boldgrid_Backup/admin |
| 10 | + * @copyright BoldGrid |
| 11 | + * @version $Id$ |
| 12 | + * @author BoldGrid <support@boldgrid.com> |
| 13 | + */ |
| 14 | + |
| 15 | +/** |
| 16 | + * Class: Boldgrid_Backup_Admin_Filelist |
| 17 | + * |
| 18 | + * @since SINCEVERSION |
| 19 | + */ |
| 20 | +class Boldgrid_Backup_Admin_Filelist_Analyzer { |
| 21 | + /** |
| 22 | + * The key used to save this setting in the Total Upkeep settings. |
| 23 | + * |
| 24 | + * @since SINCEVERSION |
| 25 | + * @var string |
| 26 | + */ |
| 27 | + public static $settings_key = 'filelist_analysis'; |
| 28 | + |
| 29 | + /** |
| 30 | + * The unix timestamp used for the parent backup's log file. |
| 31 | + * |
| 32 | + * This is passed in via the constructor, and is only used when creating the log file. We want |
| 33 | + * the unixtime in the "backup" and "backup filelist" log fies to match so you know their is a |
| 34 | + * relationship. |
| 35 | + * |
| 36 | + * @since SINCEVERSION |
| 37 | + * @access private |
| 38 | + * @var int |
| 39 | + */ |
| 40 | + private $log_time; |
| 41 | + |
| 42 | + /** |
| 43 | + * An array of files. |
| 44 | + * |
| 45 | + * This is passed in via the contructor. This is the same filelist that is passed to each compressor |
| 46 | + * so they know which files to backup. |
| 47 | + * |
| 48 | + * @since SINCEVERSION |
| 49 | + * @access private |
| 50 | + * @var array |
| 51 | + */ |
| 52 | + private $filelist; |
| 53 | + |
| 54 | + /** |
| 55 | + * Constructor. |
| 56 | + * |
| 57 | + * @since SINCEVERSION |
| 58 | + * |
| 59 | + * @param array $filelist |
| 60 | + * @param int $log_time |
| 61 | + */ |
| 62 | + public function __construct( $filelist = array(), $log_time = 0 ) { |
| 63 | + $this->filelist = is_array( $filelist ) ? $filelist : array(); |
| 64 | + $this->log_time = ! empty( $log_time ) ? $log_time : time(); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Whether or not the filelist analyer is enabled. |
| 69 | + * |
| 70 | + * @since SINCEVERSION |
| 71 | + * |
| 72 | + * @return bool |
| 73 | + */ |
| 74 | + public static function is_enabled() { |
| 75 | + $core = apply_filters( 'boldgrid_backup_get_core', null ); |
| 76 | + $setting = $core->settings->get_setting( self::$settings_key ); |
| 77 | + |
| 78 | + return ! empty( $setting ); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Run. |
| 83 | + * |
| 84 | + * Do all the magic and write the log file. |
| 85 | + * |
| 86 | + * @since SINCEVERSION |
| 87 | + */ |
| 88 | + public function run() { |
| 89 | + $core = apply_filters( 'boldgrid_backup_get_core', null ); |
| 90 | + $logger = new Boldgrid_Backup_Admin_Log( $core ); |
| 91 | + |
| 92 | + $size_by_extension = array(); |
| 93 | + $count_by_extension = array(); |
| 94 | + $size_by_dir = array(); |
| 95 | + |
| 96 | + $logger->init( 'archive-' . $this->log_time . '-filelist.log' ); |
| 97 | + |
| 98 | + // Loop through each file. |
| 99 | + foreach ( $this->filelist as $file ) { |
| 100 | + $extension = pathinfo( $file[1], PATHINFO_EXTENSION ); |
| 101 | + $dir = dirname( $file[1] ); |
| 102 | + |
| 103 | + // Generate our stats. |
| 104 | + $size_by_extension[ $extension ] = empty( $size_by_extension[ $extension ] ) ? $file[2] : $size_by_extension[ $extension ] + $file[2]; |
| 105 | + $count_by_extension[ $extension ] = empty( $count_by_extension[ $extension ] ) ? 1 : $count_by_extension[ $extension ] + 1; |
| 106 | + $size_by_dir[ $dir ] = empty( $size_by_dir[ $dir ] ) ? $file[2] : $size_by_dir[ $dir ] + $file[2]; |
| 107 | + } |
| 108 | + |
| 109 | + // Display top 100 files. |
| 110 | + $limit = 100; |
| 111 | + $to_show = count( $size_by_extension ) >= $limit ? $limit : count( $size_by_extension ); |
| 112 | + $key = 1; |
| 113 | + |
| 114 | + $logger->add_separator(); |
| 115 | + $logger->add( 'LARGEST FILES' ); |
| 116 | + |
| 117 | + usort( $this->filelist, function ( $item1, $item2 ) { |
| 118 | + return $item1[2] <= $item2[2] ? 1 : -1; |
| 119 | + } ); |
| 120 | + |
| 121 | + foreach ( $this->filelist as $file ) { |
| 122 | + $logger->add( '(' . $key . ') ' . $file[1] . ' - ' . size_format( $file[2], 2 ) ); |
| 123 | + |
| 124 | + $key++; |
| 125 | + if ( $key > $to_show ) { |
| 126 | + break; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + // Display size by extension. |
| 131 | + $limit = 30; |
| 132 | + $to_show = count( $size_by_extension ) >= $limit ? $limit : count( $size_by_extension ); |
| 133 | + $key = 1; |
| 134 | + |
| 135 | + $logger->add_separator(); |
| 136 | + $logger->add( 'SIZE BY EXTENSION' ); |
| 137 | + |
| 138 | + arsort( $size_by_extension ); |
| 139 | + |
| 140 | + foreach ( $size_by_extension as $extension => $size ) { |
| 141 | + $logger->add( '(' . $key . ') .' . $extension . ' - ' . number_format( $count_by_extension[ $extension ] ) . ' files totaling ' . size_format( $size, 2 ) ); |
| 142 | + |
| 143 | + $key++; |
| 144 | + if ( $key > $to_show ) { |
| 145 | + break; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + // Display size by directory. |
| 150 | + $limit = 30; |
| 151 | + $to_show = count( $size_by_dir ) >= $limit ? $limit : count( $size_by_dir ); |
| 152 | + $key = 1; |
| 153 | + |
| 154 | + $logger->add_separator(); |
| 155 | + $logger->add( 'SIZE BY DIRECTORY' ); |
| 156 | + |
| 157 | + arsort( $size_by_dir ); |
| 158 | + |
| 159 | + foreach ( $size_by_dir as $directory => $size ) { |
| 160 | + $logger->add( '(' . $key . ') ' . $directory . ' - ' . size_format( $size, 2 ) ); |
| 161 | + |
| 162 | + $key++; |
| 163 | + if ( $key > $to_show ) { |
| 164 | + break; |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments