/usr/bin/wg-findBrokenAssets is in webgui 7.10.28-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 | #!/usr/bin/perl
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2009 Plain Black Corporation.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#-------------------------------------------------------------------
# http://www.plainblack.com info@plainblack.com
#-------------------------------------------------------------------
use strict;
use File::Basename ();
use File::Spec;
my $webguiRoot;
BEGIN {
$webguiRoot = "/usr/share/webgui";
unshift @INC, File::Spec->catdir( $webguiRoot, 'lib' );
}
$|++; # disable output buffering
our ( $configFile, $help, $man, $fix, $delete, $no_progress, $op_assetId );
use Pod::Usage;
use Getopt::Long;
use WebGUI::Session;
# Get parameters here, including $help
GetOptions(
'configFile=s' => \$configFile,
'help' => \$help,
'man' => \$man,
'fix' => \$fix,
'delete' => \$delete,
'noProgress' => \$no_progress,
'assetId=s' => \$op_assetId,
);
pod2usage( verbose => 1 ) if $help;
pod2usage( verbose => 2 ) if $man;
pod2usage( msg => "Must specify a config file!" ) unless $configFile;
foreach my $libDir ( readLines( "preload.custom" ) ) {
if ( !-d $libDir ) {
warn "WARNING: Not adding lib directory '$libDir' from preload.custom: Directory does not exist.\n";
next;
}
unshift @INC, $libDir;
}
my $session = start( $webguiRoot, $configFile );
sub progress {
my ( $total, $current ) = @_;
local $| = 1;
my $done = int( ( ( $current / $total ) * 100 ) / 2 );
$done &&= $done - 1; # Fit the >
my $space = 49 - $done;
print "\r[", '=' x $done, '>', ' ' x $space, ']';
printf ' (%d/%d)', $current, $total;
}
## SQL statements
my $total_asset_sql = 'SELECT COUNT(*) FROM asset ';
my $total_assetdata_sql = 'SELECT COUNT( DISTINCT( assetId ) ) FROM assetData ';
my $count_shortcut_sql = q!select count(*) from asset where className='WebGUI::Asset::Shortcut' !;
my $count_files_sql = q!select count(*) from asset where className like 'WebGUI::Asset::File%' !;
# Order by lineage to put corrupt parents before corrupt children
# Join assetData to get all asset and assetData
my $iterator_sql = "SELECT assetId, className, revisionDate, parentId FROM asset LEFT JOIN assetData USING ( assetId ) ";
my $sql_args = [];
if ($op_assetId) {
my $asset_selector = 'where assetId = ? ';
$iterator_sql .= $asset_selector;
$total_asset_sql .= $asset_selector;
$total_assetdata_sql .= $asset_selector;
$count_shortcut_sql .= ' AND assetId = ? ';
$count_files_sql .= ' AND assetId = ? ';
push @{ $sql_args }, $op_assetId;
}
$iterator_sql .= "GROUP BY assetId ORDER BY lineage ASC";
my $sth = $session->db->read($iterator_sql, $sql_args);
my $totalAsset = $session->db->quickScalar($total_asset_sql, $sql_args);
my $totalAssetData = $session->db->quickScalar($total_assetdata_sql, $sql_args);
my $total = $totalAsset >= $totalAssetData ? $totalAsset : $totalAssetData;
##Guarantee that we get the most recent revisionDate
my $max_revision = $session->db->prepare('select max(revisionDate) from assetData where assetId=?');
print "Checking all assets\n";
my $count = 1;
my %classTables; # Cache definition lookups
while ( my %row = $sth->hash ) {
my $asset = eval { WebGUI::Asset->newPending( $session, $row{assetId} ) };
if ( $@ || ! $asset ) {
# Replace the progress bar with a message
printf "\r%-68s", "-- Corrupt: $row{assetId}";
# Should we do something?
if ($fix) {
my $classTables = $classTables{ $row{className} } ||= do {
eval "require $row{className}";
[ map { $_->{tableName} } reverse @{ $row{className}->definition($session) } ];
};
$max_revision->execute([$row{assetId}]);
($row{revisionDate}) = $max_revision->array();
$row{revisionDate} ||= time;
for my $table ( @{$classTables} ) {
my $sqlFind = "SELECT * FROM $table WHERE assetId=? ORDER BY revisionDate DESC";
my @params = @row{qw( assetId )};
my $insertRow = $session->db->quickHashRef( $sqlFind, \@params ) || {};
if ( $row{revisionDate} != $insertRow->{revisionDate} ) {
$insertRow->{ assetId } = $row{assetId};
$insertRow->{ revisionDate } = $row{revisionDate};
my $cols = join ",", keys %$insertRow;
my @values = values %$insertRow;
my $places = join ",", ('?') x @values;
my $sqlFix = "INSERT INTO $table ($cols) VALUES ($places)";
$session->db->write( $sqlFix, \@values );
}
}
print "Fixed.\n";
my $asset = WebGUI::Asset->newByDynamicClass( $session, $row{assetId} );
# Make sure we have a valid parent
if (!$asset) {
print "\tWARNING. Asset is still broken.\n";
}
elsif (! WebGUI::Asset->newByDynamicClass( $session, $row{parentId} )) {
$asset->setParent( WebGUI::Asset->getImportNode( $session ) );
print "\tNOTE: Invalid parent. Asset moved to Import Node\n";
}
} ## end if ($fix)
elsif ($delete) {
my $classTables = $classTables{ $row{className} } ||= do {
eval "require $row{className}";
[ map { $_->{tableName} } reverse @{ $row{className}->definition($session) } ];
};
my @params = @row{qw( assetId revisionDate )};
for my $table ( @{$classTables} ) {
my $sqlDelete = "DELETE FROM $table WHERE assetId=? AND revisionDate=?";
$session->db->write( $sqlDelete, \@params );
}
$session->db->write( "DELETE FROM asset WHERE assetId=?", [$row{assetId}] );
print "Deleted.\n";
} ## end elsif ($delete)
else { # report
print "\n";
if ( $row{revisionDate} ) {
printf "%10s: %s\n", "revised", scalar( localtime $row{revisionDate} );
}
# Classname
printf "%10s: %s\n", "class", $row{className};
# Parent
if ( my $parent = WebGUI::Asset->newByDynamicClass( $session, $row{parentId} ) ) {
printf "%10s: %s (%s)\n", "parent", $parent->getTitle, $parent->getId;
}
elsif ( $session->db->quickScalar( "SELECT * FROM asset WHERE assetId=?", [$row{parentId}] ) ) {
print "Parent corrupt ($row{parentId}).\n";
}
else {
print "Parent missing ($row{parentId}).\n";
}
# More properties
if ( $row{revisionDate} ) {
my %assetData = $session->db->quickHash( "SELECT * FROM assetData WHERE assetId=? AND revisionDate=?",
[ @row{ "assetId", "revisionDate" } ] );
for my $key (qw( title url )) {
printf "%10s: %s\n", $key, $assetData{$key};
}
}
else {
print "No current asset data.\n";
}
# Previous revisions
my %lastRev
= $session->db->quickHash(
"SELECT * FROM assetData WHERE assetId=? AND revisionDate != ? ORDER BY revisionDate DESC",
[ $row{assetId}, $row{revisionDate} ]
);
if ( $lastRev{assetId} ) {
print "Previous revision:\n";
for my $key (qw( title url )) {
printf "%10s: %s\n", $key, $lastRev{$key};
}
}
else {
print "No previous revisions.\n";
}
# Asset History
my $history = $session->db->buildArrayRefOfHashRefs(
"SELECT * FROM assetHistory LEFT JOIN users USING (userId) WHERE assetId=? ORDER BY dateStamp DESC",
[ $row{assetId} ],
);
if ( $history->[0] ) {
my $username = $history->[0]{username} || "<Unknown User>";
printf "Last action '%s'\n\tby %s\n\ton %s\n",
$history->[0]{actionTaken},
$username,
scalar( localtime $history->[0]{dateStamp} ),
;
}
} ## end else [ if ($fix) ]
} ## end if ( !$asset )
progress( $total, $count++ ) unless $no_progress;
} ## end while ( my %row = $sth->hash)
$sth->finish;
$max_revision->finish;
print "\n";
my $shortcuts = $session->db->quickScalar($count_shortcut_sql, $sql_args);
if ($shortcuts) {
print "Checking for broken shortcuts\n";
use WebGUI::Asset::Shortcut;
my $get_shortcut = WebGUI::Asset::Shortcut->getIsa($session, 0, {returnAll => 1});
$count = 0;
SHORTCUT: while (1) {
my $shortcut = eval { $get_shortcut->() };
if ( $@ || Exception::Class->caught() ) {
##Do nothing, since it would have been caught above
printf "\r%-68s", "No shortcut to check";
}
elsif (!$shortcut) {
last SHORTCUT
}
else {
my $linked_asset = eval { WebGUI::Asset->newPending($session, $shortcut->get('shortcutToAssetId')); };
if ( $@ || Exception::Class->caught() || ! $linked_asset ) {
printf "\r%-68s", "-- Broken shortcut: ".$shortcut->getId.' pointing to '.$shortcut->get('shortcutToAssetId');
if ($delete) {
my $success = $shortcut->purge;
if ($success) {
print "Purged shortcut";
}
else {
print "Could not purge shortcut";
}
}
print "\n";
}
}
progress( $shortcuts, $count++ ) unless $no_progress;
}
progress( $shortcuts, $count ) unless $no_progress;
}
print "\n";
my $file_assets = $session->db->quickScalar($count_files_sql, $sql_args);
if ($file_assets) {
print "Checking for broken File Assets\n";
use WebGUI::Asset::File;
my $get_asset = WebGUI::Asset::File->getIsa($session, 0, {returnAll => 1});
$count = 0;
FILE_ASSET: while (1) {
my $file_asset = eval { $get_asset->() };
if ( $@ || Exception::Class->caught() ) {
##Do nothing, since it would have been caught above
printf "\r%-68s\n", "No asset to check";
}
elsif (!$file_asset) {
last FILE_ASSET
}
else {
my $storage = $file_asset->getStorageLocation;
if (! $storage) {
printf "\r%-s\n", "-- No storage location: ".$file_asset->getId." storageId: ".$file_asset->get('storageId');
}
else {
my $file = $storage->getPath($file_asset->get('filename'));
if (! -e $file) {
printf "\r%-s", "-- Broken file asset: ".$file_asset->getId." file does not exist: $file";
if ($delete) {
my $success = $file_asset->purge;
if ($success) {
print "Purged File Asset";
}
else {
print "Could not purge File Asset";
}
}
print "\n";
}
}
}
progress( $file_assets, $count++ ) unless $no_progress;
}
progress( $file_assets, $count ) unless $no_progress;
}
finish($session);
print "\n";
#----------------------------------------------------------------------------
# Your sub here
#-------------------------------------------------
sub readLines {
my $file = shift;
my @lines;
if (open(my $fh, '<', $file)) {
while (my $line = <$fh>) {
$line =~ s/#.*//;
$line =~ s/^\s+//;
$line =~ s/\s+$//;
next if !$line;
push @lines, $line;
}
close $fh;
}
return @lines;
}
#----------------------------------------------------------------------------
sub start {
my $webguiRoot = shift;
my $configFile = shift;
my $session = WebGUI::Session->open( $webguiRoot, $configFile );
$session->user( { userId => 3 } );
return $session;
}
#----------------------------------------------------------------------------
sub finish {
my $session = shift;
$session->var->end;
$session->close;
}
__END__
=head1 NAME
findBrokenAssets.pl -- Find and fix broken assets
=head1 SYNOPSIS
findBrokenAssets.pl --configFile config.conf [--fix|--delete]
utility --help
=head1 DESCRIPTION
This utility will find any broken assets that cannot be instantiated and are
causing undesired operation of your website. It also checks for these kinds of
semi-working assets and reports them:
=over 4
=item *
Shortcuts pointing to assets that don't exist.
=item *
File assets that have lost their files in the uploads area.
=back
It can also automatically delete them or fix them so you can restore missing data.
=head1 ARGUMENTS
=head1 OPTIONS
=over
=item B<--configFile config.conf>
The WebGUI config file to use. Only the file name needs to be specified,
since it will be looked up inside WebGUI's configuration directory.
This parameter is required.
=item B<--delete>
Delete any corrupted assets.
=item B<--fix>
Try to fix any corrupted assets. The broken Shortcuts and File Assets cannot be fixed.
=item B<--assetId=s>
Limit the search for all broken assets to one assetId.
=item B<--help>
Shows a short summary and usage
=item B<--man>
Shows this document
=back
=head1 AUTHOR
Copyright 2001-2009 Plain Black Corporation.
=cut
#vim:ft=perl
|