1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
<?php
/**
* Code for automatic creation of categories.
*
* @file
* @author Robert Leverington
* @author Robin Pepermans
* @author Niklas Laxström
* @author Brian Wolff
* @author Purodha Blissenbach
* @author Sam Reed
* @author Siebrand Mazeland
* @license GPL-2.0-or-later
*/
use MediaWiki\MediaWikiServices;
/**
* Class for automatic creation of Babel category pages.
*/
class BabelAutoCreate {
/**
* @var User|false
*/
protected static $user = false;
public static function onUserGetReservedNames( &$names ) {
$names[] = 'msg:babel-autocreate-user';
return true;
}
/**
* Create category.
*
* @param string $category Name of category to create.
* @param string $code Code of language that the category is for.
* @param string|null $level Level that the category is for.
*/
public static function create( $category, $code, $level = null ) {
$category = strip_tags( $category );
$title = Title::makeTitleSafe( NS_CATEGORY, $category );
if ( $title === null || $title->exists() ) {
return;
}
DeferredUpdates::addCallableUpdate( function () use ( $code, $level, $title ) {
global $wgLanguageCode;
$language = BabelLanguageCodes::getName( $code, $wgLanguageCode );
$params = [ $language, $code ];
if ( $level === null ) {
$text = wfMessage( 'babel-autocreate-text-main', $params )->inContentLanguage()->text();
} else {
array_unshift( $params, $level );
$text = wfMessage( 'babel-autocreate-text-levels', $params )->inContentLanguage()->text();
}
$user = self::user();
# Do not add a message if the username is invalid or if the account that adds it, is blocked
if ( !$user || $user->isBlocked() ) {
return;
}
if ( !MediaWikiServices::getInstance()->getPermissionManager()
->quickUserCan( 'create', $user, $title ) ) {
return; # The Babel AutoCreate account is not allowed to create the page
}
$url = wfMessage( 'babel-url' )->inContentLanguage()->plain();
$article = new WikiPage( $title );
$article->doEditContent(
ContentHandler::makeContent( $text, $title ),
wfMessage( 'babel-autocreate-reason', $url )->inContentLanguage()->text(),
EDIT_FORCE_BOT,
false,
$user
);
} );
}
/**
* Get user object.
*
* @return User User object for autocreate user.
*/
public static function user() {
if ( !self::$user ) {
$userName = wfMessage( 'babel-autocreate-user' )->inContentLanguage()->plain();
self::$user = User::newSystemUser( $userName, [ 'steal' => true ] );
}
return self::$user;
}
}
|