2016-10-13 08:14:18 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace MediaWiki\Linter;
|
|
|
|
|
2024-04-09 23:37:56 +00:00
|
|
|
use MediaWiki\Config\ServiceOptions;
|
2024-10-20 07:25:08 +00:00
|
|
|
use MediaWiki\Json\FormatJson;
|
2017-11-23 07:05:04 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2022-08-22 21:24:19 +00:00
|
|
|
use stdClass;
|
2023-08-01 20:58:59 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
2023-03-07 23:07:08 +00:00
|
|
|
use Wikimedia\Rdbms\IReadableDatabase;
|
2024-04-04 22:55:43 +00:00
|
|
|
use Wikimedia\Rdbms\LBFactory;
|
2022-01-25 23:27:07 +00:00
|
|
|
use Wikimedia\Rdbms\SelectQueryBuilder;
|
2016-10-13 08:14:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Database logic
|
|
|
|
*/
|
2016-10-29 01:06:08 +00:00
|
|
|
class Database {
|
2024-06-04 21:31:37 +00:00
|
|
|
public const CONSTRUCTOR_OPTIONS = [];
|
2016-11-24 03:47:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Maximum number of errors to save per category,
|
|
|
|
* for a page, the rest are just dropped
|
|
|
|
*/
|
2020-09-19 17:06:39 +00:00
|
|
|
public const MAX_PER_CAT = 20;
|
2021-04-07 19:33:16 +00:00
|
|
|
public const MAX_ACCURATE_COUNT = 20;
|
2016-11-24 03:47:04 +00:00
|
|
|
|
2023-02-06 22:44:34 +00:00
|
|
|
/**
|
|
|
|
* The linter_tag field has a maximum length of 32 characters, linter_template field a maximum of 255 characters
|
|
|
|
* so to ensure the length is not exceeded, the tag and template strings are truncated a few bytes below that limit
|
|
|
|
*/
|
|
|
|
public const MAX_TAG_LENGTH = 30;
|
|
|
|
public const MAX_TEMPLATE_LENGTH = 250;
|
|
|
|
|
2024-04-09 23:37:56 +00:00
|
|
|
private ServiceOptions $options;
|
2024-04-04 22:55:43 +00:00
|
|
|
private CategoryManager $categoryManager;
|
|
|
|
private LBFactory $dbLoadBalancerFactory;
|
2016-12-08 07:41:48 +00:00
|
|
|
|
2016-10-13 08:14:18 +00:00
|
|
|
/**
|
2024-04-09 23:37:56 +00:00
|
|
|
* @param ServiceOptions $options
|
2024-04-04 22:55:43 +00:00
|
|
|
* @param CategoryManager $categoryManager
|
|
|
|
* @param LBFactory $dbLoadBalancerFactory
|
2016-10-13 08:14:18 +00:00
|
|
|
*/
|
2024-04-04 22:55:43 +00:00
|
|
|
public function __construct(
|
2024-04-09 23:37:56 +00:00
|
|
|
ServiceOptions $options,
|
2024-04-04 22:55:43 +00:00
|
|
|
CategoryManager $categoryManager,
|
|
|
|
LBFactory $dbLoadBalancerFactory
|
|
|
|
) {
|
2024-04-09 23:37:56 +00:00
|
|
|
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
|
2024-04-04 22:55:43 +00:00
|
|
|
$this->options = $options;
|
|
|
|
$this->categoryManager = $categoryManager;
|
|
|
|
$this->dbLoadBalancerFactory = $dbLoadBalancerFactory;
|
2016-10-13 08:14:18 +00:00
|
|
|
}
|
|
|
|
|
2022-08-02 18:34:16 +00:00
|
|
|
/**
|
|
|
|
* @param int $mode DB_PRIMARY or DB_REPLICA
|
2023-08-01 20:58:59 +00:00
|
|
|
* @return IDatabase
|
2022-08-02 18:34:16 +00:00
|
|
|
*/
|
2024-04-04 22:55:43 +00:00
|
|
|
public function getDBConnectionRef( int $mode ): IDatabase {
|
|
|
|
return $this->dbLoadBalancerFactory->getMainLB()->getConnection( $mode );
|
2022-08-02 18:34:16 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 23:07:08 +00:00
|
|
|
/**
|
|
|
|
* @return IReadableDatabase
|
|
|
|
*/
|
2024-04-04 22:55:43 +00:00
|
|
|
public function getReplicaDBConnection(): IReadableDatabase {
|
|
|
|
return $this->dbLoadBalancerFactory->getReplicaDatabase();
|
2023-03-07 23:07:08 +00:00
|
|
|
}
|
|
|
|
|
2016-10-13 08:14:18 +00:00
|
|
|
/**
|
|
|
|
* Get a specific LintError by id
|
|
|
|
*
|
|
|
|
* @param int $id linter_id
|
|
|
|
* @return bool|LintError
|
|
|
|
*/
|
2024-04-10 20:52:53 +00:00
|
|
|
public function getFromId( int $id ) {
|
2024-04-04 22:55:43 +00:00
|
|
|
$row = $this->getReplicaDBConnection()->newSelectQueryBuilder()
|
2023-03-07 23:07:08 +00:00
|
|
|
->select( [ 'linter_cat', 'linter_params', 'linter_start', 'linter_end' ] )
|
|
|
|
->from( 'linter' )
|
2024-04-10 20:52:53 +00:00
|
|
|
->where( [ 'linter_id' => $id ] )
|
2023-03-07 23:07:08 +00:00
|
|
|
->caller( __METHOD__ )
|
|
|
|
->fetchRow();
|
2016-10-13 08:14:18 +00:00
|
|
|
|
|
|
|
if ( $row ) {
|
|
|
|
$row->linter_id = $id;
|
2024-04-04 22:55:43 +00:00
|
|
|
return self::makeLintError( $this->categoryManager, $row );
|
2016-10-13 08:14:18 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn a database row into a LintError object
|
|
|
|
*
|
2024-04-04 22:55:43 +00:00
|
|
|
* @param CategoryManager $categoryManager
|
2022-08-22 21:24:19 +00:00
|
|
|
* @param stdClass $row
|
2017-10-31 17:42:07 +00:00
|
|
|
* @return LintError|bool false on error
|
2016-10-13 08:14:18 +00:00
|
|
|
*/
|
2024-04-04 22:55:43 +00:00
|
|
|
public static function makeLintError( CategoryManager $categoryManager, $row ) {
|
2017-10-31 17:42:07 +00:00
|
|
|
try {
|
2024-04-04 22:55:43 +00:00
|
|
|
$name = $categoryManager->getCategoryName( $row->linter_cat );
|
2017-10-31 17:42:07 +00:00
|
|
|
} catch ( MissingCategoryException $e ) {
|
2017-11-23 07:05:04 +00:00
|
|
|
LoggerFactory::getInstance( 'Linter' )->error(
|
|
|
|
'Could not find name for id: {linter_cat}',
|
|
|
|
[ 'linter_cat' => $row->linter_cat ]
|
|
|
|
);
|
2017-10-31 17:42:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-10-13 08:14:18 +00:00
|
|
|
return new LintError(
|
2017-10-31 17:42:07 +00:00
|
|
|
$name,
|
2016-12-01 02:47:05 +00:00
|
|
|
[ (int)$row->linter_start, (int)$row->linter_end ],
|
2016-10-13 08:14:18 +00:00
|
|
|
$row->linter_params,
|
2017-12-07 22:30:14 +00:00
|
|
|
$row->linter_cat,
|
2016-10-13 08:14:18 +00:00
|
|
|
(int)$row->linter_id
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all the lint errors for a page
|
|
|
|
*
|
2024-04-10 20:52:53 +00:00
|
|
|
* @param int $pageId
|
2016-10-13 08:14:18 +00:00
|
|
|
* @return LintError[]
|
|
|
|
*/
|
2024-04-10 20:52:53 +00:00
|
|
|
public function getForPage( int $pageId ) {
|
2024-04-04 22:55:43 +00:00
|
|
|
$rows = $this->getReplicaDBConnection()->newSelectQueryBuilder()
|
2023-03-07 23:07:08 +00:00
|
|
|
->select( [ 'linter_id', 'linter_cat', 'linter_start', 'linter_end', 'linter_params' ] )
|
|
|
|
->from( 'linter' )
|
2024-04-10 20:52:53 +00:00
|
|
|
->where( [ 'linter_page' => $pageId ] )
|
2023-03-07 23:07:08 +00:00
|
|
|
->caller( __METHOD__ )
|
|
|
|
->fetchResultSet();
|
|
|
|
|
2016-10-13 08:14:18 +00:00
|
|
|
$result = [];
|
|
|
|
foreach ( $rows as $row ) {
|
2024-04-04 22:55:43 +00:00
|
|
|
$error = self::makeLintError( $this->categoryManager, $row );
|
2017-10-31 17:42:07 +00:00
|
|
|
if ( !$error ) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-10-13 08:14:18 +00:00
|
|
|
$result[$error->id()] = $error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a LintError object into an array for
|
|
|
|
* inserting/querying in the database
|
|
|
|
*
|
2024-04-10 20:52:53 +00:00
|
|
|
* @param int $pageId
|
|
|
|
* @param int $namespaceId
|
2016-10-13 08:14:18 +00:00
|
|
|
* @param LintError $error
|
|
|
|
* @return array
|
|
|
|
*/
|
2024-04-10 20:52:53 +00:00
|
|
|
private function buildErrorRow( int $pageId, int $namespaceId, LintError $error ) {
|
2022-08-03 18:48:32 +00:00
|
|
|
$result = [
|
2024-04-10 20:52:53 +00:00
|
|
|
'linter_page' => $pageId,
|
2022-08-03 18:48:32 +00:00
|
|
|
'linter_cat' => $this->categoryManager->getCategoryId( $error->category, $error->catId ),
|
|
|
|
'linter_params' => FormatJson::encode( $error->params, false, FormatJson::ALL_OK ),
|
|
|
|
'linter_start' => $error->location[ 0 ],
|
2024-06-03 19:22:07 +00:00
|
|
|
'linter_end' => $error->location[ 1 ],
|
|
|
|
'linter_namespace' => $namespaceId
|
2022-08-03 18:48:32 +00:00
|
|
|
];
|
|
|
|
|
2024-06-04 21:31:37 +00:00
|
|
|
$templateInfo = $error->templateInfo ?? '';
|
|
|
|
if ( is_array( $templateInfo ) ) {
|
|
|
|
if ( isset( $templateInfo[ 'multiPartTemplateBlock' ] ) ) {
|
|
|
|
$templateInfo = 'multi-part-template-block';
|
|
|
|
} else {
|
|
|
|
$templateInfo = $templateInfo[ 'name' ] ?? '';
|
2022-08-03 18:48:32 +00:00
|
|
|
}
|
2022-01-21 23:53:12 +00:00
|
|
|
}
|
2024-06-04 21:31:37 +00:00
|
|
|
$templateInfo = mb_strcut( $templateInfo, 0, self::MAX_TEMPLATE_LENGTH );
|
|
|
|
$result[ 'linter_template' ] = $templateInfo;
|
|
|
|
|
|
|
|
$tagInfo = $error->tagInfo ?? '';
|
|
|
|
$tagInfo = mb_strcut( $tagInfo, 0, self::MAX_TAG_LENGTH );
|
|
|
|
$result[ 'linter_tag' ] = $tagInfo;
|
2022-08-03 18:48:32 +00:00
|
|
|
|
|
|
|
return $result;
|
2016-10-13 08:14:18 +00:00
|
|
|
}
|
|
|
|
|
2017-04-28 20:04:45 +00:00
|
|
|
/**
|
|
|
|
* @param LintError[] $errors
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function countByCat( array $errors ) {
|
|
|
|
$count = [];
|
|
|
|
foreach ( $errors as $error ) {
|
|
|
|
if ( !isset( $count[$error->category] ) ) {
|
|
|
|
$count[$error->category] = 1;
|
|
|
|
} else {
|
|
|
|
$count[$error->category] += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $count;
|
|
|
|
}
|
|
|
|
|
2016-10-13 08:14:18 +00:00
|
|
|
/**
|
|
|
|
* Save the specified lint errors in the
|
|
|
|
* database
|
|
|
|
*
|
2024-04-10 20:52:53 +00:00
|
|
|
* @param int $pageId
|
|
|
|
* @param int $namespaceId
|
2016-10-13 08:14:18 +00:00
|
|
|
* @param LintError[] $errors
|
2017-04-28 20:04:45 +00:00
|
|
|
* @return array [ 'deleted' => [ cat => count ], 'added' => [ cat => count ] ]
|
2016-10-13 08:14:18 +00:00
|
|
|
*/
|
2024-04-10 20:52:53 +00:00
|
|
|
public function setForPage( int $pageId, int $namespaceId, $errors ) {
|
|
|
|
$previous = $this->getForPage( $pageId );
|
2024-04-04 22:55:43 +00:00
|
|
|
$dbw = $this->getDBConnectionRef( DB_PRIMARY );
|
2016-10-13 08:14:18 +00:00
|
|
|
if ( !$previous && !$errors ) {
|
2017-04-28 20:04:45 +00:00
|
|
|
return [ 'deleted' => [], 'added' => [] ];
|
2016-10-13 08:14:18 +00:00
|
|
|
} elseif ( !$previous && $errors ) {
|
2016-12-01 02:17:36 +00:00
|
|
|
$toInsert = array_values( $errors );
|
2016-10-13 08:14:18 +00:00
|
|
|
$toDelete = [];
|
|
|
|
} elseif ( $previous && !$errors ) {
|
2023-12-15 12:43:53 +00:00
|
|
|
$dbw->newDeleteQueryBuilder()
|
|
|
|
->deleteFrom( 'linter' )
|
2024-04-10 20:52:53 +00:00
|
|
|
->where( [ 'linter_page' => $pageId ] )
|
2023-12-15 12:43:53 +00:00
|
|
|
->caller( __METHOD__ )
|
|
|
|
->execute();
|
2017-04-28 20:04:45 +00:00
|
|
|
return [ 'deleted' => $this->countByCat( $previous ), 'added' => [] ];
|
2016-10-13 08:14:18 +00:00
|
|
|
} else {
|
|
|
|
$toInsert = [];
|
|
|
|
$toDelete = $previous;
|
|
|
|
// Diff previous and errors
|
|
|
|
foreach ( $errors as $error ) {
|
|
|
|
$uniqueId = $error->id();
|
|
|
|
if ( isset( $previous[$uniqueId] ) ) {
|
|
|
|
unset( $toDelete[$uniqueId] );
|
|
|
|
} else {
|
|
|
|
$toInsert[] = $error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $toDelete ) {
|
|
|
|
$ids = [];
|
|
|
|
foreach ( $toDelete as $lintError ) {
|
|
|
|
if ( $lintError->lintId ) {
|
|
|
|
$ids[] = $lintError->lintId;
|
|
|
|
}
|
|
|
|
}
|
2023-12-15 12:43:53 +00:00
|
|
|
$dbw->newDeleteQueryBuilder()
|
|
|
|
->deleteFrom( 'linter' )
|
|
|
|
->where( [ 'linter_id' => $ids ] )
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
->execute();
|
2016-10-13 08:14:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $toInsert ) {
|
2016-12-02 23:54:02 +00:00
|
|
|
// Insert into db, ignoring any duplicate key errors
|
|
|
|
// since they're the same lint error
|
2023-12-15 12:43:53 +00:00
|
|
|
$dbw->newInsertQueryBuilder()
|
|
|
|
->insertInto( 'linter' )
|
|
|
|
->ignore()
|
2024-04-10 20:52:53 +00:00
|
|
|
->rows(
|
|
|
|
array_map( function ( LintError $error ) use ( $pageId, $namespaceId ) {
|
|
|
|
return $this->buildErrorRow( $pageId, $namespaceId, $error );
|
|
|
|
}, $toInsert )
|
|
|
|
)
|
2023-12-15 12:43:53 +00:00
|
|
|
->caller( __METHOD__ )
|
|
|
|
->execute();
|
2016-10-13 08:14:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
2017-04-28 20:04:45 +00:00
|
|
|
'deleted' => $this->countByCat( $toDelete ),
|
|
|
|
'added' => $this->countByCat( $toInsert ),
|
2016-10-13 08:14:18 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-12-15 03:58:18 +00:00
|
|
|
/**
|
2018-02-01 20:59:08 +00:00
|
|
|
* Get an estimate of how many rows are there for the
|
|
|
|
* specified category with EXPLAIN SELECT COUNT(*).
|
|
|
|
* If the category actually has no rows, then 0 will
|
|
|
|
* be returned.
|
|
|
|
*
|
|
|
|
* @param int $catId
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
private function getTotalsEstimate( $catId ) {
|
2024-04-04 22:55:43 +00:00
|
|
|
$dbr = $this->getReplicaDBConnection();
|
2021-04-07 19:33:16 +00:00
|
|
|
// First see if there are no rows, or a moderate number
|
|
|
|
// within the limit specified by the MAX_ACCURATE_COUNT.
|
|
|
|
// The distinction between 0, a few and a lot is important
|
|
|
|
// to determine first, as estimateRowCount seem to never
|
|
|
|
// return 0 or accurate low error counts.
|
2023-12-15 12:43:53 +00:00
|
|
|
$rows = $dbr->newSelectQueryBuilder()
|
|
|
|
->select( '*' )
|
|
|
|
->from( 'linter' )
|
|
|
|
->where( [ 'linter_cat' => $catId ] )
|
2021-08-06 19:32:07 +00:00
|
|
|
// Select 1 more so we can see if we're over the max limit
|
2023-12-15 12:43:53 +00:00
|
|
|
->limit( self::MAX_ACCURATE_COUNT + 1 )
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
->fetchRowCount();
|
2021-04-07 19:33:16 +00:00
|
|
|
// Return an accurate count if the number of errors is
|
|
|
|
// below the maximum accurate count limit
|
2021-08-06 19:32:07 +00:00
|
|
|
if ( $rows <= self::MAX_ACCURATE_COUNT ) {
|
2021-04-07 19:33:16 +00:00
|
|
|
return $rows;
|
2018-02-01 20:59:08 +00:00
|
|
|
}
|
2021-04-07 19:33:16 +00:00
|
|
|
// Now we can just estimate if the maximum accurate count limit
|
|
|
|
// was returned, which isn't the actual count but the limit reached
|
2023-12-15 12:43:53 +00:00
|
|
|
return $dbr->newSelectQueryBuilder()
|
|
|
|
->select( '*' )
|
|
|
|
->from( 'linter' )
|
|
|
|
->where( [ 'linter_cat' => $catId ] )
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
->estimateRowCount();
|
2018-02-01 20:59:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This uses COUNT(*), which is accurate, but can be significantly
|
|
|
|
* slower depending upon how many rows are in the database.
|
2024-04-10 20:52:53 +00:00
|
|
|
*
|
|
|
|
* @param int $pageId
|
2016-12-15 03:58:18 +00:00
|
|
|
* @return int[]
|
|
|
|
*/
|
2024-04-10 20:52:53 +00:00
|
|
|
public function getTotalsForPage( int $pageId ): array {
|
2024-04-04 22:55:43 +00:00
|
|
|
$rows = $this->getReplicaDBConnection()->newSelectQueryBuilder()
|
2023-03-07 23:07:08 +00:00
|
|
|
->select( [ 'linter_cat', 'COUNT(*) AS count' ] )
|
|
|
|
->from( 'linter' )
|
2024-04-10 20:52:53 +00:00
|
|
|
->where( [ 'linter_page' => $pageId ] )
|
2023-03-07 23:07:08 +00:00
|
|
|
->caller( __METHOD__ )
|
|
|
|
->groupBy( 'linter_cat' )
|
|
|
|
->fetchResultSet();
|
2016-12-08 07:41:48 +00:00
|
|
|
|
|
|
|
// Initialize zero values
|
2024-04-02 19:12:19 +00:00
|
|
|
$categories = $this->categoryManager->getVisibleCategories();
|
|
|
|
$ret = array_fill_keys( $categories, 0 );
|
2016-12-08 07:41:48 +00:00
|
|
|
foreach ( $rows as $row ) {
|
2017-10-31 17:42:07 +00:00
|
|
|
try {
|
|
|
|
$catName = $this->categoryManager->getCategoryName( $row->linter_cat );
|
|
|
|
} catch ( MissingCategoryException $e ) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-04-02 19:12:19 +00:00
|
|
|
// Only set visible categories. Alternatively, we could add another
|
|
|
|
// where clause to the selection above.
|
|
|
|
if ( !in_array( $catName, $categories, true ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-10-31 17:42:07 +00:00
|
|
|
$ret[$catName] = (int)$row->count;
|
2016-12-08 07:41:48 +00:00
|
|
|
}
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
2018-02-01 20:59:08 +00:00
|
|
|
/**
|
|
|
|
* @return int[]
|
|
|
|
*/
|
|
|
|
public function getTotals() {
|
|
|
|
$ret = [];
|
|
|
|
foreach ( $this->categoryManager->getVisibleCategories() as $cat ) {
|
|
|
|
$id = $this->categoryManager->getCategoryId( $cat );
|
|
|
|
$ret[$cat] = $this->getTotalsEstimate( $id );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
2022-01-25 23:27:07 +00:00
|
|
|
/**
|
|
|
|
* This code migrates namespace ID identified by the Linter records linter_page
|
|
|
|
* field and populates the new linter_namespace field if it is unpopulated.
|
|
|
|
* This code is intended to be run once though it could be run multiple times
|
|
|
|
* using `--force` if needed via the maintenance script.
|
|
|
|
* It is safe to run more than once, and will quickly exit if no records need updating.
|
2024-04-10 20:52:53 +00:00
|
|
|
*
|
2022-01-25 23:27:07 +00:00
|
|
|
* @param int $pageBatchSize
|
|
|
|
* @param int $linterBatchSize
|
|
|
|
* @param int $sleep
|
|
|
|
* @return int number of pages updated, each with one or more linter records
|
|
|
|
*/
|
2024-04-10 20:52:53 +00:00
|
|
|
public function migrateNamespace(
|
2024-06-03 19:22:07 +00:00
|
|
|
int $pageBatchSize, int $linterBatchSize, int $sleep
|
2024-04-10 20:52:53 +00:00
|
|
|
): int {
|
2024-08-12 20:58:49 +00:00
|
|
|
if ( $sleep < 0 ) {
|
2022-01-25 23:27:07 +00:00
|
|
|
$sleep = 0;
|
|
|
|
}
|
|
|
|
|
2024-07-03 00:10:49 +00:00
|
|
|
$logger = LoggerFactory::getInstance( 'Linter' );
|
2022-01-25 23:27:07 +00:00
|
|
|
|
2024-04-04 22:55:43 +00:00
|
|
|
$lbFactory = $this->dbLoadBalancerFactory;
|
|
|
|
$dbw = $this->getDBConnectionRef( DB_PRIMARY );
|
|
|
|
$dbread = $this->getDBConnectionRef( DB_REPLICA );
|
2022-01-25 23:27:07 +00:00
|
|
|
|
|
|
|
$logger->info( "Migrate namespace starting\n" );
|
|
|
|
|
|
|
|
$updated = 0;
|
|
|
|
$lastElement = 0;
|
|
|
|
do {
|
|
|
|
// Gather some unique pageId values in linter table records into an array
|
|
|
|
$linterPages = [];
|
|
|
|
|
2023-12-15 12:43:53 +00:00
|
|
|
$result = $dbw->newSelectQueryBuilder()
|
|
|
|
->select( 'linter_page' )
|
|
|
|
->distinct()
|
2022-01-25 23:27:07 +00:00
|
|
|
->from( 'linter' )
|
2023-12-15 12:43:53 +00:00
|
|
|
->where( $dbw->expr( 'linter_page', '>', $lastElement ) )
|
|
|
|
->andWhere( [ 'linter_namespace' => null ] )
|
2022-01-25 23:27:07 +00:00
|
|
|
->orderBy( 'linter_page' )
|
|
|
|
->limit( $linterBatchSize )
|
2023-12-15 12:43:53 +00:00
|
|
|
->caller( __METHOD__ )
|
|
|
|
->fetchResultSet();
|
2022-01-25 23:27:07 +00:00
|
|
|
|
|
|
|
foreach ( $result as $row ) {
|
|
|
|
$lastElement = intval( $row->linter_page );
|
|
|
|
$linterPages[] = $lastElement;
|
|
|
|
}
|
|
|
|
$linterPagesLength = count( $linterPages );
|
|
|
|
|
|
|
|
$pageStartIndex = 0;
|
|
|
|
do {
|
|
|
|
$pageIdBatch = array_slice( $linterPages, $pageStartIndex, $pageBatchSize );
|
|
|
|
|
|
|
|
if ( count( $pageIdBatch ) > 0 ) {
|
|
|
|
|
2023-12-15 12:43:53 +00:00
|
|
|
$pageResults = $dbread->newSelectQueryBuilder()
|
|
|
|
->select( [ 'page_id', 'page_namespace' ] )
|
2022-01-25 23:27:07 +00:00
|
|
|
->from( 'page' )
|
|
|
|
->where( [ 'page_id' => $pageIdBatch ] )
|
2023-12-15 12:43:53 +00:00
|
|
|
->caller( __METHOD__ )
|
|
|
|
->fetchResultSet();
|
2022-01-25 23:27:07 +00:00
|
|
|
|
|
|
|
foreach ( $pageResults as $pageRow ) {
|
|
|
|
$pageId = intval( $pageRow->page_id );
|
2024-04-10 20:52:53 +00:00
|
|
|
$namespaceId = intval( $pageRow->page_namespace );
|
2022-01-25 23:27:07 +00:00
|
|
|
|
|
|
|
// If a record about to be updated has been removed by another process,
|
|
|
|
// the update will not error, and continue updating the existing records.
|
2023-12-15 12:43:53 +00:00
|
|
|
$dbw->newUpdateQueryBuilder()
|
|
|
|
->update( 'linter' )
|
2024-04-10 20:52:53 +00:00
|
|
|
->set( [ 'linter_namespace' => $namespaceId ] )
|
2023-12-15 12:43:53 +00:00
|
|
|
->where( [
|
|
|
|
'linter_namespace' => null,
|
|
|
|
'linter_page' => $pageId
|
|
|
|
] )
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
->execute();
|
|
|
|
$updated += $dbw->affectedRows();
|
2022-01-25 23:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sleep between batches for replication to catch up
|
|
|
|
$lbFactory->waitForReplication();
|
|
|
|
sleep( $sleep );
|
|
|
|
}
|
|
|
|
|
|
|
|
$pageStartIndex += $pageBatchSize;
|
|
|
|
} while ( $linterPagesLength > $pageStartIndex );
|
|
|
|
|
|
|
|
$logger->info( 'Migrated ' . $updated . " page IDs\n" );
|
|
|
|
|
|
|
|
} while ( $linterPagesLength > 0 );
|
|
|
|
|
|
|
|
$logger->info( "Migrate namespace finished!\n" );
|
|
|
|
|
|
|
|
return $updated;
|
|
|
|
}
|
|
|
|
|
2022-08-22 21:24:19 +00:00
|
|
|
/**
|
|
|
|
* This code migrates the content of Linter record linter_params to linter_template and
|
|
|
|
* linter_tag fields if they are unpopulated or stale.
|
|
|
|
* This code should only be run once and thereafter disabled but must run to completion.
|
|
|
|
* It can be restarted if interrupted and will pick up where new divergences are found.
|
|
|
|
* Note: When linter_params are not set, the content is set to '[]' indicating no content
|
|
|
|
* and the code also handles a null linter_params field if found.
|
|
|
|
* This code is only run once by maintenance script migrateTagTemplate.php
|
2024-04-10 20:52:53 +00:00
|
|
|
*
|
2022-08-22 21:24:19 +00:00
|
|
|
* @param int $batchSize
|
|
|
|
* @param int $sleep
|
|
|
|
* @return int
|
|
|
|
*/
|
2024-04-10 20:52:53 +00:00
|
|
|
public function migrateTemplateAndTagInfo(
|
2024-06-04 21:31:37 +00:00
|
|
|
int $batchSize, int $sleep
|
2022-08-22 21:24:19 +00:00
|
|
|
): int {
|
2024-08-12 20:58:49 +00:00
|
|
|
if ( $sleep < 0 ) {
|
2022-08-22 21:24:19 +00:00
|
|
|
$sleep = 0;
|
|
|
|
}
|
|
|
|
|
2024-07-03 00:10:49 +00:00
|
|
|
$logger = LoggerFactory::getInstance( 'Linter' );
|
2022-08-22 21:24:19 +00:00
|
|
|
|
2024-04-04 22:55:43 +00:00
|
|
|
$lbFactory = $this->dbLoadBalancerFactory;
|
|
|
|
$dbw = $this->getDBConnectionRef( DB_PRIMARY );
|
2022-08-22 21:24:19 +00:00
|
|
|
|
|
|
|
$logger->info( "Migration of linter_params field to linter_tag and linter_template fields starting\n" );
|
|
|
|
|
|
|
|
$updated = 0;
|
|
|
|
$lastElement = 0;
|
|
|
|
do {
|
2023-12-15 12:43:53 +00:00
|
|
|
$results = $dbw->newSelectQueryBuilder()
|
|
|
|
->select( [ 'linter_id', 'linter_params', 'linter_template', 'linter_tag' ] )
|
|
|
|
->from( 'linter' )
|
|
|
|
->where( [
|
|
|
|
$dbw->expr( "linter_params", '!=', '[]' ),
|
|
|
|
$dbw->expr( "linter_params", '!=', null ),
|
|
|
|
$dbw->expr( "linter_id", '>', $lastElement )
|
|
|
|
] )
|
2022-08-22 21:24:19 +00:00
|
|
|
->orderBy( 'linter_id', selectQueryBuilder::SORT_ASC )
|
|
|
|
->limit( $batchSize )
|
2023-12-15 12:43:53 +00:00
|
|
|
->caller( __METHOD__ )
|
|
|
|
->fetchResultSet();
|
2022-08-22 21:24:19 +00:00
|
|
|
$linterBatchLength = 0;
|
|
|
|
|
|
|
|
foreach ( $results as $row ) {
|
|
|
|
$linter_id = intval( $row->linter_id );
|
|
|
|
$lastElement = $linter_id;
|
|
|
|
$linter_params = FormatJson::decode( $row->linter_params );
|
|
|
|
$templateInfo = $linter_params->templateInfo ?? '';
|
|
|
|
if ( is_object( $templateInfo ) ) {
|
|
|
|
if ( isset( $templateInfo->multiPartTemplateBlock ) ) {
|
|
|
|
$templateInfo = 'multi-part-template-block';
|
|
|
|
} else {
|
|
|
|
$templateInfo = $templateInfo->name ?? '';
|
|
|
|
}
|
|
|
|
}
|
2023-02-07 20:39:22 +00:00
|
|
|
$templateInfo = mb_strcut( $templateInfo, 0, self::MAX_TEMPLATE_LENGTH );
|
|
|
|
|
2022-08-22 21:24:19 +00:00
|
|
|
$tagInfo = $linter_params->name ?? '';
|
2023-02-07 20:39:22 +00:00
|
|
|
$tagInfo = mb_strcut( $tagInfo, 0, self::MAX_TAG_LENGTH );
|
2022-08-22 21:24:19 +00:00
|
|
|
|
|
|
|
// compare the content of linter_params to the template and tag field contents
|
|
|
|
// and if they diverge, update the field with the correct template and tag info.
|
|
|
|
// This behavior allows this function to be restarted should it be interrupted
|
|
|
|
// and avoids repeating database record updates that are already correct due to
|
|
|
|
// having been populated when the error record was created with the new recordLintError
|
|
|
|
// write code that populates the template and tag fields, or for records populated
|
|
|
|
// during a previous but interrupted run of this migrate code.
|
|
|
|
if ( $templateInfo != $row->linter_template || $tagInfo != $row->linter_tag ) {
|
|
|
|
// If the record about to be updated has been removed by another process,
|
|
|
|
// the update will not do anything and just return with no records updated.
|
2023-12-15 12:43:53 +00:00
|
|
|
$dbw->newUpdateQueryBuilder()
|
|
|
|
->update( 'linter' )
|
2024-03-11 19:43:59 +00:00
|
|
|
->set( [ 'linter_template' => $templateInfo, 'linter_tag' => $tagInfo, ] )
|
2023-12-15 12:43:53 +00:00
|
|
|
->where( [ 'linter_id' => $linter_id ] )
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
->execute();
|
|
|
|
$updated += $dbw->affectedRows();
|
2022-08-22 21:24:19 +00:00
|
|
|
}
|
|
|
|
$linterBatchLength++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sleep between batches for replication to catch up
|
|
|
|
$lbFactory->waitForReplication();
|
|
|
|
if ( $sleep > 0 ) {
|
|
|
|
sleep( $sleep );
|
|
|
|
}
|
|
|
|
|
|
|
|
$logger->info( 'Migrated ' . $updated . " linter IDs\n" );
|
|
|
|
|
|
|
|
} while ( $linterBatchLength > 0 );
|
|
|
|
|
|
|
|
$logger->info( "Migrate linter_params to linter_tag and linter_template fields finished!\n" );
|
|
|
|
|
|
|
|
return $updated;
|
|
|
|
}
|
|
|
|
|
2016-10-13 08:14:18 +00:00
|
|
|
}
|