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