Replace wfBaseConvert with Wikimedia\base_convert

Change-Id: Iadab3d018c3559daf79be90edb23d131729bdb68
This commit is contained in:
Reedy 2015-11-24 22:51:42 +00:00
parent 38ba6b620b
commit 00c426e3c2
29 changed files with 60 additions and 60 deletions

View file

@ -1499,7 +1499,7 @@ class WikiRevision {
*/
function getSha1() {
if ( $this->sha1base36 ) {
return wfBaseConvert( $this->sha1base36, 36, 16 );
return Wikimedia\base_convert( $this->sha1base36, 36, 16 );
}
return false;
}

View file

@ -1526,7 +1526,7 @@ class Revision implements IDBAccessObject {
* @return string
*/
public static function base36Sha1( $text ) {
return wfBaseConvert( sha1( $text ), 16, 36, 31 );
return Wikimedia\base_convert( sha1( $text ), 16, 36, 31 );
}
/**

View file

@ -216,7 +216,7 @@ class ApiQueryAllImages extends ApiQueryGeneratorBase {
if ( !$this->validateSha1Hash( $sha1 ) ) {
$this->dieUsage( 'The SHA1 hash provided is not valid', 'invalidsha1hash' );
}
$sha1 = wfBaseConvert( $sha1, 16, 36, 31 );
$sha1 = Wikimedia\base_convert( $sha1, 16, 36, 31 );
} elseif ( isset( $params['sha1base36'] ) ) {
$sha1 = strtolower( $params['sha1base36'] );
if ( !$this->validateSha1Base36Hash( $sha1 ) ) {

View file

@ -362,7 +362,7 @@ class ApiQueryDeletedrevs extends ApiQueryBase {
}
if ( Revision::userCanBitfield( $row->ar_deleted, Revision::DELETED_TEXT, $user ) ) {
if ( $row->ar_sha1 != '' ) {
$rev['sha1'] = wfBaseConvert( $row->ar_sha1, 36, 16, 40 );
$rev['sha1'] = Wikimedia\base_convert( $row->ar_sha1, 36, 16, 40 );
} else {
$rev['sha1'] = '';
}

View file

@ -114,7 +114,7 @@ class ApiQueryFilearchive extends ApiQueryBase {
if ( !$this->validateSha1Hash( $sha1 ) ) {
$this->dieUsage( 'The SHA1 hash provided is not valid', 'invalidsha1hash' );
}
$sha1 = wfBaseConvert( $sha1, 16, 36, 31 );
$sha1 = Wikimedia\base_convert( $sha1, 16, 36, 31 );
} elseif ( $sha1base36Set ) {
$sha1 = strtolower( $params['sha1base36'] );
if ( !$this->validateSha1Base36Hash( $sha1 ) ) {
@ -183,7 +183,7 @@ class ApiQueryFilearchive extends ApiQueryBase {
$file['user'] = $row->fa_user_text;
}
if ( $fld_sha1 ) {
$file['sha1'] = wfBaseConvert( $row->fa_sha1, 36, 16, 40 );
$file['sha1'] = Wikimedia\base_convert( $row->fa_sha1, 36, 16, 40 );
}
if ( $fld_timestamp ) {
$file['timestamp'] = wfTimestamp( TS_ISO_8601, $row->fa_timestamp );

View file

@ -518,7 +518,7 @@ class ApiQueryImageInfo extends ApiQueryBase {
}
if ( $sha1 ) {
$vals['sha1'] = wfBaseConvert( $file->getSha1(), 36, 16, 40 );
$vals['sha1'] = Wikimedia\base_convert( $file->getSha1(), 36, 16, 40 );
}
if ( $meta ) {

View file

@ -552,7 +552,7 @@ class ApiQueryRecentChanges extends ApiQueryGeneratorBase {
}
if ( Revision::userCanBitfield( $row->rev_deleted, Revision::DELETED_TEXT, $user ) ) {
if ( $row->rev_sha1 !== '' ) {
$vals['sha1'] = wfBaseConvert( $row->rev_sha1, 36, 16, 40 );
$vals['sha1'] = Wikimedia\base_convert( $row->rev_sha1, 36, 16, 40 );
} else {
$vals['sha1'] = '';
}

View file

@ -213,7 +213,7 @@ abstract class ApiQueryRevisionsBase extends ApiQueryGeneratorBase {
}
if ( $revision->userCan( Revision::DELETED_TEXT, $user ) ) {
if ( $revision->getSha1() != '' ) {
$vals['sha1'] = wfBaseConvert( $revision->getSha1(), 36, 16, 40 );
$vals['sha1'] = Wikimedia\base_convert( $revision->getSha1(), 36, 16, 40 );
} else {
$vals['sha1'] = '';
}

View file

@ -1611,7 +1611,7 @@ SQL;
* @return string Integer
*/
private function bigintFromLockName( $lockName ) {
return wfBaseConvert( substr( sha1( $lockName ), 0, 15 ), 16, 10 );
return Wikimedia\base_convert( substr( sha1( $lockName ), 0, 15 ), 16, 10 );
}
} // end DatabasePostgres class

View file

@ -78,7 +78,7 @@ class ExternalStoreMwstore extends ExternalStoreMedium {
$be = FileBackendGroup::singleton()->get( $backend );
if ( $be instanceof FileBackend ) {
// Get three random base 36 characters to act as shard directories
$rand = wfBaseConvert( mt_rand( 0, 46655 ), 10, 36, 3 );
$rand = Wikimedia\base_convert( mt_rand( 0, 46655 ), 10, 36, 3 );
// Make sure ID is roughly lexicographically increasing for performance
$id = str_pad( UIDGenerator::newTimestampedUID128( 32 ), 26, '0', STR_PAD_LEFT );
// Segregate items by wiki ID for the sake of bookkeeping

View file

@ -208,7 +208,7 @@ class FSFile {
MediaWiki\restoreWarnings();
if ( $this->sha1Base36 !== false ) {
$this->sha1Base36 = wfBaseConvert( $this->sha1Base36, 16, 36, 31 );
$this->sha1Base36 = Wikimedia\base_convert( $this->sha1Base36, 16, 36, 31 );
}
return $this->sha1Base36;

View file

@ -1551,7 +1551,7 @@ abstract class FileBackendStore extends FileBackend {
if ( $digits > 0 ) {
$numShards = pow( $base, $digits );
for ( $index = 0; $index < $numShards; $index++ ) {
$shards[] = '.' . wfBaseConvert( $index, 10, $base, $digits );
$shards[] = '.' . Wikimedia\base_convert( $index, 10, $base, $digits );
}
}

View file

@ -513,7 +513,7 @@ class CreateFileOp extends FileOp {
}
protected function getSourceSha1Base36() {
return wfBaseConvert( sha1( $this->params['content'] ), 16, 36, 31 );
return Wikimedia\base_convert( sha1( $this->params['content'] ), 16, 36, 31 );
}
public function storagePathsChanged() {
@ -581,7 +581,7 @@ class StoreFileOp extends FileOp {
$hash = sha1_file( $this->params['src'] );
MediaWiki\restoreWarnings();
if ( $hash !== false ) {
$hash = wfBaseConvert( $hash, 16, 36, 31 );
$hash = Wikimedia\base_convert( $hash, 16, 36, 31 );
}
return $hash;

View file

@ -261,7 +261,7 @@ class SwiftFileBackend extends FileBackendStore {
return $status;
}
$sha1Hash = wfBaseConvert( sha1( $params['content'] ), 16, 36, 31 );
$sha1Hash = Wikimedia\base_convert( sha1( $params['content'] ), 16, 36, 31 );
$contentType = isset( $params['headers']['content-type'] )
? $params['headers']['content-type']
: $this->getContentType( $params['dst'], $params['content'], null );
@ -319,7 +319,7 @@ class SwiftFileBackend extends FileBackendStore {
return $status;
}
$sha1Hash = wfBaseConvert( $sha1Hash, 16, 36, 31 );
$sha1Hash = Wikimedia\base_convert( $sha1Hash, 16, 36, 31 );
$contentType = isset( $params['headers']['content-type'] )
? $params['headers']['content-type']
: $this->getContentType( $params['dst'], null, $params['src'] );

View file

@ -81,9 +81,9 @@ abstract class FileJournal {
for ( $i = 0; $i < 5; $i++ ) {
$s .= mt_rand( 0, 2147483647 );
}
$s = wfBaseConvert( sha1( $s ), 16, 36, 31 );
$s = Wikimedia\base_convert( sha1( $s ), 16, 36, 31 );
return substr( wfBaseConvert( wfTimestamp( TS_MW ), 10, 36, 9 ) . $s, 0, 31 );
return substr( Wikimedia\base_convert( wfTimestamp( TS_MW ), 10, 36, 9 ) . $s, 0, 31 );
}
/**

View file

@ -376,7 +376,7 @@ class PostgreSqlLockManager extends DBLockManager {
$db = $this->getConnection( $lockSrv ); // checked in isServerUp()
$bigints = array_unique( array_map(
function ( $key ) {
return wfBaseConvert( substr( $key, 0, 15 ), 16, 10 );
return Wikimedia\base_convert( substr( $key, 0, 15 ), 16, 10 );
},
array_map( array( $this, 'sha1Base16Absolute' ), $paths )
) );

View file

@ -152,7 +152,7 @@ abstract class LockManager {
* @return string
*/
final protected function sha1Base36Absolute( $path ) {
return wfBaseConvert( sha1( "{$this->domain}:{$path}" ), 16, 36, 31 );
return Wikimedia\base_convert( sha1( "{$this->domain}:{$path}" ), 16, 36, 31 );
}
/**

View file

@ -244,7 +244,7 @@ class ForeignAPIFile extends File {
*/
function getSha1() {
return isset( $this->mInfo['sha1'] )
? wfBaseConvert( strval( $this->mInfo['sha1'] ), 16, 36, 31 )
? Wikimedia\base_convert( strval( $this->mInfo['sha1'] ), 16, 36, 31 )
: null;
}

View file

@ -727,7 +727,7 @@ class JobQueueDB extends JobQueue {
// Additional job metadata
'job_id' => $dbw->nextSequenceValue( 'job_job_id_seq' ),
'job_timestamp' => $dbw->timestamp(),
'job_sha1' => wfBaseConvert(
'job_sha1' => Wikimedia\base_convert(
sha1( serialize( $job->getDeduplicationInfo() ) ),
16, 36, 31
),

View file

@ -639,7 +639,7 @@ LUA;
// Additional job metadata
'uuid' => UIDGenerator::newRawUUIDv4( UIDGenerator::QUICK_RAND ),
'sha1' => $job->ignoreDuplicates()
? wfBaseConvert( sha1( serialize( $job->getDeduplicationInfo() ) ), 16, 36, 31 )
? Wikimedia\base_convert( sha1( serialize( $job->getDeduplicationInfo() ) ), 16, 36, 31 )
: '',
'timestamp' => time() // UNIX timestamp
);

View file

@ -204,7 +204,7 @@ final class PasswordFactory {
// Generate random hex chars
$hex = MWCryptRand::generateHex( $length );
// Convert from base 16 to base 32 to get a proper password like string
return wfBaseConvert( $hex, 16, 32 );
return Wikimedia\base_convert( $hex, 16, 32 );
}
/**

View file

@ -226,8 +226,8 @@ class UploadStash {
// see: http://www.jwz.org/doc/mid.html
list( $usec, $sec ) = explode( ' ', microtime() );
$usec = substr( $usec, 2 );
$key = wfBaseConvert( $sec . $usec, 10, 36 ) . '.' .
wfBaseConvert( mt_rand(), 10, 36 ) . '.' .
$key = Wikimedia\base_convert( $sec . $usec, 10, 36 ) . '.' .
Wikimedia\base_convert( mt_rand(), 10, 36 ) . '.' .
$this->userId . '.' .
$extension;

View file

@ -411,7 +411,7 @@ class IP {
}
}
if ( $n !== false ) {
# Floating points can handle the conversion; faster than wfBaseConvert()
# Floating points can handle the conversion; faster than Wikimedia\base_convert()
$n = strtoupper( str_pad( base_convert( $n, 10, 16 ), 8, '0', STR_PAD_LEFT ) );
}
} else {
@ -551,11 +551,11 @@ class IP {
} else {
# Native 32 bit functions WONT work here!!!
# Convert to a padded binary number
$network = wfBaseConvert( $network, 16, 2, 128 );
$network = Wikimedia\base_convert( $network, 16, 2, 128 );
# Truncate the last (128-$bits) bits and replace them with zeros
$network = str_pad( substr( $network, 0, $bits ), 128, 0, STR_PAD_RIGHT );
# Convert back to an integer
$network = wfBaseConvert( $network, 2, 10 );
$network = Wikimedia\base_convert( $network, 2, 10 );
}
} else {
$network = false;
@ -587,13 +587,13 @@ class IP {
if ( $network === false ) {
$start = $end = false;
} else {
$start = wfBaseConvert( $network, 10, 16, 32, false );
$start = Wikimedia\base_convert( $network, 10, 16, 32, false );
# Turn network to binary (again)
$end = wfBaseConvert( $network, 10, 2, 128 );
$end = Wikimedia\base_convert( $network, 10, 2, 128 );
# Truncate the last (128-$bits) bits and replace them with ones
$end = str_pad( substr( $end, 0, $bits ), 128, 1, STR_PAD_RIGHT );
# Convert to hex
$end = wfBaseConvert( $end, 2, 16, 32, false );
$end = Wikimedia\base_convert( $end, 2, 16, 32, false );
# see toHex() comment
$start = "v6-$start";
$end = "v6-$end";

View file

@ -73,8 +73,8 @@ class UIDGenerator {
}
file_put_contents( $this->nodeIdFile, $nodeId ); // cache
}
$this->nodeId32 = wfBaseConvert( substr( sha1( $nodeId ), 0, 8 ), 16, 2, 32 );
$this->nodeId48 = wfBaseConvert( $nodeId, 16, 2, 48 );
$this->nodeId32 = Wikimedia\base_convert( substr( sha1( $nodeId ), 0, 8 ), 16, 2, 32 );
$this->nodeId48 = Wikimedia\base_convert( $nodeId, 16, 2, 48 );
// If different processes run as different users, they may have different temp dirs.
// This is dealt with by initializing the clock sequence number and counters randomly.
$this->lockFile88 = wfTempDir() . '/mw-' . __CLASS__ . '-UID-88';
@ -115,7 +115,7 @@ class UIDGenerator {
$gen = self::singleton();
$time = $gen->getTimestampAndDelay( 'lockFile88', 1, 1024 );
return wfBaseConvert( $gen->getTimestampedID88( $time ), 2, $base );
return Wikimedia\base_convert( $gen->getTimestampedID88( $time ), 2, $base );
}
/**
@ -161,7 +161,7 @@ class UIDGenerator {
$gen = self::singleton();
$time = $gen->getTimestampAndDelay( 'lockFile128', 16384, 1048576 );
return wfBaseConvert( $gen->getTimestampedID128( $time ), 2, $base );
return Wikimedia\base_convert( $gen->getTimestampedID128( $time ), 2, $base );
}
/**
@ -448,7 +448,7 @@ class UIDGenerator {
': sorry, this function doesn\'t work after the year 144680' );
}
return substr( wfBaseConvert( $ts, 10, 2, 46 ), -46 );
return substr( Wikimedia\base_convert( $ts, 10, 2, 46 ), -46 );
}
/**

View file

@ -1,6 +1,6 @@
<?php
/**
* Benchmark for wfBaseConvert
* Benchmark for Wikimedia\base_convert
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -25,14 +25,14 @@
require_once __DIR__ . '/Benchmarker.php';
/**
* Maintenance script that benchmarks wfBaseConvert().
* Maintenance script that benchmarks Wikimedia\base_convert().
*
* @ingroup Benchmark
*/
class BenchWfBaseConvert extends Benchmarker {
public function __construct() {
parent::__construct();
$this->mDescription = "Benchmark for wfBaseConvert.";
$this->mDescription = "Benchmark for Wikimedia\base_convert.";
$this->addOption( "inbase", "Input base", false, true );
$this->addOption( "outbase", "Output base", false, true );
$this->addOption( "length", "Size in digits to generate for input", false, true );
@ -46,15 +46,15 @@ class BenchWfBaseConvert extends Benchmarker {
$this->bench( array(
array(
'function' => 'wfBaseConvert',
'function' => 'Wikimedia\base_convert',
'args' => array( $number, $inbase, $outbase, 0, true, 'php' )
),
array(
'function' => 'wfBaseConvert',
'function' => 'Wikimedia\base_convert',
'args' => array( $number, $inbase, $outbase, 0, true, 'bcmath' )
),
array(
'function' => 'wfBaseConvert',
'function' => 'Wikimedia\base_convert',
'args' => array( $number, $inbase, $outbase, 0, true, 'gmp' )
),
) );

View file

@ -1054,7 +1054,7 @@ class ParserTest {
'media_type' => MEDIATYPE_BITMAP,
'mime' => 'image/jpeg',
'metadata' => serialize( array() ),
'sha1' => wfBaseConvert( '1', 16, 36, 31 ),
'sha1' => Wikimedia\base_convert( '1', 16, 36, 31 ),
'fileExists' => true
), $this->db->timestamp( '20010115123500' ), $user );
@ -1068,7 +1068,7 @@ class ParserTest {
'media_type' => MEDIATYPE_BITMAP,
'mime' => 'image/png',
'metadata' => serialize( array() ),
'sha1' => wfBaseConvert( '2', 16, 36, 31 ),
'sha1' => Wikimedia\base_convert( '2', 16, 36, 31 ),
'fileExists' => true
), $this->db->timestamp( '20130225203040' ), $user );
@ -1081,7 +1081,7 @@ class ParserTest {
'media_type' => MEDIATYPE_DRAWING,
'mime' => 'image/svg+xml',
'metadata' => serialize( array() ),
'sha1' => wfBaseConvert( '', 16, 36, 31 ),
'sha1' => Wikimedia\base_convert( '', 16, 36, 31 ),
'fileExists' => true
), $this->db->timestamp( '20010115123500' ), $user );
@ -1095,7 +1095,7 @@ class ParserTest {
'media_type' => MEDIATYPE_BITMAP,
'mime' => 'image/jpeg',
'metadata' => serialize( array() ),
'sha1' => wfBaseConvert( '3', 16, 36, 31 ),
'sha1' => Wikimedia\base_convert( '3', 16, 36, 31 ),
'fileExists' => true
), $this->db->timestamp( '20010115123500' ), $user );
@ -1134,7 +1134,7 @@ class ParserTest {
</OBJECT>
</BODY>
</DjVuXML>',
'sha1' => wfBaseConvert( '', 16, 36, 31 ),
'sha1' => Wikimedia\base_convert( '', 16, 36, 31 ),
'fileExists' => true
), $this->db->timestamp( '20010115123600' ), $user );
}

View file

@ -1716,7 +1716,7 @@ class FileBackendTest extends MediaWikiTestCase {
$this->assertEquals( strlen( $fileBContents ),
$this->backend->getFileSize( array( 'src' => $fileC ) ),
"Correct file size of $fileC" );
$this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
$this->assertEquals( Wikimedia\base_convert( sha1( $fileBContents ), 16, 36, 31 ),
$this->backend->getFileSha1Base36( array( 'src' => $fileC ) ),
"Correct file SHA-1 of $fileC" );
}
@ -1815,7 +1815,7 @@ class FileBackendTest extends MediaWikiTestCase {
$this->assertEquals( strlen( $fileBContents ),
$this->backend->getFileSize( array( 'src' => $fileC ) ),
"Correct file size of $fileC" );
$this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
$this->assertEquals( Wikimedia\base_convert( sha1( $fileBContents ), 16, 36, 31 ),
$this->backend->getFileSha1Base36( array( 'src' => $fileC ) ),
"Correct file SHA-1 of $fileC" );
}
@ -1892,7 +1892,7 @@ class FileBackendTest extends MediaWikiTestCase {
$this->assertEquals( strlen( $fileBContents ),
$this->backend->getFileSize( array( 'src' => $fileA ) ),
"Correct file size of $fileA" );
$this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
$this->assertEquals( Wikimedia\base_convert( sha1( $fileBContents ), 16, 36, 31 ),
$this->backend->getFileSha1Base36( array( 'src' => $fileA ) ),
"Correct file SHA-1 of $fileA" );
}

View file

@ -243,7 +243,7 @@ class NewParserTest extends MediaWikiTestCase {
'media_type' => MEDIATYPE_BITMAP,
'mime' => 'image/jpeg',
'metadata' => serialize( array() ),
'sha1' => wfBaseConvert( '1', 16, 36, 31 ),
'sha1' => Wikimedia\base_convert( '1', 16, 36, 31 ),
'fileExists' => true ),
$this->db->timestamp( '20010115123500' ), $user
);
@ -263,7 +263,7 @@ class NewParserTest extends MediaWikiTestCase {
'media_type' => MEDIATYPE_BITMAP,
'mime' => 'image/png',
'metadata' => serialize( array() ),
'sha1' => wfBaseConvert( '2', 16, 36, 31 ),
'sha1' => Wikimedia\base_convert( '2', 16, 36, 31 ),
'fileExists' => true ),
$this->db->timestamp( '20130225203040' ), $user
);
@ -284,7 +284,7 @@ class NewParserTest extends MediaWikiTestCase {
'media_type' => MEDIATYPE_BITMAP,
'mime' => 'image/jpeg',
'metadata' => serialize( array() ),
'sha1' => wfBaseConvert( '3', 16, 36, 31 ),
'sha1' => Wikimedia\base_convert( '3', 16, 36, 31 ),
'fileExists' => true ),
$this->db->timestamp( '20010115123500' ), $user
);
@ -299,7 +299,7 @@ class NewParserTest extends MediaWikiTestCase {
'media_type' => MEDIATYPE_DRAWING,
'mime' => 'image/svg+xml',
'metadata' => serialize( array() ),
'sha1' => wfBaseConvert( '', 16, 36, 31 ),
'sha1' => Wikimedia\base_convert( '', 16, 36, 31 ),
'fileExists' => true
), $this->db->timestamp( '20010115123500' ), $user );
}
@ -340,7 +340,7 @@ class NewParserTest extends MediaWikiTestCase {
</OBJECT>
</BODY>
</DjVuXML>',
'sha1' => wfBaseConvert( '', 16, 36, 31 ),
'sha1' => Wikimedia\base_convert( '', 16, 36, 31 ),
'fileExists' => true
), $this->db->timestamp( '20140115123600' ), $user );
}

View file

@ -18,7 +18,7 @@ class UIDGeneratorTest extends PHPUnit_Framework_TestCase {
$this->assertEquals( true, ctype_digit( $id ), "UID made of digit characters" );
$this->assertLessThanOrEqual( $digitlen, strlen( $id ),
"UID has the right number of digits" );
$this->assertLessThanOrEqual( $bits, strlen( wfBaseConvert( $id, 10, 2 ) ),
$this->assertLessThanOrEqual( $bits, strlen( Wikimedia\base_convert( $id, 10, 2 ) ),
"UID has the right number of bits" );
$ids = array();
@ -31,8 +31,8 @@ class UIDGeneratorTest extends PHPUnit_Framework_TestCase {
$this->assertSame( array_unique( $ids ), $ids, "All generated IDs are unique." );
foreach ( $ids as $id ) {
$id_bin = wfBaseConvert( $id, 10, 2 );
$lastId_bin = wfBaseConvert( $lastId, 10, 2 );
$id_bin = Wikimedia\base_convert( $id, 10, 2 );
$lastId_bin = Wikimedia\base_convert( $lastId, 10, 2 );
$this->assertGreaterThanOrEqual(
substr( $lastId_bin, 0, $tbits ),