summaryrefslogtreecommitdiff
path: root/maintenance/populateCategory.inc
blob: 3d04a30bb6186149c49ffb542ff5b357c959a3ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
/**
 * @file
 * @ingroup Maintenance
 * @author Simetrical
 */

define( 'REPORTING_INTERVAL', 1000 );

function populateCategory( $begin, $maxlag, $throttle, $force ) {
	$dbw = wfGetDB( DB_MASTER );

	if( !$force ) {
		$row = $dbw->selectRow(
			'updatelog',
			'1',
			array( 'ul_key' => 'populate category' ),
			__FUNCTION__
		);
		if( $row ) {
			echo "Category table already populated.  Use php ".
			"maintenance/populateCategory.php\n--force from the command line ".
			"to override.\n";
			return true;
		}
	}

	$maxlag = intval( $maxlag );
	$throttle = intval( $throttle );
	$force = (bool)$force;
	if( $begin !== '' ) {
		$where = 'cl_to > '.$dbw->addQuotes( $begin );
	} else {
		$where = null;
	}
	$i = 0;

	while( true ) {
		# Find which category to update
		$row = $dbw->selectRow(
			'categorylinks',
			'cl_to',
			$where,
			__FUNCTION__,
			array(
				'ORDER BY' => 'cl_to'
			)
		);
		if( !$row ) {
			# Done, hopefully.
			break;
		}
		$name = $row->cl_to;
		$where = 'cl_to > '.$dbw->addQuotes( $name );

		# Use the row to update the category count
		$cat = Category::newFromName( $name );
		if( !is_object( $cat ) ) {
			echo "The category named $name is not valid?!\n";
		} else {
			$cat->refreshCounts();
		}

		++$i;
		if( !($i % REPORTING_INTERVAL) ) {
			echo "$name\n";
			wfWaitForSlaves( $maxlag );
		}
		usleep( $throttle*1000 );
	}

	if( $dbw->insert(
			'updatelog',
			array( 'ul_key' => 'populate category' ),
			__FUNCTION__,
			'IGNORE'
		)
	) {
		echo "Category population complete.\n";
		return true;
	} else {
		echo "Could not insert category population row.\n";
		return false;
	}
}