Skip to content

Commit

Permalink
Use DB_MASTER when using query() to avoid error
Browse files Browse the repository at this point in the history
This appears to be a more recent change, that query() causes the
following error when used along with DB_REPLICA:

Wikimedia\Rdbms\DBReadOnlyRoleError from line 1195 of
$MW_INSTALL_PATH/includes/libs/rdbms/database/Database.php: Cannot
write; target role is DB_REPLICA

While WatchAnalytics was not using query() to write to the database, it
is possible to use query() that way, so it makes sense for MW to be more
strict with its usage.
  • Loading branch information
jamesmontalvo3 committed Jun 12, 2020
1 parent 7cf5c17 commit 15da667
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
4 changes: 2 additions & 2 deletions includes/WatchAnalyticsParserFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static function renderUnderwatchedCategories( &$parser, $frame, $args ) {
// $args = self::processArgs( $frame, $args, array(0) );
// $namespace = $args[0];

$dbr = wfGetDB( DB_REPLICA );
$dbr = wfGetDB( DB_MASTER );

$query = "
SELECT * FROM (
Expand Down Expand Up @@ -121,7 +121,7 @@ public static function renderWatchersNeeded( &$parser, $frame, $args ) {

$rangeTimestamp = date( 'YmdHis', time() - ( $numDays * 24 * 60 * 60 ) );

$dbr = wfGetDB( DB_REPLICA );
$dbr = wfGetDB( DB_MASTER );

if ( class_exists( 'Wiretap' ) && false ) {
$query =
Expand Down
3 changes: 1 addition & 2 deletions specials/SpecialClearPendingReviews.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function validateCategory( $categoryField, $allData ) {
* @return $results
*/
public static function doSearchQuery( $data, $clearPages ) {
$dbw = wfGetDB( DB_REPLICA );
$dbw = wfGetDB( DB_MASTER );
$category = preg_replace( '/\s+/', '_', $data['category'] );
$page = preg_replace( '/\s+/', '_', $data['page'] );
$start = preg_replace( '/\s+/', '', $data['start'] );
Expand Down Expand Up @@ -133,7 +133,6 @@ public static function doSearchQuery( $data, $clearPages ) {
$results = $dbw->select( $tables, $vars, $conditions, __METHOD__, 'DISTINCT', $join_conds );

if ( $clearPages == true ) {
$dbw = wfGetDB( DB_MASTER );

foreach ( $results as $result ) {
$values = [ 'wl_notificationtimestamp' => null ];
Expand Down
6 changes: 3 additions & 3 deletions specials/SpecialWatchAnalytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function getPageHeader() {
// FROM watchlist
// INNER JOIN page ON page.page_namespace = watchlist.wl_namespace AND page.page_title = watchlist.wl_title; $dbr = wfGetDB( DB_SLAVE );

$dbr = wfGetDB( DB_REPLICA );
$db = wfGetDB( DB_MASTER );

// $res = $dbr->select(
// array(
Expand All @@ -96,7 +96,7 @@ public function getPageHeader() {
// )
// );

$res = $dbr->query( '
$res = $db->query( '
SELECT
COUNT(*) AS num_watches,
SUM( IF(watchlist.wl_notificationtimestamp IS NULL, 0, 1) ) AS num_pending,
Expand All @@ -105,7 +105,7 @@ public function getPageHeader() {
INNER JOIN page ON page.page_namespace = watchlist.wl_namespace AND page.page_title = watchlist.wl_title;
' );

$allWikiData = $dbr->fetchRow( $res );
$allWikiData = $db->fetchRow( $res );

list( $watches, $pending, $percent ) = [
$allWikiData['num_watches'],
Expand Down

0 comments on commit 15da667

Please sign in to comment.