ItSpiderman has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/335445 )
Change subject: Rewrite of ContextMenu extension
......................................................................
Rewrite of ContextMenu extension
Context menu now is now build by using API call. User permissions and
context is checked and only relevant items are returned to context menu
Needs cherry-picking to REL1_27 and master
Change-Id: I4fe25918c8eca7d636ec959b72a31e669c204253
---
M ContextMenu/ContextMenu.setup.php
A ContextMenu/includes/api/BSApiContextMenuTasks.php
M ContextMenu/resources/bluespice.contextmenu.js
3 files changed, 191 insertions(+), 211 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/BlueSpiceExtensions
refs/changes/45/335445/1
diff --git a/ContextMenu/ContextMenu.setup.php
b/ContextMenu/ContextMenu.setup.php
index 2a9860d..fcddedb 100644
--- a/ContextMenu/ContextMenu.setup.php
+++ b/ContextMenu/ContextMenu.setup.php
@@ -3,6 +3,7 @@
BsExtensionManager::registerExtension('ContextMenu',
BsRUNLEVEL::FULL|BsRUNLEVEL::REMOTE);
$GLOBALS['wgAutoloadClasses']['ContextMenu'] = __DIR__ .
'/ContextMenu.class.php';
+$GLOBALS['wgAutoloadClasses']['BSApiContextMenuTasks'] = __DIR__ .
'/includes/api/BSApiContextMenuTasks.php';
$wgMessagesDirs['ContextMenu'] = __DIR__ . '/i18n';
$wgExtensionMessagesFiles['ContextMenu'] = __DIR__ .
'/languages/ContextMenu.i18n.php';
@@ -27,4 +28,6 @@
),
'localBasePath' => __DIR__.'/resources',
'remoteExtPath' => 'BlueSpiceExtensions/ContextMenu/resources'
-);
\ No newline at end of file
+);
+
+$wgAPIModules['bs-contextmenu-tasks'] = 'BSApiContextMenuTasks';
diff --git a/ContextMenu/includes/api/BSApiContextMenuTasks.php
b/ContextMenu/includes/api/BSApiContextMenuTasks.php
new file mode 100644
index 0000000..a942690
--- /dev/null
+++ b/ContextMenu/includes/api/BSApiContextMenuTasks.php
@@ -0,0 +1,169 @@
+<?php
+
+class BSApiContextMenuTasks extends BSApiTasksBase {
+
+ protected $aTasks = array( 'getContextMenu' );
+
+ protected function task_getContextMenu ( $oData, $aParams ) {
+ $oUser = RequestContext::getMain()->getUser();
+
+ $aItems = array();
+
+ if( !empty( $oData->title ) ) {
+ $oTitle = Title::newFromText( $oData->title );
+ if ( $oTitle-> exists() ) {
+ $this->makePageItems( $aItems, $oTitle, $oUser);
+ }
+ }
+
+ if( !empty( $oData->username ) ) {
+ $oTargetUser = User::newFromName( $oData->username );
+ if( $oTargetUser ) {
+ $this->makeUserItems( $aItems, $oUser, $oTargetUser );
+ }
+ }
+
+ if( !empty( $oData->titleTag ) && empty( $oData-> title ) ) {
+ $this->makeMediaItems( $aItems, $oUser, $oData->titleTag );
+ }
+
+ if ( !empty( $oData->fileURL ) || !empty( $oData->FileName ) ) {
+ $this->makeFileItems( $aItems, $oUser, $oData->fileURL, $oData->FileName
);
+ }
+
+ $oResult = $this->makeStandardReturn();
+
+ $oResult->success = true;
+ $oResult->payload_count = count( $aItems );
+ $oResult->payload = array( 'items' => $aItems );
+
+ return $oResult;
+
+ }
+
+ protected function makeMediaItems( &$aItems, $oUser, $sTitleTag ) {
+ global $wgFileExtensions;
+
+ $aTitleParts = explode( '.', $sTitleTag );
+ $sFileExtension = $aTitleParts[count($aTitleParts) - 1];
+
+ if( !in_array( $sFileExtension, $wgFileExtensions ) ) {
+ return;
+ }
+
+ $oMediaTitle = Title::makeTitle( NS_FILE, $sTitleTag );
+
+ if( !$oMediaTitle || !$oMediaTitle->exists() ) {
+ return;
+ }
+
+ if( $oMediaTitle->userCan( 'read', $oUser ) ) {
+ $aItems[] = array (
+ 'text' => wfMessage( 'bs-contextmenu-media-view-page' )->plain(),
+ 'href' => $oMediaTitle->getLocalUrl(),
+ 'iconCls' => 'icon-arrow-right'
+ );
+ }
+
+ if( $oMediaTitle->userCan( 'reupload', $oUser ) ) {
+ $oUploadSpecialPage = Title::makeTitle( NS_SPECIAL, 'Upload');
+ $aItems[] = array (
+ 'text' => wfMessage( 'bs-contextmenu-media-reupload' )->plain(),
+ 'href' => $oUploadSpecialPage->getLocalUrl( 'wpDestFile=' .
$sTitleTag ),
+ 'iconCls' => 'icon-upload'
+ );
+ }
+ }
+
+ protected function makePageItems( &$aItems, $oTitle, $oUser ) {
+ if( $oTitle->userCan( 'edit', $oUser ) ) {
+ $aItems[] = array (
+ 'text' => wfMessage( 'bs-contextmenu-page-edit' )->plain(),
+ 'href' => $oTitle->getLocalUrl( 'action=edit' ),
+ 'id' => 'bs-cm-item-edit',
+ 'iconCls' => 'icon-pencil'
+ );
+ }
+
+ if( $oTitle->userCan( 'read', $oUser ) ) {
+ $aItems[] = array(
+ 'text' => wfMessage( 'bs-contextmenu-page-history' )->plain(),
+ 'href' => $oTitle->getLocalUrl( 'action=history' ),
+ 'id' => 'bs-cm-item-history',
+ 'iconCls' => 'icon-history'
+ );
+ }
+
+ if( $oTitle->userCan( 'delete', $oUser ) ) {
+ $aItems[] = array(
+ 'text' => wfMessage( 'bs-contextmenu-page-delete' )->plain(),
+ 'href' => $oTitle->getLocalUrl( 'action=delete' ),
+ 'id' => 'bs-cm-item-delete',
+ 'iconCls' => 'icon-trash'
+ );
+ }
+
+ if( $oTitle->userCan( 'move', $oUser ) ) {
+ $oTitleMove = Title::makeTitle( NS_SPECIAL, 'Movepage' );
+ $aItems[] = array(
+ 'text' => wfMessage( 'bs-contextmenu-page-move' )->plain(),
+ 'href' => $oTitleMove->getLocalUrl() . '/' . $oTitle->getFullText() ,
+ 'id' => 'bs-cm-item-move',
+ 'iconCls' => 'icon-shuffle'
+ );
+ }
+
+ if( $oTitle->userCan( 'protect', $oUser ) ) {
+ $aItems[] = array(
+ 'text' => wfMessage( 'bs-contextmenu-page-protect' )->plain(),
+ 'href' => $oTitle->getLocalUrl( 'action=protect' ),
+ 'id' => 'bs-cm-item-protect',
+ 'iconCls' => 'icon-shield'
+ );
+ }
+ }
+
+ protected function makeUserItems( &$aItems, $oUser, $oTargetUser ) {
+ if( $oUser->getEmail() ) {
+ $oTitleSendMail = Title::makeTitle( NS_SPECIAL, 'EmailUser' );
+ $aItems[] = array(
+ 'text' => wfMessage( 'bs-contextmenu-user-mail' )->plain(),
+ 'href' => $oTitleSendMail->getLocalUrl( 'target=' .
$oTargetUser->getName() ),
+ 'id' => 'bs-cm-item-usermail',
+ 'iconCls' => 'icon-mail'
+ );
+ }
+
+ $oTargetUserTalkPage = $oTargetUser->getTalkPage();
+ if( $oTargetUserTalkPage->exists() && $oTargetUserTalkPage->userCan(
'edit', $oUser ) ) {
+ $aItems[] = array(
+ 'text' => wfMessage( 'bs-contextmenu-user-talk' )->plain(),
+ 'href' => $oTargetUserTalkPage->getLocalUrl( 'action=edit' ),
+ 'id' => 'bs-cm-item-usertalk',
+ 'iconCls' => 'icon-bubbles'
+ );
+ }
+ }
+
+ protected function makeFileItems( &$aItems, $oUser, $sFileUrl, $sFileName ) {
+ if( $sFileUrl ) {
+ $aItems[] = array(
+ 'text' => wfMessage( 'bs-contextmenu-file-download' )->plain(),
+ 'href' => $sFileUrl,
+ 'iconCls' => 'icon-download'
+ );
+ }
+
+ if( $sFileName ) {
+ $oTargetFilePage = Title::makeTitle( NS_FILE, $sFileName );
+ if( $oTargetFilePage->exists() && $oTargetFilePage->userCan( 'reupload',
$oUser ) ) {
+ $oUploadSpecialPage = Title::makeTitle( NS_SPECIAL, 'Upload');
+ $aItems[] = array(
+ 'text' => wfMessage( 'bs-contextmenu-media-reupload' )->plain(),
+ 'href' => $oUploadSpecialPage->getLocalUrl( 'wpDestFile=' .
$sTitleTag ),
+ 'iconCls' => 'icon-upload'
+ );
+ }
+ }
+ }
+}
diff --git a/ContextMenu/resources/bluespice.contextmenu.js
b/ContextMenu/resources/bluespice.contextmenu.js
index 3c11244..d216be8 100644
--- a/ContextMenu/resources/bluespice.contextmenu.js
+++ b/ContextMenu/resources/bluespice.contextmenu.js
@@ -1,177 +1,4 @@
(function(mw, $, bs){
-
- var makePageItems = function( anchor ) {
- var title = anchor.data('bs-title');
- var items = [];
-
- if( !title || anchor.hasClass('new') ) {
- return items;
- }
-
- items.push({
- text: mw.message('bs-contextmenu-page-edit').plain(),
- href: bs.util.wikiGetlink(
- {
- action: 'edit'
- },
- title
- ),
- id: 'bs-cm-item-edit',
- iconCls: 'icon-pencil'
- });
- items.push({
- text:
mw.message('bs-contextmenu-page-history').plain(),
- href: bs.util.wikiGetlink(
- {
- action: 'history'
- },
- title
- ),
- id: 'bs-cm-item-history',
- iconCls: 'icon-history'
- });
-
- items.push({
- text: mw.message('bs-contextmenu-page-delete').plain(),
- href: bs.util.wikiGetlink(
- {
- action: 'delete'
- },
- title
- ),
- id: 'bs-cm-item-delete',
- iconCls: 'icon-trash'
- });
-
- items.push({
- text: mw.message('bs-contextmenu-page-move').plain(),
- href: mw.util.getUrl( 'Special:Movepage/'+title ),
- id: 'bs-cm-item-move',
- iconCls: 'icon-shuffle'
- });
-
- items.push({
- text: mw.message('bs-contextmenu-page-protect').plain(),
- href: bs.util.wikiGetlink(
- {
- action: 'protect'
- },
- title
- ),
- id: 'bs-cm-item-protect',
- iconCls: 'icon-shield'
- });
-
- return items;
- };
-
- var makeUserItems = function( anchor ) {
- var items = [];
- if( !anchor.data('bs-username') ) {
- return items;
- }
-
- var username = anchor.data('bs-username');
-
- if( mw.config.get( 'bsUserCanSendMail' ) &&
anchor.data('bs-user-has-email') ) {
- items.push({
- text:
mw.message('bs-contextmenu-user-mail').plain(),
- href: bs.util.wikiGetlink(
- {
- target: username
- },
- 'Special:EmailUser'
- ),
- id: 'bs-cm-item-usermail',
- iconCls: 'icon-mail'
- });
- }
- items.push({
- text: mw.message('bs-contextmenu-user-talk').plain(),
- href: bs.util.wikiGetlink(
- {
- action: 'edit'
- },
- 'User_talk:'+username
- ),
- id: 'bs-cm-item-usertalk',
- iconCls: 'icon-bubbles'
- });
- return items;
- };
-
- var makeMediaItems = function( anchor ) {
- /*
- * Unfotunately "Media" links do not have any special class or
data
- * attribute to recognize them. But the 'title' attribute always
- * contains the original file name.
- * AND: There is no data-bs-title attribute like on "File" links
- * (Links that aim to the description page)
- * This logic will need a rewrite when MW 1.24 is supported.
- */
- var title = anchor.attr('title');
- var dataTitle = anchor.data('bs-title');
- if( !title || dataTitle ) {
- return true;
- }
-
- var titleParts = title.split('.');
- var fileExtension = titleParts[titleParts.length-1];
-
- if ( mw.config.get( "wgFileExtensions" ).indexOf( fileExtension
) === -1 ) {
- return true;
- }
-
- var items = [
- {
- iconCls: 'icon-arrow-right',
- text:
mw.message('bs-contextmenu-media-view-page').plain(),
- href: mw.util.getUrl( 'File:'+title )
- },
- {
- iconCls: 'icon-upload',
- text:
mw.message('bs-contextmenu-media-reupload').plain(),
- href: bs.util.wikiGetlink(
- {
- wpDestFile: title
- },
- 'Special:Upload'
- )
- }
- ];
-
- return items;
- };
-
- var makeFileItems = function( anchor ) {
- var items = [];
- var fileurl = anchor.data('bs-fileurl');
- var filename = anchor.data('bs-filename');
-
- if( fileurl ) {
- items.push({
- iconCls: 'icon-download',
- text:
mw.message('bs-contextmenu-file-download').plain(),
- href: fileurl
- });
- }
-
- if( filename ) {
- items.push({
- iconCls: 'icon-upload',
- text:
mw.message('bs-contextmenu-media-reupload').plain(),
- href: bs.util.wikiGetlink(
- {
- wpDestFile: filename
- },
- 'Special:Upload'
- )
- });
- }
-
- return items;
- };
-
var showMenu = function( anchor, items, e ) {
$(document).trigger( 'BSContextMenuBeforeCreate', [anchor,
items]);
@@ -197,18 +24,6 @@
return false;
};
- var appendSection = function(base, additions, sectionkey) {
- if( base.length > 0 ) {
- base.push('-');
- }
- var additionsLength = additions.length;
- for( var i = 0; i < additionsLength; i++ ) {
- base.push( additions[i] );
- }
-
- return base;
- };
-
var modus = mw.user.options.get('MW::ContextMenu::Modus', 'ctrl');
$(document).on( 'contextmenu', 'a', function( e ) {
@@ -225,34 +40,27 @@
mw.loader.using( 'ext.bluespice.extjs', function() {
var items = [];
- var mediaItems = makeMediaItems( anchor );
- if( mediaItems.length > 0 ) {
- items = appendSection( items, mediaItems );
- }
+ bs.api.tasks.exec(
+ 'contextmenu',
+ 'getContextMenu',
+ {
+ title: anchor.data('bs-title'),
+ username: anchor.data('bs-username'),
+ titleTag: anchor.attr('title'),
+ fileURL: anchor.data('bs-fileurl'),
+ fileName: anchor.data('bs-filename')
+ }
+ ).done( function( response ) {
+ if( response.success === true &&
response.payload_count > 0) {
+ items = response.payload.items;
+ showMenu( anchor, items, e );
+ }
+ });
- var userItems = makeUserItems( anchor );
- if( userItems.length > 0 ) {
- items = appendSection( items, userItems );
- }
-
- var fileItems = makeFileItems( anchor );
- if( fileItems.length > 0 ) {
- items = appendSection( items, fileItems );
- }
-
- var pageItems = makePageItems( anchor );
- if( pageItems.length > 0 ) {
- items = appendSection( items, pageItems );
- }
-
- if( items.length === 0 ) {
- return true;
- }
-
- showMenu( anchor, items, e );
});
return false;
+
});
-})( mediaWiki, jQuery, blueSpice);
\ No newline at end of file
+})( mediaWiki, jQuery, blueSpice);
--
To view, visit https://gerrit.wikimedia.org/r/335445
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I4fe25918c8eca7d636ec959b72a31e669c204253
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/BlueSpiceExtensions
Gerrit-Branch: REL1_23
Gerrit-Owner: ItSpiderman <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits