-
Notifications
You must be signed in to change notification settings - Fork 0
/
DynamicPageListEngine.body.php
148 lines (122 loc) · 3.46 KB
/
DynamicPageListEngine.body.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
141
142
143
144
145
146
147
<?php
if ( !defined( 'MEDIAWIKI' ) ) {
echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" );
die( 1 );
}
/**
* @brief Class DynamicPageListEngine.
*
* @file
*
* @ingroup Extensions
* @ingroup Extensions-DynamicPageListEngine
*
* @author [RV1971](http://www.mediawiki.org/wiki/User:RV1971)
*
*/
/**
* @brief Dynamic page list backend.
*
* You can use this class as a data source for your own extensions.
*
* @ingroup Extensions
* @ingroup Extensions-DynamicPageListEngine
*/
class DynamicPageListEngine implements Countable {
/* == private data members == */
/// Array of parameters given to the constructor.
private $params_ = array();
/// Array of objects derived from DpleFeature.
private $features_ = array();
/// Mapping of result converter functions to the feature classes they are defined in.
private $converters_ = array();
private $query_; ///< DpleQuery object.
/* == magic methods == */
/**
* @brief Constructor. Evaluates parameters and fetches records
* from the database.
*
* @param array $params Associative array of parameters.
*/
public function __construct( array $params ) {
global $wgDpleFeatures;
global $wgDisableCounters;
$this->params_ = $params;
/** Construct all enabled features, regardless of whether they
* are relevant for a particular request. This simplifies
* feature development since the features do not need to
* provide information of what they are relevant for. */
foreach ( $wgDpleFeatures as $class ) {
$this->features_[$class] = new $class( $this->params_,
$this->features_ );
/** Register all result converters. */
foreach ( $this->features_[$class]->getResultConverters()
as $method ) {
$this->converters_[$method] = $class;
}
}
/** Construct the query. */
$this->query_ = new DpleQuery( 'page',
array( 'page_namespace', 'page_title', 'page_is_redirect',
'page_len' ) );
if ( !$wgDisableCounters ) {
$this->query_->addVars( 'page_counter' );
}
/** Let all features modify @ref $query_. */
foreach ( $this->features_ as $feature ) {
$feature->modifyQuery( $this->query_ );
}
/** Execute the query. */
$this->query_->execute();
wfDebug( __METHOD__ . ': ' . count( $this ) . " records\n" );
}
/**
* @brief Implementation of Countable::count.
*
* return int Number of result rows, or 0 if the query has not
* yet been executed.
*/
public function count() {
return count( $this->query_ );
}
/* == accessors == */
/// Get @ref $features_.
public function &getFeatures() {
return $this->features_;
}
/** @brief Get a specific feature.
*
* @param string $class The feature class.
*
* @return A reference to the feature object, if there is one,
* else NULL.
*/
public function &getFeature( $class ) {
static $unset;
if ( isset( $this->features_[$class] ) ) {
return $this->features_[$class];
} else {
return $unset;
}
}
/// Get @ref $query_.
public function getQuery() {
return $this->query_;
}
/**
* @brief Get the query result.
*
* To access the ResultWrapper object without any conversion, use
* getQuery()->getResult().
*
* @param string $method Method to convert the query result.
*
* @return Return value of the converter applied to the query
* result.
*/
public function getResult( $method = 'toTitles' ) {
return call_user_func(
array( $this->features_[$this->converters_[$method]], $method ),
$this->query_->getResult() );
}
}