Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
82.05% |
32 / 39 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
DumpURLs | |
96.97% |
32 / 33 |
|
50.00% |
1 / 2 |
5 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
96.30% |
26 / 27 |
|
0.00% |
0 / 1 |
4 |
1 | <?php |
2 | |
3 | use MediaWiki\Extension\UrlShortener\UrlShortenerUtils; |
4 | use Wikimedia\Rdbms\SelectQueryBuilder; |
5 | |
6 | $IP = getenv( 'MW_INSTALL_PATH' ); |
7 | if ( $IP === false ) { |
8 | $IP = __DIR__ . '/../../..'; |
9 | } |
10 | require_once "$IP/maintenance/Maintenance.php"; |
11 | |
12 | /** |
13 | * Creates a pipe-separated text file of generated short codes |
14 | * to target URLs. |
15 | */ |
16 | class DumpURLs extends Maintenance { |
17 | |
18 | public function __construct() { |
19 | parent::__construct(); |
20 | $this->addDescription( 'Create a pipe-separated dump of all the short URL codes and ' . |
21 | 'their targets' ); |
22 | $this->addArg( 'file', 'Location to save the dump', true ); |
23 | $this->setBatchSize( 1000 ); |
24 | $this->requireExtension( 'UrlShortener' ); |
25 | } |
26 | |
27 | public function execute() { |
28 | $dbr = UrlShortenerUtils::getReplicaDB(); |
29 | $file = $this->getArg( 0 ); |
30 | $this->output( "Writing to $file...\n" ); |
31 | $handle = fopen( $file, 'w' ); |
32 | if ( $handle === false ) { |
33 | $this->fatalError( "Error opening $file. Check permissions?" ); |
34 | } |
35 | $id = 0; |
36 | do { |
37 | $text = ''; |
38 | $rows = $dbr->newSelectQueryBuilder() |
39 | ->select( [ 'usc_url', 'usc_id' ] ) |
40 | ->from( 'urlshortcodes' ) |
41 | ->where( [ $dbr->expr( 'usc_id', '>', $id ), 'usc_deleted' => 0 ] ) |
42 | ->orderBy( 'usc_id', SelectQueryBuilder::SORT_ASC ) |
43 | ->limit( $this->mBatchSize ) |
44 | ->caller( __METHOD__ )->fetchResultSet(); |
45 | foreach ( $rows as $row ) { |
46 | $shortCode = UrlShortenerUtils::encodeId( $row->usc_id ); |
47 | $url = UrlShortenerUtils::convertToProtocol( $row->usc_url, PROTO_CANONICAL ); |
48 | $text .= "{$shortCode}|{$url}\n"; |
49 | $id = $row->usc_id; |
50 | } |
51 | $count = $rows->numRows(); |
52 | if ( $count ) { |
53 | $this->output( "Writing $count entries...\n" ); |
54 | fwrite( $handle, $text ); |
55 | } |
56 | } while ( $rows->numRows() == $this->mBatchSize ); |
57 | |
58 | $this->output( "Done!\n" ); |
59 | fclose( $handle ); |
60 | } |
61 | |
62 | } |
63 | |
64 | $maintClass = DumpURLs::class; |
65 | require_once RUN_MAINTENANCE_IF_MAIN; |