Toolserver:User:Kylu/emptypages.php

From mediawiki.org

This page was moved from the Toolserver wiki.
Toolserver has been replaced by Toolforge. As such, the instructions here may no longer work, but may still be of historical interest.
Please help by updating examples, links, template links, etc. If a page is still relevant, move it to a normal title and leave a redirect.

This is actually my first php program. You'd think I'd start with something easy, like "Hello, world!" ...

<head><title>Kylu's Empty Pages tool</title></head>
<body bgcolor="#00ffff">
<?php
 include('../userdata.php');
 echo "Kylu's Empty Pages tool<br>";
 echo 'One moment...';
 $count = 0;

$link = mysql_connect("enwiki-p.db.ts.wikimedia.org", $db_username, $db_password)
or die ('database connect failed: '. mysql_error());

@mysql_select_db('enwiki_p', $link)
or die ('database select failed: '. mysql_error());

 $sql_query =
 "SELECT page_title FROM page AS P"
 ." WHERE page_is_redirect = 0"
 ." AND page_namespace = 0"
 ." AND page_len < 20"
 ." ORDER BY page_title;"
 ;

$result = mysql_query($sql_query, $link)
or die ('Query failed: '. mysql_error());
echo 'done.<br>';

echo "<table>\n";
while ($line = mysql_fetch_Array($result, MYSQL_ASSOC)) {
  echo "\t<tr>\n";
  foreach ($line as $col_value) {
    $count = $count + 1;
    echo
    '<td>'
    . '<a href="http://en.wikipedia.org/w/index.php?title=' . $col_value . '&action=history">History</a> '
    . '<a href="http://en.wikipedia.org/wiki/' . $col_value . '">' . $col_value . '</a>'
    . '</td>'
    ;
  }
  echo "\t</tr>\n";
}
echo "<br><br>Number of results: $count";
echo "</table>\n Finished!<br><br>";
echo '<a href="http://tools.wikimedia.de/~kylu/emptypages.php">Run again?</a>';

mysql_free_result($result)
or die ('Free result died? That happens? '. mysql_error());

mysql_close($link)
or die ('Finished anyway, who cares. '. mysql_error());

?>
</body>