-
Notifications
You must be signed in to change notification settings - Fork 14
/
linktitles-cli.php
executable file
·219 lines (207 loc) · 6.22 KB
/
linktitles-cli.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env php
<?php
/**
* LinkTitles command line interface (CLI)/maintenance script
*
* Copyright 2012-2024 Daniel Kraus <bovender@bovender.de> @bovender
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
namespace LinkTitles;
// Attempt to include the maintenance base class from:
// $wgScriptPath/maintenance/Maintenance.php
// Our script is normally located at:
// $wgScriptPath/extensions/LinkTitles/LinkTitles_Maintenance.php
$maintenanceScript = __DIR__ . "/../../maintenance/Maintenance.php";
if ( file_exists( $maintenanceScript ) ) {
require_once $maintenanceScript;
}
else
{
// Did not find the script where we expected it (maybe because we are a
// symlinked file -- __DIR resolves symbolic links).
$maintenanceScript = __DIR__ . "/Maintenance.php";
if ( file_exists( $maintenanceScript ) ) {
require_once $maintenanceScript;
}
else
{
die("FATAL: Could not locate Maintenance.php.\n" .
"You may want to create a symbolic link named Maintenance.php in this directory\n" .
"which points to <YOUR_MEDIAWIKI_ROOT_IN_FILESYSTEM>/extensions/Maintenance.php\n" .
"Ex.: ln -s /var/www/wiki/maintenance/Maintenance.php\n\n");
}
};
require_once( __DIR__ . "/includes/Extension.php" );
/**
* Core class of the maintanance script.
* @note Note that the execution of maintenance scripts is prohibited for
* an Apache web server due to a `.htaccess` file that declares `deny from
* all`. Other webservers may exhibit different behavior. Be aware that
* anybody who is able to execute this script may place a high load on the
* server.
* @ingroup batch
*/
class Cli extends \Maintenance {
/**
* Constructor.
*/
public function __construct() {
parent::__construct();
$this->addDescription("Iterates over wiki pages and automatically adds links to other pages.");
$this->addOption(
"start",
"Set start index.",
false, // not required
true, // requires argument
"s"
);
$this->addOption(
"page",
"page name to process",
false, // not required
true, // requires argument
"p"
);
$this->addOption(
"verbose",
"print detailed progress information",
false, // not required
false, // does not require an argument
"v"
);
// TODO: Add back logging options.
// TODO: Add configuration options.
// $this->addOption(
// "log",
// "enables logging to console",
// false, // not required
// false, // requires no argument
// "l"
// );
// $this->addOption(
// "debug",
// "enables debug logging to console",
// false, // not required
// false // requires no argument
// );
}
/*
* Main function of the maintenance script.
* Will iterate over all pages in the wiki (starting at a certain index,
* if the `--start` option is given) and call LinkTitles::processPage() for
* each page.
*/
public function execute() {
// if ($this->hasOption('log'))
// {
// Extension::$ltConsoleOutput = true;
// }
// if ($this->hasOption('debug'))
// {
// Extension::$ltConsoleOutputDebug = true;
// }
if ( $this->hasOption('page') ) {
if ( !$this->hasOption( 'start' ) ) {
$this->singlePage();
}
else {
$this->error( 'FATAL: Must not use --start option with --page option.', 2 );
}
}
else {
$startIndex = intval( $this->getOption( 'start', 0 ) );
if ( $startIndex < 0 ) {
$this->error( 'FATAL: Start index must be 0 or greater.', 1 );
};
$this->allPages( $startIndex );
}
}
/**
* Processes a single page.
* @return bool True on success, false on failure.
*/
private function singlePage() {
$pageName = strval( $this->getOption( 'page' ) );
$this->output( "Processing single page: '$pageName'\n" );
$title = \Title::newFromText( $pageName );
$success = Extension::processPage( $title, \RequestContext::getMain() );
if ( $success ) {
$this->output( "Finished.\n" );
}
else {
$this->error( 'FATAL: There is no such page.', 3 );
}
return $success;
}
/**
* Process all pages in the Wiki.
* @param integer $index Index of the start page.
* @return bool True on success, false on failure.
*/
private function allPages( $index = 0 ) {
$config = new Config();
$verbose = $this->hasOption( 'verbose' );
// Retrieve page names from the database.
$dbr = $this->getDB( DB_REPLICA );
$namespacesClause = str_replace( '_', ' ','(' . implode( ', ', $config->sourceNamespaces ) . ')' );
$res = $dbr->select(
'page',
array( 'page_title', 'page_namespace' ),
array(
'page_namespace IN ' . $namespacesClause,
),
__METHOD__,
array(
'LIMIT' => 999999999,
'OFFSET' => $index
)
);
$numPages = $res->numRows();
$context = \RequestContext::getMain();
$this->output( "Processing {$numPages} pages, starting at index {$index}...\n" );
$numProcessed = 0;
foreach ( $res as $row ) {
$title = \Title::makeTitleSafe( $row->page_namespace, $row->page_title );
$numProcessed += 1;
$index += 1;
if ( $verbose ) {
$this->output(
sprintf(
"%s - processed %5d of %5d (%2.0f%%) - index %5d - %s\r\n",
date("c", time()),
$numProcessed,
$numPages,
$numProcessed / $numPages * 100,
$index,
$title
)
);
} else {
$this->output( sprintf( "\rPage #%d (%02.0f%%) ", $index, $numProcessed / $numPages * 100 ) );
}
Extension::processPage( $title, $context );
}
$this->output( "\rFinished. \n" );
}
}
$maintClass = 'LinkTitles\Cli';
if( defined('RUN_MAINTENANCE_IF_MAIN') ) {
require_once( RUN_MAINTENANCE_IF_MAIN );
} else {
require_once( DO_MAINTENANCE );
}
// vim: ts=2:sw=2:noet:comments^=\:///