Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Community
WSSlots
Commits
54fbb3af
Commit
54fbb3af
authored
Apr 04, 2022
by
Marijn van Wezel
❤
Browse files
Merge branch 'add-structured-logging'
parents
a5b452c0
b222e506
Pipeline
#2769
passed with stages
in 28 seconds
Changes
6
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
i18n/en.json
View file @
54fbb3af
...
...
@@ -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"
}
src/ApiEditSlot.php
View file @
54fbb3af
...
...
@@ -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
);
}
}
...
...
src/Logger.php
0 → 100644
View file @
54fbb3af
<?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
;
}
}
src/MediaWikiServicesHookHandler.php
View file @
54fbb3af
...
...
@@ -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
);
}
...
...
src/SlotRoleRegistryServiceManipulator.php
View file @
54fbb3af
...
...
@@ -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
=
$l
ogger
;
$this
->
logger
=
L
ogger
::
getLogger
()
;
}
/**
...
...
src/WSSlots.php
View file @
54fbb3af
...
...
@@ -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
);
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment