Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hook to pass certain extra field data from cw_requests to cw_wikis #579

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
12 changes: 12 additions & 0 deletions includes/Hooks/CreateWikiCreationExtraFieldsHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Miraheze\CreateWiki\Hooks;

interface CreateWikiCreationExtraFieldsHook {

/**
* @param array &$extraFields
* @return void
*/
public function onCreateWikiCreationExtraFields( array &$extraFields ): void;
}
9 changes: 9 additions & 0 deletions includes/Hooks/CreateWikiHookRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

class CreateWikiHookRunner implements
CreateWikiAfterCreationWithExtraDataHook,
CreateWikiCreationExtraFieldsHook,
CreateWikiCreationHook,
CreateWikiDataFactoryBuilderHook,
CreateWikiDeletionHook,
Expand Down Expand Up @@ -43,6 +44,14 @@ public function onCreateWikiAfterCreationWithExtraData( array $extraData, string
);
}

/** @inheritDoc */
public function onCreateWikiCreationExtraFields( array &$extraFields ): void {
$this->hookContainer->run(
'CreateWikiCreationExtraFields',
[ &$extraFields ]
);
}

/** @inheritDoc */
public function onCreateWikiCreation( string $dbname, bool $private ): void {
$this->hookContainer->run(
Expand Down
9 changes: 9 additions & 0 deletions includes/Hooks/Handlers/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ public function onLoadExtensionSchemaUpdates( $updater ) {
true,
] );

$updater->addExtensionUpdateOnVirtualDomain( [
'virtual-createwiki',
'addField',
'cw_wikis',
'wiki_extra',
"$dir/patches/patch-cw_wikis-add-wiki_extra.sql",
true,
] );

$updater->addExtensionUpdateOnVirtualDomain( [
'virtual-createwiki',
'modifyTable',
Expand Down
25 changes: 25 additions & 0 deletions includes/Services/RemoteWikiFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class RemoteWikiFactory {
private array $logParams = [];
private array $newRows = [];
private array $hooks = [];
private array $extra;

private string $dbname;
private string $sitename;
Expand Down Expand Up @@ -104,6 +105,8 @@ public function newInstance( string $wiki ): self {

$this->deletedTimestamp = $row->wiki_deleted_timestamp;

$this->extra = json_decode( $row->wiki_extra ?: '[]', true );

if ( $this->options->get( ConfigNames::UsePrivateWikis ) ) {
$this->private = (bool)$row->wiki_private;
}
Expand Down Expand Up @@ -380,6 +383,28 @@ public function unMarkExperimental(): void {
$this->newRows['wiki_experimental'] = 0;
}

public function getExtraFieldData( string $field ): mixed {
return $this->extra[$field] ?? null;
}

public function setExtraFieldData( string $field, mixed $value ): void {
if ( $value !== $this->getExtraFieldData( $field ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

@Universal-Omega Universal-Omega Oct 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically I like early returns but decided not to here as my plan is to eventually make RemoteWikiFactory more similar to how WikiRequestManager works which means it being more inline with that here makes it a tiny bit easier down the line which is why I decided against it. But I guess it wouldn't be horrible to early return also...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright then, feel free to resolve this conversation if you decide not to early return

$extra = $this->extra;
$extra[$field] = $value;

$newExtra = json_encode( $extra );

if ( $newExtra === false ) {
// Can not set invalid JSON data to wiki_extra.
return;
}

$this->extra = $extra;
$this->trackChange( $field, $this->getExtraFieldData( $field ), $value );
$this->newRows['wiki_extra'] = $newExtra;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personal Note: This definitely needs tested with more than one field at once (calling this function more than once) as unlike RequestWiki we can't have just a set all field data at once function here, so like the first version of WikiRequestManager this may have an issue with that and will need to be tested and handled some other way if so.

}
}

public function trackChange( string $field, mixed $oldValue, mixed $newValue ): void {
$this->changes[$field] = [
'old' => $oldValue,
Expand Down
8 changes: 8 additions & 0 deletions includes/Services/WikiManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ public function create(

$this->doCreateDatabase();

$extraFields = [];
$this->hookRunner->onCreateWikiCreationExtraFields( $extraFields );

// Filter $extra to only include keys present in $extraFields
$filteredData = array_intersect_key( $extra, array_flip( $extraFields ) );
$extraData = json_encode( $filteredData ) ?: '[]';

$this->cwdb->newInsertQueryBuilder()
->insertInto( 'cw_wikis' )
->row( [
Expand All @@ -200,6 +207,7 @@ public function create(
'wiki_private' => (int)$private,
'wiki_creation' => $this->dbw->timestamp(),
'wiki_category' => $category,
'wiki_extra' => $extraData,
] )
->caller( __METHOD__ )
->execute();
Expand Down
3 changes: 2 additions & 1 deletion sql/cw_wikis.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ CREATE TABLE /*_*/cw_wikis (
wiki_locked TINYINT NOT NULL DEFAULT '0',
wiki_dbcluster VARCHAR(5) DEFAULT 'c1',
wiki_category VARCHAR(64) NOT NULL,
wiki_experimental TINYINT NOT NULL DEFAULT '0'
wiki_experimental TINYINT NOT NULL DEFAULT '0',
wiki_extra LONGTEXT NULL
) /*$wgDBTableOptions*/;

CREATE INDEX /*i*/wiki_dbname ON /*_*/cw_wikis (wiki_dbname);
2 changes: 2 additions & 0 deletions sql/patches/patch-cw_wikis-add-wiki_extra.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE /*$wgDBprefix*/cw_wikis
ADD COLUMN wiki_extra LONGTEXT NULL AFTER wiki_experimental;
Loading