2010-09-15 03:02:35 +00:00
|
|
|
<?php
|
2010-09-15 22:40:50 +00:00
|
|
|
/**
|
2010-10-01 18:15:10 +00:00
|
|
|
* Hooks for WikiEditor extension
|
2011-09-13 08:56:32 +00:00
|
|
|
*
|
2010-09-15 22:40:50 +00:00
|
|
|
* @file
|
|
|
|
* @ingroup Extensions
|
|
|
|
*/
|
|
|
|
|
2018-07-30 16:28:56 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
2010-09-15 22:40:50 +00:00
|
|
|
class WikiEditorHooks {
|
2015-02-05 16:47:49 +00:00
|
|
|
// ID used for grouping entries all of a session's entries together in
|
|
|
|
// EventLogging.
|
|
|
|
private static $statsId = false;
|
2011-09-13 08:56:32 +00:00
|
|
|
|
2011-02-25 00:01:57 +00:00
|
|
|
/* Static Methods */
|
2011-09-13 08:56:32 +00:00
|
|
|
|
2015-02-05 16:47:49 +00:00
|
|
|
/**
|
|
|
|
* Log stuff to EventLogging's Schema:Edit - see https://meta.wikimedia.org/wiki/Schema:Edit
|
|
|
|
* 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.
|
|
|
|
*/
|
2016-05-08 22:57:53 +00:00
|
|
|
public static function doEventLogging( $action, $article, $data = [] ) {
|
2018-10-15 22:53:59 +00:00
|
|
|
global $wgVersion, $wgWMESchemaEditSamplingRate;
|
2018-08-19 15:17:03 +00:00
|
|
|
if ( !ExtensionRegistry::getInstance()->isLoaded( 'EventLogging' ) ) {
|
2015-02-05 16:47:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-10-15 22:53:59 +00:00
|
|
|
// Sample 6.25%
|
|
|
|
$samplingRate = $wgWMESchemaEditSamplingRate ?? 0.0625;
|
|
|
|
if ( !EventLogging::sessionInSample( 1 / $samplingRate, $data['editingSessionId'] ) ) {
|
2015-03-23 21:32:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-02-05 16:47:49 +00:00
|
|
|
|
|
|
|
$user = $article->getContext()->getUser();
|
|
|
|
$page = $article->getPage();
|
|
|
|
$title = $article->getTitle();
|
|
|
|
|
2016-05-08 22:57:53 +00:00
|
|
|
$data = [
|
2015-02-05 16:47:49 +00:00
|
|
|
'action' => $action,
|
|
|
|
'version' => 1,
|
|
|
|
'editor' => 'wikitext',
|
|
|
|
'platform' => 'desktop', // FIXME
|
|
|
|
'integration' => 'page',
|
|
|
|
'page.id' => $page->getId(),
|
|
|
|
'page.title' => $title->getPrefixedText(),
|
|
|
|
'page.ns' => $title->getNamespace(),
|
2015-03-13 23:43:44 +00:00
|
|
|
'page.revid' => $page->getRevision() ? $page->getRevision()->getId() : 0,
|
2015-02-05 16:47:49 +00:00
|
|
|
'user.id' => $user->getId(),
|
2015-03-13 23:08:21 +00:00
|
|
|
'user.editCount' => $user->getEditCount() ?: 0,
|
2015-02-05 16:47:49 +00:00
|
|
|
'mediawiki.version' => $wgVersion
|
2016-05-08 22:57:53 +00:00
|
|
|
] + $data;
|
2015-02-05 16:47:49 +00:00
|
|
|
|
|
|
|
if ( $user->isAnon() ) {
|
|
|
|
$data['user.class'] = 'IP';
|
|
|
|
}
|
|
|
|
|
2018-09-15 00:22:14 +00:00
|
|
|
return EventLogging::logEvent( 'Edit', 17541122, $data );
|
2015-02-05 16:47:49 +00:00
|
|
|
}
|
|
|
|
|
2010-09-15 22:40:50 +00:00
|
|
|
/**
|
2010-09-16 21:11:46 +00:00
|
|
|
* EditPage::showEditForm:initial hook
|
2011-09-13 08:56:32 +00:00
|
|
|
*
|
2010-09-15 22:40:50 +00:00
|
|
|
* Adds the modules to the edit form
|
2011-09-13 08:56:32 +00:00
|
|
|
*
|
2014-09-22 02:50:01 +00:00
|
|
|
* @param EditPage $editPage the current EditPage object.
|
|
|
|
* @param OutputPage $outputPage object.
|
2012-01-31 20:19:22 +00:00
|
|
|
* @return bool
|
2010-09-15 22:40:50 +00:00
|
|
|
*/
|
2017-09-01 21:15:26 +00:00
|
|
|
public static function editPageShowEditFormInitial( EditPage $editPage, OutputPage $outputPage ) {
|
2014-06-22 14:30:30 +00:00
|
|
|
if ( $editPage->contentModel !== CONTENT_MODEL_WIKITEXT ) {
|
|
|
|
return true;
|
|
|
|
}
|
2011-09-13 08:56:32 +00:00
|
|
|
|
2015-02-05 16:47:49 +00:00
|
|
|
$article = $editPage->getArticle();
|
|
|
|
$request = $article->getContext()->getRequest();
|
2017-11-22 19:59:00 +00:00
|
|
|
|
|
|
|
// Add modules if enabled
|
|
|
|
$user = $article->getContext()->getUser();
|
|
|
|
if ( $user->getOption( 'usebetatoolbar' ) ) {
|
|
|
|
$outputPage->addModuleStyles( 'ext.wikiEditor.styles' );
|
|
|
|
$outputPage->addModules( 'ext.wikiEditor' );
|
|
|
|
}
|
|
|
|
|
2015-02-05 16:47:49 +00:00
|
|
|
// 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.
|
2018-08-19 15:17:03 +00:00
|
|
|
if ( ExtensionRegistry::getInstance()->isLoaded( 'EventLogging' ) && !$request->wasPosted() ) {
|
2016-05-08 22:57:53 +00:00
|
|
|
$data = [];
|
2015-02-05 16:47:49 +00:00
|
|
|
$data['editingSessionId'] = self::getEditingStatsId();
|
2015-03-18 19:40:30 +00:00
|
|
|
if ( $request->getVal( 'section' ) ) {
|
2015-02-05 16:47:49 +00:00
|
|
|
$data['action.init.type'] = 'section';
|
|
|
|
} else {
|
|
|
|
$data['action.init.type'] = 'page';
|
|
|
|
}
|
|
|
|
if ( $request->getHeader( 'Referer' ) ) {
|
|
|
|
if ( $request->getVal( 'section' ) === 'new' || !$article->exists() ) {
|
|
|
|
$data['action.init.mechanism'] = 'new';
|
|
|
|
} else {
|
|
|
|
$data['action.init.mechanism'] = 'click';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$data['action.init.mechanism'] = 'url';
|
|
|
|
}
|
|
|
|
|
|
|
|
self::doEventLogging( 'init', $article, $data );
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* EditPage::showEditForm:fields hook
|
|
|
|
*
|
|
|
|
* Adds the event fields to the edit form
|
|
|
|
*
|
|
|
|
* @param EditPage $editPage the current EditPage object.
|
|
|
|
* @param OutputPage $outputPage object.
|
|
|
|
* @return bool
|
|
|
|
*/
|
2017-09-01 21:15:26 +00:00
|
|
|
public static function editPageShowEditFormFields( EditPage $editPage, OutputPage $outputPage ) {
|
2015-02-05 16:47:49 +00:00
|
|
|
if ( $editPage->contentModel !== CONTENT_MODEL_WIKITEXT ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-01 21:15:26 +00:00
|
|
|
$req = $outputPage->getRequest();
|
2015-02-05 16:47:49 +00:00
|
|
|
$editingStatsId = $req->getVal( 'editingStatsId' );
|
2015-04-19 23:31:10 +00:00
|
|
|
if ( !$editingStatsId || !$req->wasPosted() ) {
|
2015-02-05 16:47:49 +00:00
|
|
|
$editingStatsId = self::getEditingStatsId();
|
|
|
|
}
|
2015-03-23 21:32:28 +00:00
|
|
|
|
2015-02-05 16:47:49 +00:00
|
|
|
$outputPage->addHTML(
|
|
|
|
Xml::element(
|
|
|
|
'input',
|
2016-05-08 22:57:53 +00:00
|
|
|
[
|
2015-02-05 16:47:49 +00:00
|
|
|
'type' => 'hidden',
|
|
|
|
'name' => 'editingStatsId',
|
|
|
|
'id' => 'editingStatsId',
|
|
|
|
'value' => $editingStatsId
|
2016-05-08 22:57:53 +00:00
|
|
|
]
|
2015-02-05 16:47:49 +00:00
|
|
|
)
|
|
|
|
);
|
2010-09-15 22:40:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
2011-09-13 08:56:32 +00:00
|
|
|
|
2012-02-17 22:13:11 +00:00
|
|
|
/**
|
|
|
|
* EditPageBeforeEditToolbar hook
|
|
|
|
*
|
|
|
|
* Disable the old toolbar if the new one is enabled
|
|
|
|
*
|
2017-07-26 09:15:17 +00:00
|
|
|
* @param string &$toolbar
|
2012-02-17 22:13:11 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function EditPageBeforeEditToolbar( &$toolbar ) {
|
2017-11-22 19:59:00 +00:00
|
|
|
global $wgUser;
|
|
|
|
if ( $wgUser->getOption( 'usebetatoolbar' ) ) {
|
2017-12-29 14:09:21 +00:00
|
|
|
$toolbar = '';
|
2017-04-26 23:15:54 +00:00
|
|
|
// Return false to signify that the toolbar has been over-written, so
|
|
|
|
// the old toolbar code shouldn't be added to the page.
|
|
|
|
return false;
|
2012-02-17 22:13:11 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-09-15 22:40:50 +00:00
|
|
|
/**
|
|
|
|
* GetPreferences hook
|
2011-09-13 08:56:32 +00:00
|
|
|
*
|
2014-09-22 02:50:01 +00:00
|
|
|
* Adds WikiEditor-related items to the preferences
|
2011-09-13 08:56:32 +00:00
|
|
|
*
|
2014-09-22 02:50:01 +00:00
|
|
|
* @param User $user current user
|
2017-07-26 09:15:17 +00:00
|
|
|
* @param array &$defaultPreferences list of default user preference controls
|
2012-01-31 20:19:22 +00:00
|
|
|
* @return bool
|
2010-09-15 22:40:50 +00:00
|
|
|
*/
|
|
|
|
public static function getPreferences( $user, &$defaultPreferences ) {
|
2017-11-22 19:59:00 +00:00
|
|
|
// Ideally this key would be 'wikieditor-toolbar'
|
|
|
|
$defaultPreferences['usebetatoolbar'] = [
|
|
|
|
'type' => 'toggle',
|
|
|
|
'label-message' => 'wikieditor-toolbar-preference',
|
|
|
|
'section' => 'editing/editor',
|
|
|
|
];
|
2011-09-13 08:56:32 +00:00
|
|
|
|
2010-09-15 22:40:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
2011-09-13 08:56:32 +00:00
|
|
|
|
2010-09-15 22:40:50 +00:00
|
|
|
/**
|
2017-10-14 15:24:34 +00:00
|
|
|
* @param array &$vars
|
2012-01-31 20:19:22 +00:00
|
|
|
* @return bool
|
2010-09-15 22:40:50 +00:00
|
|
|
*/
|
2011-03-13 10:51:45 +00:00
|
|
|
public static function resourceLoaderGetConfigVars( &$vars ) {
|
2015-03-14 15:49:04 +00:00
|
|
|
// expose magic words for use by the wikieditor toolbar
|
2017-07-26 21:15:10 +00:00
|
|
|
self::getMagicWords( $vars );
|
2015-08-05 03:59:00 +00:00
|
|
|
|
2015-12-13 21:05:21 +00:00
|
|
|
$vars['mw.msg.wikieditor'] = wfMessage( 'sig-text', '~~~~' )->inContentLanguage()->text();
|
|
|
|
|
2010-09-15 22:40:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
2011-09-13 08:56:32 +00:00
|
|
|
|
2012-01-31 20:19:22 +00:00
|
|
|
/**
|
2014-09-01 14:07:28 +00:00
|
|
|
* ResourceLoaderTestModules hook
|
|
|
|
*
|
|
|
|
* Registers JavaScript test modules
|
|
|
|
*
|
2017-10-14 15:24:34 +00:00
|
|
|
* @param array &$testModules array of javascript testing modules. 'qunit' is fed using
|
2015-03-14 15:49:04 +00:00
|
|
|
* tests/qunit/QUnitTestResources.php.
|
2017-10-14 15:24:34 +00:00
|
|
|
* @param ResourceLoader &$resourceLoader
|
2014-09-01 14:07:28 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function resourceLoaderTestModules( &$testModules, &$resourceLoader ) {
|
2016-05-08 22:57:53 +00:00
|
|
|
$testModules['qunit']['ext.wikiEditor.toolbar.test'] = [
|
|
|
|
'scripts' => [ 'tests/qunit/ext.wikiEditor.toolbar.test.js' ],
|
2017-11-22 20:17:37 +00:00
|
|
|
'dependencies' => [ 'ext.wikiEditor' ],
|
2018-02-16 22:07:34 +00:00
|
|
|
'localBasePath' => __DIR__ . '/..',
|
2014-09-01 14:07:28 +00:00
|
|
|
'remoteExtPath' => 'WikiEditor',
|
2016-05-08 22:57:53 +00:00
|
|
|
];
|
2014-09-01 14:07:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MakeGlobalVariablesScript hook
|
|
|
|
*
|
|
|
|
* Adds enabled/disabled switches for WikiEditor modules
|
2017-10-14 15:24:34 +00:00
|
|
|
* @param array &$vars
|
2012-01-31 20:19:22 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2011-03-13 10:55:33 +00:00
|
|
|
public static function makeGlobalVariablesScript( &$vars ) {
|
|
|
|
// Build and export old-style wgWikiEditorEnabledModules object for back compat
|
2017-11-22 19:59:00 +00:00
|
|
|
$vars['wgWikiEditorEnabledModules'] = [];
|
2011-03-13 10:55:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-05-27 19:14:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Expose useful magic words which are used by the wikieditor toolbar
|
2017-10-14 15:24:34 +00:00
|
|
|
* @param array &$vars
|
2012-05-27 19:14:34 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2015-03-14 15:49:04 +00:00
|
|
|
private static function getMagicWords( &$vars ) {
|
2016-05-08 22:57:53 +00:00
|
|
|
$requiredMagicWords = [
|
2012-05-27 19:14:34 +00:00
|
|
|
'redirect',
|
|
|
|
'img_right',
|
|
|
|
'img_left',
|
|
|
|
'img_none',
|
|
|
|
'img_center',
|
|
|
|
'img_thumbnail',
|
|
|
|
'img_framed',
|
|
|
|
'img_frameless',
|
2016-05-08 22:57:53 +00:00
|
|
|
];
|
|
|
|
$magicWords = [];
|
2018-07-30 16:28:56 +00:00
|
|
|
if ( class_exists( MagicWordFactory::class ) ) {
|
|
|
|
$factory = MediaWikiServices::getInstance()->getMagicWordFactory();
|
|
|
|
}
|
2012-05-27 19:14:34 +00:00
|
|
|
foreach ( $requiredMagicWords as $name ) {
|
2018-07-30 16:28:56 +00:00
|
|
|
if ( class_exists( MagicWordFactory::class ) ) {
|
|
|
|
$magicWords[$name] = $factory->get( $name )->getSynonym( 0 );
|
|
|
|
} else {
|
|
|
|
$magicWords[$name] = MagicWord::get( $name )->getSynonym( 0 );
|
|
|
|
}
|
2014-09-22 02:50:01 +00:00
|
|
|
}
|
2012-05-27 19:14:34 +00:00
|
|
|
$vars['wgWikiEditorMagicWords'] = $magicWords;
|
2017-12-24 03:36:04 +00:00
|
|
|
return true;
|
2012-05-27 19:14:34 +00:00
|
|
|
}
|
|
|
|
|
2015-02-05 16:47:49 +00:00
|
|
|
/**
|
|
|
|
* Gets a 32 character alphanumeric random string to be used for stats.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private static function getEditingStatsId() {
|
2018-02-06 20:16:40 +00:00
|
|
|
if ( !self::$statsId ) {
|
|
|
|
self::$statsId = MWCryptRand::generateHex( 32 );
|
2015-02-05 16:47:49 +00:00
|
|
|
}
|
2018-02-06 20:16:40 +00:00
|
|
|
return self::$statsId;
|
2015-02-05 16:47:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is attached to the MediaWiki 'EditPage::attemptSave' hook.
|
|
|
|
*
|
|
|
|
* @param EditPage $editPage
|
2017-07-26 21:15:10 +00:00
|
|
|
* @return bool
|
2015-02-05 16:47:49 +00:00
|
|
|
*/
|
|
|
|
public static function editPageAttemptSave( EditPage $editPage ) {
|
|
|
|
$article = $editPage->getArticle();
|
|
|
|
$request = $article->getContext()->getRequest();
|
2015-03-18 19:40:30 +00:00
|
|
|
if ( $request->getVal( 'editingStatsId' ) ) {
|
2015-02-05 16:47:49 +00:00
|
|
|
self::doEventLogging(
|
|
|
|
'saveAttempt',
|
|
|
|
$article,
|
2016-05-08 22:57:53 +00:00
|
|
|
[ 'editingSessionId' => $request->getVal( 'editingStatsId' ) ]
|
2015-02-05 16:47:49 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is attached to the MediaWiki 'EditPage::attemptSave:after' hook.
|
|
|
|
*
|
|
|
|
* @param EditPage $editPage
|
|
|
|
* @param Status $status
|
2017-07-26 21:15:10 +00:00
|
|
|
* @return bool
|
2015-02-05 16:47:49 +00:00
|
|
|
*/
|
|
|
|
public static function editPageAttemptSaveAfter( EditPage $editPage, Status $status ) {
|
|
|
|
$article = $editPage->getArticle();
|
|
|
|
$request = $article->getContext()->getRequest();
|
2015-03-18 19:40:30 +00:00
|
|
|
if ( $request->getVal( 'editingStatsId' ) ) {
|
2016-05-08 22:57:53 +00:00
|
|
|
$data = [];
|
2015-03-13 23:19:12 +00:00
|
|
|
$data['editingSessionId'] = $request->getVal( 'editingStatsId' );
|
2015-02-05 16:47:49 +00:00
|
|
|
|
|
|
|
if ( $status->isOK() ) {
|
|
|
|
$action = 'saveSuccess';
|
|
|
|
} else {
|
|
|
|
$action = 'saveFailure';
|
|
|
|
$errors = $status->getErrorsArray();
|
|
|
|
|
|
|
|
if ( isset( $errors[0][0] ) ) {
|
|
|
|
$data['action.saveFailure.message'] = $errors[0][0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $status->value === EditPage::AS_CONFLICT_DETECTED ) {
|
|
|
|
$data['action.saveFailure.type'] = 'editConflict';
|
2015-03-14 15:49:04 +00:00
|
|
|
} elseif ( $status->value === EditPage::AS_ARTICLE_WAS_DELETED ) {
|
2015-02-05 16:47:49 +00:00
|
|
|
$data['action.saveFailure.type'] = 'editPageDeleted';
|
2015-03-14 15:49:04 +00:00
|
|
|
} elseif ( isset( $errors[0][0] ) && $errors[0][0] === 'abusefilter-disallowed' ) {
|
2015-02-05 16:47:49 +00:00
|
|
|
$data['action.saveFailure.type'] = 'extensionAbuseFilter';
|
2015-03-14 15:49:04 +00:00
|
|
|
} elseif ( isset( $editPage->getArticle()->getPage()->ConfirmEdit_ActivateCaptcha ) ) {
|
2015-02-05 16:47:49 +00:00
|
|
|
// TODO: :(
|
|
|
|
$data['action.saveFailure.type'] = 'extensionCaptcha';
|
2015-03-14 15:49:04 +00:00
|
|
|
} elseif ( isset( $errors[0][0] ) && $errors[0][0] === 'spamprotectiontext' ) {
|
2015-02-05 16:47:49 +00:00
|
|
|
$data['action.saveFailure.type'] = 'extensionSpamBlacklist';
|
|
|
|
} else {
|
2015-03-14 15:49:04 +00:00
|
|
|
// Catch everything else... We don't seem to get userBadToken or
|
|
|
|
// userNewUser through this hook.
|
|
|
|
$data['action.saveFailure.type'] = 'responseUnknown';
|
2015-02-05 16:47:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
self::doEventLogging( $action, $article, $data );
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2010-11-19 13:59:34 +00:00
|
|
|
}
|