Page MenuHomePhabricator
Paste P4725

Creating edits by random IPs on a devwiki
ActivePublic

Authored by MusikAnimal on Jan 6 2017, 11:56 PM.
Referenced Files
F9090948: Creating edits by random IPs on a devwiki
Aug 16 2017, 10:05 PM
F9082500: Creating edits by random IPs on a devwiki
Aug 15 2017, 9:09 PM
F9071706: Creating edits by random IPs on a devwiki
Aug 15 2017, 1:46 AM
F9071515: Creating edits by random IPs on a devwiki
Aug 15 2017, 1:33 AM
F6860557: Creating edits by random IPs on a devwiki
Mar 22 2017, 5:57 PM
F6860525: Creating edits by random IPs on a devwiki
Mar 22 2017, 5:56 PM
F5340407: Creating edits by random IPs on a devwiki
Jan 23 2017, 11:21 PM
F5340105: Creating edits by random IPs on a devwiki
Jan 23 2017, 10:29 PM
Subscribers
None
<?php
// 1) Open Psy Shell with `php maintenance/shell.php`
// 2) Copy and paste the below
// 3) You'll have 1,000 edits by random IPs to the pages [[Foo]], [[Bar]] and [[Baz]], in the range 2600:387:0:0:0:0:0:0/32
// NOTE: Some things may not have been properly updated, such as page_latest in the page table
$numRevisions = 1000;
$oldestTimestamp = '20100123123456';
use MediaWiki\MediaWikiServices;
use Revision as Revision;
use WikiPage as WikiPage;
/**
* Expand an IPv6 address to long notation.
* Courtesy of bohwaz.net, released under WTFPL (http://www.wtfpl.net/)
* http://bohwaz.net/p/PHP-IP-utils-Normalize-IPv6-address-generate-random-IPv4-or-v6-address-and-more
*
* Examples:
* -- ::1
* -> 0000:0000:0000:0000:0000:0000:0000:0001
* -- 2001:db8:85a3::8a2e:370:7334
* -> 2001:0db8:85a3:0000:0000:8a2e:0370:7334
*
* @param string $ip Input IPv6 address
* @return string IPv6 address
*/
function expand_ipv6($ip)
{
return implode(':', str_split(bin2hex(inet_pton($ip), 4)));
}
/**
* Returns an IPv6 formatted as recommended by RFC 5952 (short notation)
* Courtesy of bohwaz.net, released under WTFPL (http://www.wtfpl.net/)
* http://bohwaz.net/p/PHP-IP-utils-Normalize-IPv6-address-generate-random-IPv4-or-v6-address-and-more
*
* @param string $ip Input IPv6 address
* @return string IPv6 address
*/
function normalize_ipv6($ip)
{
return inet_ntop(inet_pton($ip));
}
/**
* Returns a random IP address, either real random a using an existing IP address as a seed.
* Courtesy of bohwaz.net, released under WTFPL (http://www.wtfpl.net/)
* http://bohwaz.net/p/PHP-IP-utils-Normalize-IPv6-address-generate-random-IPv4-or-v6-address-and-more
*
* @param bool $ipv6 Set to true to return a IPv6 address, or else will return an IPv4.
* @param string $from_ip Optional IP address (v4 or v6) for seed.
* @param bool $filter_reserved If set to true, won't return any reserved or private space IP address.
*
* @return string IP address
*/
function get_random_ip($ipv6 = null, $from_ip = null, $filter_reserved = true)
{
$ip = false;
if ($from_ip)
{
if (!is_int($from_ip))
{
if (strpos($from_ip, ':') !== false)
{
$from_ip = normalize_ipv6($from_ip);
}
// We can't use seeds bigger than 32 bits, so here is a little hack
$seed = hexdec(substr(md5($from_ip), -8));
}
else
{
$seed = $from_ip;
}
mt_srand($seed);
}
else
{
mt_srand();
}
if ($ipv6)
{
$ip = normalize_ipv6(wordwrap('2001' . substr(sha1(mt_rand()), -28), 4, ':', true));
}
else
{
$ip = long2ip(mt_rand(0, '4294967295'));
}
if ($filter_reserved && !filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE))
{
// OK here we can't give $from_ip directly or else you will get a infinite loop
$ip = get_random_ip($ipv6, $from_ip ? $seed + mt_rand(-65535, 65535) : false, $filter_reserved);
}
return $ip;
}
# START OF SCRIPT
$dbw = wfGetDB( DB_MASTER );
# First create a few test pages
$pages = [];
foreach ( [ 'Foo', 'Bar', 'Baz' ] as $pageTitle ) {
$page = new WikiPage( Title::newFromText( $pageTitle ) );
$text = substr( md5( mt_rand() ), 0, 7 );
$content = ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT );
$page->doEditContent( $content, 'testing', EDIT_NEW );
# Get the oldest revision and make it REALLY OLD!
# Later we're going to add edits spanning many years.
$firstRevId = $page->getOldestRevision()->getId();
$dbw->update( 'revision', [ 'rev_timestamp' => $oldestTimestamp ], [ 'rev_page' => $page->getId() ] );
$pages[] = $page;
}
# Generate random timestamps in chronological order,
# so that the corresponding rev_ids will also be chronological.
$timestamps = [];
for ( $i = 0; $i < $numRevisions; $i++ ) {
$timestamps[] = date( 'YmdHis', mt_rand( strtotime( 20100123123456), time() ) );
}
sort( $timestamps );
for ( $i = 0; $i < $numRevisions; $i++ ) {
echo "$i of $numRevisions\n";
$ipv6 = get_random_ip( true );
$ipv6Groups = explode( ':', $ipv6 );
$ipv6 = '2600:387:' . implode(':', array_slice( $ipv6Groups, 2 ) );
$user = User::newFromName( $ipv6 );
$summaries = [ 'lorem', 'ipsum', 'dolor', 'sit', 'amet' ];
$summary = $summaries[array_rand( $summaries )];
$summary = str_repeat( $summary . ' ', mt_rand( 0, 5 ) );
$page = $pages[array_rand( $pages )];
$text = substr( md5( mt_rand() ), 0, 7 );
$rev = new Revision( [
'page' => $page->getId(),
'title' => $page->getTitle(),
'user_text' => $ipv6,
'user' => 0,
'timestamp' => $timestamps[$i],
'text' => $text,
'comment' => $summary,
'content_model' => CONTENT_MODEL_WIKITEXT,
] );
$rev->insertOn( $dbw );
}

Event Timeline

MusikAnimal changed the title of this paste from Creating random IP edits on a devwiki to Creating edits by random IPs on a devwiki.Jan 7 2017, 12:07 AM
MusikAnimal edited the content of this paste. (Show Details)
MusikAnimal updated the paste's language from autodetect to php.Aug 15 2017, 9:09 PM
MusikAnimal edited the content of this paste. (Show Details)