[MediaWiki-commits] [Gerrit] mediawiki...MOOC[master]: resource models added

2017-03-24 Thread Sebschlicht2 (Code Review)
Sebschlicht2 has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/340732 )

Change subject: resource models added
..


resource models added

ContentModels for resource files have been added, to allow resource files
(e.g. scripts, quizzes) to be placed in the MOOC's course namespace.
Three hooks have been implemented to limit the users view to the resource
file content while editing - but preview and save this content wrapped
inside a MoocResource behind the scenes.

Change-Id: Icd2ef6b6325e0c060eeb82be969c10c66bbc57d7
---
M MOOC.hooks.php
M extension.json
M includes/content/MoocContent.php
A includes/model/MoocEntity.php
M includes/model/MoocItem.php
M includes/model/MoocLesson.php
A includes/model/MoocQuiz.php
A includes/model/MoocResource.php
A includes/model/MoocScript.php
M includes/model/MoocUnit.php
M includes/rendering/MoocContentRenderer.php
M includes/structure/MoocContentStructureProvider.php
12 files changed, 315 insertions(+), 99 deletions(-)



diff --git a/MOOC.hooks.php b/MOOC.hooks.php
index 2459659..8aa1e00 100644
--- a/MOOC.hooks.php
+++ b/MOOC.hooks.php
@@ -6,15 +6,99 @@
  * @file
  * @ingroup Extensions
  *
- * @author Rene Pickhardt ([User:renepick]), Sebastian Schlicht 
(sebast...@jablab.de [User:sebschlicht])
+ * @author Sebastian Schlicht (sebast...@jablab.de [User:sebschlicht]), Rene 
Pickhardt ([User:renepick])
  * @license GPLv2
  */
 class MOOCHooks {
 
 /**
- * Registers parser functions for magic keywords.
+ * Transforms a MOOC resource into Wikitext to allow users to edit its 
content.
  *
- * @param Parser $parser
+ * @param EditPage $editPage edit page
  */
-public static function onParserFirstCallInit(Parser &$parser) {}
+public static function onEditFormInitialText( $editPage ) {
+if ( $editPage->contentModel === MoocContent::CONTENT_MODEL_MOOC_ITEM 
) {
+$pageContent = 
$editPage->getArticle()->getPage()->getContent()->serialize();
+$moocContent = new MoocContent( $pageContent );
+if ( $moocContent->isValid() && ( $moocContent->entity instanceof 
MoocResource ) ) {
+$entity = $moocContent->entity;
+if ( $entity !== null ) {
+$editPage->textbox1 = $entity->content;
+}
+}
+}
+}
+
+/**
+ * Transforms the resource file content into a MOOC resource when 
previewing an edit.
+ * Behind the scenes, the original MOOC resource is loaded and its content 
field is overwritten.
+ *
+ * @param EditPage $editPage edit page
+ * @param Content $content previewed content
+ */
+public static function onEditPageGetPreviewContent( $editPage, &$content ) 
{
+if ( $editPage->contentModel === MoocContent::CONTENT_MODEL_MOOC_ITEM 
) {
+if ( $editPage->getArticle()->getPage()->exists() ) {
+// existing page: inject edit-content into MOOC resource
+$pageContent = 
$editPage->getArticle()->getPage()->getContent()->serialize();
+$moocContent = new MoocContent( $pageContent );
+$newMoocContent = self::mergeResourceFileIntoMoocContent( 
$moocContent, $content );
+if ( $newMoocContent === null ) {
+// invalid MOOC entity or not a MoocResource
+} else {
+$content = $newMoocContent;
+}
+} else {
+// new page: TODO unclear whether MoocItem or MoocResource
+// maybe decide depending on JSON validity but invalid 
MoocItem == MoocResource
+// so this is a modelling choice
+}
+}
+}
+
+/**
+ * Transforms the resource file content into a MOOC resource when the user 
finishes editing.
+ * Behind the scenes, the original MOOC resource is loaded and its content 
field is overwritten.
+ *
+ * @param WikiPage $wikiPage wiki page being saved
+ * @param User $user user saving the article
+ * @param Content $content new article content
+ * @param string $summary edit summary
+ * @param bool $isMinor minor flag
+ * @param bool $isWatch null
+ * @param bool $section null
+ * @param $flags
+ * @param Status $status edit status
+ */
+public static function onPageContentSave( &$wikiPage, &$user, &$content, 
&$summary,
+  $isMinor, $isWatch, $section, 
&$flags, &$status ) {
+// limit hook to saves (not creates) of MOOC entities
+if ( $wikiPage->getContentModel() === 
MoocContent::CONTENT_MODEL_MOOC_ITEM && $wikiPage->exists() ) {
+$pageContent = $wikiPage->getContent()->serialize();
+$moocContent = new MoocContent( $pageContent );
+$newMoocContent = self::mergeResourceFileIntoMoocContent( 
$moocContent, $content 

[MediaWiki-commits] [Gerrit] mediawiki...MOOC[master]: resource models added

2017-03-02 Thread Sebschlicht2 (Code Review)
Sebschlicht2 has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/340732 )

Change subject: resource models added
..

resource models added

ContentModels for resource files have been added, to allow resource files
(e.g. scripts, quizzes) to be placed in the MOOC's course namespace.
Three hooks have been implemented to limit the users view to the resource
file content while editing - but preview and save this content wrapped
inside a MoocResource behind the scenes.

Change-Id: Icd2ef6b6325e0c060eeb82be969c10c66bbc57d7
---
M MOOC.hooks.php
M extension.json
M includes/content/MoocContent.php
A includes/model/MoocEntity.php
M includes/model/MoocItem.php
M includes/model/MoocLesson.php
A includes/model/MoocQuiz.php
A includes/model/MoocResource.php
A includes/model/MoocScript.php
M includes/model/MoocUnit.php
M includes/rendering/MoocContentRenderer.php
M includes/structure/MoocContentStructureProvider.php
12 files changed, 315 insertions(+), 99 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MOOC 
refs/changes/32/340732/1

diff --git a/MOOC.hooks.php b/MOOC.hooks.php
index 2459659..8aa1e00 100644
--- a/MOOC.hooks.php
+++ b/MOOC.hooks.php
@@ -6,15 +6,99 @@
  * @file
  * @ingroup Extensions
  *
- * @author Rene Pickhardt ([User:renepick]), Sebastian Schlicht 
(sebast...@jablab.de [User:sebschlicht])
+ * @author Sebastian Schlicht (sebast...@jablab.de [User:sebschlicht]), Rene 
Pickhardt ([User:renepick])
  * @license GPLv2
  */
 class MOOCHooks {
 
 /**
- * Registers parser functions for magic keywords.
+ * Transforms a MOOC resource into Wikitext to allow users to edit its 
content.
  *
- * @param Parser $parser
+ * @param EditPage $editPage edit page
  */
-public static function onParserFirstCallInit(Parser &$parser) {}
+public static function onEditFormInitialText( $editPage ) {
+if ( $editPage->contentModel === MoocContent::CONTENT_MODEL_MOOC_ITEM 
) {
+$pageContent = 
$editPage->getArticle()->getPage()->getContent()->serialize();
+$moocContent = new MoocContent( $pageContent );
+if ( $moocContent->isValid() && ( $moocContent->entity instanceof 
MoocResource ) ) {
+$entity = $moocContent->entity;
+if ( $entity !== null ) {
+$editPage->textbox1 = $entity->content;
+}
+}
+}
+}
+
+/**
+ * Transforms the resource file content into a MOOC resource when 
previewing an edit.
+ * Behind the scenes, the original MOOC resource is loaded and its content 
field is overwritten.
+ *
+ * @param EditPage $editPage edit page
+ * @param Content $content previewed content
+ */
+public static function onEditPageGetPreviewContent( $editPage, &$content ) 
{
+if ( $editPage->contentModel === MoocContent::CONTENT_MODEL_MOOC_ITEM 
) {
+if ( $editPage->getArticle()->getPage()->exists() ) {
+// existing page: inject edit-content into MOOC resource
+$pageContent = 
$editPage->getArticle()->getPage()->getContent()->serialize();
+$moocContent = new MoocContent( $pageContent );
+$newMoocContent = self::mergeResourceFileIntoMoocContent( 
$moocContent, $content );
+if ( $newMoocContent === null ) {
+// invalid MOOC entity or not a MoocResource
+} else {
+$content = $newMoocContent;
+}
+} else {
+// new page: TODO unclear whether MoocItem or MoocResource
+// maybe decide depending on JSON validity but invalid 
MoocItem == MoocResource
+// so this is a modelling choice
+}
+}
+}
+
+/**
+ * Transforms the resource file content into a MOOC resource when the user 
finishes editing.
+ * Behind the scenes, the original MOOC resource is loaded and its content 
field is overwritten.
+ *
+ * @param WikiPage $wikiPage wiki page being saved
+ * @param User $user user saving the article
+ * @param Content $content new article content
+ * @param string $summary edit summary
+ * @param bool $isMinor minor flag
+ * @param bool $isWatch null
+ * @param bool $section null
+ * @param $flags
+ * @param Status $status edit status
+ */
+public static function onPageContentSave( &$wikiPage, &$user, &$content, 
&$summary,
+  $isMinor, $isWatch, $section, 
&$flags, &$status ) {
+// limit hook to saves (not creates) of MOOC entities
+if ( $wikiPage->getContentModel() === 
MoocContent::CONTENT_MODEL_MOOC_ITEM && $wikiPage->exists() ) {
+$pageContent = $wikiPage->getContent()->serialize();
+$moocContent = new MoocContent( $pageContent );
+