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
6 changes: 6 additions & 0 deletions includes/Hooks/Handlers/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public function onLoadExtensionSchemaUpdates( $updater ) {
"$dir/patches/patch-cw_requests-add-cw_extra.sql"
);

$updater->addExtensionField(
'cw_wikis',
'wiki_extra',
"$dir/patches/patch-cw_wikis-add-wiki_extra.sql"
);

$updater->modifyExtensionTable(
'cw_wikis',
"$dir/patches/patch-cw_wikis-update-smallint-to-tinyint.sql"
Expand Down
25 changes: 25 additions & 0 deletions includes/Services/RemoteWikiFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,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 @@ -107,6 +108,8 @@ public function newInstance( string $wiki ): self {

$this->deletedTimestamp = (int)$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 @@ -369,6 +372,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 @@ -194,6 +194,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 @@ -204,6 +211,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 @@ -18,7 +18,8 @@ CREATE TABLE /*_*/cw_wikis (
wiki_dbcluster VARCHAR(5) DEFAULT 'c1',
wiki_category VARCHAR(64) NOT NULL,
wiki_extensions MEDIUMTEXT 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