Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Janitors' Tools - Bulk node retitler

by Corion (Patriarch)
on Jul 16, 2003 at 13:31 UTC ( #274951=sourcecode: print w/replies, xml ) Need Help??
Category: PerlMonks Related Scripts
Author/Contact Info

From time to time, a janitor has to clean up a thread where the original poster chose a bad title for his question. If the janitor is too late, there are many nodes to retitle. This WWW::Mechanize script automates this process.

Of course, this script will only work, if you are a janitor, and it will break horribly if the PerlMonks HTML changes enough.

Update: castaway noted that the retitler broke due to recent changes in how internal links are done. The current version fixes that.

#!/usr/bin/perl -w
use strict;
use WWW::Mechanize;
use URI::URL;
use POSIX;
use HTML::Entities qw( encode_entities );

# Bulk node retitler

my ($old,$new,$node_id) = @ARGV;
die "Need the part to look for" unless $old;
die "Need something to replace it with " unless defined $new;
die "Need a node id" unless $node_id;

$old = qr/$old/;

my $agent = WWW::Mechanize->new();

my ($user,$password) = ($ENV{PERLMONKS_USER},$ENV{PERLMONKS_PASS});
die "No Perlmonks password found" unless $password;
die "No Perlmonks user found" unless $user;

# Login and get the node-to-be-retitled
$agent->current_form->value('user', $user);
$agent->current_form->value('passwd', $password );
$agent->current_form->value('expires', '+10y');

die "No title found in node $node_id\n"
  unless $agent->content =~ m!<title>\s*(.+?)\s*</title>!ism;
my $fulltitle = $1;

my @links = @{$agent->links};
my $counter = -1;
my @node_ids = map { $counter++;
                     ($_->[1] =~ m!^(?:Re(\^\d+)?:\s*)*\Q$fulltitle\E!
+i and $_->[0] =~ m!^(?:/\?node_id=(\d+)$!i) ? $1 : ()
                   } @links;

my $history = sprintf "<p><small>%s Edit by [%s]: Changed title from '
                  strftime('%Y%m%d',gmtime()),$user, encode_entities($

  for (@node_ids);

sub retitle_node {
  my ($node_id,$old,$new,$history) = @_;
  $agent->get(sprintf '
+ditors;node_id=%s', $node_id);
  my $old_title = $agent->current_form->value('update_title');
  my $new_title = $old_title;
  $new_title =~ s!$old!$new!;
  if ($new_title ne $old_title) {
    warn "Retitling $old_title to $new_title\n";
    $agent->current_form->value('update_title', $new_title);
    if ($history) {
      my $doctext = $agent->current_form->value('update_doctext') . $h
      $agent->current_form->value('update_doctext', $doctext);
    # Maybe also /msg the respective user ...
  $agent->follow_link( text => 'display');

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://274951]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2023-12-09 19:14 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (38 votes). Check out past polls.