-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyvalue.body.php
188 lines (161 loc) · 4.93 KB
/
keyvalue.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
# MediaWiki KeyValue extension
#
# Copyright 2011 Pieter Iserbyt <[email protected]>
#
# Released under GNU LGPL
# Alert the user that this is not a valid entry point to MediaWiki
# if they try to access the special pages or extension file directly.
if ( !defined( 'MEDIAWIKI' ) ) {
echo ( 'This is not a valid entry point to MediaWiki.' );
exit ( 1 );
}
// TODO: an API module would be more suited to output this data
/**
* Special page implementation for the KeyValue extension
*/
class SpecialKeyValue extends SpecialPage {
private $isSysOp;
/**
* Constructor.
*/
public function __construct() {
parent::__construct( 'KeyValue' );
// This function has been deprecated in 1.16, but needed for earlier versions.
global $wgVersion;
if ( version_compare( $wgVersion, '1.16', '<' ) ) {
wfLoadExtensionMessages('KeyValue');
}
global $wgUser;
$this->isSysOp = in_array( 'sysop', $wgUser->getEffectiveGroups() );
}
/**
* execute implementation. Depending on url, will either
* do the main page, a specific category, or a csv file.
*
* @param $par The name of the subpage if present.
*/
public function execute( $par ) {
global $wgRequest, $wgOut;
$this->setHeaders();
if ($this->isSysOp && $wgRequest->getCheck('recreate')) {
$keyValue = KeyValue::getInstance();
$keyValue->dropTable();
$keyValue->createTable();
return $this->executeTableRecreatedPage();
}
# The $par parameter should be safe (comes via executePath
# and Title::getDBKey - Title::secureAndSplit
if ( !$par ) {
return $this->executeMainPage();
}
if ( $wgRequest->getCheck('csv') ) {
return $this->executeCategoryCsv($par);
}
return $this->executeCategoryPage($par);
}
/**
* Renders the main special page, renders a list of used
* categories.
*/
public function executeMainPage() {
global $wgOut;
$wgOut->addWikiText( wfMsg( 'available_categories' ) );
$keyValue = KeyValue::getInstance();
$categories = $keyValue->GetCategories();
foreach ( $categories as $category ) {
$line = '* [[';
$line .= $this->getTitle( $category->category );
$line .= '|';
$line .= $category->category;
$line .= ']] (';
$line .= $category->count;
$line .= ' ';
$line .= wfMsg( 'values' );
$line .= ')';
$wgOut->addWikiText( $line );
}
if ($this->isSysOp) {
$this->addRecreateTableButton();
}
$wgOut->setPageTitle( wfMsg( 'keyvalue_categories' ) );
}
/**
* Adds a "recreate tables" button to the output.
*/
public function addRecreateTableButton() {
global $wgOut;
$wgOut->addWikiText("\n");
$wgOut->addHTML('<form method="post"><input type="submit" name="recreate" value="Recreate KeyValue table" /></form>');
}
/**
* Renders the page shown after a table re-create.
*/
public function executeTableRecreatedPage() {
global $wgOut;
$wgOut->addWikiText( wfMsg( 'table_has_been_recreated' ) );
$wgOut->addHTML( '<p><a href="" alt="'.wfMsg( 'proceed' ).'">'.wfMsg( 'proceed' ).'<a/></p>');
$wgOut->setPageTitle( wfMsg( 'keyvalue_categories' ) );
}
/**
* Renders the page for a specific category.
*
* @param $category The category to render
*/
public function executeCategoryPage( $category ) {
global $wgOut;
$parentLink = substr( $this->getTitle( $category ), 0, - strlen( $category) - 1 );
$csvLink = $this->getTitle( $category )->getFullURL( array( "csv" => "true" ) );
$line = wfMsg( 'download_category_as' );
$line .= ' [';
$line .= $csvLink;
$line .= ' ';
$line .= wfMsg( 'csv_file' );
$line .= '].';
$wgOut->addWikiText( $line );
$keyValue = KeyValue::getInstance();
$kvis = $keyValue->getByCategory( $category );
foreach ( $kvis as $kvi ) {
$line = '* ';
$line .= $kvi->key;
$line .= ' = ';
$line .= $kvi->value;
$line .= " ''([[";
$line .= $kvi->title->getFullText();
$line .= "]])''";
$wgOut->addWikiText( $line );
}
$line = wfMsg( 'return' );
$line .= ' [[';
$line .= $parentLink;
$line .= ']]';
$wgOut->addWikiText( $line );
$wgOut->setPageTitle( wfMsg( 'keyvalues_for' ) . " \"$category\"" );
}
/**
* Returns a csv file that gets downloaded for the specified
* category.
*
* @param $category The category for the csv file
*/
public function executeCategoryCsv( $category ) {
global $wgOut, $wgRequest;
$keyValue = KeyValue::getInstance();
$kvis = $keyValue->getByCategory( $category, false );
header( "Pragma: no-cache" );
header( "Expires: 0" );
header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
header( "Cache-Control: public" );
header( "Content-Description: File Transfer" );
header( "Content-type: text/csv" );
header( "Content-Transfer-Encoding: binary" );
header( "Content-Disposition: attachment; filename=\"$category.csv\"" );
header( "Accept-Ranges: bytes" );
foreach ( $kvis as $kvi ) {
$key = str_replace(',', '', $kvi->key);
$value = str_replace(',', '', $kvi->value);
echo("$key,$value\n");
}
die;
}
}