mediawiki-extensions-Confir.../includes/Store/CaptchaCacheStore.php
Derick Alangi bb201b86e4
Store: Enable ConfirmEdit to use MicroStash for captcha storage
ConfirmEdit uses MainStash as the backend to write its captchas. We
are migrating this extension to use the MicroStash store instead which
is more suitable.

This patch will store the captcha in MicroStash, read it from there
or fallback to MainStash if lookup was not successful. The code will
then clear both stores once after processing.

Migration plan
==============

step .1: Write to microstash store only, read from it or
         fallback to mainstash store. Then delete from
         both backends.

step .2: Read from microstash store only, delete from the
         microstash store, and remove dead code afterward.

Bug: T336004
Change-Id: Ie7c50a6efe7a0aefc97a712b2ad961e7837cc4cf
2023-11-29 11:35:45 +01:00

72 lines
1.7 KiB
PHP

<?php
namespace MediaWiki\Extension\ConfirmEdit\Store;
use BagOStuff;
use MediaWiki\MediaWikiServices;
class CaptchaCacheStore extends CaptchaStore {
/** @var BagOStuff */
private $mainStashStore;
/** @var BagOStuff */
private $microStashStore;
public function __construct() {
parent::__construct();
$services = MediaWikiServices::getInstance();
$this->mainStashStore = $services->getMainObjectStash();
$this->microStashStore = $services->getMicroStash();
}
/**
* @inheritDoc
*/
public function store( $index, $info ) {
global $wgCaptchaSessionExpiration;
$microStashStore = $this->microStashStore;
$microStashStore->set(
$microStashStore->makeKey( 'captcha', $index ),
$info,
$wgCaptchaSessionExpiration,
// Assume the write will reach the master DC before the user sends the
// HTTP POST request attempted to solve the captcha and perform an action
$microStashStore::WRITE_BACKGROUND
);
}
/**
* @inheritDoc
*/
public function retrieve( $index ) {
$microStashStore = $this->microStashStore;
$data = $microStashStore->get( $microStashStore->makeKey( 'captcha', $index ) );
if ( !$data ) {
$mainStashStore = $this->mainStashStore;
$data = $mainStashStore->get( $mainStashStore->makeKey( 'captcha', $index ) );
}
return $data;
}
/**
* @inheritDoc
*/
public function clear( $index ) {
$mainStashStore = $this->mainStashStore;
$mainStashStore->delete( $mainStashStore->makeKey( 'captcha', $index ) );
$microStashStore = $this->microStashStore;
$microStashStore->delete( $microStashStore->makeKey( 'captcha', $index ) );
}
public function cookiesNeeded() {
return false;
}
}
class_alias( CaptchaCacheStore::class, 'CaptchaCacheStore' );