Addshore has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/316583

Change subject: Add script to populate table
......................................................................

Add script to populate table

Bug: T146991
Change-Id: Iddcb8f60714983c9c8f13bf984b7e10ad8419407
---
A maintenance/populateCognateTitles.php
1 file changed, 96 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Cognate 
refs/changes/83/316583/1

diff --git a/maintenance/populateCognateTitles.php 
b/maintenance/populateCognateTitles.php
new file mode 100644
index 0000000..d151fa6
--- /dev/null
+++ b/maintenance/populateCognateTitles.php
@@ -0,0 +1,96 @@
+<?php
+
+namespace Cognate;
+
+use Maintenance;
+use MediaWiki\MediaWikiServices;
+
+if ( getenv( 'MW_INSTALL_PATH' ) !== false ) {
+       require_once( getenv( 'MW_INSTALL_PATH' ) . 
'/maintenance/Maintenance.php' );
+} else {
+       require_once( __DIR__ . '/../../../maintenance/Maintenance.php' );
+}
+
+/**
+ * Maintenance script for populating the cognate titles table.
+ *
+ * @license GPL-2.0+
+ * @author Addshore
+ */
+class PopulateCognateTitles extends Maintenance {
+
+       public function __construct() {
+               parent::__construct();
+
+               $this->addDescription( 'Populate the cognate titles table' );
+               $this->setBatchSize( 100 );
+       }
+
+       public function execute() {
+               $services = MediaWikiServices::getInstance();
+               $dbName = $services->getMainConfig()->get( 'DBname' );
+               $namespaces = $services->getMainConfig()->get( 
'CognateNamespaces' );
+               $namespaces = array_map( 'intval', $namespaces );
+
+               $this->output( "Started processing.\n" );
+               $dbr = $this->getDB( DB_SLAVE );
+
+               $start = $dbr->selectField( 'page', 'MIN(page_id)', false, 
__METHOD__ );
+               if ( !$start ) {
+                       $this->output( "Nothing to do.\n" );
+
+                       return true;
+               }
+               $end = $dbr->selectField( 'page', 'MAX(page_id)', false, 
__METHOD__ );
+               $end += $this->mBatchSize - 1;
+               $blockStart = $start;
+               $blockEnd = $start + $this->mBatchSize - 1;
+
+               $dbw = $this->getDB( DB_MASTER );
+               $normalizer = new StringNormalizer();
+               while ( $blockEnd <= $end ) {
+
+                       $rows = $dbr->select(
+                               'page',
+                               [ 'page_namespace', 'page_title' ],
+                               [
+                                       "page_id BETWEEN $blockStart AND 
$blockEnd",
+                                       'page_namespace IN (' . $dbr->makeList( 
$namespaces ) . ')',
+                                       'page_namespace BETWEEN ' . NS_MAIN . ' 
AND ' . NS_CATEGORY_TALK,
+                               ],
+                               __METHOD__
+                       );
+
+                       $toInsert = [];
+                       foreach ( $rows as $key => $row ) {
+                               $toInsert[] = [
+                                       'cgti_site' => $dbName,
+                                       'cgti_namespace' => 
$row->page_namespace,
+                                       'cgti_title' => $row->page_title,
+                                       'cgti_key' => $normalizer->normalize( 
$row->page_title ),
+                               ];
+                       }
+
+                       $numberOfRows = count( $toInsert );
+                       $this->output( "Inserting $numberOfRows rows.\n" );
+
+                       $dbw->insert(
+                               CognateStore::TITLES_TABLE_NAME,
+                               $toInsert,
+                               __METHOD__,
+                               [ 'IGNORE' ]
+                       );
+
+                       $blockStart += $this->mBatchSize;
+                       $blockEnd += $this->mBatchSize;
+                       $this->output( "Pass finished.\n" );
+               }
+
+               $this->output( "Done.\n" );
+               return true;
+       }
+
+}
+
+$maintClass = PopulateCognateTitles::class;
+require_once RUN_MAINTENANCE_IF_MAIN;

-- 
To view, visit https://gerrit.wikimedia.org/r/316583
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iddcb8f60714983c9c8f13bf984b7e10ad8419407
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Cognate
Gerrit-Branch: master
Gerrit-Owner: Addshore <addshorew...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to