/
Scribunto_LuaDynamicPageListEngineLibrary.php
140 lines (120 loc) · 3.61 KB
/
Scribunto_LuaDynamicPageListEngineLibrary.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
if ( !defined( 'MEDIAWIKI' ) ) {
echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" );
die( 1 );
}
/**
* @brief Class LuaDynamicPageListEngineLibrary.
*
* @file
*
* @ingroup Extensions
* @ingroup Extensions-DynamicPageListEngine
*
* @author [RV1971](http://www.mediawiki.org/wiki/User:RV1971)
*
*/
/**
* @brief
* [Scribunto](http://www.mediawiki.org/wiki/Extension:Scribunto) Lua
* interface to DynamicPageListEngine.
*
* @ingroup Extensions
* @ingroup Extensions-DynamicPageListEngine
*/
class Scribunto_LuaDynamicPageListEngineLibrary
extends Scribunto_LuaLibraryBase {
/* == static methods == */
/**
* @brief [ScribuntoExternalLibraries]
* (https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Library) hook.
*
* Register this library.
*
* @param Scribunto_LuaEngine $engine Scribunto engine.
*
* @param array $extraLibraries Libraries to register.
*
* @return boolean
*/
public static function onScribuntoExternalLibraries( $engine,
array &$extraLibraries ) {
$extraLibraries['mw.ext.dpl'] =
'Scribunto_LuaDynamicPageListEngineLibrary';
return true;
}
/* == magic methods == */
/**
* @brief Constructor.
*
* @param Scribunto_LuaEngine $engine Scribunto engine.
*/
public function __construct( $engine ) {
parent::__construct( $engine );
}
/* == special functions == */
/// Register this library.
public function register() {
$lib = array(
'getFullpagenames' => array( $this, 'getFullpagenames' ),
'getPagenames' => array( $this, 'getPagenames' ),
'getPages' => array( $this, 'getPages' )
);
$this->getEngine()->registerInterface(
__DIR__ . '/DynamicPageListEngine.lua',
$lib, array() );
}
/* == Functions to be called from Lua == */
/**
* @brief Get an array of pages from the database.
*
* @param array $params Array of parameters.
*
* @param string $method Method to transform the result.
* @return array Result of $method applied to the query
* result. For the default value `toArrays`, return
* numerically-indexed array of associative arrays, each of which
* represents a page. See DpleFeatureResults::toArrays() for
* details.
* @throws Scribunto_LuaError
*/
public function getPages( $params, $method = 'toArrays' ) {
/** Increment the [expensive function count]
* (https://www.mediawiki.org/wiki/Manual:$wgExpensiveParserFunctionLimit). */
if ( !$this->getParser()->incrementExpensiveFunctionCount() ) {
throw new Scribunto_LuaError( wfMessage(
'dple-error-too-many-expensive-functions' )->text() );
}
/** Add the page to the [tracking category]
* (http://www.mediawiki.org/wiki/Help:Tracking_categories)
* `dple-tracking-category`. */
$this->getParser()->addTrackingCategory( 'dple-tracking-category' );
$dpl = new DynamicPageListEngine( $params );
$pages = $dpl->getResult( $method );
/* Renumber the records starting with 1, to match the Lua
* convention. */
return array( $pages
? array_combine( range( 1, count( $pages ) ), $pages )
: null );
}
/**
* @brief Get an array of full page names from the database.
*
* @param array $params Array of parameters.
*
* @return array Numerically-indexed array of full page names.
*/
public function getFullpagenames( $params ) {
return $this->getPages( $params, 'toFullpagenames' );
}
/**
* @brief Get an array of page names from the database.
*
* @param array $params Array of parameters.
*
* @return array Numerically-indexed array of page names.
*/
public function getPagenames( $params ) {
return $this->getPages( $params, 'toPagenames' );
}
}