root/trunk/tools/rebuild-pages

Revision 4196, 6.7 kB (checked in by takayama, 3 months ago)

* Set svn keywords

  • Property svn:keywords set to Author Date Id Revision
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use lib qw(lib ../lib ../extlib extlib );
7
8use LWP::UserAgent::Local;
9use HTTP::Request;
10use Data::Dumper;
11use Getopt::Long;
12use Web::Scraper;
13use MT;
14
15### Parses options.
16my @blog_ids;
17my @types;
18my $username    = '';
19my $password    = '';
20my $script_path = '/mt/';
21my $hostname    = 'localhost';
22my $silent      = 0;
23my $usage       = 0;
24my $elapse      = 0;
25GetOptions(
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
37if ($usage || !$username || !$password) {
38    use Pod::Usage;
39    pod2usage(1);
40    exit;
41}
42
43if ($elapse) {
44    use Time::HiRes;
45}
46
47### Creates MT object.
48my $mt = MT->new() or die MT->errstr;
49
50### Loads blogs
51if (!@blog_ids) {
52    push @blog_ids, '1';
53}
54
55my @blogs = MT::Blog->load( { id => \@blog_ids } )
56    or die "Couldn't load blogs. [" . Dumper(@blog_ids) . "]\n";
57
58# Creates scraper object
59my $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
68my $ua = new LWP::UserAgent::Local( { ScriptAlias => $script_path } );
69
70### Runs build script.
71foreach my $blog (@blogs) {
72    logging( "rebuilding (" . $blog->name . " ...\n" );
73    run_rebuild($blog);
74}
75
76sub 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
198sub logging {
199    my ($msg) = @_;
200    print $msg if !$silent;
201}
202
2031;
204
205__END__
206
207=head1 NAME
208
209rebuild_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
Note: See TracBrowser for help on using the browser.