Created
March 14, 2013 07:23
-
-
Save atdt/5159480 to your computer and use it in GitHub Desktop.
Populates GettingStarted task categories in redis.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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