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