Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Populates GettingStarted task categories in redis.
<?php
if ( PHP_SAPI !== 'cli' ) {
die( 'This script can only be run from the command line.' );
}
$IP = getenv( 'MW_INSTALL_PATH' ) ?: realpath( __DIR__ . '/../..' );
require_once( $IP . '/index.php' );
function populateCategory( $category ) {
$key = RedisCategorySync::makeCategoryKey( $category );
echo "Key: $key\n";
$dbr = wfGetDB( DB_SLAVE );
$res = $dbr->select(
array( 'page', 'categorylinks' ),
array( 'page_id' ),
array(
'cl_from = page_id',
'cl_to' => $category->getName(),
'page_is_redirect' => 0,
'page_namespace' => NS_MAIN,
),
__FUNCTION__
);
$pages = array();
foreach( $res as $row ) {
$pages[] = $row->page_id;
}
if ( !count( $pages ) ) {
return 0;
}
$conn = RedisCategorySync::getClient();
$redis = $conn->multi( Redis::PIPELINE );
$batches = array_chunk( $pages, 100 );
foreach( $batches as $batch ) {
array_unshift( $batch, $key );
call_user_func_array( array( $redis, 'sAdd' ), $batch );
}
return $redis->exec() ? count( $pages ) : 0;
}
foreach( RedisCategorySync::getCategories() as $catName ) {
echo "Populating category '${catName}' ...\n";
$cat = Category::newFromName( $catName );
$count = populateCategory( $cat );
echo "Updated / refreshed $count pages in category.\n\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.