jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/374637 )

Change subject: Refactor: Use mustache templates to create download box
......................................................................


Refactor: Use mustache templates to create download box

The existing way of building HTML is prone to error and hard to
make sense of. In porting this to a Mustache template, I discovered
some invalid HTML relating to stray <tr><td> tags before a <tbody>

I've started with the download box, but as I touch other pieces of
code I'd like to also port them over.

Change-Id: Id5e3bfdc7ea5a7d908da2b9d904d97f794df844a
---
M templates/CollectionPageTemplate.php
A templates/download-box.mustache
2 files changed, 71 insertions(+), 38 deletions(-)

Approvals:
  Pmiazga: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/templates/CollectionPageTemplate.php 
b/templates/CollectionPageTemplate.php
index 7d84668..28e451a 100644
--- a/templates/CollectionPageTemplate.php
+++ b/templates/CollectionPageTemplate.php
@@ -24,6 +24,44 @@
                return $templateParser->processTemplate( 'warning', $args );
        }
 
+       /**
+        * Create a download form which allows you to download the book as pdf
+        *
+        * @param {ContextSource} $context being rendered in
+        * @param {Array} $formatNames a list of keys of the available formats 
e.g. [ 'electron', 'rdf2text' ]
+        * @return string
+        */
+       public function getDownloadForm( $context, $formatNames ) {
+               if ( count( $formatNames ) == 1 ) {
+                       $formatName = array_rand( $formatNames );
+                       $description = wfMessage( 'coll-download_as_text', 
$this->data['formats'][$formatName] )->parseAsBlock();
+                       $buttonLabel = wfMessage( 'coll-download_as', 
$this->data['formats'][$formatName] )->escaped();
+               } else {
+                       $description = $context->getOutput()->parse( 
$this->translator->translate( 'coll-download_text' ) );
+                       $buttonLabel = wfMessage( 'coll-download' )->escaped();
+               }
+               $templateParser = new TemplateParser( __DIR__ );
+               // we need to map the template formats to an object that the 
template will be able to render
+               $templateDataFormats = [];
+               foreach ( $formatNames as $formatName ) {
+                       $templateDataFormats[] = [
+                               'name' => $formatName,
+                               'label' => wfMessage( 'coll-format-' . 
$formatName )->escaped(),
+                       ];
+               }
+               $downloadForm = $templateParser->processTemplate( 
'download-box', [
+                       'headline' => wfMessage( 'coll-download_title' ),
+                       'description' => $description,
+                       'formAction' => SkinTemplate::makeSpecialUrl( 'Book' ),
+                       'formats' => $templateDataFormats,
+                       'writer' => count( $formatNames ) == 1 ? $writer : 
false,
+                       'formatSelectLabel' => wfMessage( 'coll-format_label' ),
+                       'returnTo' => SpecialPage::getTitleFor( 'Book' 
)->getPrefixedText(),
+                       'buttonLabel' => $buttonLabel,
+                       'downloadDisabled' => count( 
$this->data['collection']['items'] ),
+               ] );
+               return $downloadForm;
+       }
        public function execute() {
                $data = [
                        'collectionTitle' => $this->data['collection']['title'],
@@ -146,44 +184,8 @@
                                                ?>
                                        </ul></div>
                                <?php
-                       } /* if */
-                       ?>
-
-                       <div class="collection-column-right-box" 
id="coll-downloadbox">
-                               <h2><span class="mw-headline"><?php $this->msg( 
'coll-download_title' ) ?></span></h2>
-                               <?php if ( count( $this->data['formats'] ) == 1 
) {
-                                       $writer = array_rand( 
$this->data['formats'] );
-                                       echo wfMessage( 
'coll-download_as_text', $this->data['formats'][$writer] )->parseAsBlock();
-                                       $buttonLabel = wfMessage( 
'coll-download_as', $this->data['formats'][$writer] )->escaped();
-                               } else {
-                                       $this->msgWiki( 'coll-download_text' );
-                                       $buttonLabel = wfMessage( 
'coll-download' )->escaped();
-                               } ?>
-                               <form id="downloadForm" action="<?php echo 
htmlspecialchars( SkinTemplate::makeSpecialUrl( 'Book' ) ) ?>" method="post">
-                                       <table style="width:100%; 
background-color: transparent;"><tr><td><tbody><tr><td>
-                                                                               
<?php if ( count( $this->data['formats'] ) == 1 ) { ?>
-                                                                               
        <input type="hidden" name="writer" value="<?php echo htmlspecialchars( 
$writer ) ?>" />
-                                                                               
<?php } else { ?>
-                                                                               
        <label for="formatSelect"><?php $this->msg( 'coll-format_label' ) 
?></label>
-                                                                               
        <select id="formatSelect" name="writer">
-                                                                               
                <?php foreach ( $this->data['formats'] as $writer => $name ) {
-                                                                               
                        // Give grep a chance to find the usages:
-                                                                               
                        // coll-format-rl, coll-format-epub, coll-format-odf,
-                                                                               
                        // coll-format-zim, coll-format-docbook, 
coll-format-okawix_zeno
-                                                                               
                        ?>
-                                                                               
                        <option value="<?php echo htmlspecialchars( $writer ) 
?>"><?php echo wfMessage( 'coll-format-' . $writer )->escaped() ?></option>
-                                                                               
                <?php   } ?>
-                                                                               
        </select>
-                                                                               
<?php } ?>
-                                                                       
</td><td id="collection-download-button">
-                                                                               
<input type="hidden" name="bookcmd" value="render" />
-                                                                               
<input type="hidden" name="returnto" value="<?php echo 
SpecialPage::getTitleFor( 'Book' )->getPrefixedText(); ?>" />
-                                                                               
<input id="downloadButton" type="submit" value="<?php echo $buttonLabel 
?>"<?php if ( count( $this->data['collection']['items'] ) == 0 ) { ?> 
disabled="disabled"<?php } ?> />
-                                                                       
</td></tr></tbody></table>
-                               </form>
-                       </div>
-
-                       <?php
+                       }
+                       echo $this->getDownloadForm( $context, array_keys( 
$this->data['formats'] ) );
                        if ( $GLOBALS['wgUser']->isLoggedIn() ) {
                                $canSaveUserPage = 
$GLOBALS['wgUser']->isAllowed( 'collectionsaveasuserpage' );
                                $canSaveCommunityPage = 
$GLOBALS['wgUser']->isAllowed( 'collectionsaveascommunitypage' );
diff --git a/templates/download-box.mustache b/templates/download-box.mustache
new file mode 100644
index 0000000..167d3a7
--- /dev/null
+++ b/templates/download-box.mustache
@@ -0,0 +1,31 @@
+<div class="collection-column-right-box" id="coll-downloadbox">
+       <h2><span class="mw-headline">{{headline}}</span></h2>
+       {{{description}}}
+       <form id="downloadForm" action="{{formAction}}" method="post">
+               <table style="width:100%; background-color: transparent;">
+                       <tbody>
+                               <tr>
+                                       <td>
+                                               {{#writer}}
+                                               <input type="hidden" 
name="writer" value="{{writer}}" />
+                                               {{/writer}}
+                                               {{^writer}}
+                                               <label 
for="formatSelect">{{formatSelectLabel}}</label>
+                                               <select id="formatSelect" 
name="writer">
+                                                       {{#formats}}
+                                                       <option 
value="{{name}}">{{label}}</option>
+                                                       {{/formats}}
+                                               </select>
+                                               {{/writer}}
+                                       </td>
+                                       <td id="collection-download-button">
+                                               <input type="hidden" 
name="bookcmd" value="render" />
+                                               <input type="hidden" 
name="returnto" value="{{returnTo}}" />
+                                               <input id="downloadButton" 
type="submit" value="{{buttonLabel}}"
+                                                       
{{#downloadDisabled}}disabled="disabled"{{/downloadDisabled}}/>
+                                       </td>
+                               </tr>
+                       </tbody>
+               </table>
+       </form>
+</div>

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Id5e3bfdc7ea5a7d908da2b9d904d97f794df844a
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/Collection
Gerrit-Branch: master
Gerrit-Owner: Jdlrobson <jrob...@wikimedia.org>
Gerrit-Reviewer: C. Scott Ananian <canan...@wikimedia.org>
Gerrit-Reviewer: Gergő Tisza <gti...@wikimedia.org>
Gerrit-Reviewer: Jdlrobson <jrob...@wikimedia.org>
Gerrit-Reviewer: Pmiazga <pmia...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to