
As it is not difficult to read
on MediaWiki , the current line of stable versions (1.16.x) comes with the built-in jQuery library, the code of which lies at
skins / common / jquery.min.js. This is a jQuery of a relatively old version (1.3.2, released in February 2009), slightly patched to overcome one of its bugs, and it runs in compatibility mode
(“ noConflict () ”) so as to fit
into the $ j variable instead of $ . This jQuery code does not contain any plugins and is, in essence, optional, connecting to the page only if the MediaWiki code contains a call to the
$ wgOut-> includeJQuery () method. Say, the
UsabilityInitiative extension contains just such a call on line 128 of its
“UsabilityInitiative.hooks.php” file
, so that jQuery appears in some skin themes (for example, in Vector) when the UsabilityInitiative extension is connected to a wiki.
I suggest that you get to the point of view of the
wiki technician, that is, the administrator of the MediaWiki server with direct access (like SSH) and the ability to change the settings of the wiki (LocalSettings.php), install MediaWiki extensions and carry out other similar actions. What circumstances might cause
a wiki MediaWiki 1.16.x desire to change for the better the state of affairs mentioned in the previous paragraph? Which way is most appropriate for a
wiki technique to translate this aspiration?
')
The first impetus of such a striving is the natural and almost inevitable realization of how inconvenient is the appearance of jQuery in the page code, which is configured by default. Any
wiki technician, if he has enough leisure for reflection, sooner or later comprehends that the simplicity and power of
the $ ( ...
) function , if it were
always at the disposal of administrators who compose scripts for a wiki, would definitely create a
JS code that is more compact than the former. Take for example the
May 22nd, 2011 version of the “MediaWiki: Common.js” page from the Russian-language Wikipedia. It is not difficult to notice the function code here:
function editZeroSection(){ var body = document.getElementById('bodyContent') if (!body) return var h2s = body.getElementsByTagName('H2') var h2 = h2s[0] if (!h2) return if (h2.parentNode.id == 'toctitle') h2 = h2s[1] if (!h2) return var span = h2.firstChild if (!span || span.className != 'editsection') return var zero = span.cloneNode(true) body.insertBefore(zero, body.firstChild) var a = zero.getElementsByTagName('a')[0] if (a.href.indexOf('§ion=T') == -1 ) a.title = a.title.replace(/:.*$/,': 0') else a.title = ' : 0' a.setAttribute('href', wgScript + '?title='+encodeURIComponent(wgPageName) + '&action=edit§ion=0') }
Any jQuery fan is well aware of how this library would completely spare the function authors from having to resort
to getElementById () and getElementsByTagName () , and would also provide abbreviated analogs of some other methods and fields. But such a function is not there alone. Not one and such a wiki: it is not difficult to see similar bulkiness, for example,
in the version of May 28, 2011, the page “MediaWiki: Common.js” from “Absurdopedia”.
Assume
that the wiki technician ensured the rigorous appearance of jQuery in the page code, following the
official instructions and adding to the LocalSettings.php file with the appropriate code:
// Include jQuery function wfIncludeJQuery() { global $wgOut; $wgOut->includeJQuery(); } $wgExtensionFunctions[] = 'wfIncludeJQuery';
The wiki administrators, with rights to edit the MediaWiki: Common.js page, began rewriting the functions on jQuery,
using $ ( ... ) instead of
addOnloadHook ( ... ) , and so on. What two stumbling blocks await this process in the future?
Firstly, when meeting jQuery history, it will certainly reveal
that version 1.3.2 (as I mentioned a little higher) appeared quite a while ago -
as early
as February 2009 . There is no need to tell the regular readers of the habrahabrovsky
jQuery blog how great and significant has been the development of the library over the past 2⅓ years; for everyone else, I’ll just give an example of
accelerating animation in version 1.4.3, new interfaces
deferred ([ 1 ], [ 2 ]) in version 1.5,
full support for IE9 in version 1.5.1 ... and changes in version 1.4 are
14 pages in general set out.
Secondly, hardly having finished transferring existing scripts
to the jQ-basis, wiki administrators will naturally think about what other possibilities open up for automation (scripting) of the wiki for the better. Almost inevitably, their acquaintance with such an amazing tool as jQuery plug-ins serve, allowing in a simple way (“copy-paste”) in an instant to provide the site with significant, impressive improvements. For example, it is enough to add
jQuery.ScrollTo and jQuery.LocalScroll - and immediately
immediately all the inside - page hyperlinks can be provided with a spectacular page scrolling animation, using a simple script:
$(function(){ $.localScroll({ hash: true, onAfter: function(target){ location = '#' + ( target.id || target.name ); } }); });
For a wiki on the MediaWiki engine, with its automatic table of contents articles, such an improvement in behavior is invaluable.
We see: at this stage, the course of events inexorably pushes the
wiki technician to create a file (best of all,
one file, so that time is not wasted on unnecessary requests to the server) containing both the new jQuery version of the library and all the desired plugins for it . Will
skins / common / jquery.min.js fit the role of such a file? It is unlikely and there are two reasons.
First, as you know , any transition to a new version of MediaWiki (even from version 1.16.x to version
1.16.x + 1) is associated with writing new files on top of old ones - the slightest inattention of the update may lead to the fact that the file
“skins / common /jquery.min.js ” will be replaced again by the jQuery version shipped with MediaWiki (1.3.2, without plug-ins).
Secondly, it’s generally
not very logical to assign jQuery updates and
wiki technology plug
- ins and require direct access
to the wiki server for this
, since many other MediaWiki javascripts (such as the aforementioned MediaWiki: Common.js, for example ) any administrator can edit through the wiki interface itself, besides equipped with a convenient built-in version control system and viewing the difference of edits. It turns out that it would be more logical for jQuery, along with all relevant plugins in a specific wiki, to be placed on a page with the name “MediaWiki: jQuery.js”, but calm down.
Is this placement technically feasible? Yes, it is quite achievable; all you need to do is remove the code from the
LocalSettings.php file with the call to the
includeJQuery () method that was added there in the previous step - and instead invoke a call to the MediaWiki extension that would enable the connection of the MediaWiki: jQuery.js script , yes, in addition, it would bite the call to the
“skins / common / jquery.min.js” script in case the call to the
includeJQuery () method still comes
from some other extension.
The task of writing such a MediaWiki extension code that would connect “MediaWiki: jQuery.js” to each wiki page was solved by me long ago - its answer (its code) looks like this (including the MIT license):
<?php # Confirm MW environment if (defined('MEDIAWIKI')) { # Credits $wgExtensionCredits['other'][] = array( 'name' => 'jQuery', 'author' => 'Mithgol the Webmaster', 'url' => 'http://traditio.ru/wiki/JQuery_%D0%B4%D0%BB%D1%8F_MediaWiki', 'description' => 'Adds jQuery to the list of scripts (HEAD element).', 'version' => '0.2' ); function jQueryMyHook( &$output, &$skin ) { $output->mScripts = "\t\t". '<script type="text/javascript" src="/w/index.php?title=MediaWiki:jQuery.js&action=raw&ctype=text/javascript"></script>'. "\n" . preg_replace('/<script src="\/w\/skins\/common\/jquery\.min\.js\?\d+"><\/script>/', '<!-- jQuery double, erased -->', $output->mScripts); return true; } $wgHooks['BeforePageDisplay'][] = 'jQueryMyHook'; } # End MW Environment wrapper ?>
Please note that the
“skins / common / jquery.min.js” file clipper takes into account the MediaWiki style of adding
“?” And the number to the script URL.
When filling in the MediaWiki: jQuery.js file, it is appropriate to provide backward compatibility
with skins / common / jquery.min.js in the jQuery name format. For this purpose, it is quite enough to place the following code in the MediaWiki: jQuery.js file:
/* MediaWiki: * 1) /skins/common/edit.js * 2) /skins/common/preview.js * , jQuery $j. * : */ $j=$;
Obviously, I hope that this synonym should be created after the code of the jQuery library itself.
The above expansion was introduced in the Russian “Tradition”
wiki-encyclopedia in 2010 and managed to fully prove its suitability. Some of the improvements and conveniences created by the scripts that the use of jQuery in MediaWiki makes possible deserve, perhaps, separate blogs in Habrahabr, but my current story is over.