Commit 54fbb3af authored by Marijn van Wezel's avatar Marijn van Wezel
Browse files

Merge branch 'add-structured-logging'

parents a5b452c0 b222e506
Pipeline #2769 passed with stages
in 28 seconds
......@@ -15,6 +15,7 @@
"apihelp-editslot-param-append": "Whether to append the text to the existing slot content",
"apihelp-editslot-param-summary": "Edit summary.",
"wsslots-apierror-unknownslot": "Unknown slot '$1'.",
"wsslots-error-invalid-wikipage-object": "The given WikiPage object is not valid, since it does not have a Title.",
"tag-wsslots-slot-edit": "Metadata slot edit",
"tag-wsslots-slot-edit-description": "Edit made using WSSlots"
}
......@@ -69,6 +69,11 @@ class ApiEditSlot extends ApiBase {
if ($result !== true) {
list($message, $code) = $result;
Logger::getLogger()->alert('Editing slot failed while performing edit through the "editslot" API: {message}', [
'message' => $message
]);
$this->dieWithError($message, $code);
}
}
......
<?php
/**
* WSSlots MediaWiki extension
* Copyright (C) 2021 Wikibase Solutions
*
* 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 WSSlots;
use MediaWiki\Logger\LoggerFactory;
use Psr\Log\LoggerInterface;
/**
* Class Logger
*
* @package WSSlots
*/
class Logger {
// The logging channel for this extension
const LOGGING_CHANNEL = 'wsslots';
/**
* @var LoggerInterface An instance of a logger
*/
private static $loggerInstance;
/**
* Returns the logger instance.
*
* @return LoggerInterface
*/
public static function getLogger(): LoggerInterface {
if (!isset(self::$loggerInstance)) {
self::$loggerInstance = LoggerFactory::getInstance(self::LOGGING_CHANNEL);
}
return self::$loggerInstance;
}
}
......@@ -21,12 +21,9 @@ class MediaWikiServicesHookHandler implements MediaWikiServicesHook {
* @inheritDoc
*/
public function onMediaWikiServices( $services ) {
/** @var MediaWikiServices $services */
$logger = LoggerFactory::getInstance( "WSSlots" );
$config = $services->getMainConfig();
$service_manipulator = new SlotRoleRegistryServiceManipulator( $config, $logger );
$service_manipulator = new SlotRoleRegistryServiceManipulator( $config );
$manipulator = [ $service_manipulator, "defineRoles" ];
$services->addServiceManipulator( "SlotRoleRegistry", $manipulator );
}
......
......@@ -29,11 +29,10 @@ class SlotRoleRegistryServiceManipulator {
* SlotRoleRegistryServiceManipulator constructor.
*
* @param Config $config The Config to use
* @param LoggerInterface $logger The logger to send log messages to
*/
public function __construct( Config $config, LoggerInterface $logger ) {
public function __construct( Config $config ) {
$this->config = $config;
$this->logger = $logger;
$this->logger = Logger::getLogger();
}
/**
......
......@@ -49,13 +49,30 @@ abstract class WSSlots {
bool $append = false,
string $watchlist = ""
) {
$logger = Logger::getLogger();
$title_object = $wikipage_object->getTitle();
$page_updater = $wikipage_object->newPageUpdater( $user );
$old_revision_record = $wikipage_object->getRevisionRecord();
$slot_role_registry = MediaWikiServices::getInstance()->getSlotRoleRegistry();
if ($title_object === null) {
$logger->alert('The WikiPage object given to editSlot is not valid, since it does not contain a Title');
return [wfMessage( "wsslots-error-invalid-wikipage-object")];
}
$logger->debug('Editing slot {slotName} on page {page}', [
'slotName' => $slot_name,
'page' => $title_object->getFullText()
]);
// Make sure the slot we are editing exists
if ( !$slot_role_registry->isDefinedRole( $slot_name ) ) {
$logger->alert('Tried to edit non-existent slot {slotName} on page {page}', [
'slotName' => $slot_name,
'page' => $title_object->getFullText()
]);
return [wfMessage( "wsslots-apierror-unknownslot", $slot_name ), "unknownslot"];
}
......@@ -68,6 +85,13 @@ abstract class WSSlots {
if ( !( $content instanceof TextContent ) ) {
$slot_content_handler = $content->getContentHandler();
$model_id = $slot_content_handler->getModelID();
$logger->alert('Tried to append to slot {slotName} with non-textual content model {modelId} while editing page {page}', [
'slotName' => $slot_name,
'modelId' => $model_id,
'page' => $title_object->getFullText()
]);
return [wfMessage( "apierror-appendnotsupported" ), $model_id];
}
......@@ -79,6 +103,10 @@ abstract class WSSlots {
if ( $text === "" && $slot_name !== SlotRecord::MAIN ) {
// Remove the slot if $text is empty and the slot name is not MAIN
$logger->debug('Removing slot {slotName} since it is empty', [
'slotName' => $slot_name
]);
$page_updater->removeSlot( $slot_name );
} else {
// Set the content for the slot we want to edit
......@@ -94,12 +122,16 @@ abstract class WSSlots {
->getDefaultModel( $title_object );
}
$logger->debug('Setting content in PageUpdater');
$slot_content = ContentHandler::makeContent( $text, $title_object, $model_id );
$page_updater->setContent( $slot_name, $slot_content );
}
if ( $old_revision_record === null ) {
if ( $old_revision_record === null && $slot_name !== SlotRecord::MAIN ) {
// The 'main' content slot MUST be set when creating a new page
$logger->debug('Setting empty "main" slot');
$main_content = ContentHandler::makeContent("", $title_object);
$page_updater->setContent( SlotRecord::MAIN, $main_content );
}
......@@ -115,9 +147,14 @@ abstract class WSSlots {
$flags |= EDIT_SUPPRESS_RC;
}
$logger->debug('Calling saveRevision on PageUpdater');
$page_updater->saveRevision( $comment, $flags );
$logger->debug('Finished calling saveRevision on PageUpdater');
if ( !$page_updater->isUnchanged() ) {
$logger->debug('Refreshing data for page {page}', [
'page' => $title_object->getFullText()
]);
self::refreshData( $wikipage_object, $user );
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment