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