MediaWiki master
updateCredits.php
Go to the documentation of this file.
1<?php
8
9use Collator;
10
11// @codeCoverageIgnoreStart
12require_once __DIR__ . '/Maintenance.php';
13// @codeCoverageIgnoreEnd
14
25
26 private const CREDITS = MW_INSTALL_PATH . '/CREDITS';
27 private const START_CONTRIBUTORS = '<!-- BEGIN CONTRIBUTOR LIST -->';
28 private const END_CONTRIBUTORS = '<!-- END CONTRIBUTOR LIST -->';
29
30 public function __construct() {
31 parent::__construct();
32 $this->addDescription( 'Update the CREDITS list by merging in the list of git commit authors' );
33 }
34
35 public function execute() {
36 $inHeader = true;
37 $inFooter = false;
38 $header = [];
39 $contributors = [];
40 $footer = [];
41
42 if ( !file_exists( self::CREDITS ) ) {
43 $this->fatalError( 'No CREDITS file found.' );
44 }
45
46 $lines = explode( "\n", file_get_contents( self::CREDITS ) );
47 foreach ( $lines as $line ) {
48 if ( $inHeader ) {
49 $header[] = $line;
50 $inHeader = $line !== self::START_CONTRIBUTORS;
51 } elseif ( $inFooter ) {
52 $footer[] = $line;
53 } elseif ( $line === self::END_CONTRIBUTORS ) {
54 $inFooter = true;
55 $footer[] = $line;
56 } else {
57 $name = substr( $line, 2 );
58 $contributors[$name] = true;
59 }
60 }
61 unset( $lines );
62
63 $lines = explode( "\n", (string)shell_exec( 'git log --format="%aN"' ) );
64 foreach ( $lines as $line ) {
65 if ( !$line ) {
66 continue;
67 }
68 if ( str_starts_with( $line, '[BOT]' ) ) {
69 continue;
70 }
71 $contributors[$line] = true;
72 }
73
74 $contributors = array_keys( $contributors );
75 $collator = Collator::create( 'root' );
76 $collator->setAttribute( Collator::NUMERIC_COLLATION, Collator::ON );
77 $collator->sort( $contributors );
78 array_walk( $contributors, static function ( &$v, $k ) {
79 $v = "* {$v}";
80 } );
81
82 file_put_contents(
83 self::CREDITS,
84 implode( "\n", array_merge( $header, $contributors, $footer ) )
85 );
86
87 $this->output( "Done! CREDITS file updated\n" );
88 }
89}
90
91// @codeCoverageIgnoreStart
92$maintClass = UpdateCredits::class;
93require_once RUN_MAINTENANCE_IF_MAIN;
94// @codeCoverageIgnoreEnd
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addDescription( $text)
Set the description text.
Update the CREDITS list by merging in the list of git commit authors.