Skip to content

Commit

Permalink
Laravel 10 Upgrade (#730)
Browse files Browse the repository at this point in the history
* wip composer.json

* WIP - departure to integrate
https://github.com/googleapis/google-cloud-php-errorreporting

* replace `gluedev/laravel-stackdriver`

* configure `absszero/laravel-stackdriver-error-reporting`

see #731

* remove call to `registerPolicies`

see https://laravel.com/docs/10.x/upgrade#register-policies

* schedule redis stale tag pruning

* use `$casts` property

see https://laravel.com/docs/10.x/upgrade#model-dates-property

* run `composer update --ignore-platform-reqs`

Composer version 2.6.5 2023-10-06 10:11:52

* rename `$routeMiddleware`

see https://laravel.com/docs/10.x/upgrade#middleware-aliases

* Apply curated changes from
laravel/laravel@9.x...10.x

* test: php 8.2

* update php version in composer

* remove custom stackdriver repo

* upgrade to phpunit 10

* migrate phpunit configuration

* re-add "absszero/laravel-stackdriver-error-reporting"

* replace CORS Handler

* refactor data providers

* refactor `$this->dispatch` & `dispatchNow`

* fix test class name

* refactor some tests

* update phpunit to 10.5, run composer update

* migrate ElasticSearchIndexDeleteTest.php

* fix deprecated string interpolation

* tests: replace job to queue dispatching with calling handle()

* implement ForceSearchIndex::uniqueId

* move to `dispatch` as mostly recommended example
  • Loading branch information
deer-wmde authored Feb 26, 2024
1 parent e59eda7 commit 00c5872
Show file tree
Hide file tree
Showing 49 changed files with 1,038 additions and 1,281 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/composer.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Install dependencies
uses: php-actions/composer@v6
with:
php_version: 8.1
php_version: 8.2
command: install

- name: Copy example .env file
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Homestead.yaml
.env
.php_cs.cache
.phpunit.result.cache
.phpunit.cache
/storage/*.key
/public/storage
_ide_helper.php
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# api

## 10x.0.0 - 26 February 2024
- Update Laravel to v10.10 - T341797
- schedule redis stale tag pruning
- refactor: `dispatchNow` was removed
- Update to PHPUnit v10.5
- fix: Implemented ForceSearchIndex::uniqueId T358106

## 9x.1.0 - 16 Februrary 2024
- Switching to PHP 8.2

Expand Down
4 changes: 2 additions & 2 deletions app/Console/Commands/Wiki/SetSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function handle()
'wiki_id' => $wiki->id,
'name' => $settingKey,
])->delete();
$this->line("Deleted setting ${settingKey} for wiki id ${wikiId}");
$this->line("Deleted setting {$settingKey} for wiki id {$wikiId}");
return;
}

Expand All @@ -54,7 +54,7 @@ public function handle()
'value' => $settingValue,
]
);
$this->line("Set setting ${settingKey} to ${settingValue} for wiki id ${wikiId}");
$this->line("Set setting {$settingKey} to {$settingValue} for wiki id {$wikiId}");

return 0;
}
Expand Down
10 changes: 5 additions & 5 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ class Kernel extends ConsoleKernel
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
protected function schedule(Schedule $schedule): void
{
// Make sure that the DB and QS pools are always populated somewhat.
// This will create at most 1 new entry for each per minute...
Expand All @@ -48,6 +47,9 @@ protected function schedule(Schedule $schedule)
// Schedule site stat updates for each wiki and platform-summary
$schedule->command('schedule:stats')->dailyAt('7:00');

// https://laravel.com/docs/10.x/upgrade#redis-cache-tags
$schedule->command('cache:prune-stale-tags')->hourly();

$schedule->job(new PollForMediaWikiJobsJob)->everyFifteenMinutes();

$schedule->job(new UpdateWikiSiteStatsJob)->dailyAt('19:00');
Expand All @@ -57,10 +59,8 @@ protected function schedule(Schedule $schedule)

/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
protected function commands(): void
{
$this->load(__DIR__.'/Commands');
$this->load(__DIR__.'/Commands/User');
Expand Down
22 changes: 3 additions & 19 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,8 @@

class Handler extends ExceptionHandler
{

/**
* A list of exception types with their corresponding custom log levels.
*
* @var array<class-string<\Throwable>, \Psr\Log\LogLevel::*>
*/
protected $levels = [
//
];

/**
* A list of the exception types that are not reported.
*
* @var array<int, class-string<\Throwable>>
*/
protected $dontReport = [];

/**
* A list of the inputs that are never flashed for validation exceptions.
* The list of the inputs that are never flashed to the session on validation exceptions.
*
* @var array
*/
Expand All @@ -37,10 +20,11 @@ class Handler extends ExceptionHandler
/**
* Register the exception handling callbacks for the application.
*/
public function register()
public function register(): void
{
$this->reportable(function (Throwable $e) {
(new \Absszero\ErrorReporting)->report($e);
});
}
}

3 changes: 1 addition & 2 deletions app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;

class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
use AuthorizesRequests, ValidatesRequests;
}
6 changes: 3 additions & 3 deletions app/Http/Controllers/Sandbox/SandboxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ public function create(Request $request): \Illuminate\Http\Response
// For now only schedule this job for 1 dataSet type
// As the extension API is dumb currently
if ($dataSet === 'library') {
$this->dispatch(new MediawikiSandboxLoadData($domain, $dataSet));
dispatch(new MediawikiSandboxLoadData($domain, $dataSet));
}

// opportunistic dispatching of jobs to make sure storage pools are topped up
$this->dispatch(new ProvisionWikiDbJob(null, null, 10));
$this->dispatch(new ProvisionQueryserviceNamespaceJob(null, 10));
dispatch(new ProvisionWikiDbJob(null, null, 10));
dispatch(new ProvisionQueryserviceNamespaceJob(null, 10));

$res['success'] = true;
$res['message'] = 'Success!';
Expand Down
12 changes: 6 additions & 6 deletions app/Http/Controllers/WikiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function create(Request $request): \Illuminate\Http\Response
$maxWikis = config('wbstack.wiki_max_per_user');

if ( config('wbstack.wiki_max_per_user') !== false && $numWikis > config('wbstack.wiki_max_per_user')) {
abort(403, "Too many wikis. Your new total of {$numWikis} would exceed the limit of ${maxWikis} per user.");
abort(403, "Too many wikis. Your new total of {$numWikis} would exceed the limit of {$maxWikis} per user.");
}

$wiki = Wiki::create([
Expand Down Expand Up @@ -114,22 +114,22 @@ public function create(Request $request): \Illuminate\Http\Response
]);

// TODO maybe always make these run in a certain order..?
$this->dispatch(new MediawikiInit($wiki->domain, $request->input('username'), $user->email));
dispatch(new MediawikiInit($wiki->domain, $request->input('username'), $user->email));
// Only dispatch a job to add a k8s ingress IF we are using a custom domain...
if (! $isSubdomain) {
$this->dispatch(new KubernetesIngressCreate($wiki->id, $wiki->domain));
dispatch(new KubernetesIngressCreate($wiki->id, $wiki->domain));
}
});


// dispatch elasticsearch init job to enable the feature
if ( Config::get('wbstack.elasticsearch_enabled_by_default') ) {
$this->dispatch(new ElasticSearchIndexInit($wiki->id));
dispatch(new ElasticSearchIndexInit($wiki->id));
}

// opportunistic dispatching of jobs to make sure storage pools are topped up
$this->dispatch(new ProvisionWikiDbJob(null, null, 10));
$this->dispatch(new ProvisionQueryserviceNamespaceJob(null, 10));
dispatch(new ProvisionWikiDbJob(null, null, 10));
dispatch(new ProvisionQueryserviceNamespaceJob(null, 10));

$res['success'] = true;
$res['message'] = 'Success!';
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Kernel extends HttpKernel
* @var array
*/
protected $middleware = [
\Fruitcake\Cors\HandleCors::class,
\Illuminate\Http\Middleware\HandleCors::class,
\Illuminate\Http\Middleware\TrustProxies::class,
];

Expand All @@ -32,7 +32,7 @@ class Kernel extends HttpKernel
*
* @var array
*/
protected $routeMiddleware = [
protected $middlewareAliases = [
// Came with Laravel
'auth' => \App\Http\Middleware\Authenticate::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
Expand Down
15 changes: 9 additions & 6 deletions app/Http/Middleware/RedirectIfAuthenticated.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\Response;

/**
* The purpose of RedirectIfAuthenticated is to keep an already authenticated user
Expand All @@ -17,15 +19,16 @@ class RedirectIfAuthenticated
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
public function handle(Request $request, Closure $next, string ...$guards): Response
{
if (Auth::guard($guard)->check()) {
return response()->json('Endpoint not needed', 400);
//return redirect('/home');
}
$guards = empty($guards) ? [null] : $guards;

foreach ($guards as $guard) {
if (Auth::guard($guard)->check()) {
return response()->json('Endpoint not needed', 400);
}
}
return $next($request);
}
}
11 changes: 9 additions & 2 deletions app/Jobs/CirrusSearch/ForceSearchIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*
* Example:
*
* php artisan job:dispatchNow CirrusSearch\\ForceSearchIndex id 1 0 1000
* php artisan job:dispatch CirrusSearch\\ForceSearchIndex id 1 0 1000
*/
class ForceSearchIndex extends CirrusSearchJob
{
Expand All @@ -25,6 +25,13 @@ public function __construct( string $selectCol, $selectValue, int $fromId, int $
$this->toId = $toId;
parent::__construct($wiki->id);
}

public function uniqueId() {
return parent::uniqueId() . '_'
. $this->fromId . '_'
. $this->toId;
}

public function fromId(): int {
return $this->fromId;
}
Expand Down Expand Up @@ -59,7 +66,7 @@ public function handleResponse( string $rawResponse, $error ) : void {

if ( count($successMatches) === 2 && is_numeric($successMatches[1][0]) ) {
$numIndexedPages = intVal($successMatches[1][0]);
Log::info(__METHOD__ . ": Finished batch! Indexed ${numIndexedPages} pages. From id {$this->fromId} to {$this->toId}");
Log::info(__METHOD__ . ": Finished batch! Indexed {$numIndexedPages} pages. From id {$this->fromId} to {$this->toId}");
} else {
dd($successMatches);
Log::error(__METHOD__ . ": Job finished but did not contain the expected output.");
Expand Down
4 changes: 2 additions & 2 deletions app/Jobs/CirrusSearch/QueueSearchIndexBatches.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* Example:
*
* php artisan job:dispatchNow CirrusSearch\\QueueSearchIndexBatches 1
* php artisan job:dispatch CirrusSearch\\QueueSearchIndexBatches 1
*/
class QueueSearchIndexBatches extends CirrusSearchJob
{
Expand Down Expand Up @@ -84,7 +84,7 @@ public function handleResponse( string $rawResponse, $error ) : void {
if ( !empty($batches) ) {
// todo rewrite as batch
foreach ($batches as $job) {
$this->dispatch($job);
dispatch($job);
}

} else {
Expand Down
2 changes: 1 addition & 1 deletion app/Jobs/CreateEmptyWikiDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* To be deleted after migration
*
* Example usage that will always provision a new DB:
* php artisan job:dispatchNow CreateEmptyWikiDb
* php artisan job:dispatch CreateEmptyWikiDb
*/
class CreateEmptyWikiDb extends Job
{
Expand Down
2 changes: 1 addition & 1 deletion app/Jobs/DeleteWikiFinalizeJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function handle( HttpRequest $request )
continue;
}

$this->fail(new \RuntimeException("WikiDb for ${$wiki->id} still exists"));
$this->fail(new \RuntimeException("WikiDb for {$wiki->id} still exists"));
}
return;
}
Expand Down
2 changes: 1 addition & 1 deletion app/Jobs/KubernetesIngressCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

/**
* This can be run with for example:
* php artisan job:dispatchNow KubernetesIngressCreate 999999999 wiki.addshore.com
* php artisan job:dispatch KubernetesIngressCreate 999999999 wiki.addshore.com
*
* If you need to cleanup a test run of this you need to remove the ingress and the related secret
*/
Expand Down
2 changes: 1 addition & 1 deletion app/Jobs/PlatformStatsSummaryJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* TODO The stats from wiki statistics doesn't add up https://github.com/wbstack/mediawiki/issues/59
* We need to fix that before we can get total pages/edits/users
*
* Example: php artisan job:dispatchNow PlatformStatsSummaryJob
* Example: php artisan job:dispatch PlatformStatsSummaryJob
*/
class PlatformStatsSummaryJob extends Job
{
Expand Down
2 changes: 1 addition & 1 deletion app/Jobs/ProvisionQueryserviceNamespaceJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

/**
* Example usage
* php artisan job:dispatchNow ProvisionQueryserviceNamespaceJob
* php artisan job:dispatch ProvisionQueryserviceNamespaceJob
*/
class ProvisionQueryserviceNamespaceJob extends Job
{
Expand Down
2 changes: 1 addition & 1 deletion app/Jobs/ProvisionWikiDbJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

/**
* Example usage that will always provision a new DB:
* php artisan job:dispatchNow ProvisionWikiDbJob
* php artisan job:dispatch ProvisionWikiDbJob
*/
class ProvisionWikiDbJob extends Job
{
Expand Down
4 changes: 2 additions & 2 deletions app/Jobs/SetWikiLogo.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

/**
* This can be run with the artisan job command, for example:
* php artisan job:dispatchNow SetWikiLogo domain wiki.addshore.com /path/to/logo.png
* php artisan job:dispatchNow SetWikiLogo id 1234 /path/to/logo.png
* php artisan job:dispatch SetWikiLogo domain wiki.addshore.com /path/to/logo.png
* php artisan job:dispatch SetWikiLogo id 1234 /path/to/logo.png
*
* NOTE: This job needs to be run as the correct user if run via artisan (instead of via the UI)
*/
Expand Down
2 changes: 1 addition & 1 deletion app/Jobs/SiteStatsUpdateJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* Job that updates site_stats table in mediawiki by calling initSiteStats.php
*
* Example: php artisan job:dispatchNow SiteStatsUpdateJob
* Example: php artisan job:dispatch SiteStatsUpdateJob
*/
class SiteStatsUpdateJob extends Job
{
Expand Down
8 changes: 2 additions & 6 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,16 @@ class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
public function register(): void
{
$this->app->bind(HttpRequest::class, CurlRequest::class);
}

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
public function boot(): void
{
Queue::failing(function (JobFailed $event) {
$name = data_get($event->job->payload(), 'data.commandName');
Expand Down
Loading

0 comments on commit 00c5872

Please sign in to comment.