-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
T12708: Add APIs for RottenLinks to allow fetching a link's status (#81)
* T12708: Add {{#rl_status}} * Add tests for {{#rl_status}} * T12708: Add the ability to get the status code through Scribunto If Scribunto is not installed, the hook is never activated and nothing happens. Compatibility for non-Scribunto wikis is achieved by doing nothing. * CI: lint code to MediaWiki standards Check commit and GitHub actions for more details * Appease CI * Appease CI, the proper way * Remove strict type hint on RottenLinksLuaLibrary::onGetStatus() If Lua passes us a non-string (and non-null), PHP blows up as type hinting here is strict, unlike Python. * Appease CI, config edition * Make changes as requested * Change namespace as requested in review * Remove trailing newline * Type hint RottenLinksLuaLibrary::onGetStatus() per request on IRC Also require PHP >= 8 since apparently `mixed` only existed since then * Bump versions and add changelog * Update CHANGELOG.md --------- Co-authored-by: github-actions <[email protected]>
- Loading branch information
1 parent
573f18a
commit a201914
Showing
14 changed files
with
292 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Scribunto: | ||
branch: auto | ||
repo: auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
$magicWords = []; | ||
|
||
$magicWords['en'] = [ | ||
'rl_status' => [ 0, 'rl_status' ], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Miraheze\RottenLinks\HookHandlers; | ||
|
||
use MediaWiki\Extension\Scribunto\Hooks\ScribuntoExternalLibrariesHook; | ||
use Miraheze\RottenLinks\RottenLinksLuaLibrary; | ||
|
||
class Scribunto implements ScribuntoExternalLibrariesHook { | ||
|
||
/** | ||
* Handler for ScribuntoExternalLibraries hook. | ||
* @param string $engine | ||
* @param array &$externalLibraries | ||
* @return bool | ||
*/ | ||
public function onScribuntoExternalLibraries( $engine, &$externalLibraries ) { | ||
if ( $engine === 'lua' ) { | ||
$externalLibraries['mw.ext.rottenLinks'] = RottenLinksLuaLibrary::class; | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
namespace Miraheze\RottenLinks; | ||
|
||
use MediaWiki\Extension\Scribunto\Engines\LuaCommon\LibraryBase; | ||
use MediaWiki\Extension\Scribunto\Engines\LuaCommon\LuaEngine; | ||
use MediaWiki\Extension\Scribunto\Engines\LuaCommon\LuaError; | ||
use MediaWiki\MediaWikiServices; | ||
use Wikimedia\Rdbms\IConnectionProvider; | ||
|
||
class RottenLinksLuaLibrary extends LibraryBase { | ||
|
||
private IConnectionProvider $connectionProvider; | ||
|
||
/** | ||
* @param LuaEngine $engine | ||
*/ | ||
public function __construct( LuaEngine $engine ) { | ||
parent::__construct( $engine ); | ||
// Unfortunately, Scribunto currently does not offer us any options to do | ||
// dependency injection, so we have to pretend that we do. Luckily, there | ||
// is already an upstream task: https://phabricator.wikimedia.org/T375835 | ||
$this->connectionProvider = MediaWikiServices::getInstance()->getConnectionProvider(); | ||
} | ||
|
||
/** | ||
* @param mixed $url | ||
* @return array | ||
* @internal | ||
*/ | ||
public function onGetStatus( mixed $url = null ): array { | ||
$name = 'mw.ext.rottenLinks.getStatus'; | ||
$this->checkType( $name, 1, $url, 'string' ); | ||
// $this->checkType() validates that $url is a string, therefore... | ||
'@phan-var string $url'; | ||
|
||
// I think Lua errors are untranslated? LibraryBase::checkType() returns | ||
// a plain ol' English string too. | ||
if ( $url === '' ) { | ||
throw new LuaError( "bad argument #1 to '{$name}' (url is empty)" ); | ||
} | ||
|
||
$dbr = $this->connectionProvider->getReplicaDatabase(); | ||
return [ RottenLinks::getResponseFromDatabase( $dbr, $url ) ]; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function register(): array { | ||
$functions = [ | ||
'getStatus' => [ $this, 'onGetStatus' ], | ||
]; | ||
$arguments = []; | ||
|
||
return $this->getEngine()->registerInterface( __DIR__ . '/mw.ext.rottenLinks.lua', $functions, $arguments ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace Miraheze\RottenLinks; | ||
|
||
use MediaWiki\Html\Html; | ||
use MediaWiki\Parser\Parser; | ||
use PPFrame; | ||
use PPNode; | ||
use Wikimedia\Rdbms\IConnectionProvider; | ||
|
||
class RottenLinksParserFunctions { | ||
|
||
private IConnectionProvider $connectionProvider; | ||
|
||
/** | ||
* @param IConnectionProvider $connectionProvider | ||
*/ | ||
public function __construct( IConnectionProvider $connectionProvider ) { | ||
$this->connectionProvider = $connectionProvider; | ||
} | ||
|
||
/** | ||
* The function responsible for handling {{#rl_status}}. | ||
* | ||
* @param Parser $parser | ||
* @param PPFrame $frame | ||
* @param PPNode[] $args | ||
* @return string | ||
*/ | ||
public function onRLStatus( Parser $parser, PPFrame $frame, array $args ): string { | ||
$url = trim( $frame->expand( $args[0] ?? '' ) ); | ||
if ( $url === '' ) { | ||
return Html::element( 'strong', [ | ||
'class' => 'error', | ||
], $parser->msg( 'rottenlinks-rlstatus-no-url' ) ); | ||
} | ||
|
||
$dbr = $this->connectionProvider->getReplicaDatabase(); | ||
return (string)RottenLinks::getResponseFromDatabase( $dbr, $url ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
local p = {} | ||
|
||
function p.setupInterface(arguments) | ||
p.setupInterface = nil | ||
|
||
local php = mw_interface | ||
mw_interface = nil | ||
|
||
mw = mw or {} | ||
mw.ext = mw.ext or {} | ||
|
||
p.getStatus = php.getStatus | ||
|
||
mw.ext.rottenLinks = p | ||
package.loaded['mw.ext.rottenLinks'] = p | ||
end | ||
|
||
return p |
Oops, something went wrong.