-
Notifications
You must be signed in to change notification settings - Fork 2
/
SpecialListTransclusions.php
197 lines (163 loc) · 5.15 KB
/
SpecialListTransclusions.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
189
190
191
192
193
194
195
196
197
<?php
use MediaWiki\MediaWikiServices;
/**
* ListTransclusions extension, Special page
*
* @author Patrick Westerhoff [poke]
*/
class SpecialListTransclusions extends SpecialPage {
protected $dbr;
protected $target;
protected $opts;
/**
* constructor
*/
public function __construct() {
parent::__construct( 'ListTransclusions' );
}
/**
* Entry point for the special page.
*
* @param String $par Subpage of called special page
*/
public function execute( $par ) {
global $wgRequest, $wgOut, $wgUser;
$this->setHeaders();
$this->dbr = wfGetDB( DB_REPLICA );
$this->opts = new FormOptions();
$this->opts->add( 'target', '' );
$this->opts->fetchValuesFromRequest( $wgRequest );
if ( isset( $par ) ) {
$this->opts->setValue( 'target', $par );
}
$this->target = Title::newFromURL( $this->opts->getValue( 'target' ) );
// output :: showForm
$wgOut->addHTML( $this->generateForm() );
if ( !$this->target ) {
if ( trim( $this->opts->getValue( 'target' ) ) != '' ) {
$wgOut->addWikiMsg( 'listtransclusions-invalid' );
}
return;
}
// output :: titles
$targetText = $this->target->getPrefixedText();
$wgOut->setPageTitle( wfMessage( 'listtransclusions-title', $targetText )->text() );
$wgOut->setSubtitle( wfMessage( 'listtransclusions-backlink',
Linker::link( $this->target, htmlspecialchars( $targetText ), array(),
array( 'redirect' => 'no' ) ) )->text() );
// check target's existance
if ( !$this->target->exists() ) {
$wgOut->addWikiMsg( 'listtransclusions-notexist', $targetText );
return;
}
// receive transcluded pages
$targetId = $this->target->getArticleId();
$loadedTpls = $this->loadTemplates( $targetId );
$loadedImgs = $this->loadImages( $targetId );
// output
$wgOut->addWikiMsg( 'listtransclusions-header', $targetText );
$wgOut->addHTML( $this->generatePagesList( $loadedTpls, 'listtransclusions-tpls' ) );
$wgOut->addHTML( $this->generatePagesList( $loadedImgs, 'listtransclusions-imgs' ) );
$wgOut->addWikiMsg( 'listtransclusions-footer', $targetText );
}
/**
* Loads the used templates on the target page
*
* @param String $targetId Target page
* @return Array Loaded templates
*/
private function loadTemplates( $targetId ) {
$linksMigration = MediaWikiServices::getInstance()->getLinksMigration();
$queryInfo = $linksMigration->getQueryInfo( 'templatelinks' );
list( $nsField, $titleField ) = $linksMigration->getTitleFields( 'templatelinks' );
$pages = array();
$dbRes = $this->dbr->select(
$queryInfo['tables'],
$queryInfo['fields'],
array( 'tl_from' => $targetId ),
__METHOD__,
array(),
$queryInfo['joins']);
if ( $dbRes !== false ) {
foreach ( $dbRes as $row ) {
$pages[] = Title::makeTitle( $row->$nsField, $row->$titleField );
}
}
$dbRes->free();
return $pages;
}
/**
* Load the used images on the target page
*
* @param String $targetId Target page
* @return Array Loaded images
*/
private function loadImages( $targetId ) {
$pages = array();
$dbRes = $this->dbr->select(
array( 'imagelinks' ),
array( 'il_to' ),
array( 'il_from' => $targetId ),
__METHOD__ );
if ( $dbRes !== false ) {
foreach ( $dbRes as $row ) {
$pages[] = Title::makeTitle( NS_FILE, $row->il_to );
}
}
$dbRes->free();
return $pages;
}
/**
* Generates a list of the pages using the message.
* The function uses the message and the message with an appended '-no'.
*
* @param Array $pages Array of the loaded pages
* @param String $msg Message name to use
* @return String Generated HTML code
*/
private function generatePagesList( $pages, $msg ) {
if ( count( $pages ) <= 0 ) {
return wfMessage( $msg . '-no', $this->target->getPrefixedText() )->parseAsBlock();
}
usort( $pages, array( 'Title', 'compare' ) );
$t = wfMessage( $msg, $this->target->getPrefixedText() )->parseAsBlock();
$t .= "<ul>\n";
foreach ( $pages as $titleObj ) {
$t .= '<li>' . Linker::link( $titleObj ) . ' (';
$t .= Linker::link( $titleObj, wfMessage( 'hist' )->text(), array(), array( 'action' => 'history' ) );
$t .= ")</li>\n";
}
$t .= "</ul>\n";
return $t;
}
/**
* Generates the form used for this special page
*
* @return String Generated HTML code
*/
private function generateForm() {
global $wgScript, $wgTitle;
$this->opts->consumeValue( 'target' );
$target = $this->target ? $this->target->getPrefixedText() : '';
$f = Xml::openElement( 'form', array( 'action' => $wgScript ) );
$f .= Xml::fieldset( wfMessage( 'listtransclusions' )->text() );
// hidden values
$f .= Html::hidden( 'title', $wgTitle->getPrefixedText() );
foreach ( $this->opts->getUnconsumedValues() as $name => $value ) {
$f .= Html::hidden( $name, $value );
}
// form elements
$f .= Xml::inputLabel( wfMessage( 'listtransclusions-page' )->text(), 'target',
'mw-listtransclusions-target', 40, $target );
$f .= ' ';
$f .= Xml::submitButton( wfMessage( 'allpagessubmit' )->text() );
$f .= Xml::closeElement( 'fieldset' ) . Xml::closeElement( 'form' );
return $f . "\n";
}
/**
* Set special page group name.
*/
protected function getGroupName() {
return 'pagetools';
}
}