Mollywhite has uploaded a new change for review.
https://gerrit.wikimedia.org/r/73150
Change subject: Bug 50957: Populate navbar from JSON block.
......................................................................
Bug 50957: Populate navbar from JSON block.
The chapterlist, metadata, and previous/next links are now
populated from the page's corresponding JSON block. A category
on each section of the book (of the form
[[Category:Book:Book Title]]) indicates which Book: JSON block
belongs to the book. It then pulls in the JSON and populates
the navbar from that.
This is a pretty huge commit, and since I'm not just tweaking
someone else's code, I'm going to wait for some reviews before
merging this in.
Bug: 50957
Change-Id: I9c2d77ce649c678ae34343032ef68b2b673dc92e
---
M BookManagerv2.hooks.php
M BookManagerv2.i18n.php
2 files changed, 341 insertions(+), 44 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/BookManagerv2
refs/changes/50/73150/1
diff --git a/BookManagerv2.hooks.php b/BookManagerv2.hooks.php
index 7fa43d3..beaf109 100644
--- a/BookManagerv2.hooks.php
+++ b/BookManagerv2.hooks.php
@@ -1,9 +1,11 @@
<?php
/**
* Hooks for the BookManagerv2 extension
- *
+ *
* @file
* @ingroup Extensions
+ *
+ * @author Molly White
*
* @section LICENSE
* This program is free software; you can redistribute it and/or
@@ -22,8 +24,19 @@
*/
class BookManagerv2Hooks {
-
- public static function readingInterfaceUX( $out, $prev, $next,
$chapterList, $metadata ) {
+
+ /**
+ * Adds a navigation bar to the page
+ *
+ * @param OutputPage $out OutputPage object
+ * @param object $prev Object representing the preceding page; contains
+ * "link" and "name" properties.
+ * @param object $next Object representing the following page; contains
+ * "link" and "name" properties.
+ * @param string $chapterList String containing the chapter list HTML
+ * @param string $metadata String containing the metadata HTML
+ */
+ public static function readingInterfaceUX( OutputPage &$out, $prev,
$next, $chapterList, $metadata ) {
global $wgExtensionAssetsPath;
$imagePath = $wgExtensionAssetsPath . "/BookManagerv2/images/";
@@ -31,9 +44,9 @@
. Html::openElement( 'div', array( 'class' =>
'mw-bookmanagerv2-nav-constrain' ) )
. Html::openElement( 'div', array( 'class' =>
'mw-bookmanagerv2-nav-bar' ) )
. Html::openElement( 'a', array(
- 'class' => array(
+ 'class' => array(
'mw-bookmanagerv2-nav-icon',
- 'mw-bookmanagerv2-nav-data' )
+ 'mw-bookmanagerv2-nav-data' )
)
)
. Html::element( 'img', array(
@@ -51,30 +64,34 @@
'class' => 'mw-bookmanagerv2-nav-toc',
'src' => $imagePath . 'Ul_font_awesome.png'
), '' )
- . Html::closeElement( 'a' )
- . Html::openElement( 'a', array(
- 'class' => 'mw-bookmanagerv2-nav-prev',
- 'href' => $prev->link )
- )
- . Html::element( 'img', array(
- 'class' => 'mw-bookmanagerv2-nav-prev',
- 'src' => $imagePath .
'Angle_left_font_awesome.png'
- ), '' )
- . $prev->title
- . Html::closeElement( 'a' )
- . Html::openElement( 'a', array(
- 'class' => 'mw-bookmanagerv2-nav-next',
- 'href' => $next->link )
- )
- . $next->title
- . Html::element( 'img', array(
- 'class' => 'mw-bookmanagerv2-nav-next',
- 'src' => $imagePath .
'Angle_right_font_awesome.png'
- ), '' )
- . Html::closeElement( 'a' )
- . Html::closeElement( 'div' )
- . Html::rawElement( 'div', array(
- 'class' => array(
+ . Html::closeElement( 'a' );
+ if ( $prev ) {
+ $linkContents = Html::element( 'img', array(
+ 'class' => 'mw-bookmanagerv2-nav-prev',
+ 'src' => $imagePath .
'Angle_left_font_awesome.png'
+ ), '' )
+ . $prev->title;
+ $html .= Linker::link(
+ Title::newFromText( $prev->link ),
+ $linkContents,
+ array( 'class' => 'mw-bookmanagerv2-nav-prev' )
+ );
+ }
+ if ( $next ) {
+ $linkContents = $next->title
+ .Html::element( 'img', array(
+ 'class' => 'mw-bookmanagerv2-nav-next',
+ 'src' => $imagePath .
'Angle_right_font_awesome.png'
+ ), '' );
+ $html .= Linker::link(
+ Title::newFromText( $next->link ),
+ $linkContents,
+ array( 'class' => 'mw-bookmanagerv2-nav-next' )
+ );
+ }
+ $html .= Html::closeElement( 'div' )
+ . Html::rawElement( 'div', array(
+ 'class' => array(
'mw-bookmanagerv2-nav-dropdown',
'mw-bookmanagerv2-nav-data' )
),
@@ -89,29 +106,251 @@
)
. Html::closeElement( 'div' )
. Html::closeElement( 'div' );
-
+
$out->prependHTML( $html );
}
+ /**
+ * Pulls the text from an indicated page, decodes it as JSON.
+ *
+ * @param Title $title Title of the page from which the JSON is pulled
+ * @returns object JSON object
+ */
+ public static function getJson( Title $title ) {
+ if ( !$title->exists() ) {
+ return false;
+ }
+ $jsonPage = WikiPage::factory( $title );
+ return json_decode( $jsonPage->getText( Revision::FOR_PUBLIC )
);
+ }
+
+ /**
+ * Creates a list item element with a comma-separated list of the array
values
+ *
+ * @param string $key Name of the JSON property
+ * @param array $array Array of strings
+ * @returns string HTML list item element
+ */
+ public static function addArray( $key, $array ) {
+ $output = Html::element( 'li', array(),
+ wfMessage( 'bookmanagerv2-' . $key, count( $array ) )
+ . ": "
+ . implode( ', ', $array ) );
+ return $output;
+ }
+
+ /**
+ * Creates a list item element with a string
+ *
+ * @param string $key Name of the JSON property
+ * @param string $string Name of the string value
+ * @returns string HTML list item element
+ */
+ public static function addString( $key, $string ) {
+ $output = Html::element( 'li', array(),
+ wfMessage( 'bookmanagerv2-' . $key )
+ . ": "
+ . $string );
+ return $output;
+ }
+
+ /**
+ * Creates a list item element with a date.
+ *
+ * @param int|null $year Year
+ * @param int|null $month Month
+ * @param int|null $day Day
+ * @returns string HTML list item element
+ */
+ public static function addDate( $year, $month, $day ) {
+ //TODO: This needs to be localized.
+ $output = Html::openElement( 'li', array() );
+ $output .= wfMessage( 'bookmanagerv2-publication-date' )
+ . ": ";
+ if ( $day && !$month ) {
+ //Having a day without a month doesn't make much sense
+ $output .= $year;
+ } else {
+ $output .= $day ? $day . "/" : "";
+ $output .= $month ? $month . "/" : "";
+ $output .= $year ? $year : "";
+ }
+ $output .= Html::closeElement( 'li' );
+ return $output;
+ }
+
+ /**
+ * Generates HTML for the chapter list. All pages except the current one
+ * will be linked.
+ *
+ * @param object $sections Sections property of the JSON
+ * @param string $currentPageTitle Title of the page that's being viewed
+ * @returns string HTML ordered list element
+ */
+ public static function formatChapterList( $sections, $currentPageTitle
) {
+ $html = Html::openElement( 'ol', array() );
+ foreach( $sections as $key => $val ) {
+ if ( $val->{"link"} != $currentPageTitle ) {
+ $html .= Html::openElement( 'li', array() )
+ . Linker::link(
+ Title::newFromText(
$val->{"link"} ),
+ $val->{"name"}
+ )
+ . Html::closeElement( 'li' );
+ } else {
+ $html .= Html::element( 'li', array(),
$val->{"name"} );
+ }
+ }
+ $html .= Html::closeElement( 'ol' );
+ return $html;
+ }
+
+ /**
+ * Generates HTML for the metadata list.
+ *
+ * @param object $jsonBook JSON representation of the book
+ * @returns string HTML unordered list element
+ */
+ public static function formatMetadata( $jsonBook ) {
+ $metadata = Html::openElement( 'ul', array() )
+ . Html::openElement( 'li', array() )
+ . wfMessage( 'bookmanagerv2-title' )
+ . ": "
+ . Html::element( 'span', array( 'class' => 'title' ),
+ $jsonBook->{'title'} )
+ . Html::closeElement( 'li' );
+ if ( isset( $jsonBook->{"alternate_titles"} ) ) {
+ $metadata .= self::addArray( "alternate-titles",
+ $jsonBook->{"alternate_titles"} );
+ }
+ if ( isset( $jsonBook->{"authors"} ) ) {
+ $metadata .= self::addArray( "authors",
$jsonBook->{"authors"} );
+ }
+ if ( isset( $jsonBook->{"translators"} ) ) {
+ $metadata .= self::addArray( "translators",
+ $jsonBook->{"translators"} );
+ }
+ if ( isset( $jsonBook->{"editors"} ) ) {
+ $metadata .= self::addArray( "editors",
$jsonBook->{"editors"} );
+ }
+ if ( isset( $jsonBook->{"illustrators"} ) ) {
+ $metadata .= self::addArray( "illustrators",
+ $jsonBook->{"illustrators"} );
+ }
+ if ( isset( $jsonBook->{"subtitle"} ) ) {
+ $metadata .= self::addString( "subtitle",
$jsonBook->{"subtitle"} );
+ }
+ if ( isset( $jsonBook->{"series_title"} ) ) {
+ $metadata .= self::addString( "series-title",
+ $jsonBook->{"series_title"} );
+ }
+ if ( isset( $jsonBook->{"volume"} ) ) {
+ $metadata .= self::addString( "volume",
+ (string)$jsonBook->{"volume"} );
+ }
+ if ( isset( $jsonBook->{"edition"} ) ) {
+ $metadata .= self::addString( "edition",
+ (string)$jsonBook->{"edition"} );
+ }
+ if ( isset( $jsonBook->{"publisher"} ) ) {
+ $metadata .= self::addString( "publisher",
$jsonBook->{"publisher"} );
+ }
+ if ( isset( $jsonBook->{"publication_city"} ) ) {
+ $metadata .= self::addString( "publication-city",
+ $jsonBook->{"publication_city"} );
+ }
+ if ( isset( $jsonBook->{"publication_year"} ) )
+ {
+ $year = isset( $jsonBook->{"publication_year"} ) ?
+ $jsonBook->{"publication_year"} : NULL;
+ $month = isset( $jsonBook->{"publication_month"} ) ?
+ $jsonBook->{"publication_month"} : NULL;
+ $day = isset( $jsonBook->{"publication_day"} ) ?
+ $jsonBook->{"publication_day"} : NULL;
+ $metadata .= self::addDate( $year, $month, $day );
+ }
+ if ( isset( $jsonBook->{"printer"} ) ) {
+ $metadata .= self::addString( "printer",
$jsonBook->{"printer"} );
+ }
+ if ( isset( $jsonBook->{"language"} ) ) {
+ //TODO: Transform the language code to the correct
long-form language
+ $metadata .= self::addString( "language",
$jsonBook->{"language"} );
+ }
+ if ( isset( $jsonBook->{"description"} ) ) {
+ $metadata .= self::addString( "description",
+ $jsonBook->{"description"} );
+ }
+ if ( isset( $jsonBook->{"isbn"} ) ) {
+ $metadata .= self::addString( "isbn",
$jsonBook->{"isbn"} );
+ }
+ if ( isset( $jsonBook->{"lccn"} ) ) {
+ $metadata .= self::addString( "lccn",
$jsonBook->{"lccn"} );
+ }
+ if ( isset( $jsonBook->{"oclc"} ) ) {
+ $metadata .= self::addString( "oclc",
$jsonBook->{"oclc"} );
+ }
+ $metadata .= Html::closeElement( 'ul' );
+
+ return $metadata;
+ }
+
+ /**
+ * Adds the navigation bar if the page is in the mainspace, and if it
contains
+ * a category of the format [[Category:Book:Book Title]], where
+ * [[Book:Book Title]] is an existing page that contains a valid JSON
+ * block.
+ */
public static function onBeforePageDisplay( OutputPage &$out, Skin
&$skin ) {
// Check that the navigation bar is only added to mainspace
pages.
if ( $out->getTitle()->getNamespace() == NS_MAIN ) {
if ( $out->getRevisionId() != null ) {
- $out->addModules( "ext.BookManagerv2" );
- $prev = (object) array( "link"=>"/prev.html",
"title"=>"Previous" );
- $next = (object) array( "link"=>"/next.html",
"title"=>"Next" );
- $chapterList = Html::element( 'p', array(),
'Chapter 1' )
- . Html::element( 'p', array(), 'Chapter
2' )
- . Html::element( 'p', array(), 'Chapter
3' );
- $metadata = Html::openElement( 'p', array() )
- . 'Title: '
- . Html::element( 'span', array( 'class'
=> 'title' ), 'Title' )
- . Html::closeElement( 'p' )
- . Html::element( 'p', array(), 'Author:
Author');
- self::readingInterfaceUX( $out, $prev, $next,
$chapterList, $metadata );
- }
+ $categories = $out->getCategories();
+ foreach( $categories as $cat )
+ {
+ if ( substr( $cat, 0, 5 ) == "Book:" ) {
+ $jsonPageTitle =
Title::newFromText( $cat );
+ $jsonBook = self::getJson(
$jsonPageTitle );
+ if ( $jsonBook ) {
+ $out->addModules(
"ext.BookManagerv2" );
+ $currentPageTitle =
$out->getTitle()->getText();
+ $prev = (object)
array();
+ $next = (object)
array();
+
+ //Get the previous/next
pages
+ foreach(
$jsonBook->{"sections"} as $key => $val ) {
+ if (
$val->{"link"} == $currentPageTitle ) {
+ if (
$key != 0 ) {
+
$prev->{"title"} =
+
$jsonBook->{"sections"}[ $key - 1 ]->{"name"};
+
$prev->{"link"} =
+
$jsonBook->{"sections"}[ $key - 1 ]->{"link"};
+ } else {
+
$prev = NULL;
+ }
+ if (
$key != count( $jsonBook->{"sections"} ) ) {
+
$next->{"title"} =
+
$jsonBook->{"sections"}[ $key + 1 ]->{"name"};
+
$next->{"link"} =
+
$jsonBook->{"sections"}[ $key + 1 ]->{"link"};
+ } else {
+
$next = NULL;
+ }
+ break;
+ }
+ }
+
+ $chapterList =
self::formatChapterList(
+
$jsonBook->{"sections"}, $currentPageTitle );
+ $metadata =
self::formatMetadata( $jsonBook );
+
+
self::readingInterfaceUX( $out, $prev, $next,
+ $chapterList,
$metadata );
+ return true;
+ }
+ }
+ }
+ }
}
return true;
}
-
}
diff --git a/BookManagerv2.i18n.php b/BookManagerv2.i18n.php
index 156079e..e37a906 100644
--- a/BookManagerv2.i18n.php
+++ b/BookManagerv2.i18n.php
@@ -15,6 +15,28 @@
$messages['en'] = array(
'bookmanagerv2-desc' => 'Adds functionality to enter and store book
metadata and structure',
'bookmanagerv2-invalid-json' => 'Invalid JSON',
+ 'bookmanagerv2-title' => 'Title',
+ 'bookmanagerv2-alternate-titles' => '{{PLURAL:$1|Alternate
title|Alternate titles}}',
+ 'bookmanagerv2-authors' => '{{PLURAL:$1|Author|Authors}}',
+ 'bookmanagerv2-translators' => '{{PLURAL:$1|Translator|Translators}}',
+ 'bookmanagerv2-editors' => '{{PLURAL:$1|Editor|Editors}}',
+ 'bookmanagerv2-illustrators' =>
'{{PLURAL:$1|Illustrator|Illustrators}}',
+ 'bookmanagerv2-subtitle' => 'Subtitle',
+ 'bookmanagerv2-series-title' => 'Series title',
+ 'bookmanagerv2-volume' => 'Volume',
+ 'bookmanagerv2-edition' => 'Edition',
+ 'bookmanagerv2-publisher' => 'Publisher',
+ 'bookmanagerv2-printer' => 'Printer',
+ 'bookmanagerv2-publication-date' => 'Publication date',
+ 'bookmanagerv2-publication-city' => 'Publication city',
+ 'bookmanagerv2-language' => 'Language',
+ 'bookmanagerv2-description' => 'Description',
+ 'bookmanagerv2-source' => 'Source',
+ 'bookmanagerv2-permission' => 'Permission',
+ 'bookmanagerv2-other-versions' => '{{PLURAL:$1|Other version|Other
versions}}',
+ 'bookmanagerv2-isbn' => 'ISBN',
+ 'bookmanagerv2-lccn' => 'LCCN',
+ 'bookmanagerv2-oclc' => 'OCLC',
);
/** Message documentation (Message documentation)
@@ -23,4 +45,40 @@
$messages['qqq'] = array(
'bookmanagerv2-desc' =>
'{{desc|name=BookManagerv2|url=http://www.mediawiki.org/wiki/Extension:BookManagerv2}}',
'bookmanagerv2-invalid-json' => 'Error message shown when an editor
tries to save an invalid JSON block',
+ 'bookmanagerv2-title' => 'Primary title of the work',
+ 'bookmanagerv2-alternate-titles' => 'Any alternate titles by which the
work is known. $1 is the length of the array of titles',
+ 'bookmanagerv2-authors' => 'Authors of the work. $1 is the length of
the array of authors',
+ 'bookmanagerv2-translators' => 'Translators of the work. $1 is the
length of the array of translators',
+ 'bookmanagerv2-editors' => 'Editors of the work. $1 is the length of
the array of editors',
+ 'bookmanagerv2-illustrators' => 'Illustrators of the work. $1 is the
length of the array of illustrators',
+ 'bookmanagerv2-subtitle' => 'Subtitle of the work',
+ 'bookmanagerv2-series-title' => 'Title of the series of which the work
is a member',
+ 'bookmanagerv2-volume' => 'Volume number, if the work is a part of a
set',
+ 'bookmanagerv2-edition' => 'Edition number, if the work has been
republished',
+ 'bookmanagerv2-publisher' => 'Publishing company who published the
work',
+ 'bookmanagerv2-printer' => 'Company that printed the work',
+ 'bookmanagerv2-publication-date' => 'The date the work was published.
This may be just a year, a month and a year, or a year, month, and day.',
+ 'bookmanagerv2-publication-city' => 'City in which the work was
published',
+ 'bookmanagerv2-language' => 'Primary language of the work',
+ 'bookmanagerv2-description' => 'Short description of the work',
+ 'bookmanagerv2-source' => 'Source where the scans or text was
obtained',
+ 'bookmanagerv2-permission' => 'Permission under which the work has been
uploaded',
+ 'bookmanagerv2-other-versions' => 'Other versions of the text on this
wiki',
+ 'bookmanagerv2-isbn' => 'ISBNLURAL:$1|Illustrator|Illustrators}}',
+ 'bookmanagerv2-subtitle' => 'Subtitle',
+ 'bookmanagerv2-series-title' => 'Series title',
+ 'bookmanagerv2-volume' => 'Volume',
+ 'bookmanagerv2-edition' => 'Edition',
+ 'bookmanagerv2-publisher' => 'Publisher',
+ 'bookmanagerv2-printer' => 'Printer',
+ 'bookmanagerv2-publication-date' => 'Publication date',
+ 'bookmanagerv2-publication-city' => 'Publication city',
+ 'bookmanagerv2-language' => 'Language',
+ 'bookmanagerv2-description' => 'Description',
+ 'bookmanagerv2-source' => 'Source',
+ 'bookmanagerv2-permission' => 'Permission',
+ 'bookmanagerv2-other-versions' => '{{PLURAL:$1|Other version|Other
versions}}',
+ 'bookmanagerv2-isbn' => 'International Standard Book Number',
+ 'bookmanagerv2-lccn' => 'Library of Congress Control Number',
+ 'bookmanagerv2-oclc' => 'Online Computer Library Center',
);
--
To view, visit https://gerrit.wikimedia.org/r/73150
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I9c2d77ce649c678ae34343032ef68b2b673dc92e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/BookManagerv2
Gerrit-Branch: master
Gerrit-Owner: Mollywhite <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits