-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppropediaLua.php
121 lines (112 loc) · 3.18 KB
/
AppropediaLua.php
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
use MediaWiki\MediaWikiServices;
/**
* This class queries the database for the Appropedia Lua library
*/
class AppropediaLua extends Scribunto_LuaLibraryBase {
public static function onScribuntoExternalLibraries( string $engine, array &$extraLibraries ) {
$extraLibraries['appropedia'] = self::class;
}
public function register() {
$this->getEngine()->registerInterface( __DIR__ . '/AppropediaLua.lua', [
'emailDomain' => [ $this, 'emailDomain' ],
'pageExists' => [ $this, 'pageExists' ],
'pageCategories' => [ $this, 'pageCategories' ],
'fileUses' => [ $this, 'fileUses' ],
] );
}
/**
* Get the domain of the email of a user
*
* @param string $name User name
* @return string Email domain
*/
public function emailDomain( $name ) {
$domain = '';
$user = MediaWikiServices::getInstance()->getUserFactory()->newFromName( $name );
if ( $user ) {
$email = $user->getEmail();
if ( $email ) {
$domain = substr( $email, strpos( $email, '@' ) + 1 );
}
}
return [ $domain ];
}
/**
* Check if the given page exists
*
* @param string $page Page name
* @return array Lua table
*/
public function pageExists( $page ) {
$title = Title::newFromText( $page );
if ( !$title ) {
return [ false ];
}
if ( $title->isKnown() ) {
return [ true ];
}
return [ false ];
}
/**
* Get the categories of a page
*
* @param string $page Page name
* @return array Lua table
*/
public function pageCategories( $page ) {
$title = Title::newFromText( $page );
$id = $title->getArticleID();
$services = MediaWikiServices::getInstance();
$provider = $services->getConnectionProvider();
$dbr = $provider->getReplicaDatabase();
$results = $dbr->newSelectQueryBuilder()
->select( 'cl_to' )
->from( 'categorylinks' )
->where( [ 'cl_from' => $id ] )
->fetchResultSet();
$categories = [];
foreach ( $results as $result ) {
$category = $result->cl_to;
$category = str_replace( '_', ' ', $category );
$categories[] = $category;
}
return [ self::toLuaTable( $categories ) ];
}
/**
* Get the number of uses of the given file
*
* @param string $file File name
* @return array Lua table
*/
public function fileUses( $file ) {
$title = Title::newFromText( $file, NS_FILE );
$services = MediaWikiServices::getInstance();
$provider = $services->getConnectionProvider();
$dbr = $provider->getReplicaDatabase();
$queryBuilder = $dbr->newSelectQueryBuilder();
$count = $queryBuilder->select( 'COUNT(*)' )->from( 'imagelinks' )->where( [ 'il_to' => $file ] )->fetchField();
$count = intval( $count );
return [ $count ];
}
/**
* Helper method to convert an array to a viable Lua table
*
* The resulting table has its numerical indices start with 1
* If $array is not an array, it is simply returned
*
* @param mixed $array
* @return mixed Lua object
* @see https://github.com/SemanticMediaWiki/SemanticScribunto/blob/master/src/ScribuntoLuaLibrary.php
*/
private static function toLuaTable( $array ) {
if ( is_array( $array ) ) {
foreach ( $array as $key => $value ) {
$array[ $key ] = self::toLuaTable( $value );
}
array_unshift( $array, '' );
unset( $array[0] );
}
return $array;
}
}