Skip to content

Commit

Permalink
Improve Special:RandomWiki (#107)
Browse files Browse the repository at this point in the history
This does three main things:

- Prevent being sent to a closed wiki
- Add an inactive wiki switch in the front-end
- Do random selection in the DB to save a query and use less data (this will be a little slower than Special:RandomPage's method, but on the size of the cw_wikis table less likely to matter)

Closes T11683
  • Loading branch information
labster authored Feb 23, 2024
1 parent 8da9a75 commit a839b3f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
2 changes: 1 addition & 1 deletion i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"wikidiscover-header-info": "You may search for wikis across this farm by selecting parameters such as language and more.",
"wikidiscover-table-category": "Category",
"wikidiscover-table-description": "Description",
"wikidiscover-table-established": "Established",
"wikidiscover-table-established": "Established",
"wikidiscover-table-language": "Language",
"wikidiscover-table-state": "State",
"wikidiscover-table-visibility": "Visibility",
Expand Down
22 changes: 20 additions & 2 deletions includes/SpecialRandomWiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ public function execute( $subPage ) {
],
];

if ( $this->config->get( 'CreateWikiUseInactiveWikis' ) ) {
$formDescriptor['inactive'] = [
'type' => 'select',
'name' => 'inactive',
'label-message' => 'wikidiscover-table-state',
'options' => [
'(any)' => 'any',
'active' => 'active',
'inactive' => 'inactive',
],
'default' => 'any',
];
}

$htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() );
$htmlForm
->setSubmitCallback( [ $this, 'redirectWiki' ] )
Expand All @@ -57,9 +71,13 @@ protected function getGroupName() {
* @return bool
*/
public function redirectWiki( $formData ) {
$randomwiki = WikiDiscoverRandom::randomWiki( 0, $category = $formData['category'], $formData['language'] );
$randomwiki = WikiDiscoverRandom::randomWiki( $formData['inactive'], $formData['category'], $formData['language'] );

header( "Location: https://" . substr( $randomwiki->wiki_dbname, 0, -4 ) . ".{$this->config->get( 'CreateWikiSubdomain' )}/" );
if ( $randomwiki->wiki_url ) {
header( "Location: https://" . $randomwiki->wiki_url . "/" );
} else {
header( "Location: https://" . substr( $randomwiki->wiki_dbname, 0, -4 ) . ".{$this->config->get( 'CreateWikiSubdomain' )}/" );
}

return true;
}
Expand Down
34 changes: 14 additions & 20 deletions includes/WikiDiscoverRandom.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ public static function randomWiki( $state = 0, $category = 0, $language = 0 ) {

if ( $config->get( 'CreateWikiUseInactiveWikis' ) && $state === 'inactive' ) {
$conditions['wiki_inactive'] = 1;
} elseif ( $config->get( 'CreateWikiUseClosedWikis' ) && $state === 'closed' ) {
$conditions['wiki_closed'] = 1;
} elseif ( $config->get( 'CreateWikiUseClosedWikis' ) && $state === 'open' ) {
$conditions['wiki_closed'] = 0;
} elseif ( $config->get( 'CreateWikiUseInactiveWikis' ) && $state === 'active' ) {
$conditions['wiki_inactive'] = 0;
}

/* Never randomly offer closed or private wikis */
if ( $config->get( 'CreateWikiUseClosedWikis' ) ) {
$conditions['wiki_closed'] = 0;
}
if ( $config->get( 'CreateWikiUsePrivateWikis' ) ) {
$conditions['wiki_private'] = 0;
}
Expand All @@ -48,23 +50,15 @@ protected static function randFromConds( $conds ) {
$dbr = $lbFactory->getMainLB( $config->get( 'CreateWikiDatabase' ) )
->getMaintenanceConnectionRef( DB_REPLICA, [], $config->get( 'CreateWikiDatabase' ) );

$possiblewikis = $dbr->selectFieldValues( 'cw_wikis', 'wiki_dbname', $conds, __METHOD__ );

$randwiki = $possiblewikis[array_rand( $possiblewikis )];

$fields = [];
if ( $config->get( 'CreateWikiUseClosedWikis' ) ) {
$fields[] = 'wiki_closed';
}

if ( $config->get( 'CreateWikiUseInactiveWikis' ) ) {
$fields[] = 'wiki_inactive';
}
/* MySQL is ever the outlier */
$random_function = $dbr->getType() === 'mysql' ? 'RAND()' : 'random()';

if ( $config->get( 'CreateWikiUsePrivateWikis' ) ) {
$fields[] = 'wiki_private';
}
return $dbr->selectRow(
'cw_wikis',
[ 'wiki_dbname', 'wiki_url' ],
__METHOD__,
[ 'ORDER BY' => $random_function, 'LIMIT' => 1 ]
);

return $dbr->selectRow( 'cw_wikis', array_merge( [ 'wiki_dbname', 'wiki_sitename', 'wiki_language', 'wiki_category' ], $fields ), [ 'wiki_dbname' => $randwiki ], __METHOD__ );
}
}

0 comments on commit a839b3f

Please sign in to comment.