Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
88.00% |
66 / 75 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
PopulateCognatePages | |
94.29% |
66 / 70 |
|
50.00% |
1 / 2 |
8.01 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
93.44% |
57 / 61 |
|
0.00% |
0 / 1 |
7.01 |
1 | <?php |
2 | |
3 | namespace Cognate; |
4 | |
5 | use Maintenance; |
6 | use MediaWiki\MediaWikiServices; |
7 | |
8 | if ( getenv( 'MW_INSTALL_PATH' ) !== false ) { |
9 | require_once getenv( 'MW_INSTALL_PATH' ) . '/maintenance/Maintenance.php'; |
10 | } else { |
11 | require_once __DIR__ . '/../../../maintenance/Maintenance.php'; |
12 | } |
13 | |
14 | /** |
15 | * Maintenance script for populating the cognate page and title tables. |
16 | * |
17 | * The script will add an entry in the cognate_pages table for every page that |
18 | * exists in the mediawiki page table as well as adding the needed entries to |
19 | * the cognate_titles table. |
20 | * |
21 | * @license GPL-2.0-or-later |
22 | * @author Addshore |
23 | */ |
24 | class PopulateCognatePages extends Maintenance { |
25 | |
26 | public function __construct() { |
27 | parent::__construct(); |
28 | |
29 | $this->addDescription( 'Populate the Cognate page and title tables' ); |
30 | $this->addOption( 'start', 'The page ID to start from.', false, true ); |
31 | $this->addOption( |
32 | 'clear-first', |
33 | 'Clear entries in the table for the current wiki before population starts.' |
34 | ); |
35 | $this->setBatchSize( 100 ); |
36 | $this->requireExtension( 'Cognate' ); |
37 | } |
38 | |
39 | public function execute() { |
40 | $services = MediaWikiServices::getInstance(); |
41 | $dbName = $services->getMainConfig()->get( 'DBname' ); |
42 | $namespaces = $services->getMainConfig()->get( 'CognateNamespaces' ); |
43 | $namespaces = array_filter( |
44 | array_map( 'intval', $namespaces ), |
45 | static function ( $namespace ) { |
46 | return $namespace >= NS_MAIN && $namespace <= NS_CATEGORY_TALK; |
47 | } |
48 | ); |
49 | |
50 | /** @var CognateStore $store */ |
51 | $store = $services->getService( 'CognateStore' ); |
52 | |
53 | if ( $this->hasOption( 'clear-first' ) ) { |
54 | $this->output( "Clearing cognate_pages table of entries for $dbName.\n" ); |
55 | $store->deletePagesForSite( $dbName ); |
56 | } |
57 | |
58 | $this->output( "Started processing.\n" ); |
59 | $dbr = $this->getDB( DB_REPLICA ); |
60 | |
61 | $start = $this->getOption( 'start' ); |
62 | if ( $start === null ) { |
63 | $start = $dbr->newSelectQueryBuilder() |
64 | ->select( 'MIN(page_id)' ) |
65 | ->from( 'page' ) |
66 | ->caller( __METHOD__ ) |
67 | ->fetchField(); |
68 | } |
69 | if ( !$start ) { |
70 | $this->output( "Nothing to do.\n" ); |
71 | return true; |
72 | } |
73 | |
74 | $end = $dbr->newSelectQueryBuilder() |
75 | ->select( 'MAX(page_id)' ) |
76 | ->from( 'page' ) |
77 | ->caller( __METHOD__ ) |
78 | ->fetchField(); |
79 | |
80 | $blockStart = (int)$start; |
81 | $blockEnd = $blockStart + $this->mBatchSize - 1; |
82 | $loadBalancerFactory = $services->getDBLoadBalancerFactory(); |
83 | |
84 | while ( $blockStart <= $end ) { |
85 | $rows = $dbr->newSelectQueryBuilder() |
86 | ->select( [ 'page_namespace', 'page_title' ] ) |
87 | ->from( 'page' ) |
88 | ->where( [ |
89 | $dbr->expr( 'page_id', '>=', $blockStart ), |
90 | $dbr->expr( 'page_id', '<=', $blockEnd ), |
91 | 'page_namespace' => $namespaces, |
92 | 'page_is_redirect' => 0, |
93 | ] ) |
94 | ->caller( __METHOD__ ) |
95 | ->fetchResultSet(); |
96 | |
97 | $titleDetails = []; |
98 | foreach ( $rows as $row ) { |
99 | $titleDetails[] = [ |
100 | 'site' => $dbName, |
101 | 'namespace' => (int)$row->page_namespace, |
102 | 'title' => $row->page_title, |
103 | ]; |
104 | } |
105 | |
106 | $numberOfRows = count( $titleDetails ); |
107 | $store->insertPages( $titleDetails ); |
108 | |
109 | $this->output( "$numberOfRows rows processed.\n" ); |
110 | |
111 | $blockStart += $this->mBatchSize; |
112 | $blockEnd += $this->mBatchSize; |
113 | $this->output( "Pass finished.\n" ); |
114 | |
115 | $loadBalancerFactory->waitForReplication(); |
116 | } |
117 | |
118 | $this->output( "Done.\n" ); |
119 | return true; |
120 | } |
121 | |
122 | } |
123 | |
124 | $maintClass = PopulateCognatePages::class; |
125 | require_once RUN_MAINTENANCE_IF_MAIN; |