Skip to content

Commit

Permalink
Upgrade to PHP 8.1, use replicas instead of API for better performance (
Browse files Browse the repository at this point in the history
#23)

The following changes are made:
* Upgrade to PHP 8.1 and bump dependencies (platform set as PHP 8.2)
* Move classes to src/ and executables to bin/
* Write logs to /logs
* Add .editorconfig
* Rename ApiHelper to WikiRepository
* Remove unused files
* Add a 'nowrap' CSS rule around the updated timestamp on the index page

Bug: T362281
  • Loading branch information
MusikAnimal committed Apr 16, 2024
1 parent f089d62 commit 275c9b7
Show file tree
Hide file tree
Showing 23 changed files with 2,358 additions and 958 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# https://EditorConfig.org

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

# tab indentation
[*.{twig,json,yaml,xml}]
indent_style = tab
indent_size = 4
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.2
php-version: 8.2

- name: Validate composer.json and composer.lock
run: composer validate --strict
Expand Down
9 changes: 2 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
/config.inc.php
/tests/config.inc.php
/.idea
.DS_Store
composer.lock
config.ini
nopageviewdata.txt
/scripts
log*.txt
/vendor
/docs
coverage.xml
phpDocumentor.phar
/logs/*.txt
.phpunit.result.cache
12 changes: 0 additions & 12 deletions .travis.yml

This file was deleted.

33 changes: 0 additions & 33 deletions Logger.php

This file was deleted.

17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@ See [the tool's homepage](https://wikitech.wikimedia.org/wiki/Tool:Popular_Pages

##### How does the bot work?
* Fetches config from [on wiki config page](https://en.wikipedia.org/wiki/User:Community_Tech_bot/Popular_pages_config.json) (example for English Wikipedia).
* Runs on all of the projects listed in the config, compiling pageview statistics for the previous month.
* Runs on all projects listed in the config, compiling pageviews statistics for the previous month.
* Updates [the info page on wiki](https://en.wikipedia.org/wiki/User:Community_Tech_bot/Popular_pages) with the timestamp of page update.

##### App structure:
* **`checkReports.php`**: Starting point for a new bot run. Gets config info for all projects not already updated for past month and then passes it to `ReportUpdater`.
* **`ReportUpdater.php`**: The file that actually updates projects.
* **`ApiHelper.php`**: Contains all helper functions for dealing with the API and Database (bit of a misnomer).
* **`Logger.php`**: Responsible for logging updates to `log.txt`.
* **`generateReport.php`**: Script to manually regenerate a report for a single project.
* **`generateIndex.php`**: Script for generating the index page.
* **`bin/checkReports.php`**: Starting point for a new bot run. Gets config info for all projects not already updated for past month and then passes it to `ReportUpdater`.
* **`bin/generateReport.php`**: Script to manually regenerate a report for a single project.
* **`bin/generateIndex.php`**: Script for generating the index page.
* **`src/ReportUpdater.php`**: The file that actually updates projects.
* **`src/WikiRepository.php`**: Contains all helper functions for dealing with the API and Database (bit of a misnomer).
* **`src/PageviewsRepository.php`**: Contains all helper functions for dealing with the Pageviews API.
* **`src/Logger.php`**: Responsible for logging updates to the files in the `logs` directory.

##### Setting up a new wiki
* Make sure the translations for the language are in the /messages directory.
* Add the configuration for the project in `wikis.yml`. This indicates where the WikiProjects configuration and index pages live.
* Add the configuration for the project in `wikis.yaml`. This indicates where the WikiProjects configuration and index pages live.
* Add your WikiProjects configuration on the corresponding on-wiki JSON page.
* Add a new cron job for the wiki, such as `0 0 1 * * checkReports.php en.wikipedia`.
11 changes: 7 additions & 4 deletions checkReports.php → bin/checkReports.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
*
* Example usage:
* php checkReports.php en.wikipedia
*
* Pass in --dry as a second argument to print the output to stdout instead of editing the wiki.
*/

include_once 'vendor/autoload.php';

// Exit if not run from command-line
if ( PHP_SAPI !== 'cli' ) {
echo "This script should be ran from the command-line.";
Expand All @@ -19,11 +23,10 @@
die();
}

include_once 'vendor/autoload.php';

date_default_timezone_set( 'UTC' );

$api = new ApiHelper( $argv[1] );
$dryRun = ( $argv[2] ?? '' ) === '--dry';
$api = new WikiRepository( $argv[1], $dryRun );

wfLogToFile( 'Beginning new cycle', $argv[1] );

Expand All @@ -32,5 +35,5 @@
wfLogToFile( 'Number of projects pending update: ' . count( $notUpdated ), $argv[1] );

// Instantiate a new ReportUpdater with projects not updated yet
$updater = new ReportUpdater( $argv[1] );
$updater = new ReportUpdater( $argv[1], $dryRun );
$updater->updateReports( $notUpdated );
8 changes: 5 additions & 3 deletions generateIndex.php → bin/generateIndex.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
*
* Example usage:
* php generateIndex.php en.wikipedia
*
* Pass in --dry as a second argument to print the output to stdout instead of editing the wiki.
*/

include_once 'vendor/autoload.php';

// Exit if not run from command-line
if ( PHP_SAPI !== 'cli' ) {
echo "This script should be ran from the command-line.";
Expand All @@ -18,10 +22,8 @@
die();
}

include_once 'vendor/autoload.php';

date_default_timezone_set( 'UTC' );

// Instantiate the ReportUpdater and create the index page.
$updater = new ReportUpdater( $argv[1] );
$updater = new ReportUpdater( $argv[1], ( $argv[2] ?? '' ) === '--dry' );
$updater->updateIndex();
13 changes: 9 additions & 4 deletions generateReport.php → bin/generateReport.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
*
* Example usage:
* php generateReport.php en.wikipedia "Alternative education"
*
* Pass in --dry as a third argument to print the output to stdout instead of editing the wiki.
*/

include_once 'vendor/autoload.php';

// Exit if not run from command-line
if ( PHP_SAPI !== 'cli' ) {
echo "This script should be run from the command-line.\n";
Expand All @@ -25,10 +29,11 @@
die();
}

$dryRun = ( $argv[3] ?? '' ) === '--dry';

date_default_timezone_set( 'UTC' );
include_once 'vendor/autoload.php';

$api = new ApiHelper( $argv[1] );
$api = new WikiRepository( $argv[1], $dryRun );

wfLogToFile(
'Running script to generate report for project ' . $argv[2] . ' on ' . $argv[1],
Expand All @@ -37,10 +42,10 @@

$projectConfig = $api->getProject( $argv[2] );

if ( null === $projectConfig ) {
if ( $projectConfig === null ) {
echo "Project configuration not found.\n";
} else {
// Instantiate a new ReportUpdater with the specified project
$updater = new ReportUpdater( $argv[1] );
$updater = new ReportUpdater( $argv[1], $dryRun );
$updater->updateReports( $projectConfig );
}
30 changes: 17 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,37 @@
"license": "MIT",
"homepage": "https://wikitech.wikimedia.org/wiki/Tool:Popular_Pages",
"require": {
"php" : "^7.2",
"php" : "^8.1",
"addwiki/mediawiki-api-base": "^2.3",
"krinkle/intuition": "^1.2.0",
"twig/twig": "^1.0",
"symfony/yaml": "^3.4",
"krinkle/intuition": "^1.0",
"twig/twig": "^3.0",
"symfony/yaml": "^5.0",
"ext-json": "*",
"ext-mysqli": "*",
"ext-mysqli": "*",
"caseyamcl/guzzle_retry_middleware": "^2.2"
},
},
"require-dev": {
"mediawiki/mediawiki-codesniffer": "^0.7",
"jakub-onderka/php-parallel-lint": "^0.9",
"phpunit/phpunit": "^4.8"
"mediawiki/mediawiki-codesniffer": "^36.0",
"phpunit/phpunit": "^9.5"
},
"autoload": {
"files": [
"Logger.php",
"ApiHelper.php",
"ReportUpdater.php",
"PageviewsRepository.php"
"src/Logger.php",
"src/WikiRepository.php",
"src/ReportUpdater.php",
"src/PageviewsRepository.php"
]
},
"autoload-dev": {
"psr-4": {
"Wikimedia\\PopularPages\\Tests\\": "tests/"
}
},
"config": {
"platform": {
"php": "8.2"
}
},
"scripts": {
"test": [
"composer validate --ansi",
Expand Down
Loading

0 comments on commit 275c9b7

Please sign in to comment.