1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
<?php
/**
* Hooks for WikiEditor extension
*
* @file
* @ingroup Extensions
*/
use MediaWiki\MediaWikiServices;
use WikimediaEvents\WikimediaEventsHooks;
class WikiEditorHooks {
// ID used for grouping entries all of a session's entries together in
// EventLogging.
private static $statsId = false;
/* Static Methods */
/**
* Log stuff to EventLogging's Schema:EditAttemptStep -
* see https://meta.wikimedia.org/wiki/Schema:EditAttemptStep
* If you don't have EventLogging installed, does nothing.
*
* @param string $action
* @param Article $article Which article (with full context, page, title, etc.)
* @param array $data Data to log for this action
* @return bool Whether the event was logged or not.
*/
public static function doEventLogging( $action, $article, $data = [] ) {
global $wgWMESchemaEditAttemptStepSamplingRate;
$extensionRegistry = ExtensionRegistry::getInstance();
if ( !$extensionRegistry->isLoaded( 'EventLogging' ) ) {
return false;
}
// Sample 6.25%
$samplingRate = $wgWMESchemaEditAttemptStepSamplingRate ?? 0.0625;
$inSample = EventLogging::sessionInSample(
(int)( 1 / $samplingRate ), $data['editing_session_id']
);
$shouldOversample = $extensionRegistry->isLoaded( 'WikimediaEvents' ) &&
WikimediaEventsHooks::shouldSchemaEditAttemptStepOversample( $article->getContext() );
if ( !$inSample && !$shouldOversample ) {
return false;
}
$user = $article->getContext()->getUser();
$page = $article->getPage();
$title = $article->getTitle();
$revisionRecord = $page->getRevisionRecord();
$data = [
'action' => $action,
'version' => 1,
'is_oversample' => !$inSample,
'editor_interface' => 'wikitext',
'platform' => 'desktop', // FIXME
'integration' => 'page',
'page_id' => $page->getId(),
'page_title' => $title->getPrefixedText(),
'page_ns' => $title->getNamespace(),
'revision_id' => $revisionRecord ? $revisionRecord->getId() : 0,
'user_id' => $user->getId(),
'user_editcount' => $user->getEditCount() ?: 0,
'mw_version' => MW_VERSION,
] + $data;
if ( $user->isAnon() ) {
$data['user_class'] = 'IP';
}
return EventLogging::logEvent( 'EditAttemptStep', 18530416, $data );
}
/**
* EditPage::showEditForm:initial hook
*
* Adds the modules to the edit form
*
* @param EditPage $editPage the current EditPage object.
* @param OutputPage $outputPage object.
*/
public static function editPageShowEditFormInitial( EditPage $editPage, OutputPage $outputPage ) {
if ( $editPage->contentModel !== CONTENT_MODEL_WIKITEXT ) {
return;
}
$article = $editPage->getArticle();
$request = $article->getContext()->getRequest();
// Add modules if enabled
$user = $article->getContext()->getUser();
if ( $user->getOption( 'usebetatoolbar' ) ) {
$outputPage->addModuleStyles( 'ext.wikiEditor.styles' );
$outputPage->addModules( 'ext.wikiEditor' );
}
// Don't run this if the request was posted - we don't want to log 'init' when the
// user just pressed 'Show preview' or 'Show changes', or switched from VE keeping
// changes.
if ( ExtensionRegistry::getInstance()->isLoaded( 'EventLogging' ) && !$request->wasPosted() ) {
$data = [];
$data['editing_session_id'] = self::getEditingStatsId( $request );
if ( $request->getVal( 'section' ) ) {
$data['init_type'] = 'section';
} else {
$data['init_type'] = 'page';
}
if ( $request->getHeader( 'Referer' ) ) {
if (
$request->getVal( 'section' ) === 'new'
|| !$article->getPage()->exists()
) {
$data['init_mechanism'] = 'new';
} else {
$data['init_mechanism'] = 'click';
}
} else {
$data['init_mechanism'] = 'url';
}
self::doEventLogging( 'init', $article, $data );
}
}
/**
* EditPage::showEditForm:fields hook
*
* Adds the event fields to the edit form
*
* @param EditPage $editPage the current EditPage object.
* @param OutputPage $outputPage object.
*/
public static function editPageShowEditFormFields( EditPage $editPage, OutputPage $outputPage ) {
if ( $editPage->contentModel !== CONTENT_MODEL_WIKITEXT ) {
return;
}
$req = $outputPage->getRequest();
$editingStatsId = self::getEditingStatsId( $req );
$shouldOversample = ExtensionRegistry::getInstance()->isLoaded( 'WikimediaEvents' ) &&
WikimediaEventsHooks::shouldSchemaEditAttemptStepOversample( $outputPage->getContext() );
$outputPage->addHTML(
Xml::element(
'input',
[
'type' => 'hidden',
'name' => 'editingStatsId',
'id' => 'editingStatsId',
'value' => $editingStatsId
]
)
);
if ( $shouldOversample ) {
$outputPage->addHTML(
Xml::element(
'input',
[
'type' => 'hidden',
'name' => 'editingStatsOversample',
'id' => 'editingStatsOversample',
'value' => 1
]
)
);
}
}
/**
* GetPreferences hook
*
* Adds WikiEditor-related items to the preferences
*
* @param User $user current user
* @param array &$defaultPreferences list of default user preference controls
*/
public static function getPreferences( $user, &$defaultPreferences ) {
// Ideally this key would be 'wikieditor-toolbar'
$defaultPreferences['usebetatoolbar'] = [
'type' => 'toggle',
'label-message' => 'wikieditor-toolbar-preference',
'help-message' => 'wikieditor-toolbar-preference-help',
'section' => 'editing/editor',
];
}
/**
* @param ResourceLoaderContext $context
* @param Config $config
* @return array
*/
public static function getModuleData( ResourceLoaderContext $context, Config $config ) {
return [
// expose magic words for use by the wikieditor toolbar
'magicWords' => self::getMagicWords(),
'signature' => self::getSignatureMessage( $context )
];
}
/**
* @param ResourceLoaderContext $context
* @param Config $config
* @return array
*/
public static function getModuleDataSummary( ResourceLoaderContext $context, Config $config ) {
return [
'magicWords' => self::getMagicWords(),
'signature' => self::getSignatureMessage( $context, true )
];
}
private static function getSignatureMessage( MessageLocalizer $ml, $raw = false ) {
$msg = $ml->msg( 'sig-text' )->params( '~~~~' )->inContentLanguage();
return $raw ? $msg->plain() : $msg->text();
}
/**
* Expose useful magic words which are used by the wikieditor toolbar
* @return string[]
*/
private static function getMagicWords() {
$requiredMagicWords = [
'redirect',
'img_right',
'img_left',
'img_none',
'img_center',
'img_thumbnail',
'img_framed',
'img_frameless',
];
$magicWords = [];
$factory = MediaWikiServices::getInstance()->getMagicWordFactory();
foreach ( $requiredMagicWords as $name ) {
$magicWords[$name] = $factory->get( $name )->getSynonym( 0 );
}
return $magicWords;
}
/**
* Gets a 32 character alphanumeric random string to be used for stats.
* @param WebRequest $request
* @return string
*/
private static function getEditingStatsId( WebRequest $request ) {
$fromRequest = $request->getVal( 'editingStatsId' );
if ( $fromRequest ) {
return $fromRequest;
}
if ( !self::$statsId ) {
self::$statsId = MWCryptRand::generateHex( 32 );
}
return self::$statsId;
}
/**
* This is attached to the MediaWiki 'EditPage::attemptSave' hook.
*
* @param EditPage $editPage
*/
public static function editPageAttemptSave( EditPage $editPage ) {
$article = $editPage->getArticle();
$request = $article->getContext()->getRequest();
if ( $request->getVal( 'editingStatsId' ) ) {
self::doEventLogging(
'saveAttempt',
$article,
[ 'editing_session_id' => $request->getVal( 'editingStatsId' ) ]
);
}
}
/**
* This is attached to the MediaWiki 'EditPage::attemptSave:after' hook.
*
* @param EditPage $editPage
* @param Status $status
*/
public static function editPageAttemptSaveAfter( EditPage $editPage, Status $status ) {
$article = $editPage->getArticle();
$request = $article->getContext()->getRequest();
if ( $request->getVal( 'editingStatsId' ) ) {
$data = [];
$data['editing_session_id'] = $request->getVal( 'editingStatsId' );
if ( $status->isOK() ) {
$action = 'saveSuccess';
} else {
$action = 'saveFailure';
$errors = $status->getErrorsArray();
if ( isset( $errors[0][0] ) ) {
$data['save_failure_message'] = $errors[0][0];
}
$wikiPage = $editPage->getArticle()->getPage();
if ( $status->value === EditPage::AS_CONFLICT_DETECTED ) {
$data['save_failure_type'] = 'editConflict';
} elseif ( $status->value === EditPage::AS_ARTICLE_WAS_DELETED ) {
$data['save_failure_type'] = 'editPageDeleted';
} elseif ( isset( $errors[0][0] ) && $errors[0][0] === 'abusefilter-disallowed' ) {
$data['save_failure_type'] = 'extensionAbuseFilter';
} elseif ( isset( $wikiPage->ConfirmEdit_ActivateCaptcha ) ) {
// TODO: :(
$data['save_failure_type'] = 'extensionCaptcha';
} elseif ( isset( $errors[0][0] ) && $errors[0][0] === 'spam-blacklisted-link' ) {
$data['save_failure_type'] = 'extensionSpamBlacklist';
} else {
// Catch everything else... We don't seem to get userBadToken or
// userNewUser through this hook.
$data['save_failure_type'] = 'responseUnknown';
}
}
self::doEventLogging( $action, $article, $data );
}
}
}
|