';
$nr = -1;
foreach ( $rules as $rule ) {
if ( preg_match( '/^\s*#/', $rule ) > 0 ) {
continue; // # is comment symbol
}
$rule = preg_replace( '/^[\s]*/', '', $rule ); // strip leading white space
$cmd = preg_split( "/ +/", $rule, 2 );
if ( count( $cmd ) > 1 ) {
$arg = $cmd[1];
} else {
$arg = '';
}
$cmd[0] = trim( $cmd[0] );
// after ... insert ... , before ... insert ...
if ( $cmd[0] == 'before' ) {
$before = $arg;
$lastCmd = 'B';
}
if ( $cmd[0] == 'after' ) {
$after = $arg;
$lastCmd = 'A';
}
if ( $cmd[0] == 'insert' && $lastCmd != '' ) {
if ( $lastCmd == 'A' ) {
$insertionAfter = $arg;
}
if ( $lastCmd == 'B' ) {
$insertionBefore = $arg;
}
}
if ( $cmd[0] == 'template' ) {
$template = $arg;
}
if ( $cmd[0] == 'parameter' ) {
$nr++;
$parameter[$nr] = $arg;
if ( $nr > 0 ) {
$afterparm[$nr] = [
$parameter[$nr - 1]
];
$n = $nr - 1;
while ( $n > 0 && array_key_exists( $n, $optional ) ) {
$n--;
$afterparm[$nr][] = $parameter[$n];
}
}
}
if ( $cmd[0] == 'value' ) {
$value[$nr] = $arg;
}
if ( $cmd[0] == 'format' ) {
$format[$nr] = $arg;
}
if ( $cmd[0] == 'tooltip' ) {
$tooltip[$nr] = $arg;
}
if ( $cmd[0] == 'optional' ) {
$optional[$nr] = true;
}
if ( $cmd[0] == 'afterparm' ) {
$afterparm[$nr] = [
$arg
];
}
if ( $cmd[0] == 'legend' ) {
$legendPage = $arg;
}
if ( $cmd[0] == 'instruction' ) {
$instructionPage = $arg;
}
if ( $cmd[0] == 'table' ) {
$table = $arg;
}
if ( $cmd[0] == 'field' ) {
$fieldFormat = $arg;
}
if ( $cmd[0] == 'replace' ) {
$replaceThis = $arg;
}
if ( $cmd[0] == 'by' ) {
$replacement = $arg;
}
if ( $cmd[0] == 'editform' ) {
$editForm = $arg;
}
if ( $cmd[0] == 'action' ) {
$action = $arg;
}
if ( $cmd[0] == 'hidden' ) {
$hidden[] = $arg;
}
if ( $cmd[0] == 'preview' ) {
$preview[] = $arg;
}
if ( $cmd[0] == 'save' ) {
$save[] = $arg;
}
if ( $cmd[0] == 'summary' ) {
$summary = $arg;
}
if ( $cmd[0] == 'exec' ) {
$exec = $arg; // desired action (set or edit or preview)
}
}
if ( $summary == '' ) {
$summary .= "\nbulk update:";
if ( $replaceThis != '' ) {
$summary .= "\n replace $replaceThis\n by $replacement";
}
if ( $before != '' ) {
$summary .= "\n before $before\n insertionBefore";
}
if ( $after != '' ) {
$summary .= "\n after $after\n insertionAfter";
}
}
// $message.= '';
// perform changes to the wiki source text =======================================
if ( $replaceThis != '' ) {
$text = preg_replace( "$replaceThis", $replacement, $text );
}
if ( $insertionBefore != '' && $before != '' ) {
$text = preg_replace( "/($before)/", $insertionBefore . '\1', $text );
}
if ( $insertionAfter != '' && $after != '' ) {
$text = preg_replace( "/($after)/", '\1' . $insertionAfter, $text );
}
// deal with template parameters =================================================
global $wgRequest, $wgUser;
if ( $template != '' ) {
if ( $exec == 'edit' ) {
$tpv = self::getTemplateParmValues( $text, $template );
$legendText = '';
if ( $legendPage != '' ) {
$legendTitle = '';
global $wgParser, $wgUser;
$parser = clone $wgParser;
LST::text( $parser, $legendPage, $legendTitle, $legendText );
$legendText = preg_replace( '/^.*?\/s', '', $legendText );
$legendText = preg_replace( '/\.*/s', '', $legendText );
}
$instructionText = '';
$instructions = [];
if ( $instructionPage != '' ) {
$instructionTitle = '';
global $wgParser, $wgUser;
$parser = clone $wgParser;
LST::text( $parser, $instructionPage, $instructionTitle, $instructionText );
$instructions = self::getTemplateParmValues( $instructionText, 'Template field' );
}
// construct an edit form containing all template invocations
$form = "\n";
return $form;
} elseif ( $exec == 'set' || $exec == 'preview' ) {
// loop over all invocations and parameters, this could be improved to enhance performance
$matchCount = 10;
for ( $call = 0; $call < 10; $call++ ) {
foreach ( $parameter as $nr => $parm ) {
// set parameters to values specified in the dpl source or get them from the http request
if ( $exec == 'set' ) {
$myvalue = $value[$nr];
} else {
if ( $call >= $matchCount ) {
break;
}
$myValue = $wgRequest->getVal( urlencode( $call . '_' . $parm ), '' );
}
$myOptional = array_key_exists( $nr, $optional );
$myAfterParm = [];
if ( array_key_exists( $nr, $afterparm ) ) {
$myAfterParm = $afterparm[$nr];
}
$text = self::updateTemplateCall( $matchCount, $text, $template, $call, $parm, $myValue, $myAfterParm, $myOptional );
}
if ( $exec == 'set' ) {
break; // values taken from dpl text only populate the first invocation
}
}
}
}
if ( $exec == 'set' ) {
return self::updateArticle( $title, $text, $summary );
} elseif ( $exec == 'preview' ) {
global $wgScriptPath, $wgRequest;
$titleX = \Title::newFromText( $title );
$articleX = new \Article( $titleX );
$form = '
';
return $form;
}
return "exec must be one of the following: edit, preview, set";
}
private static function updateArticle( $title, $text, $summary ) {
global $wgUser, $wgRequest, $wgOut;
if ( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
$wgOut->addWikiMsg( 'sessionfailure' );
return 'session failure';
}
$titleX = \Title::newFromText( $title );
$permission_errors = $titleX->getUserPermissionsErrors( 'edit', $wgUser );
if ( count( $permission_errors ) == 0 ) {
$articleX = \WikiPage::factory( $titleX );
$articleXContent = \ContentHandler::makeContent( $text, $titleX );
$articleX->doEditContent( $articleXContent, $summary, EDIT_UPDATE | EDIT_DEFER_UPDATES | EDIT_AUTOSUMMARY );
$wgOut->redirect( $titleX->getFullUrl( $articleX->isRedirect() ? 'redirect=no' : '' ) );
return '';
} else {
$wgOut->showPermissionsErrorPage( $permission_errors );
return 'permission error';
}
}
private static function editTemplateCall( $text, $template, $call, $parameter, $type, $value, $format, $legend, $instruction, $optional, $fieldFormat ) {
$matches = [];
$nlCount = preg_match_all( '/\n/', $value, $matches );
if ( $nlCount > 0 ) {
$rows = $nlCount + 1;
} else {
$rows = floor( strlen( $value ) / 50 ) + 1;
}
if ( preg_match( '/rows\s*=/', $format ) <= 0 ) {
$format .= " rows=$rows";
}
$cols = 50;
if ( preg_match( '/cols\s*=/', $format ) <= 0 ) {
$format .= " cols=$cols";
}
$textArea = "" . htmlspecialchars( $value ) . "";
return str_replace( '%NAME%', htmlspecialchars( str_replace( '_', ' ', $parameter ) ), str_replace( '%TYPE%', $type, str_replace( '%INPUT%', $textArea, str_replace( '%LEGEND%', "