Revise Special class.

- Change: The $wgLinkTitlesBatchTimeLimit configuration variable was renamed to $wgLinkTitlesSpecialPageReloadAfter.
This commit is contained in:
Daniel Kraus
2017-08-27 23:06:43 +02:00
parent f1eae017b9
commit 2c0eb6839b
4 changed files with 35 additions and 29 deletions

View File

@ -27,7 +27,7 @@
"LinkTitlesSmartMode": true, "LinkTitlesSmartMode": true,
"LinkTitlesWordStartOnly": true, "LinkTitlesWordStartOnly": true,
"LinkTitlesWordEndOnly": true, "LinkTitlesWordEndOnly": true,
"LinkTitlesBatchTimeLimit": 1, "LinkTitlesSpecialPageReloadAfter": 1,
"LinkTitlesNamespaces": [ "LinkTitlesNamespaces": [
0 0
] ]

View File

@ -149,6 +149,12 @@ class Config {
*/ */
public $enableNoTargetMagicWord; public $enableNoTargetMagicWord;
/**
* Time (in seconds) after which to reload the special page.
* @var integer reload interval (in seconds)
*/
public $specialPageReloadAfter;
public $enableConsoleOutput; public $enableConsoleOutput;
public $enableDebugConsoleOutput; public $enableDebugConsoleOutput;
@ -174,6 +180,7 @@ class Config {
global $wgLinkTitlesParseHeadings; global $wgLinkTitlesParseHeadings;
global $wgLinkTitlesEnableNoTargetMagicWord; global $wgLinkTitlesEnableNoTargetMagicWord;
global $wgLinkTitlesCheckRedirect; global $wgLinkTitlesCheckRedirect;
global $wgLinkTitlesSpecialPageReloadAfter;
$this->parseOnEdit = $wgLinkTitlesParseOnEdit; $this->parseOnEdit = $wgLinkTitlesParseOnEdit;
$this->parseOnRender = $wgLinkTitlesParseOnRender; $this->parseOnRender = $wgLinkTitlesParseOnRender;
$this->preferShortTitles = $wgLinkTitlesPreferShortTitles; $this->preferShortTitles = $wgLinkTitlesPreferShortTitles;
@ -189,6 +196,7 @@ class Config {
$this->parseHeadings = $wgLinkTitlesParseHeadings; $this->parseHeadings = $wgLinkTitlesParseHeadings;
$this->enableNoTargetMagicWord = $wgLinkTitlesEnableNoTargetMagicWord;; $this->enableNoTargetMagicWord = $wgLinkTitlesEnableNoTargetMagicWord;;
$this->checkRedirect = $wgLinkTitlesCheckRedirect;; $this->checkRedirect = $wgLinkTitlesCheckRedirect;;
$this->specialPageReloadAfter = $wgLinkTitlesSpecialPageReloadAfter;
$this->enableConsoleOutput = false; $this->enableConsoleOutput = false;
$this->enableDebugConsoleOutput = false; $this->enableDebugConsoleOutput = false;
} }

View File

@ -84,13 +84,13 @@ class Extension {
* @returns bool True if the page exists, false if the page does not exist * @returns bool True if the page exists, false if the page does not exist
*/ */
public static function processPage( \Title $title, \RequestContext $context ) { public static function processPage( \Title $title, \RequestContext $context ) {
$page = \WikiPage::factory($title); $page = \WikiPage::factory( $title );
$content = $page->getContent(); $content = $page->getContent();
if ( $content != null ) { if ( $content != null ) {
$text = $content->getContentHandler()->serializeContent($content); $text = $content->getContentHandler()->serializeContent( $content );
$config = new Config(); $config = new Config();
$linker = new Linker( $config ); $linker = new Linker( $config );
$newText = $linker->linkContent($title, $text); $newText = $linker->linkContent( $title, $text );
if ( $text != $newText ) { if ( $text != $newText ) {
$content = $content->getContentHandler()->unserializeContent( $newText ); $content = $content->getContentHandler()->unserializeContent( $newText );
$page->doEditContent( $page->doEditContent(

View File

@ -37,6 +37,7 @@ if ( !defined( 'MEDIAWIKI' ) ) {
* *
*/ */
class Special extends \SpecialPage { class Special extends \SpecialPage {
private $config;
/** /**
* Constructor. Announces the special page title and required user right to the parent constructor. * Constructor. Announces the special page title and required user right to the parent constructor.
@ -46,6 +47,7 @@ class Special extends \SpecialPage {
// users who have the 'linktitles-batch' right get to see this page (by // users who have the 'linktitles-batch' right get to see this page (by
// default, this are all sysop users). // default, this are all sysop users).
parent::__construct( 'LinkTitles', 'linktitles-batch' ); parent::__construct( 'LinkTitles', 'linktitles-batch' );
$this->config = new Config();
} }
function getGroupName() { function getGroupName() {
@ -57,7 +59,7 @@ class Special extends \SpecialPage {
* Entry function of the special page class. Will abort if the user does not have appropriate permissions ('linktitles-batch'). * Entry function of the special page class. Will abort if the user does not have appropriate permissions ('linktitles-batch').
* @param $par Additional parameters (required by interface; currently not used) * @param $par Additional parameters (required by interface; currently not used)
*/ */
function execute($par) { function execute( $par ) {
// Prevent non-authorized users from executing the batch processing. // Prevent non-authorized users from executing the batch processing.
if ( !$this->userCanExecute( $this->getUser() ) ) { if ( !$this->userCanExecute( $this->getUser() ) ) {
$this->displayRestrictionError(); $this->displayRestrictionError();
@ -94,14 +96,11 @@ class Special extends \SpecialPage {
* @param OutputPage $output Output page for the special page. * @param OutputPage $output Output page for the special page.
*/ */
private function process( \WebRequest &$request, \OutputPage &$output) { private function process( \WebRequest &$request, \OutputPage &$output) {
global $wgLinkTitlesTimeLimit;
global $wgLinkTitlesNamespaces;
// get our Namespaces // get our Namespaces
$namespacesClause = str_replace( '_', ' ','(' . implode( ', ',$wgLinkTitlesNamespaces ) . ')' ); $namespacesClause = str_replace( '_', ' ','(' . implode( ', ',$this->config->nameSpaces ) . ')' );
// Start the stopwatch // Start the stopwatch
$startTime = microtime(true); $startTime = microtime( true );
// Connect to the database // Connect to the database
$dbr = wfGetDB( DB_SLAVE ); $dbr = wfGetDB( DB_SLAVE );
@ -112,11 +111,11 @@ class Special extends \SpecialPage {
// Convert the start index to an integer; this helps preventing // Convert the start index to an integer; this helps preventing
// SQL injection attacks via forged POST requests. // SQL injection attacks via forged POST requests.
$start = intval($postValues['s']); $start = intval( $postValues['s'] );
// If an end index was given, we don't need to query the database // If an end index was given, we don't need to query the database
if ( array_key_exists('e', $postValues) ) { if ( array_key_exists( 'e', $postValues ) ) {
$end = intval($postValues['e']); $end = intval( $postValues['e'] );
} }
else else
{ {
@ -124,7 +123,7 @@ class Special extends \SpecialPage {
$end = $this->countPages($dbr, $namespacesClause ); $end = $this->countPages($dbr, $namespacesClause );
}; };
array_key_exists('r', $postValues) ? $reloads = $postValues['r'] : $reloads = 0; array_key_exists( 'r', $postValues ) ? $reloads = $postValues['r'] : $reloads = 0;
// Retrieve page names from the database. // Retrieve page names from the database.
$res = $dbr->select( $res = $dbr->select(
@ -143,17 +142,17 @@ class Special extends \SpecialPage {
// Iterate through the pages; break if a time limit is exceeded. // Iterate through the pages; break if a time limit is exceeded.
foreach ( $res as $row ) { foreach ( $res as $row ) {
$curTitle = \Title::makeTitleSafe( $row->page_namespace, $row->page_title); $curTitle = \Title::makeTitleSafe( $row->page_namespace, $row->page_title);
Extension::processPage($curTitle, $this->getContext()); Extension::processPage( $curTitle, $this->getContext() );
$start += 1; $start += 1;
// Check if the time limit is exceeded // Check if the time limit is exceeded
if ( microtime(true)-$startTime > $wgLinkTitlesTimeLimit ) if ( microtime( true ) - $startTime > $config->specialPageReloadAfter )
{ {
break; break;
} }
} }
$this->addProgressInfo($output, $curTitle, $start, $end); $this->addProgressInfo( $output, $curTitle, $start, $end );
// If we have not reached the last page yet, produce code to reload // If we have not reached the last page yet, produce code to reload
// the extension's special page. // the extension's special page.
@ -162,12 +161,12 @@ class Special extends \SpecialPage {
$reloads += 1; $reloads += 1;
// Build a form with hidden values and output JavaScript code that // Build a form with hidden values and output JavaScript code that
// immediately submits the form in order to continue the process. // immediately submits the form in order to continue the process.
$output->addHTML($this->getReloaderForm($request->getRequestURL(), $output->addHTML( $this->getReloaderForm( $request->getRequestURL(),
$start, $end, $reloads)); $start, $end, $reloads) );
} }
else // Last page has been processed else // Last page has been processed
{ {
$this->addCompletedInfo($output, $start, $end, $reloads); $this->addCompletedInfo( $output, $start, $end, $reloads );
} }
} }
@ -244,14 +243,14 @@ EOF
); );
} }
/* /**
* Generates an HTML form and JavaScript to automatically submit the * Generates an HTML form and JavaScript to automatically submit the
* form. * form.
* @param $url URL to reload with a POST request. * @param $url URL to reload with a POST request.
* @param $start Index of the next page that shall be processed. * @param $start Index of the next page that shall be processed.
* @param $end Index of the last page to be processed. * @param $end Index of the last page to be processed.
* @param $reloads Counter that holds the number of reloads so far. * @param $reloads Counter that holds the number of reloads so far.
* @returns String that holds the HTML for a form and a JavaScript command. * @return String that holds the HTML for a form and a JavaScript command.
*/ */
private function getReloaderForm( $url, $start, $end, $reloads ) { private function getReloaderForm( $url, $start, $end, $reloads ) {
return return
@ -268,16 +267,15 @@ EOF
; ;
} }
/* /**
* Adds statistics to the page when all processing is done. * Adds statistics to the page when all processing is done.
* @param $output Output object * @param $output Output object
* @param $start Index of the first page that was processed. * @param $start Index of the first page that was processed.
* @param $end Index of the last processed page. * @param $end Index of the last processed page.
* @param $reloads Number of reloads of the page. * @param $reloads Number of reloads of the page.
* @returns undefined * @return undefined
*/ */
private function addCompletedInfo( &$output, $start, $end, $reloads ) { private function addCompletedInfo( &$output, $start, $end, $reloads ) {
global $wgLinkTitlesTimeLimit;
$pagesPerReload = sprintf('%0.1f', $end / $reloads); $pagesPerReload = sprintf('%0.1f', $end / $reloads);
$output->addWikiText( $output->addWikiText(
<<<EOF <<<EOF
@ -286,7 +284,7 @@ EOF
|- |-
| total number of pages: || ${end} | total number of pages: || ${end}
|- |-
| timeout setting [s]: || ${wgLinkTitlesTimeLimit} | timeout setting [s]: || {$config->specialPageReloadAfter}
|- |-
| webpage reloads: || ${reloads} | webpage reloads: || ${reloads}
|- |-
@ -296,12 +294,12 @@ EOF
); );
} }
/* /**
* Counts the number of pages in a read-access wiki database ($dbr). * Counts the number of pages in a read-access wiki database ($dbr).
* @param $dbr Read-only `Database` object. * @param $dbr Read-only `Database` object.
* @returns Number of pages in the default namespace (0) of the wiki. * @return Number of pages in the default namespace (0) of the wiki.
*/ */
private function countPages(&$dbr, $namespacesClause) { private function countPages( &$dbr, $namespacesClause ) {
$res = $dbr->select( $res = $dbr->select(
'page', 'page',
array('pagecount' => "COUNT(page_id)"), array('pagecount' => "COUNT(page_id)"),