| 1 | #!/usr/bin/perl -w |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | use lib qw( lib extlib ); |
|---|
| 5 | |
|---|
| 6 | use Getopt::Long; |
|---|
| 7 | use LWP::UserAgent; |
|---|
| 8 | use HTTP::Request; |
|---|
| 9 | use Web::Scraper; |
|---|
| 10 | use Data::Dumper; |
|---|
| 11 | use Time::HiRes; |
|---|
| 12 | use MT; |
|---|
| 13 | use MT::Blog; |
|---|
| 14 | |
|---|
| 15 | # Set default option value |
|---|
| 16 | my @blog_ids; |
|---|
| 17 | my @templates; |
|---|
| 18 | my $entry_id = 0; |
|---|
| 19 | my $count = 1; |
|---|
| 20 | my $mt_url = ''; |
|---|
| 21 | my $username = 'Melody'; |
|---|
| 22 | my $password = 'Nelson'; |
|---|
| 23 | my $silent = 0; |
|---|
| 24 | my $usage = 0; |
|---|
| 25 | |
|---|
| 26 | GetOptions( |
|---|
| 27 | 'blog_id=s' => \@blog_ids, |
|---|
| 28 | 'template=s' => \@templates, |
|---|
| 29 | 'count=i' => \$count, |
|---|
| 30 | 'mt_url=s' => \$mt_url, |
|---|
| 31 | 'user=s' => \$username, |
|---|
| 32 | 'pass=s' => \$password, |
|---|
| 33 | 'silent' => \$silent, |
|---|
| 34 | 'uaage' => \$usage, |
|---|
| 35 | ); |
|---|
| 36 | |
|---|
| 37 | # Construct MT object |
|---|
| 38 | my $mt = new MT |
|---|
| 39 | or die "Not MT object " . MT->errstr(); |
|---|
| 40 | |
|---|
| 41 | # Validation of required options |
|---|
| 42 | if ( $usage |
|---|
| 43 | || ( !$mt_url ) |
|---|
| 44 | || ( !$username ) |
|---|
| 45 | || ( !$password ) ) |
|---|
| 46 | { |
|---|
| 47 | usage(); |
|---|
| 48 | exit; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | # Parse and load blog |
|---|
| 52 | if (@blog_ids) { |
|---|
| 53 | @blog_ids = split(/,/, join(',', @blog_ids)); |
|---|
| 54 | } |
|---|
| 55 | my @blogs = MT::Blog->load( { ( @blog_ids ? ( id => @blog_ids ) : () ), } ) |
|---|
| 56 | or die "Couldn't load blogs. [" . Dumper(@blog_ids) . "]\n"; |
|---|
| 57 | |
|---|
| 58 | # Parse template option |
|---|
| 59 | if (@templates) { |
|---|
| 60 | @templates = split(/,/, join(',', @templates)); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | # Construct UserAgent object |
|---|
| 64 | my $ua = new LWP::UserAgent; |
|---|
| 65 | |
|---|
| 66 | # Create scraper object |
|---|
| 67 | my $scraper = scraper { |
|---|
| 68 | process "div.msg-publishing", status_publishing => 'TEXT'; |
|---|
| 69 | process "div.msg-error", status_error => 'TEXT'; |
|---|
| 70 | process "div.msg-success", status_success => 'TEXT'; |
|---|
| 71 | |
|---|
| 72 | result 'status_publishing', 'status_error', 'status_success'; |
|---|
| 73 | }; |
|---|
| 74 | |
|---|
| 75 | # call rebuild |
|---|
| 76 | for ( my $count_i = 0 ; $count_i < $count ; $count_i++ ) { |
|---|
| 77 | foreach my $blog (@blogs) { |
|---|
| 78 | logging( "rebuilding (" . ($count_i + 1) . ") " . $blog->name . " ...\n" ); |
|---|
| 79 | run_rebuild($blog); |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | sub run_rebuild { |
|---|
| 84 | my $blog = shift; |
|---|
| 85 | |
|---|
| 86 | my @at; |
|---|
| 87 | if ( !@templates ) { |
|---|
| 88 | @at = split( ',', $blog->archive_type ); |
|---|
| 89 | push @at, 'index'; |
|---|
| 90 | } |
|---|
| 91 | else { |
|---|
| 92 | @at = @templates; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | foreach (@at) { |
|---|
| 96 | my $at = $_; |
|---|
| 97 | my $archiver = $mt->publisher->archiver($at); |
|---|
| 98 | next if ( !$archiver ) && ( $at ne 'index' ); |
|---|
| 99 | |
|---|
| 100 | my $start = Time::HiRes::time(); |
|---|
| 101 | my $total = 0; |
|---|
| 102 | if ($archiver) { |
|---|
| 103 | if ( ( $archiver->entry_based || $archiver->date_based ) |
|---|
| 104 | && !$entry_id ) |
|---|
| 105 | { |
|---|
| 106 | my $entry_class = $archiver->entry_class || 'entry'; |
|---|
| 107 | require MT::Entry; |
|---|
| 108 | my $terms = { |
|---|
| 109 | class => $entry_class, |
|---|
| 110 | status => MT::Entry::RELEASE(), |
|---|
| 111 | blog_id => $blog->id, |
|---|
| 112 | }; |
|---|
| 113 | $total = MT::Entry->count($terms); |
|---|
| 114 | } |
|---|
| 115 | elsif ( $archiver->category_based ) { |
|---|
| 116 | require MT::Category; |
|---|
| 117 | my $terms = { |
|---|
| 118 | blog_id => $blog->id, |
|---|
| 119 | class => $archiver->category_class, |
|---|
| 120 | }; |
|---|
| 121 | $total = MT::Category->count($terms); |
|---|
| 122 | } |
|---|
| 123 | elsif ( $archiver->author_based ) { |
|---|
| 124 | require MT::Author; |
|---|
| 125 | require MT::Entry; |
|---|
| 126 | my $terms = { |
|---|
| 127 | blog_id => $blog->id, |
|---|
| 128 | status => MT::Entry::RELEASE(), |
|---|
| 129 | class => 'entry', |
|---|
| 130 | }; |
|---|
| 131 | $total = MT::Author->count( |
|---|
| 132 | undef, |
|---|
| 133 | { |
|---|
| 134 | join => MT::Entry->join_on( |
|---|
| 135 | 'author_id', $terms, { unique => 1 } |
|---|
| 136 | ), |
|---|
| 137 | unique => 1, |
|---|
| 138 | } |
|---|
| 139 | ); |
|---|
| 140 | } |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | my $url = |
|---|
| 144 | $mt_url |
|---|
| 145 | . "?__mode=rebuild" |
|---|
| 146 | . "&blog_id=" |
|---|
| 147 | . $blog->id |
|---|
| 148 | . "&next=0" |
|---|
| 149 | . "&offset=0" |
|---|
| 150 | . "&limit=20" |
|---|
| 151 | . "&entry_id=" |
|---|
| 152 | . "&is_new=" |
|---|
| 153 | . "&old_status=" |
|---|
| 154 | . "&old_previous=" |
|---|
| 155 | . "&old_next=" |
|---|
| 156 | . "&total=" |
|---|
| 157 | . $total |
|---|
| 158 | . "&type=" |
|---|
| 159 | . $at; |
|---|
| 160 | do { |
|---|
| 161 | $url .= "&username=" . $username; |
|---|
| 162 | $url .= "&password=" . $password; |
|---|
| 163 | my $req = new HTTP::Request( GET => $url ); |
|---|
| 164 | my $resp = $ua->request($req); |
|---|
| 165 | if ( $resp->is_success ) { |
|---|
| 166 | my $res = $scraper->scrape( $resp->content() ); |
|---|
| 167 | if ( $res->{status_publishing} ) { |
|---|
| 168 | ( undef, $url ) = |
|---|
| 169 | $resp->content() =~ /window.location='(.*)\?(.*)'/; |
|---|
| 170 | $url = $mt_url . "?" . $url; |
|---|
| 171 | } |
|---|
| 172 | elsif ( $res->{status_success} ) { |
|---|
| 173 | logging( "\t" . $at . " built success." . "\n" ); |
|---|
| 174 | $url = undef; |
|---|
| 175 | } |
|---|
| 176 | elsif ( $res->{status_error} ) { |
|---|
| 177 | logging("\t" |
|---|
| 178 | . $at |
|---|
| 179 | . " built failed. " |
|---|
| 180 | . $res->{status_error} |
|---|
| 181 | . "\n" ); |
|---|
| 182 | $url = undef; |
|---|
| 183 | } |
|---|
| 184 | else { |
|---|
| 185 | logging("\t" |
|---|
| 186 | . $at |
|---|
| 187 | . " built failed.\n" |
|---|
| 188 | . $resp->content() |
|---|
| 189 | . "\n" ); |
|---|
| 190 | $url = undef; |
|---|
| 191 | } |
|---|
| 192 | } |
|---|
| 193 | else { |
|---|
| 194 | logging( |
|---|
| 195 | "\t" . $at . " request failed (" . $resp->code . ")\n" ); |
|---|
| 196 | $url = undef; |
|---|
| 197 | } |
|---|
| 198 | } while ($url); |
|---|
| 199 | |
|---|
| 200 | logging( "\t total build time:" . ( Time::HiRes::time() - $start ) . "\n" ); |
|---|
| 201 | } |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | sub logging { |
|---|
| 205 | my ($msg) = @_; |
|---|
| 206 | print $msg if !$silent; |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | sub usage { |
|---|
| 210 | print STDERR << "EOT"; |
|---|
| 211 | usage: $0 |
|---|
| 212 | require: |
|---|
| 213 | -mt_url='URL to mt.cgi' |
|---|
| 214 | -user='login account of mt' |
|---|
| 215 | -pass='login password of mt' |
|---|
| 216 | |
|---|
| 217 | optional: |
|---|
| 218 | -blog_id='target blog id.' |
|---|
| 219 | -template='target archive type' |
|---|
| 220 | -count='the count of cyclic call' |
|---|
| 221 | -silent='no output any logs (1|0)' |
|---|
| 222 | -usage='show this message' |
|---|
| 223 | |
|---|
| 224 | * This script requires Web::Scraper. You must install from CPAN if you not installed yet. |
|---|
| 225 | EOT |
|---|
| 226 | } |
|---|