Manual:Chris G's botclasses/AllCategoryMembersBot.php

This bot retrieves a list of all members of a category and stores that list in a text file.

<?php
/* AllCategoryMembersBot
 * By Leucosticte, https://www.mediawiki.org/wiki/User:Leucosticte
 * GNU Public License 2.0
 *
 * This bot retrieves a list of all category members for a given category
 * and stores it in a text file.
 */

/* Setup my classes. */
include( 'botclasses.php' );
$wiki      = new wikipedia;
$wiki->url = "http://en.wikipedia.org/api.php";

/* All the login stuff. */
$user = 'REMOVED';
$pass = 'REMOVED';
$wiki->login( $user, $pass );
$category = "All";

$pageTitlesFile = 'CategoryMembers.txt';
$pageTitles = fopen ( $pageTitlesFile, 'w' );

$cmcontinue = '';
$done = false;
while ( !$done ) {
	$query = "?action=query&format=php&list=categorymembers&cmtype=page|cmlimit=500&cmtitle=Category:$category";
	if ( $cmcontinue ) {
		$query .= "&cmcontinue=$cmcontinue";
	}
	$ret = $wiki->query ( $query );
	if ( !isset ( $ret['query-continue'] ) ) {
		$done = true;
	} else {
		$cmcontinue = $ret['query-continue']['categorymembers']['cmcontinue'];
	}
	foreach ( $ret['query']['categorymembers'] as $thisCategory ) {
	    fwrite ( $pageTitles, $thisCategory['title'] . "\n" );
	}
}
fclose ( $pageTitles );