Files
LinkTitles/LinkTitles_Maintenance.php
2016-11-09 22:45:44 +01:00

165 lines
5.2 KiB
PHP
Executable File

<?php
/*
* Copyright 2012-2016 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/LinkTitles_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 {
/// The constructor adds a description and one option.
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 to process",
false, // not required
true, // requires argument
"p"
);
$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() {
global $wgLinkTitlesNamespaces;
$index = intval($this->getOption('start', 0));
if ( $index < 0 ) {
$this->error('FATAL: Start index must be 0 or greater.', 1);
};
if ($this->hasOption('log'))
{
Extension::$ltConsoleOutput = true;
}
if ($this->hasOption('debug'))
{
Extension::$ltConsoleOutputDebug = true;
}
$pagename = strval($this->getOption('page'));
if ($pagename != null)
{
$curTitle = Title::newFromDBkey( $pagename );
LinkTitles::processPage($curTitle,RequestContext::getMain() );
$this->output("\nFinished parsing.\n");
return;
}
// get our Namespaces
$namespacesClause = str_replace( '_', ' ','(' . implode( ', ',$wgLinkTitlesNamespaces ) . ')' );
// Connect to the database
$dbr = $this->getDB( DB_SLAVE );
// Retrieve page names from the database.
$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");
// Iterate through the pages; break if a time limit is exceeded.
foreach ( $res as $row ) {
$index += 1;
$curTitle = \Title::makeTitleSafe( $row->page_namespace, $row->page_title);
$this->output(
sprintf("\rPage #%d (%02.0f%%) ", $index, $index / $numPages * 100)
);
Extension::processPage($curTitle, $context);
}
$this->output("\nFinished parsing.\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^=\:///