DynamicPageList3/includes/UpdateArticle.php

743 lines
20 KiB
PHP
Raw Normal View History

2020-11-22 20:00:48 +00:00
<?php
namespace MediaWiki\Extension\DynamicPageList3;
2020-11-22 20:00:48 +00:00
2021-10-01 22:52:30 +00:00
use Article;
use CommentStoreComment;
use MediaWiki\MediaWikiServices;
use MediaWiki\Revision\SlotRecord;
use ReadOnlyError;
use RequestContext;
use Title;
use WikiPage;
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
class UpdateArticle {
/**
* this fucntion hast three tasks (depending on $exec):
* (1) show an edit dialogue for template fields (exec = edit)
2021-10-01 22:52:30 +00:00
* (2) set template parameters to values specified in the query (exec=set)v
2020-11-22 20:00:48 +00:00
* (2) preview the source code including any changes of these parameters made in the edit form or with other changes (exec=preview)
* (3) save the article with the changed value set or with other changes (exec=save)
* "other changes" means that a regexp can be applied to the source text or arbitrary text can be
* inserted before or after a pattern occuring in the text
*/
2021-02-22 23:48:01 +00:00
public static function updateArticleByRule( $title, $text, $rulesText ) {
2020-11-22 20:00:48 +00:00
// we use ; as command delimiter; \; stands for a semicolon
// \n is translated to a real linefeed
2021-10-01 22:52:30 +00:00
$rulesText = str_replace( ";", '°', $rulesText );
$rulesText = str_replace( '\°', ';', $rulesText );
$rulesText = str_replace( "\\n", "\n", $rulesText );
$rules = explode( '°', $rulesText );
$exec = 'edit';
$replaceThis = '';
$replacement = '';
$after = '';
$insertionAfter = '';
$before = '';
2020-11-22 20:00:48 +00:00
$insertionBefore = '';
2021-10-01 22:52:30 +00:00
$template = '';
$parameter = [];
$value = [];
$afterparm = [];
$format = [];
$preview = [];
$save = [];
$tooltip = [];
$optional = [];
$lastCmd = '';
$summary = '';
$editForm = false;
$action = '';
$hidden = [];
$legendPage = '';
2020-11-22 20:00:48 +00:00
$instructionPage = '';
2021-10-01 22:52:30 +00:00
$table = '';
$fieldFormat = '';
2020-11-22 20:00:48 +00:00
$nr = -1;
2021-02-22 23:48:01 +00:00
foreach ( $rules as $rule ) {
if ( preg_match( '/^\s*#/', $rule ) > 0 ) {
2021-10-01 22:52:30 +00:00
continue;
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
$rule = preg_replace( '/^[\s]*/', '', $rule );
$cmd = preg_split( "/ +/", $rule, 2 );
2021-02-22 23:48:01 +00:00
if ( count( $cmd ) > 1 ) {
2020-11-22 20:00:48 +00:00
$arg = $cmd[1];
} else {
$arg = '';
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$cmd[0] = trim( $cmd[0] );
2020-11-22 20:00:48 +00:00
2021-10-01 22:52:30 +00:00
// after ... insert ..., before ... insert ...
2021-02-22 23:48:01 +00:00
if ( $cmd[0] == 'before' ) {
2021-10-01 22:52:30 +00:00
$before = $arg;
2020-11-22 20:00:48 +00:00
$lastCmd = 'B';
}
2021-02-22 23:48:01 +00:00
if ( $cmd[0] == 'after' ) {
2021-10-01 22:52:30 +00:00
$after = $arg;
2020-11-22 20:00:48 +00:00
$lastCmd = 'A';
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $cmd[0] == 'insert' && $lastCmd != '' ) {
if ( $lastCmd == 'A' ) {
2020-11-22 20:00:48 +00:00
$insertionAfter = $arg;
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $lastCmd == 'B' ) {
2020-11-22 20:00:48 +00:00
$insertionBefore = $arg;
}
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $cmd[0] == 'template' ) {
2020-11-22 20:00:48 +00:00
$template = $arg;
}
2021-02-22 23:48:01 +00:00
if ( $cmd[0] == 'parameter' ) {
2020-11-22 20:00:48 +00:00
$nr++;
$parameter[$nr] = $arg;
2021-02-22 23:48:01 +00:00
if ( $nr > 0 ) {
2020-11-22 20:00:48 +00:00
$afterparm[$nr] = [
$parameter[$nr - 1]
];
2021-10-01 22:52:30 +00:00
$n = $nr - 1;
2021-02-22 23:48:01 +00:00
while ( $n > 0 && array_key_exists( $n, $optional ) ) {
2020-11-22 20:00:48 +00:00
$n--;
$afterparm[$nr][] = $parameter[$n];
}
}
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $cmd[0] == 'value' ) {
2020-11-22 20:00:48 +00:00
$value[$nr] = $arg;
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $cmd[0] == 'format' ) {
2020-11-22 20:00:48 +00:00
$format[$nr] = $arg;
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $cmd[0] == 'tooltip' ) {
2020-11-22 20:00:48 +00:00
$tooltip[$nr] = $arg;
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $cmd[0] == 'optional' ) {
2020-11-22 20:00:48 +00:00
$optional[$nr] = true;
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $cmd[0] == 'afterparm' ) {
2020-11-22 20:00:48 +00:00
$afterparm[$nr] = [
$arg
];
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $cmd[0] == 'legend' ) {
2020-11-22 20:00:48 +00:00
$legendPage = $arg;
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $cmd[0] == 'instruction' ) {
2020-11-22 20:00:48 +00:00
$instructionPage = $arg;
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $cmd[0] == 'table' ) {
2020-11-22 20:00:48 +00:00
$table = $arg;
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $cmd[0] == 'field' ) {
2020-11-22 20:00:48 +00:00
$fieldFormat = $arg;
}
2021-02-22 23:48:01 +00:00
if ( $cmd[0] == 'replace' ) {
2020-11-22 20:00:48 +00:00
$replaceThis = $arg;
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $cmd[0] == 'by' ) {
2020-11-22 20:00:48 +00:00
$replacement = $arg;
}
2021-02-22 23:48:01 +00:00
if ( $cmd[0] == 'editform' ) {
2020-11-22 20:00:48 +00:00
$editForm = $arg;
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $cmd[0] == 'action' ) {
2020-11-22 20:00:48 +00:00
$action = $arg;
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $cmd[0] == 'hidden' ) {
2020-11-22 20:00:48 +00:00
$hidden[] = $arg;
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $cmd[0] == 'preview' ) {
2020-11-22 20:00:48 +00:00
$preview[] = $arg;
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $cmd[0] == 'save' ) {
2020-11-22 20:00:48 +00:00
$save[] = $arg;
}
2021-02-22 23:48:01 +00:00
if ( $cmd[0] == 'summary' ) {
2020-11-22 20:00:48 +00:00
$summary = $arg;
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $cmd[0] == 'exec' ) {
// desired action (set or edit or preview)
$exec = $arg;
2020-11-22 20:00:48 +00:00
}
}
2021-02-22 23:48:01 +00:00
if ( $summary == '' ) {
2020-11-22 20:00:48 +00:00
$summary .= "\nbulk update:";
2021-02-22 23:48:01 +00:00
if ( $replaceThis != '' ) {
2020-11-22 20:00:48 +00:00
$summary .= "\n replace $replaceThis\n by $replacement";
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $before != '' ) {
2021-10-01 22:52:30 +00:00
$summary .= "\n before $before\n insertionBefore";
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $after != '' ) {
2021-10-01 22:52:30 +00:00
$summary .= "\n after $after\n insertionAfter";
2020-11-22 20:00:48 +00:00
}
}
// perform changes to the wiki source text =======================================
2021-02-22 23:48:01 +00:00
if ( $replaceThis != '' ) {
$text = preg_replace( "$replaceThis", $replacement, $text );
2020-11-22 20:00:48 +00:00
}
2021-02-22 23:48:01 +00:00
if ( $insertionBefore != '' && $before != '' ) {
$text = preg_replace( "/($before)/", $insertionBefore . '\1', $text );
2020-11-22 20:00:48 +00:00
}
2021-02-22 23:48:01 +00:00
if ( $insertionAfter != '' && $after != '' ) {
$text = preg_replace( "/($after)/", '\1' . $insertionAfter, $text );
2020-11-22 20:00:48 +00:00
}
// deal with template parameters =================================================
2021-10-01 22:52:30 +00:00
$user = RequestContext::getMain()->getUser();
2020-11-22 20:00:48 +00:00
2021-10-01 22:52:30 +00:00
if ( $template != '' ) {
2021-02-22 23:48:01 +00:00
if ( $exec == 'edit' ) {
2021-10-01 22:52:30 +00:00
$tpv = self::getTemplateParmValues( $text, $template );
2020-11-22 20:00:48 +00:00
$legendText = '';
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $legendPage != '' ) {
2020-11-22 20:00:48 +00:00
$legendTitle = '';
2021-10-01 22:52:30 +00:00
$parser = clone MediaWikiServices::getInstance()->getParser();
2021-02-22 23:48:01 +00:00
LST::text( $parser, $legendPage, $legendTitle, $legendText );
$legendText = preg_replace( '/^.*?\<section\s+begin\s*=\s*legend\s*\/\>/s', '', $legendText );
$legendText = preg_replace( '/\<section\s+end\s*=\s*legend\s*\/\>.*/s', '', $legendText );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$instructionText = '';
2021-10-01 22:52:30 +00:00
$instructions = [];
2021-02-22 23:48:01 +00:00
if ( $instructionPage != '' ) {
2020-11-22 20:00:48 +00:00
$instructionTitle = '';
2021-10-01 22:52:30 +00:00
$parser = clone MediaWikiServices::getInstance()->getParser();
2021-02-22 23:48:01 +00:00
LST::text( $parser, $instructionPage, $instructionTitle, $instructionText );
$instructions = self::getTemplateParmValues( $instructionText, 'Template field' );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
// construct an edit form containing all template invocations
$form = "<html><form method=post action=\"$action\" $editForm>\n";
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
foreach ( $tpv as $call => $tplValues ) {
2020-11-22 20:00:48 +00:00
$form .= "<table $table>\n";
2021-02-22 23:48:01 +00:00
foreach ( $parameter as $nr => $parm ) {
2020-11-22 20:00:48 +00:00
// try to extract legend from the docs of the template
$myToolTip = '';
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( array_key_exists( $nr, $tooltip ) ) {
2020-11-22 20:00:48 +00:00
$myToolTip = $tooltip[$nr];
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$myInstruction = '';
2021-10-01 22:52:30 +00:00
$myType = '';
2021-02-22 23:48:01 +00:00
foreach ( $instructions as $instruct ) {
if ( array_key_exists( 'field', $instruct ) && $instruct['field'] == $parm ) {
if ( array_key_exists( 'doc', $instruct ) ) {
2020-11-22 20:00:48 +00:00
$myInstruction = $instruct['doc'];
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( array_key_exists( 'type', $instruct ) ) {
2020-11-22 20:00:48 +00:00
$myType = $instruct['type'];
}
break;
}
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$myFormat = '';
2021-02-22 23:48:01 +00:00
if ( array_key_exists( $nr, $format ) ) {
2020-11-22 20:00:48 +00:00
$myFormat = $format[$nr];
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $legendText != '' && $myToolTip == '' ) {
$myToolTip = preg_replace( '/^.*\<section\s+begin\s*=\s*' . preg_quote( $parm, '/' ) . '\s*\/\>/s', '', $legendText );
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( strlen( $myToolTip ) == strlen( $legendText ) ) {
2020-11-22 20:00:48 +00:00
$myToolTip = '';
} else {
2021-02-22 23:48:01 +00:00
$myToolTip = preg_replace( '/\<section\s+end\s*=\s*' . preg_quote( $parm, '/' ) . '\s*\/\>.*/s', '', $myToolTip );
2020-11-22 20:00:48 +00:00
}
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$myValue = '';
2021-02-22 23:48:01 +00:00
if ( array_key_exists( $parm, $tpv[$call] ) ) {
2020-11-22 20:00:48 +00:00
$myValue = $tpv[$call][$parm];
}
2021-10-01 22:52:30 +00:00
$form .= self::editTemplateCall( $call, $parm, $myType, $myValue, $myFormat, $myToolTip, $myInstruction, $fieldFormat );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$form .= "</table>\n<br/><br/>";
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
foreach ( $hidden as $hide ) {
2020-11-22 20:00:48 +00:00
$form .= "<input type='hidden' " . $hide . " />";
}
2021-10-01 22:52:30 +00:00
$form .= "<input type='hidden' name='wpEditToken' value='{$user->getEditToken()}'/>";
2021-02-22 23:48:01 +00:00
foreach ( $preview as $prev ) {
2020-11-22 20:00:48 +00:00
$form .= "<input type='submit' " . $prev . " /> ";
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$form .= "</form></html>\n";
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
return $form;
2021-02-22 23:48:01 +00:00
} elseif ( $exec == 'set' || $exec == 'preview' ) {
2020-11-22 20:00:48 +00:00
// loop over all invocations and parameters, this could be improved to enhance performance
$matchCount = 10;
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
for ( $call = 0; $call < 10; $call++ ) {
foreach ( $parameter as $nr => $parm ) {
2020-11-22 20:00:48 +00:00
// set parameters to values specified in the dpl source or get them from the http request
2021-02-22 23:48:01 +00:00
if ( $exec == 'set' ) {
2022-03-16 22:10:03 +00:00
$myValue = $value[$nr];
2020-11-22 20:00:48 +00:00
} else {
2021-02-22 23:48:01 +00:00
if ( $call >= $matchCount ) {
2020-11-22 20:00:48 +00:00
break;
}
2022-03-09 20:17:24 +00:00
$request = RequestContext::getMain()->getRequest();
$myValue = $request->getVal( urlencode( $call . '_' . $parm ), '' );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
$myOptional = array_key_exists( $nr, $optional );
2020-11-22 20:00:48 +00:00
$myAfterParm = [];
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( array_key_exists( $nr, $afterparm ) ) {
2020-11-22 20:00:48 +00:00
$myAfterParm = $afterparm[$nr];
}
2021-10-01 22:52:30 +00:00
$text = self::updateTemplateCall( $matchCount, $text, $template, $call, $parm, $myValue ?? '', $myAfterParm, $myOptional );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $exec == 'set' ) {
2021-10-01 22:52:30 +00:00
break;
2020-11-22 20:00:48 +00:00
}
}
}
}
2021-02-22 23:48:01 +00:00
if ( $exec == 'set' ) {
2021-10-01 22:52:30 +00:00
return self::doUpdateArticle( $title, $text, $summary );
2021-02-22 23:48:01 +00:00
} elseif ( $exec == 'preview' ) {
2022-03-09 20:17:24 +00:00
global $wgScriptPath;
$request = RequestContext::getMain()->getRequest();
2021-10-01 22:52:30 +00:00
$titleX = Title::newFromText( $title );
$articleX = new Article( $titleX );
$userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
$form = '<html>
2021-02-22 23:48:01 +00:00
<form id="editform" name="editform" method="post" action="' . $wgScriptPath . '/index.php?title=' . urlencode( $title ) . '&action=submit" enctype="multipart/form-data">
2020-11-22 20:00:48 +00:00
<input type="hidden" value="" name="wpSection" />
<input type="hidden" value="' . wfTimestampNow() . '" name="wpStarttime" />
2021-10-01 22:52:30 +00:00
<input type="hidden" value="' . $articleX->getPage()->getTimestamp() . '" name="wpEdittime" />
2020-11-22 20:00:48 +00:00
<input type="hidden" value="" name="wpScrolltop" id="wpScrolltop" />
2021-10-01 22:52:30 +00:00
<textarea tabindex="1" accesskey="," name="wpTextbox1" id="wpTextbox1" rows="' . $userOptionsLookup->getIntOption( $user, 'rows' ) . '" cols="' . $userOptionsLookup->getIntOption( $user, 'cols' ) . '" >' . htmlspecialchars( $text ) . '</textarea>
2020-11-22 20:00:48 +00:00
<input type="hidden" name="wpSummary value="' . $summary . '" id="wpSummary" />
<input name="wpAutoSummary" type="hidden" value="" />
<input id="wpSave" name="wpSave" type="submit" value="Save page" accesskey="s" title="Save your changes [s]" />
2022-03-09 20:17:24 +00:00
<input type="hidden" value="' . $request->getVal( 'token' ) . '" name="wpEditToken" />
2020-11-22 20:00:48 +00:00
</form>
</html>';
return $form;
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
return "exec must be one of the following: edit, preview, set";
}
2021-10-01 22:52:30 +00:00
private static function doUpdateArticle( $title, $text, $summary ) {
2022-03-09 20:17:24 +00:00
$context = RequestContext::getMain();
$request = $context->getRequest();
$out = $context->getOutput();
$user = $context->getUser();
2021-10-01 22:52:30 +00:00
2022-03-09 20:17:24 +00:00
if ( !$user->matchEditToken( $request->getVal( 'wpEditToken' ) ) ) {
$out->addWikiMsg( 'sessionfailure' );
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
return 'session failure';
}
2021-10-01 22:52:30 +00:00
$titleX = Title::newFromText( $title );
$permission_errors = MediaWikiServices::getInstance()->getPermissionManager()->getPermissionErrors( 'edit', $user, $titleX );
2021-02-22 23:48:01 +00:00
if ( count( $permission_errors ) == 0 ) {
$services = MediaWikiServices::getInstance();
// MW 1.36+
if ( method_exists( $services, 'getWikiPageFactory' ) ) {
$wikiPageFactory = $services->getWikiPageFactory();
$page = $wikiPageFactory->newFromTitle( $titleX );
} else {
// @phan-suppress-next-line PhanDeprecatedFunction
$page = WikiPage::factory( $titleX );
}
2021-10-01 22:52:30 +00:00
$updater = $page->newPageUpdater( $user );
$content = $page->getContentHandler()->makeContent( $text, $titleX );
$updater->setContent( SlotRecord::MAIN, $content );
$comment = CommentStoreComment::newUnsavedComment( $summary );
$updater->saveRevision(
$comment,
EDIT_UPDATE | EDIT_DEFER_UPDATES | EDIT_AUTOSUMMARY
);
2022-03-09 20:17:24 +00:00
$out->redirect( $titleX->getFullUrl( $page->isRedirect() ? 'redirect=no' : '' ) );
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
return '';
} else {
2022-03-09 20:17:24 +00:00
$out->showPermissionsErrorPage( $permission_errors );
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
return 'permission error';
}
}
private static function editTemplateCall( $call, $parameter, $type, $value, $format, $legend, $instruction, $fieldFormat ) {
2020-11-22 20:00:48 +00:00
$matches = [];
2021-02-22 23:48:01 +00:00
$nlCount = preg_match_all( '/\n/', $value, $matches );
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $nlCount > 0 ) {
2020-11-22 20:00:48 +00:00
$rows = $nlCount + 1;
} else {
2021-02-22 23:48:01 +00:00
$rows = floor( strlen( $value ) / 50 ) + 1;
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( preg_match( '/rows\s*=/', $format ) <= 0 ) {
2020-11-22 20:00:48 +00:00
$format .= " rows=$rows";
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$cols = 50;
2021-02-22 23:48:01 +00:00
if ( preg_match( '/cols\s*=/', $format ) <= 0 ) {
2020-11-22 20:00:48 +00:00
$format .= " cols=$cols";
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$textArea = "<textarea name=\"" . urlencode( $call . '_' . $parameter ) . "\" $format/>" . htmlspecialchars( $value ) . "</textarea>";
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
return str_replace( '%NAME%', htmlspecialchars( str_replace( '_', ' ', $parameter ) ), str_replace( '%TYPE%', $type, str_replace( '%INPUT%', $textArea, str_replace( '%LEGEND%', "</html>" . htmlspecialchars( $legend ) . "<html>", str_replace( '%INSTRUCTION%', "</html>" . htmlspecialchars( $instruction ) . "<html>", $fieldFormat ) ) ) ) );
2020-11-22 20:00:48 +00:00
}
/**
* return an array of template invocations; each element is an associative array of parameter and value
*/
2021-02-22 23:48:01 +00:00
private static function getTemplateParmValues( $text, $template ) {
2021-10-01 22:52:30 +00:00
$matches = [];
2021-02-22 23:48:01 +00:00
$noMatches = preg_match_all( '/\{\{\s*' . preg_quote( $template, '/' ) . '\s*[|}]/i', $text, $matches, PREG_OFFSET_CAPTURE );
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $noMatches <= 0 ) {
2020-11-22 20:00:48 +00:00
return '';
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$textLen = strlen( $text );
2021-10-01 22:52:30 +00:00
$tval = [];
$call = -1;
2020-11-22 20:00:48 +00:00
2021-02-22 23:48:01 +00:00
foreach ( $matches as $matchA ) {
foreach ( $matchA as $matchB ) {
2021-10-01 22:52:30 +00:00
$match = $matchB[0];
$start = $matchB[1];
2020-11-22 20:00:48 +00:00
$tval[++$call] = [];
2021-10-01 22:52:30 +00:00
$nr = 0;
$parmValue = '';
$parmName = '';
$parm = '';
2020-11-22 20:00:48 +00:00
2021-02-22 23:48:01 +00:00
if ( $match[strlen( $match ) - 1] == '}' ) {
2021-10-01 22:52:30 +00:00
break;
2020-11-22 20:00:48 +00:00
}
// search to the end of the template call
$cbrackets = 2;
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
for ( $i = $start + strlen( $match ); $i < $textLen; $i++ ) {
2020-11-22 20:00:48 +00:00
$c = $text[$i];
2021-02-22 23:48:01 +00:00
if ( $c == '{' || $c == '[' ) {
2021-10-01 22:52:30 +00:00
$cbrackets++;
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $c == '}' || $c == ']' ) {
2020-11-22 20:00:48 +00:00
$cbrackets--;
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( ( $cbrackets == 2 && $c == '|' ) || ( $cbrackets == 1 && $c == '}' ) ) {
2020-11-22 20:00:48 +00:00
// parameter (name or value) found
2021-02-22 23:48:01 +00:00
if ( $parmName == '' ) {
$tval[$call][++$nr] = trim( $parm );
2020-11-22 20:00:48 +00:00
} else {
2021-02-22 23:48:01 +00:00
$tval[$call][$parmName] = trim( $parmValue );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
$parmName = '';
2020-11-22 20:00:48 +00:00
$parmValue = '';
2021-10-01 22:52:30 +00:00
$parm = '';
2020-11-22 20:00:48 +00:00
continue;
} else {
2021-02-22 23:48:01 +00:00
if ( $parmName == '' ) {
if ( $c == '=' ) {
$parmName = trim( $parm );
2020-11-22 20:00:48 +00:00
}
} else {
$parmValue .= $c;
}
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$parm .= $c;
2021-02-22 23:48:01 +00:00
if ( $cbrackets == 0 ) {
2021-10-01 22:52:30 +00:00
break;
2020-11-22 20:00:48 +00:00
}
}
}
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
return $tval;
}
/*
* Changes a single parameter value within a certain call of a template
*/
2021-02-22 23:48:01 +00:00
private static function updateTemplateCall( &$matchCount, $text, $template, $call, $parameter, $value, $afterParm, $optional ) {
2020-11-22 20:00:48 +00:00
// if parameter is optional and value is empty we leave everything as it is (i.e. we do not remove the parm)
2021-02-22 23:48:01 +00:00
if ( $optional && $value == '' ) {
2020-11-22 20:00:48 +00:00
return $text;
}
2021-10-01 22:52:30 +00:00
$matches = [];
2021-02-22 23:48:01 +00:00
$noMatches = preg_match_all( '/\{\{\s*' . preg_quote( $template, '/' ) . '\s*[|}]/i', $text, $matches, PREG_OFFSET_CAPTURE );
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $noMatches <= 0 ) {
2020-11-22 20:00:48 +00:00
return $text;
}
2021-10-01 22:52:30 +00:00
$beginSubst = -1;
$endSubst = -1;
2020-11-22 20:00:48 +00:00
$posInsertAt = 0;
2021-10-01 22:52:30 +00:00
$apNrLast = 1000;
2020-11-22 20:00:48 +00:00
2021-02-22 23:48:01 +00:00
foreach ( $matches as $matchA ) {
$matchCount = count( $matchA );
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
foreach ( $matchA as $occurence => $matchB ) {
if ( $occurence < $call ) {
2020-11-22 20:00:48 +00:00
continue;
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$match = $matchB[0];
$start = $matchB[1];
2021-02-22 23:48:01 +00:00
if ( $match[strlen( $match ) - 1] == '}' ) {
2020-11-22 20:00:48 +00:00
// template was called without parameters, add new parameter and value
// append parameter and value
2021-10-01 22:52:30 +00:00
$beginSubst = 0;
$endSubst = 0;
2020-11-22 20:00:48 +00:00
$substitution = "|$parameter = $value";
break;
} else {
// there is already a list of parameters; we search to the end of the template call
$cbrackets = 2;
2021-10-01 22:52:30 +00:00
$parm = '';
$pos = $start + strlen( $match ) - 1;
$textLen = strlen( $text );
2021-02-22 23:48:01 +00:00
for ( $i = $pos + 1; $i < $textLen; $i++ ) {
2020-11-22 20:00:48 +00:00
$c = $text[$i];
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $c == '{' || $c == '[' ) {
// we count both types of brackets
$cbrackets++;
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $c == '}' || $c == ']' ) {
2020-11-22 20:00:48 +00:00
$cbrackets--;
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( ( $cbrackets == 2 && $c == '|' ) || ( $cbrackets == 1 && $c == '}' ) ) {
2020-11-22 20:00:48 +00:00
// parameter (name / value) found
2021-02-22 23:48:01 +00:00
$token = explode( '=', $parm, 2 );
if ( count( $token ) == 2 ) {
2020-11-22 20:00:48 +00:00
// we need a pair of name / value
2021-02-22 23:48:01 +00:00
$parmName = trim( $token[0] );
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $parmName == $parameter ) {
2020-11-22 20:00:48 +00:00
// we found the parameter, now replace the current value
2021-02-22 23:48:01 +00:00
$parmValue = trim( $token[1] );
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $parmValue == $value ) {
// no need to change when values are identical
break;
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
// keep spaces;
2021-02-22 23:48:01 +00:00
if ( $parmValue == '' ) {
if ( strlen( $token[1] ) > 0 && $token[1][strlen( $token[1] ) - 1] == "\n" ) {
$substitution = str_replace( "\n", $value . "\n", $token[1] );
2020-11-22 20:00:48 +00:00
} else {
$substitution = $value . $token[1];
}
} else {
2021-02-22 23:48:01 +00:00
$substitution = str_replace( $parmValue, $value, $token[1] );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$beginSubst = $pos + strlen( $token[0] ) + 2;
2021-10-01 22:52:30 +00:00
$endSubst = $i;
2020-11-22 20:00:48 +00:00
break;
} else {
2021-02-22 23:48:01 +00:00
foreach ( $afterParm as $apNr => $ap ) {
2020-11-22 20:00:48 +00:00
// store position for insertion
2021-02-22 23:48:01 +00:00
if ( $parmName == $ap && $apNr < $apNrLast ) {
2020-11-22 20:00:48 +00:00
$posInsertAt = $i;
2021-10-01 22:52:30 +00:00
$apNrLast = $apNr;
2020-11-22 20:00:48 +00:00
break;
}
}
}
}
2021-02-22 23:48:01 +00:00
if ( $c == '}' ) {
2020-11-22 20:00:48 +00:00
// end of template call reached, insert at stored position or here
2021-02-22 23:48:01 +00:00
if ( $posInsertAt != 0 ) {
2020-11-22 20:00:48 +00:00
$beginSubst = $posInsertAt;
} else {
$beginSubst = $i;
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$substitution = "|$parameter = $value";
2021-02-22 23:48:01 +00:00
if ( $text[$beginSubst - 1] == "\n" ) {
2020-11-22 20:00:48 +00:00
--$beginSubst;
$substitution = "\n" . $substitution;
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$endSubst = $beginSubst;
break;
}
2021-10-01 22:52:30 +00:00
$pos = $i;
2020-11-22 20:00:48 +00:00
$parm = '';
} else {
$parm .= $c;
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $cbrackets == 0 ) {
2020-11-22 20:00:48 +00:00
break;
}
}
}
break;
}
break;
}
2021-02-22 23:48:01 +00:00
if ( $beginSubst < 0 ) {
2020-11-22 20:00:48 +00:00
return $text;
}
2021-10-01 22:52:30 +00:00
return substr( $text, 0, $beginSubst ) . ( $substitution ?? '' ) . substr( $text, $endSubst );
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
public static function deleteArticleByRule( $title, $text, $rulesText ) {
2020-11-22 20:00:48 +00:00
// return "deletion of articles by DPL is disabled.";
// we use ; as command delimiter; \; stands for a semicolon
// \n is translated to a real linefeed
2021-02-22 23:48:01 +00:00
$rulesText = str_replace( ";", '°', $rulesText );
$rulesText = str_replace( '\°', ';', $rulesText );
$rulesText = str_replace( "\\n", "\n", $rulesText );
2021-10-01 22:52:30 +00:00
$rules = explode( '°', $rulesText );
$exec = false;
$message = '';
$reason = '';
2020-11-22 20:00:48 +00:00
2021-02-22 23:48:01 +00:00
foreach ( $rules as $rule ) {
if ( preg_match( '/^\s*#/', $rule ) > 0 ) {
// # is comment symbol
continue;
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
$rule = preg_replace( '/^[\s]*/', '', $rule );
$cmd = preg_split( "/ +/", $rule, 2 );
2021-02-22 23:48:01 +00:00
if ( count( $cmd ) > 1 ) {
2020-11-22 20:00:48 +00:00
$arg = $cmd[1];
} else {
$arg = '';
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$cmd[0] = trim( $cmd[0] );
2020-11-22 20:00:48 +00:00
2021-02-22 23:48:01 +00:00
if ( $cmd[0] == 'reason' ) {
2020-11-22 20:00:48 +00:00
$reason = $arg;
}
// we execute only if "exec" is given, otherwise we merely show what would be done
2021-02-22 23:48:01 +00:00
if ( $cmd[0] == 'exec' ) {
2020-11-22 20:00:48 +00:00
$exec = true;
}
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$reason .= "\nbulk delete by DPL query";
2021-10-01 22:52:30 +00:00
$titleX = Title::newFromText( $title );
2021-02-22 23:48:01 +00:00
if ( $exec ) {
2022-03-09 20:17:24 +00:00
$context = RequestContext::getMain();
$out = $context->getOutput();
$user = $context->getUser();
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
# Check permissions
2021-10-01 22:52:30 +00:00
$permission_errors = MediaWikiServices::getInstance()->getPermissionManager()->getPermissionErrors( 'delete', $user, $titleX );
$isReadOnly = MediaWikiServices::getInstance()->getReadOnlyMode()->isReadOnly();
2021-02-22 23:48:01 +00:00
if ( count( $permission_errors ) > 0 ) {
2022-03-09 20:17:24 +00:00
$out->showPermissionsErrorPage( $permission_errors );
2020-11-22 20:00:48 +00:00
return 'permission error';
2021-10-01 22:52:30 +00:00
} elseif ( $isReadOnly ) {
throw new ReadOnlyError;
2020-11-22 20:00:48 +00:00
} else {
2021-10-01 22:52:30 +00:00
$articleX = new Article( $titleX );
2021-02-22 23:48:01 +00:00
$articleX->doDelete( $reason );
2020-11-22 20:00:48 +00:00
}
} else {
$message .= "set 'exec yes' to delete &#160; &#160; <big>'''$title'''</big>\n";
}
2021-10-01 22:52:30 +00:00
$message .= "<pre><nowiki>\n{$text}</nowiki></pre>";
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
return $message;
}
}