Skip to content

Commit

Permalink
Merge changes (#6)
Browse files Browse the repository at this point in the history
* (doc) Fix typo in comment

* (tests) Run phpcs/phan via GitHub Actions

* (1.43) Replace deprecated wfGetDB()

* (1.43) Replace removed method User::getGroups()

* (1.43) Replace nonexistent method ParserOutput::getRootText()

* (1.43) Replace deprecated WikiPage::factory()

* (1.43) Fix phan warning caused by incorrect type annotation

* (1.43) Fix "undeclared class LinksUpdate" (class was renamed)

* (phan) Fix "unused parameter" warnings

* (1.43) Replace deprecated ContentHandler::getContentText()

* (1.43) Replace deprecated ParserOutput::getText()

* (1.43) Replace deprecated Status::getHTML()

---------

Co-authored-by: Edward Chernenko <[email protected]>
  • Loading branch information
davidhaslip and edwardspec authored Jan 1, 2025
1 parent 632ee15 commit 0934e6b
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 26 deletions.
54 changes: 54 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: JSON linter + luacheck

on: [push, pull_request, workflow_dispatch]

env:
DBTYPE: mysql
DBUSER: root

jobs:
# PHP linters: phpcs, parallel-lint, etc.
linter:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
tools: composer
- uses: actions/cache@v4
with:
path: ~/.composer/cache
key: buildcache-linter
- run: sudo apt-get install -y composer && composer install
- run: composer test

# Phan (PHP static analyzer)
phan:
runs-on: ubuntu-22.04
env:
branch: REL1_43
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
extensions: ast
tools: composer
- uses: actions/cache@v4
with:
path: |
~/.composer/cache
buildcache
key: buildcache-phan
- uses: edwardspec/github-action-build-mediawiki@v1
with:
branch: ${{ env.branch }}
noinstall: 1
- name: Install dependencies
run: |
rsync -a --exclude buildcache --exclude mediawiki --exclude .git . mediawiki/extensions/AccessControl/
cd mediawiki/extensions/AccessControl
composer install
- name: RUN -- phan
run: cd mediawiki/extensions/AccessControl && ./vendor/bin/phan --analyze-twice
16 changes: 16 additions & 0 deletions .phan/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

$cfg = require __DIR__ . '/../vendor/mediawiki/mediawiki-phan-config/src/config.php';

# Detect unused method parameters, etc.
$cfg['unused_variable_detection'] = true;

if ( getenv( 'PHAN_CHECK_DEPRECATED' ) ) {
# Optional: warn about the use of @deprecated methods, etc.
# Not enabled by default (without PHAN_CHECK_DEPRECATED=1) for backward compatibility.
$cfg['suppress_issue_types'] = array_filter( $cfg['suppress_issue_types'], static function ( $issue ) {
return strpos( $issue, 'PhanDeprecated' ) === false;
} );
}

return $cfg;
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"require-dev": {
"mediawiki/mediawiki-codesniffer": "44.0.0",
"mediawiki/minus-x": "1.1.0",
"mediawiki/mediawiki-codesniffer": "45.0.0",
"mediawiki/minus-x": "1.1.3",
"php-parallel-lint/php-console-highlighter": "0.5.0",
"php-parallel-lint/php-parallel-lint": "1.2.0"
"php-parallel-lint/php-parallel-lint": "1.4.0",
"mediawiki/mediawiki-phan-config": "0.14.0"
},
"scripts": {
"test": [
Expand Down
2 changes: 1 addition & 1 deletion extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"descriptionmsg": "accesscontrol-desc",
"license-name": "GPL-2.0-or-later",
"requires": {
"MediaWiki": ">= 1.32.0"
"MediaWiki": ">= 1.43.0"
},
"AutoloadClasses": {
"AccessControlHooks": "src/AccessControlHooks.php"
Expand Down
52 changes: 30 additions & 22 deletions src/AccessControlHooks.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use MediaWiki\Content\TextContent;
use MediaWiki\Deferred\LinksUpdate\LinksUpdate;
use MediaWiki\MediaWikiServices;

class AccessControlHooks {
Expand All @@ -11,7 +13,7 @@ class AccessControlHooks {

/**
* @var array
* @phan-var array<string,mixed>
* @phan-var array<string|int,?mixed>
*/
private static $cache = [];

Expand All @@ -20,7 +22,7 @@ class AccessControlHooks {
* @phan-var array<string,bool>
*
* Format: [ 'pageName1' => true, ... ]
* This is only used of $wgAccessControlAllowTextSnippetInSearchResultsForAll is true,
* This is only used if $wgAccessControlAllowTextSnippetInSearchResultsForAll is true,
* which allows restricted pages to appear in search results.
*
* This array will contain the list of all restricted pages (which current user can't read)
Expand All @@ -42,12 +44,11 @@ public static function accessControlExtension( Parser $parser ) {
/**
* Function called by accessControlExtension
* @param string $input
* @param string[] $args
* @param string[] $args @phan-unused-param
* @param Parser $parser
* @param PPFrame $frame
* @return string
*/
public static function doControlUserAccess( string $input, array $args, Parser $parser, PPFrame $frame ) {
public static function doControlUserAccess( string $input, array $args, Parser $parser ) {
$parserOutput = $parser->getOutput();
$data = $parserOutput->getExtensionData( self::TAG_CONTENT_ARRAY ) ?: [];
$inputArray = explode( ',', $input );
Expand Down Expand Up @@ -90,11 +91,12 @@ private static function canUserDoAction( User $user, ?array $tagContentArray, st
return $return;
}

if ( self::getConfigValue( 'AdminCanReadAll' ) &&
in_array( 'sysop', $user->getGroups(), true )
) {
// Admin can read all
return $return;
if ( self::getConfigValue( 'AdminCanReadAll' ) ) {
$ugm = MediaWikiServices::getInstance()->getUserGroupManager();
if ( in_array( 'sysop', $ugm->listAllGroups() ) ) {
// Admin can read all
return $return;
}
}

$userName = $user->isAnon() ? '*' : $user->getName();
Expand Down Expand Up @@ -165,12 +167,14 @@ public static function onOutputPageParserOutput( OutputPage $out, ParserOutput $
$status = self::canUserDoAction( $user, $tagContentArray, $actionName );
if ( !$status->getValue() ) {
// User has no access
$parserOutput->setText( wfMessage( 'accesscontrol-info-box', $parserOutput->getRootText() )->parse() );
$parserOutput->setRawText(
$out->msg( 'accesscontrol-info-box', $out->getTitle()->getRootText() )->parse()
);
}
if ( !$status->isGood() ) {
$text = $parserOutput->getRawText();
$text = Html::rawElement( 'div', [ 'class' => 'error' ], $status->getHTML() ) . "\n$text";
$parserOutput->setText( $text );
$text = Html::rawElement( 'div', [ 'class' => 'error' ], $status->getMessage()->escaped() ) . "\n$text";
$parserOutput->setRawText( $text );
}
}

Expand Down Expand Up @@ -299,11 +303,15 @@ private static function getUsersFromPages( string $group ): Status {
if ( !$gt->exists() ) {
return Status::newFatal( 'accesscontrol-group-does-not-exist', $gt->getFullText() );
}
// Article::fetchContent() is deprecated.
// Replaced by WikiPage::getContent()
$groupPage = WikiPage::factory( $gt );
$allowedUsers = ContentHandler::getContentText( $groupPage->getContent() );
$groupPage = null;

$groupPage = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromLinkTarget( $gt );
$content = $groupPage->getContent();
if ( !( $content instanceof TextContent ) ) {
// Non-text page, treat it as empty.
return Status::newGood( [] );
}

$allowedUsers = $content->getText();
$usersAccess = explode( "\n", $allowedUsers );
foreach ( $usersAccess as $userEntry ) {
$userItem = trim( $userEntry );
Expand Down Expand Up @@ -394,9 +402,9 @@ public static function onGetUserPermissionsErrors( $title, $user, $action, &$res
}

/**
* @param SpecialSearch $searchPage
* @param SpecialSearch $searchPage @phan-unused-param
* @param SearchResult $result
* @param string[] $terms
* @param string[] $terms @phan-unused-param
* @param string &$link
* @param string &$redirect
* @param string &$section
Expand Down Expand Up @@ -448,7 +456,7 @@ private static function getRestrictionForTitle( Title $title, User $user ) {
return self::$cache[$pageId];
}

$dbr = wfGetDB( DB_REPLICA );
$dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
try {
$row = $dbr->selectRow(
self::TABLE,
Expand Down Expand Up @@ -494,7 +502,7 @@ private static function updateRestrictionInDatabase( int $pageId, ?array $tagCon
$tagContentArray = FormatJson::encode( $tagContentArray );
}

$db = wfGetDB( DB_MASTER );
$db = MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase();
$index = [
self::C_PAGE => $pageId,
];
Expand Down

0 comments on commit 0934e6b

Please sign in to comment.