Jeroen De Dauw has submitted this change and it was merged.
Change subject: Add autocompletion, options to modify existing pages & use
VisualEditor
......................................................................
Add autocompletion, options to modify existing pages & use VisualEditor
* Autocomplete on page names in appropriate namespaces.
* When user selects a page that exists, optionally open it in edit mode.
* Optionally open pages in VisualEditor.
Change-Id: I969e4904bdb0613fd09f997aa2326f2e2dfe1d71
---
M CreatePage.php
M RELEASE-NOTES
M SpecialCreatePageRedirect.php
A modules/createPage.searchSuggest.js
4 files changed, 221 insertions(+), 32 deletions(-)
Approvals:
Jeroen De Dauw: Verified; Looks good to me, approved
jenkins-bot: Checked
diff --git a/CreatePage.php b/CreatePage.php
index 1b1f5fc..88a2dbb 100644
--- a/CreatePage.php
+++ b/CreatePage.php
@@ -25,13 +25,14 @@
}
global $wgVersion, $wgExtensionCredits, $wgExtensionMessagesFiles,
$wgAutoloadClasses;
-global $wgSpecialPages, $wgHooks;
+global $wgSpecialPages, $wgHooks, $wgResourceModules;
-if ( version_compare( $wgVersion, '1.18c', '<' ) ) { // Needs to be 1.18c
because version_compare() works in confusing ways.
+// Needs to be 1.18c because version_compare() works in confusing ways.
+if ( version_compare( $wgVersion, '1.18c', '<' ) ) {
die( '<b>Error:</b> Create Page requires MediaWiki 1.18 or above.' );
}
-define( 'CP_VERSION', '0.3.0' );
+define( 'CP_VERSION', '0.4.0' );
$wgExtensionCredits['other'][] = array(
'path' => __FILE__,
@@ -39,19 +40,32 @@
'version' => CP_VERSION,
'author' => array(
'[https://www.mediawiki.org/wiki/User:Jeroen_De_Dauw Jeroen De
Dauw]',
+ 'Ike Hecht'
),
'url' => 'https://www.mediawiki.org/wiki/Extension:Create_Page',
'descriptionmsg' => 'cp-desc'
);
+//Configuration
+/* Set to true to edit existing pages. */
+$wgCreatePageEditExisting = false;
+/* Set to true to redirect to VisualEditor for page creation. */
+$wgCreatePageUseVisualEditor = false;
// i18n
$wgMessagesDirs['CreatePage'] = __DIR__ . '/i18n';
-$wgExtensionMessagesFiles['CreatePage'] = dirname( __FILE__ ) .
'/CreatePage.i18n.php';
-$wgExtensionMessagesFiles['CreatePageAlias'] = dirname( __FILE__ ) .
'/CreatePage.alias.php';
-$wgExtensionMessagesFiles['CreatePageMagic'] = dirname( __FILE__ ) .
'/CreatePage.magic.php';
+$wgExtensionMessagesFiles['CreatePage'] = __DIR__ . '/CreatePage.i18n.php';
+$wgExtensionMessagesFiles['CreatePageAlias'] = __DIR__ .
'/CreatePage.alias.php';
+$wgExtensionMessagesFiles['CreatePageMagic'] = __DIR__ .
'/CreatePage.magic.php';
-$wgAutoloadClasses['SpecialCreatePageRedirect'] = dirname( __FILE__ ) .
'/SpecialCreatePageRedirect.php';
+$wgAutoloadClasses['SpecialCreatePageRedirect'] = __DIR__ .
'/SpecialCreatePageRedirect.php';
$wgSpecialPages['CreatePageRedirect'] = 'SpecialCreatePageRedirect';
+
+$wgResourceModules['ext.createPage'] = array(
+ 'scripts' => 'modules/createPage.searchSuggest.js',
+ 'dependencies' => 'mediawiki.searchSuggest',
+ 'localBasePath' => __DIR__,
+ 'remoteExtPath' => 'CreatePage'
+);
$wgHooks['ParserFirstCallInit'][] = function( Parser &$parser ) {
$parser->setFunctionHook( 'createpage', function( Parser $parser,
PPFrame $frame, array $args ) {
@@ -59,23 +73,33 @@
'action' => SpecialPage::getTitleFor(
'CreatePageRedirect' )->getLocalURL(),
'method' => 'post',
'style' => 'display: inline',
- ) );
+ 'class' => 'createpageform'
+ ) );
$html .= Html::input(
'pagename',
- array_key_exists( 1, $args ) ? trim( $frame->expand(
$args[1] ) ) : ''
+ array_key_exists( 1, $args ) ? trim( $frame->expand(
$args[1] ) ) : '', 'text',
+ array( 'class' => 'pagenameinput' )
);
if ( array_key_exists( 0, $args ) ) {
- $html .= Html::hidden( 'pagens', trim( $frame->expand(
$args[0] ) ) );
+ $namespaceText = trim( $frame->expand( $args[0] ) );
+ $attribs = array();
+
+ // Find the ID of this namespace, if there is one.
+ $namespaceID = MWNamespace::getCanonicalIndex(
strtolower( $namespaceText ) );
+ if ( $namespaceID != 0 ) {
+ $attribs['nsid'] = $namespaceID;
+ }
+ $html .= Html::hidden( 'pagens', $namespaceText,
$attribs );
}
$html .= ' ';
$html .= Html::input(
'createpage',
- array_key_exists( 2, $args ) ? trim( $frame->expand(
$args[2] ) ) : wfMessage( 'cp-create' )->text(),
- 'submit'
+ array_key_exists( 2, $args ) ? trim( $frame->expand(
$args[2] ) ) :
+ wfMessage( 'cp-create' )->text(),
'submit'
);
if ( array_key_exists( 3, $args ) ) {
@@ -89,3 +113,8 @@
return true;
};
+
+$wgHooks['BeforePageDisplay'][] = function( OutputPage &$out ) {
+ $out->addModules( 'ext.createPage' );
+ return true;
+};
diff --git a/RELEASE-NOTES b/RELEASE-NOTES
index b909b9c..ae40ccd 100644
--- a/RELEASE-NOTES
+++ b/RELEASE-NOTES
@@ -2,6 +2,10 @@
Extension page on mediawiki.org:
https://www.mediawiki.org/wiki/Extension:Create_Page
+== Version 0.4 ==
+* Added $wgCreatePageEditExisting variable. Set to true to edit existing pages.
+* Added $wgCreatePageUseVisualEditor variable. Set to true to redirect to
VisualEditor for page creation.
+* Added autocomplete to input field.
=== Version 0.2 ===
(2013-07-07)
@@ -13,4 +17,4 @@
=== Version 0.1 ===
(2012-02-04)
-Initial release.
\ No newline at end of file
+Initial release.
diff --git a/SpecialCreatePageRedirect.php b/SpecialCreatePageRedirect.php
index 3b98e29..86730f6 100644
--- a/SpecialCreatePageRedirect.php
+++ b/SpecialCreatePageRedirect.php
@@ -12,32 +12,47 @@
* @author Jeroen De Dauw < [email protected] >
*/
class SpecialCreatePageRedirect extends UnlistedSpecialPage {
-
+
public function __construct() {
parent::__construct( 'CreatePageRedirect' );
}
-
+
public function execute( $subPage ) {
$req = $this->getRequest();
-
- if ( $req->wasPosted() && $req->getCheck( 'pagename' ) ) {
- $parts = array( $req->getText( 'pagename' ) );
-
- if ( $req->getCheck( 'pagens' ) ) {
- array_unshift( $parts, $req->getText( 'pagens'
) );
+ if ( $req->getCheck( 'pagename' ) ) {
+ $pageName = $req->getText( 'pagename' );
+ $pageNamespace = $req->getText( 'pagens' );
+ if ( $pageNamespace == '' ||
+ substr( $pageName, 0, strlen( "$pageNamespace:"
) ) == "$pageNamespace:" ) {
+ $title = Title::newFromText( $pageName );
+ } else {
+ $title = Title::newFromText(
"$pageNamespace:$pageName" );
}
-
- $target = Title::newFromText( implode( ':', $parts )
)->getLocalUrl( array(
- 'action' => 'edit',
- 'redlink' => '1',
- 'preload' => $req->getText( 'preload', '' )
- ) );
- }
- else {
+ $target = $this->getTargetURL( $title );
+ } else {
$target = Title::newMainPage()->getLocalURL();
}
-
$this->getOutput()->redirect( $target, '301' );
}
-
-}
\ No newline at end of file
+
+ private function getTargetURL( Title $title ) {
+ global $wgCreatePageEditExisting, $wgCreatePageUseVisualEditor;
+
+ $isKnown = $title->isKnown();
+ $query = array();
+ if ( !$isKnown || $wgCreatePageEditExisting ) {
+ # Preload is not yet supported by VisualEditor, but
probably will be eventually.
+ # See https://phabricator.wikimedia.org/T51622
+ $query['preload'] = $this->getRequest()->getText(
'preload', '' );
+ if ( !$isKnown ) {
+ $query['redlink'] = '1';
+ }
+ if ( $wgCreatePageUseVisualEditor ) {
+ $query['veaction'] = 'edit';
+ } else {
+ $query['action'] = 'edit';
+ }
+ }
+ return $title->getLocalUrl( $query );
+ }
+}
diff --git a/modules/createPage.searchSuggest.js
b/modules/createPage.searchSuggest.js
new file mode 100644
index 0000000..d4aa1fc
--- /dev/null
+++ b/modules/createPage.searchSuggest.js
@@ -0,0 +1,141 @@
+/*!
+ * Add page name suggestions to the createPage form.
+ * Borrowed from mediawiki.searchSuggest.js - main modifications include
removal of unneeded code and
+ * renaming of the selectors.
+ */
+( function ( mw, $ ) {
+ $( function () {
+ var api, map, resultRenderCache, searchboxesSelectors,
+ // Region where the suggestions box will appear
directly below
+ // (using the same width). Can be a container element
or the input
+ // itself, depending on what suits best in the
environment.
+ // For Vector the suggestion box should align with the
createpageform
+ // container's borders, in other skins it should align
with the input
+ // element (not the search form, as that would leave
the buttons
+ // vertically between the input and the suggestions).
+ $searchRegion = $( '.createpageform' );
+
+ // Compatibility map
+ map = {
+ // SimpleSearch is broken in Opera < 9.6
+ opera: [['>=', 9.6]],
+ // Older Konquerors are unable to position the
suggestions correctly (bug 50805)
+ konqueror: [['>=', '4.11']],
+ docomo: false,
+ blackberry: false,
+ // Support for iOS 6 or higher. It has not been tested
on iOS 5 or lower
+ ipod: [['>=', 6]],
+ iphone: [['>=', 6]]
+ };
+
+ if ( !$.client.test( map ) ) {
+ return;
+ }
+
+ // Compute form data for search suggestions functionality.
+ function computeResultRenderCache( context ) {
+ var $form, baseHref, linkParams;
+
+ // Compute common parameters for links' hrefs
+ $form = context.config.$region.closest( 'form' );
+
+ baseHref = $form.attr( 'action' );
+ baseHref += baseHref.indexOf( '?' ) > -1 ? '&' : '?';
+
+ linkParams = $form.serializeObject();
+
+ return {
+ textParam: context.data.$textbox.attr( 'name' ),
+ linkParams: linkParams,
+ baseHref: baseHref
+ };
+ }
+
+ // The function used to render the suggestions.
+ function renderFunction( text, context ) {
+ resultRenderCache = computeResultRenderCache( context );
+
+ // linkParams object is modified and reused
+ resultRenderCache.linkParams[
resultRenderCache.textParam ] = text;
+
+ // this is the container <div>, jQueryfied
+ this.text( text )
+ .wrap(
+ $( '<a>' )
+ .attr( 'href',
resultRenderCache.baseHref + $.param( resultRenderCache.linkParams ) )
+ .attr( 'title', text )
+ .addClass(
'mw-searchSuggest-link' )
+ );
+ }
+
+ searchboxesSelectors = [
+ '.pagenameinput'
+ ];
+ $( searchboxesSelectors.join( ', ' ) )
+ .suggestions( {
+ fetch: function ( query, response, maxRows ) {
+ var node = this[0];
+
+ api = api || new mw.Api();
+
+ // Set the namespace on which to search,
+ // potentially based on a hidden HTML
+ // attribute.
+ var searchNS =
$(this).parent().find('input[name=pagens]').attr('nsid');
+ if ( searchNS === undefined ) {
+ searchNS = 0;
+ }
+
+ $.data( node, 'request', api.get( {
+ action: 'opensearch',
+ search: query,
+ namespace: searchNS,
+ limit: maxRows,
+ suggest: ''
+ } ).done( function ( data ) {
+ response( data[ 1 ] );
+ } ) );
+ },
+ cancel: function () {
+ var node = this[0],
+ request = $.data( node,
'request' );
+
+ if ( request ) {
+ request.abort();
+ $.removeData( node, 'request' );
+ }
+ },
+ result: {
+ render: renderFunction,
+ select: function () {
+ // allow the form to be
submitted
+ return true;
+ }
+ },
+ cache: true,
+ highlightInput: true
+ } )
+ .bind( 'paste cut drop', function () {
+ // make sure paste and cut events from the
mouse and drag&drop events
+ // trigger the keypress handler and cause the
suggestions to update
+ $( this ).trigger( 'keypress' );
+ } )
+ // In most skins (at least Monobook and Vector), the
font-size is messed up in <body>.
+ // (they use 2 elements to get a sane font-height). So,
instead of making exceptions for
+ // each skin or adding more stylesheets, just copy it
from the active element so auto-fit.
+ .each( function () {
+ var $this = $( this );
+ $this
+ .data( 'suggestions-context' )
+ .data.$container
+ .css( 'fontSize', $this.css(
'fontSize' ) );
+ } );
+
+ // Ensure that the thing is actually present!
+ if ( $searchRegion.length === 0 ) {
+ // Don't try to set anything up if no CreatePage inputs
exist on this page
+ return;
+ }
+ } );
+
+}( mediaWiki, jQuery ) );
--
To view, visit https://gerrit.wikimedia.org/r/192467
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I969e4904bdb0613fd09f997aa2326f2e2dfe1d71
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/CreatePage
Gerrit-Branch: master
Gerrit-Owner: tosfos <[email protected]>
Gerrit-Reviewer: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: Yaron Koren <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits