#!/usr/bin/perl

# getphpfunctionlist.pl

# Leonard den Ottolander (leonardjo@hetnet.nl), 2003
# GPL v.2

# Produces a list of php functions from the docs at http://www.php.net/manual/en
# This version is tested on the docs at 25-12-2003

require LWP::UserAgent;

$startline = 'Table of Contents';
$stopline = '<div id="usernotes">';
$ua = LWP::UserAgent->new();
$phpfuncs = '';
$url = 'http://www.php.net/manual/en/funcref.php';
$response = $ua->get($url);
if ($response->is_success) 
{ $_ = $response->content;
  s/\n>/>\n/g;
  s/A\nHREF/A HREF/g;
  $i = 0;
  $process = 0;
  $pages = '';
  @lines = split(/\n/,$_);
  foreach(@lines)
  { $_ = @lines[$i];
    if ($process == 0)
    {
      if (/$startline/)
      {
        $process = 1;
      };
    }
    else
    {
      if (/$stopline/)
      {
        last;
      };
      if (/<A HREF/)
      {
        $pos = index($_, '"');
	$_ = substr($_, $pos + 1, length($_) - $pos - 1);
	$pos = index($_, '"');
	$_ = substr($_, 0, $pos);
        $pages .= "http://www.php.net/manual/en/$_\n";
      };
    };
    $i++;
  };
};

@page = split(/\n/,$pages);
foreach(@page)
{ $url = @page[$j];
  $response = $ua->get($url);
  if ($response->is_success) 
  { $res = $response->content;
    $_ = $res;
    s/\n>/>\n/g;
    s/A\nHREF/A HREF/g;
    $res = $_;
    $i = 0;
    $process = 0;
    $pages = '';
    @lines = split(/\n/,$res);
    foreach(@lines)
    { $_ = @lines[$i];
      if ($process == 0)
      {
        if (/$startline/)
        {
          $process = 1;
        };
      }
      else
      {
        if (/$stopline/)
        {
          last;
        };
        if (/<\/A/)
        {
          $pos = index($_, '<');
  	  $_ = substr($_, 0, $pos);
	  # Some add hoc filtering
	  # Aliases produce empty lines and one url is produced
	  if (($_ ne '') && (! /^http\:\/\//))
	  {
            $phpfuncs .= "$_\n";
	  };
	};
      };
      $i++;
    };
    $j++;
  };
};
$_ = $phpfuncs;
s/\&\#62\;/\>/g;
print $_;
