From: mdew192837 Date: Wed, 24 May 2017 21:00:08 +0000 (-0500) Subject: Add a maintenance script for populating pp_sortkey X-Git-Tag: 1.31.0-rc.0~2594^2 X-Git-Url: http://git.cyclocoop.org/%28?a=commitdiff_plain;h=993ce4d4111c3ac4be92106f7c8d93e9e0d1427f;p=lhc%2Fweb%2Fwiklou.git Add a maintenance script for populating pp_sortkey The addition of a pp_sortkey field in T60032 necessitates an update to old entries in the page_props table that don't have a pp_sortkey. The script uses the pp_value as the sort key if it's numeric. The script extends LoggedUpdateMaintenance so it only runs once. Added the script to MysqlUpdater so that it automatically runs. Bug: T66949 Change-Id: Id482dc73ec1963010324e51fe9273a72dd31a7f7 --- diff --git a/autoload.php b/autoload.php index 2560bdbdb9..003ebd92f4 100644 --- a/autoload.php +++ b/autoload.php @@ -1117,6 +1117,7 @@ $wgAutoloadLocalClasses = [ 'PopulateInterwiki' => __DIR__ . '/maintenance/populateInterwiki.php', 'PopulateLogSearch' => __DIR__ . '/maintenance/populateLogSearch.php', 'PopulateLogUsertext' => __DIR__ . '/maintenance/populateLogUsertext.php', + 'PopulatePPSortKey' => __DIR__ . '/maintenance/populatePPSortKey.php', 'PopulateParentId' => __DIR__ . '/maintenance/populateParentId.php', 'PopulateRecentChangesSource' => __DIR__ . '/maintenance/populateRecentChangesSource.php', 'PopulateRevisionLength' => __DIR__ . '/maintenance/populateRevisionLength.php', diff --git a/includes/installer/DatabaseUpdater.php b/includes/installer/DatabaseUpdater.php index e5cbb7ce71..53acc985fd 100644 --- a/includes/installer/DatabaseUpdater.php +++ b/includes/installer/DatabaseUpdater.php @@ -83,6 +83,7 @@ abstract class DatabaseUpdater { FixDefaultJsonContentPages::class, CleanupEmptyCategories::class, AddRFCAndPMIDInterwiki::class, + PopulatePPSortKey::class ]; /** diff --git a/maintenance/populatePPSortKey.php b/maintenance/populatePPSortKey.php new file mode 100644 index 0000000000..519c6653a9 --- /dev/null +++ b/maintenance/populatePPSortKey.php @@ -0,0 +1,105 @@ +addDescription( 'Populate the pp_sortkey field' ); + $this->setBatchSize( 100 ); + } + + protected function doDBUpdates() { + $dbw = $this->getDB( DB_MASTER ); + + $lastProp = null; + $lastPageValue = 0; + $editedRowCount = 0; + + while ( true ) { + $conditions = [ 'pp_sortkey IS NULL' ]; + if ( $lastPageValue !== 0 ) { + $conditions[] = 'pp_page > ' . $dbw->addQuotes( $lastPageValue ) . ' OR ' . + '( pp_page = ' . $dbw->addQuotes( $lastPageValue ) . + ' AND pp_propname > ' . $dbw->addQuotes( $lastProp ) . ' )'; + } + + $res = $dbw->select( + 'page_props', + [ 'pp_propname', 'pp_page', 'pp_sortkey', 'pp_value' ], + $conditions, + __METHOD__, + [ + 'ORDER BY' => 'pp_page, pp_propname', + 'LIMIT' => $this->mBatchSize + ] + ); + + if ( $res->numRows() === 0 ) { + break; + } + + $this->beginTransaction( $dbw, __METHOD__ ); + + foreach ( $res as $row ) { + if ( !is_numeric( $row->pp_value ) ) { + continue; + } + $dbw->update( + 'page_props', + [ 'pp_sortkey' => $row->pp_value ], + [ + 'pp_page' => $row->pp_page, + 'pp_propname' => $row->pp_propname + ], + __METHOD__ + ); + $editedRowCount++; + } + + $this->output( "Updated " . $editedRowCount . " rows\n" ); + $this->commitTransaction( $dbw, __METHOD__ ); + + // We need to get the last element's page ID + $lastPageValue = $row->pp_value; + // And the propname... + $lastProp = $row->pp_propname; + } + + $this->output( "Done!\n" ); + } + + protected function getUpdateKey() { + return 'populate pp_sortkey'; + } +} + +$maintClass = 'PopulatePPSortKey'; +require_once RUN_MAINTENANCE_IF_MAIN;