Harej has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/312086

Change subject: Implementing Special:CreateHubFeature for creating subpage 
features on Collaboration Hubs.
......................................................................

Implementing Special:CreateHubFeature for creating subpage features on 
Collaboration Hubs.

Bug: T141011
Change-Id: I77a313b74bd9dbb01f55c0a887bd160f6bc3287b
---
M extension.json
M i18n/en.json
M i18n/qqq.json
A includes/SpecialCreateHubFeature.php
M includes/content/CollaborationHubContent.php
5 files changed, 290 insertions(+), 29 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/CollaborationKit 
refs/changes/86/312086/1

diff --git a/extension.json b/extension.json
index 2fd2145..4f156ad 100644
--- a/extension.json
+++ b/extension.json
@@ -1,7 +1,7 @@
 {
        "name": "CollaborationKit",
        "version": "0.1",
-       "author": [ "Kim Schoonover", "Brian Wolff" ],
+       "author": [ "Kim Schoonover", "Brian Wolff", "James Hare" ],
        "url": "https://www.mediawiki.org/wiki/Extension:CollaborationKit";,
        "descriptionmsg": "colaborationkit-desc",
        "type": "other",
@@ -23,6 +23,7 @@
                "CollaborationListContent": 
"includes/content/CollaborationListContent.php",
                "CollaborationListContentHandler": 
"includes/content/CollaborationListContentHandler.php",
                "SpecialCreateCollaborationHub": 
"includes/SpecialCreateCollaborationHub.php",
+               "SpecialCreateHubFeature": 
"includes/SpecialCreateHubFeature.php",
                "CollaborationListContentEditor": 
"includes/CollaborationListContentEditor.php",
                "ResourceLoaderListStyleModule": 
"includes/ResourceLoaderListStyleModule.php"
        },
@@ -31,7 +32,8 @@
                "CollaborationListContent": "CollaborationListContentHandler"
        },
        "SpecialPages": {
-               "CreateCollaborationHub": "SpecialCreateCollaborationHub"
+               "CreateCollaborationHub": "SpecialCreateCollaborationHub",
+               "CreateHubFeature": "SpecialCreateHubFeature"
        },
        "Hooks": {
                "SkinTemplateNavigation": [
diff --git a/i18n/en.json b/i18n/en.json
index 1feab00..930cefa 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -4,6 +4,7 @@
        },
        "colaborationkit-desc": "Next-generation WikiProjects and collaborative 
workspaces for wikis",
        "createcollaborationhub": "Create new Collaboration Hub project",
+       "createhubfeature": "Create a new Collaboration Hub feature",
        "collaborationkit-createhub-title": "Page title",
        "collaborationkit-createhub-displayname": "Project display name",
        "collaborationkit-createhub-image": "Project image",
@@ -19,6 +20,20 @@
        "collaborationkit-createhub-nopermission": "You do not have permission 
to create a Collaboration Hub with this title",
        "collaborationkit-createhub-invalidsource": "The specified source 
cannot be used",
        "collaborationkit-createhub-editsummary": "Create new Collaboration 
Hub",
+       "collaborationkit-createhubfeature-collaborationhub": "Collaboration 
Hub",
+       "collaborationkit-createhubfeature-featurename": "Name of feature",
+       "collaborationkit-createhubfeature-icon": "Icon",
+       "collaborationkit-createhubfeature-contenttype": "What type of page is 
this?",
+       "collaborationkit-createhubfeature-invalidtitle": "You have chosen an 
invalid page title",
+       "collaborationkit-createhubfeature-exists": "A page already exists at 
the title you specified",
+       "collaborationkit-createhubfeature-nopermission": "You do not have 
permission to create a feature with this title",
+       "collaborationkit-createhubfeature-hubdoesnotexist": "A Collaboration 
Hub does not exist at this title",
+       "collaborationkit-createhubfeature-hubisnotahub": "The page you 
specified is not a Collaboration Hub",
+       "collaborationkit-createhubfeature-hubeditsummary": "/* $1 */ Adding",
+       "collaborationkit-createhubfeature-invalidcontenttype": "This content 
model is not permitted for features at this time",
+       "collaborationkit-createhubfeature-editsummary": "Creating new feature",
+       "collaborationkit-createhubfeature-freetext": "Generic wiki page",
+       "collaborationkit-createhubfeature-articlelist": "List of pages",
        "collaborationkit-list-isempty": "This list has no items in it.",
        "collaborationkit-list-notlist": "[[$1]] is not a CollaborationKit list 
page",
        "collaborationkit-list-taglist": "'''{{PLURAL:$2|Tagged:|Tags:}}''' $1 
",
diff --git a/i18n/qqq.json b/i18n/qqq.json
index a59e42b..f76b867 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -4,6 +4,7 @@
        },
        "colaborationkit-desc": 
"{{desc|name=CollaborationKit|url=https://www.mediawiki.org/wiki/Extension:CollaborationKit}}";,
        "createcollaborationhub": "Header for Special:CreateCollaborationHub",
+       "createhubfeature": "Header for Special:CreateHubFeature",
        "collaborationkit-createhub-title": "Label for target page title input 
on Special:CreateCollaborationHub",
        "collaborationkit-createhub-displayname": "Label for page display name 
input on Special:CreateCollaborationHub",
        "collaborationkit-createhub-image": "Label for the page icon field on 
specialcreatecollaborationhub",
@@ -19,6 +20,20 @@
        "collaborationkit-createhub-nopermission": "Error message for no 
permission to edit/create/whatever",
        "collaborationkit-createhub-invalidsource": "Error message for an 
invalid title for the source import",
        "collaborationkit-createhub-editsummary": "Edit summary for creating a 
new Collaboration Hub via Special:CreateCollaborationHub",
+       "collaborationkit-createhubfeature-freetext": "Label for selecting 
content type in the Create Hub Feature form corresponding to freeform text 
(i.e. wikitext)",
+       "collaborationkit-createhubfeature-articlelist": "Label for selecting 
content type in the Create Hub Feature form corresponding to 
CollaborationListContent",
+       "collaborationkit-createhubfeature-collaborationhub": "Label for form 
field for specifying the Collaboration Hub on Special:CreateHubFeature",
+       "collaborationkit-createhubfeature-featurename": "Label for form field 
for specifying the name of the feature to be created on 
Special:CreateHubFeature",
+       "collaborationkit-createhubfeature-icon": "Label for form field for 
specifying the feature's icon on Special:CreateHubFeature",
+       "collaborationkit-createhubfeature-contenttype": "Label for the content 
model selector on Special:CreateHubFeature",
+       "collaborationkit-createhubfeature-invalidtitle": "Error message on 
Special:CreateHubFeature when an invalid page title is specified",
+       "collaborationkit-createhubfeature-exists": "Error message on 
Special:CreateHubFeature when the specified feature already exists as a page",
+       "collaborationkit-createhubfeature-nopermission": "Error message on 
Special:CreateHubFeature when the user lacks the edit, create, and 
editcontentmodel permissions",
+       "collaborationkit-createhubfeature-hubdoesnotexist": "Error message on 
Special:CreateHubFeature when the user specifies a Collaboration Hub that does 
not exist",
+       "collaborationkit-createhubfeature-hubisnotahub": "Error message on 
Special:CreateHubFeature when the user specifies a Collaboration Hub that is 
not of the CollaborationHubContent content model",
+       "collaborationkit-createhubfeature-hubeditsummary": "The edit summary 
used by Special:CreateHubFeature when adding a new feature to a Collaboration 
Hub. $1 = name of feature",
+       "collaborationkit-createhubfeature-invalidcontenttype": "Error message 
on Special:CreateHubFeature in the unlikely event that an invalid content type 
is specified for a feature",
+       "collaborationkit-createhubfeature-editsummary": "Edit summary used by 
Special:CreateHubFeature when creating a new feature",
        "collaborationkit-list-isempty": "Shown on lists that are empty 
immediately after the description",
        "collaborationkit-list-notlist": "Error when 
<nowiki>{{#transcludelist:page}}</nowiki> is given a page that is not a 
CollaborationKit list page. $1 Name of page given.",
        "collaborationkit-list-taglist": "Box for showing tags a specific item 
has on a list. $1 = comma separated list of tags. $2 = number of tags",
diff --git a/includes/SpecialCreateHubFeature.php 
b/includes/SpecialCreateHubFeature.php
new file mode 100644
index 0000000..d8fb4d8
--- /dev/null
+++ b/includes/SpecialCreateHubFeature.php
@@ -0,0 +1,233 @@
+<?php
+
+/**
+ * Form to create features for Collaboration Hubs
+ * Based on code from MassMessage
+ *
+ * @file
+ */
+
+class SpecialCreateHubFeature extends FormSpecialPage {
+
+       public function __construct() {
+               parent::__construct( 'CreateHubFeature' );
+       }
+
+       /**
+        * @param string $par
+        */
+       public function execute( $par ) {
+               parent::execute( $par );
+       }
+
+       /**
+        * @return array
+        */
+       protected function getFormFields() {
+
+               // Allow the collaboration hub to be passed via parameter (full 
page title) ?collaborationhub=
+               // Allow the feature name to be passed via parameter (subpage 
title) ?feature=
+               if ( isset($_GET['collaborationhub']) ) {
+                       $defaultCollabHub = $_GET['collaborationhub'];
+               }
+               else {
+                       $defaultCollabHub = '';
+               }
+
+               if ( isset($_GET['feature']) ) {
+                       $defaultFeatureName = $_GET['feature'];
+               }
+               else {
+                       $defaultFeatureName = '';
+               }
+
+               $fields = [
+                       'collaborationhub' => [
+                               'type' => 'text',
+                               'cssclass' => 'mw-ck-titleinput',
+                               'label-message' => 
'collaborationkit-createhubfeature-collaborationhub',
+                               'default' => $defaultCollabHub
+                       ],
+                       'featurename' => [
+                               'type' => 'text',
+                               'cssclass' => 'mw-ck-displayinput',
+                               'label-message' => 
'collaborationkit-createhubfeature-featurename',
+                               'default' => $defaultFeatureName
+                       ],
+                       // TODO replace with icon selector
+                       'icon' => [
+                               'type' => 'text',
+                               'cssclass' => 'mw-ck-iconinput',
+                               'label-message' => 
'collaborationkit-createhubfeature-icon',
+                       ],
+                       'contenttype' => [
+                               'type' => 'radio',
+                               'cssclass' => 'mw-ck-contenttypeinput',
+                               'label-message' => 
'collaborationkit-createhubfeature-contenttype',
+                               'options' => 
[$this->msg('collaborationkit-createhubfeature-freetext')->text() => 'wikitext',
+                                       
$this->msg('collaborationkit-createhubfeature-articlelist')->text() => 
'CollaborationListContent']
+                       ]
+               ];
+
+               // If either of these fields is set, that means the user came 
to the special page
+               // by way of a special workflow, meaning that the name of the 
hub and/or the feature
+               // is already known. Changing it would cause problems (e.g. the 
hub saying the feature
+               // is called one thing and then the user changes their mind) so 
we disable further edits
+               // in the middle of the workflow.
+               if ( $defaultCollabHub != '' ) {
+                       $fields['collaborationhub']['readonly'] = true;
+               }
+               if ( $defaultFeatureName != '' ) {
+                       $fields['featurename']['readonly'] = true;
+               }
+
+               return $fields;
+       }
+
+       /**
+        * @param array $data
+        * @return Status
+        */
+       public function onSubmit( array $data ) {
+               $collaborationHub = $data[ 'collaborationhub' ];
+               $featureName = $data[ 'featurename' ];
+
+               $titleText = $collaborationHub . '/' . $featureName;
+
+               $title = Title::newFromText( $titleText );
+               if ( !$title ) {
+                       return Status::newFatal( 
'collaborationkit-createhubfeature-invalidtitle' );
+               } elseif ( $title->exists() ) {
+                       return Status::newFatal( 
'collaborationkit-createhubfeature-exists' );
+               } elseif (
+                       !$title->userCan( 'edit' ) ||
+                       !$title->userCan( 'create' ) ||
+                       !$title->userCan( 'editcontentmodel' )
+               ) {
+                       return Status::newFatal( 
'collaborationkit-createhubfeature-nopermission' );
+               }
+
+               // Update hub with link to new feature
+               $newFeature = ['title' => $titleText, 'display_title' => 
$featureName];
+
+               if ( $data[ 'icon' ] ) {
+                       $newFeature['image'] = $data[ 'icon' ];
+               }
+
+               $hubTitleObject = Title::newFromText( $collaborationHub );
+
+               if ( !$hubTitleObject->exists() ) {
+                       return Status::newFatal( 
'collaborationkit-createhubfeature-hubdoesnotexist' );
+               }
+
+               if ( $hubTitleObject->getContentModel() != 
"CollaborationHubContent" ) {
+                       return Status::newFatal( 
'collaborationkit-createhubfeature-hubisnotahub' );
+               }
+
+               $hubWikiPageObject = WikiPage::factory( $hubTitleObject );
+               $hubRawContent = $hubWikiPageObject->getContent();
+               $hubContent = json_decode( $hubRawContent->serialize(), true );
+
+               // Don't actually update the hub if the hub includes the 
feature.
+               $found = false;
+               foreach( $hubContent['content'] as $c ) {
+                       if ( $c['title'] === $titleText ) {
+                               $found = true;
+                               break;
+                       }
+               }
+
+               if ( !$found ) {
+                       $hubContent['content'][] = $newFeature;
+                       $newHubRawContent = json_encode( $hubContent );
+                       $editHubSummary = $this->msg( 
'collaborationkit-createhubfeature-hubeditsummary', $featureName )->plain();
+
+                       $context = $this->getContext();
+                       $der = new DerivativeContext( $context );
+                       $request = new DerivativeRequest(
+                               $context->getRequest(),
+                               [
+                                       'action' => 'edit',
+                                       'title' => 
$hubTitleObject->getFullText(),
+                                       'contentmodel' => 
'CollaborationHubContent',
+                                       'text' => $newHubRawContent,
+                                       'summary' => $editHubSummary,
+                                       'token' => 
$context->getUser()->getEditToken(),
+                               ],
+                               true // Treat data as POSTed
+                       );
+                       $der->setRequest( $request );
+                       try {
+                               $api = new ApiMain( $der, true );
+                               $api->execute();
+                       } catch ( UsageException $e ) {
+                               return Status::newFatal( $context->msg( 
'collaborationkit-hub-edit-apierror',
+                                       $e->getCodeString() ) );
+                       }
+               }
+
+               // Create feature
+               $contentModel = $data[ 'contenttype' ];
+               if ( $contentModel != 'wikitext' && $contentModel != 
'CollaborationListContent' ) {
+                       return Status::newFatal( 
'collaborationkit-createhubfeature-invalidcontenttype' );
+               }
+
+               if ( $contentModel == 'wikitext' ) {
+                       $contentFormat = 'text/x-wiki';
+               }
+               elseif ( $contentModel == 'CollaborationListContent' ) {
+                       $contentFormat = 'application/json';
+               }
+               else {
+                       return Status::newFatal( 
'collaborationkit-createhubfeature-invalidcontenttype' );
+               }
+
+               $initialContent = ''; // Create empty page by default; 
exception is if there needs to be something such as JSON.
+               if ( $contentModel == 'CollaborationListContent' ) {
+                       $initialContent = 
'{"items":[],"options":{},"description":""}';
+               }
+
+               $summary = $this->msg( 
'collaborationkit-createhubfeature-editsummary' )->plain();
+
+               $context = $this->getContext();
+               $der = new DerivativeContext( $context );
+               $request = new DerivativeRequest(
+                       $context->getRequest(),
+                       [
+                               'action' => 'edit',
+                               'title' => $title->getFullText(),
+                               'contentmodel' => $contentModel,
+                               'contentformat' => $contentFormat,
+                               'text' => $initialContent,
+                               'summary' => $summary,
+                               'token' => $context->getUser()->getEditToken(),
+                       ],
+                       true // Treat data as POSTed
+               );
+               $der->setRequest( $request );
+               try {
+                       $api = new ApiMain( $der, true );
+                       $api->execute();
+               } catch ( UsageException $e ) {
+                       return Status::newFatal( $context->msg( 
'collaborationkit-hub-edit-apierror',
+                               $e->getCodeString() ) );
+               }
+
+               // Once all the pages we want to create are created, we send 
them to the first one
+               $this->getOutput()->redirect( $title->getFullUrl() );
+               return Status::newGood();
+       }
+
+       public function onSuccess() {
+               // No-op: We have already redirected.
+       }
+
+       /**
+        * Set the form format to ooui for consistency with the rest of the ck 
stuff
+        * @param HTMLForm $form
+        *
+        */
+       protected function getDisplayFormat() {
+               return 'ooui';
+       }
+}
diff --git a/includes/content/CollaborationHubContent.php 
b/includes/content/CollaborationHubContent.php
index fb9cb80..39f50ea 100644
--- a/includes/content/CollaborationHubContent.php
+++ b/includes/content/CollaborationHubContent.php
@@ -416,11 +416,6 @@
        protected function getParsedContent( Title $title, ParserOptions 
$options, ParserOutput &$output ) {
                global $wgParser;
 
-               $lang = $options->getTargetLanguage();
-               if ( !$lang ) {
-                       $lang = $title->getPageLanguage();
-               }
-
                $html = '';
 
                foreach ( $this->getContent() as $item ) {
@@ -447,6 +442,10 @@
                                        );
                                        $text .= 
$spContent->getParsedIntroduction( $spTitle, $options );
                                } elseif ( $spContentModel == 
'CollaborationListContent' ) {
+                                       $lang = $options->getTargetLanguage();
+                                       if ( !$lang ) {
+                                               $lang = 
$title->getPageLanguage();
+                                       }
                                        // convert to wikitext with maxItems 
limit in place
                                        $wikitext = 
$spContent->convertToWikitext(
                                                $lang,
@@ -457,21 +456,12 @@
                                                        'defaultSort' => 
'random'
                                                ]
                                        );
-                                       $text = $wgParser->parse( $wikitext, 
$title, $options )->getText();
-                               } elseif ( $spContentModel == 'wikitext' ) {
-                                       // to grab first section only
-                                       $spContent = $spContent->getSection( 0 
);
-
-                                       // Do template preproccessing magic
-                                       // ... parse, get text into $text
-                                       $rawText = $spContent->serialize();
-                                       // Get rid of all <noinclude>'s.
-                                       $wgParser->startExternalParse( $title, 
$options, Parser::OT_WIKI );
-                                       $frame = 
$wgParser->getPreprocessor()->newFrame()->newChild( [], $spTitle );
-                                       $node = $wgParser->preprocessToDom( 
$rawText, Parser::PTD_FOR_INCLUSION );
-                                       $processedText = $frame->expand( $node, 
PPFrame::RECOVER_ORIG & ( ~PPFrame::NO_IGNORE ) );
-                                       $text = $wgParser->parse( 
$processedText, $title, $options )->getText();
+                                       $text = $wgParser->parse( $wikitext, 
$spTitle, $options )->getText();
                                } else {
+                                       if ( $spContentModel == 'wikitext' ) {
+                                               // to grab first section only
+                                               $spContent = 
$spContent->getSection( 0 );
+                                       }
                                        // Parse whatever (else) as whatever
                                        $contentOutput = 
$spContent->getParserOutput( $spTitle, $spRev, $options );
 
@@ -493,7 +483,7 @@
                                $linkRenderer = $wgParser->getLinkRenderer();
                                $html .= new OOUI\ButtonWidget( [
                                        'label' => wfMessage( 
'collaborationkit-hub-missingpage-create' )->inContentLanguage()->text(),
-                                       'href' => $spTitle->getEditURL()
+                                       'href' => Title::newFromText( 
'Special:CreateHubFeature' )->getFullUrl( ['collaborationhub' => 
$title->getFullText(), 'feature' => $spTitle->getSubpageText()] )
                                ] );
 
                                // register as template for stuff
@@ -549,17 +539,23 @@
                if ( $spTitle->userCan( 'edit' ) ) {
                        if ( isset( $spRev ) ) {
                                $linkString = 'edit';
+                               // TODO get appropriate edit link if it's 
something weird
+                               $sectionLinks['edit'] = $linkRenderer->makeLink(
+                                       $spTitle,
+                                       wfMessage( $linkString 
)->inContentLanguage()->text(),
+                                       [],
+                                       [ 'action' => 'edit' ]
+                               );
                        } else {
                                $linkString = 'create';
+                               $sectionLinks['edit'] = $linkRenderer->makeLink(
+                                       Title::newFromText( 
'Special:CreateHubFeature' ),
+                                       wfMessage( $linkString 
)->inContentLanguage()->text(),
+                                       [],
+                                       [ 'collaborationhub' => 
$title->getFullText(), 'feature' => $spTitle ]
+                               );
                        }
 
-                       // TODO get appropriate edit link if it's something 
weird
-                       $sectionLinks['edit'] = $linkRenderer->makeLink(
-                               $spTitle,
-                               wfMessage( $linkString 
)->inContentLanguage()->text(),
-                               [],
-                               [ 'action' => 'edit' ]
-                       );
                }
                if ( $title->userCan( 'edit' ) ) {
                        $sectionLinks['removeLink'] = $linkRenderer->makeLink(

-- 
To view, visit https://gerrit.wikimedia.org/r/312086
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I77a313b74bd9dbb01f55c0a887bd160f6bc3287b
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/CollaborationKit
Gerrit-Branch: master
Gerrit-Owner: Harej <jamesmh...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to