Bmansurov has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/381133 )
Change subject: WIP: add image info ...................................................................... WIP: add image info Bug: T175856 Change-Id: I7ef0444c474a904a2610c3960ac8524bacad67dd --- M i18n/en.json M i18n/qqq.json M includes/BookRenderer.php M includes/DataProvider.php A templates/images.mustache 5 files changed, 100 insertions(+), 4 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Collection refs/changes/33/381133/1 diff --git a/i18n/en.json b/i18n/en.json index 0627409..478cf6b 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -189,6 +189,10 @@ "coll-notice-download-pdf-text": "While the Book Creator PDF rendering is being replaced, you can try '''''Download as PDF''''' from the sidebar tools for individual articles.", "coll-toc-title": "Table of Contents", "coll-contributors-title": "Contributors", + "coll-images-title": "Images", + "coll-images-source": "Source", + "coll-images-license": "License", + "coll-images-original-author": "Original artist", "coll-rendererror-title": "Could not generate the book", "coll-rendererror-no-session": "Book definition not found in session. It might have been discarded due to inactivity.", "coll-rendererror-pdf": "PDF rendering failed: $1", diff --git a/i18n/qqq.json b/i18n/qqq.json index 0a1befa..9d5da30 100644 --- a/i18n/qqq.json +++ b/i18n/qqq.json @@ -216,6 +216,10 @@ "coll-notice-download-pdf-text": "Text describing how to download article as PDF while Book Creator PDF feature is unavailable", "coll-toc-title": "Text shown on top of PDF table of contents", "coll-contributors-title": "Text shown on top of PDF contributors section", + "coll-images-title": "Text shown on top of PDF images section", + "coll-images-source": "Text shown beside the image title of each image in the images section", + "coll-images-license": "Text shown beside the image title of each image in the images section", + "coll-images-original-author": "Text shown beside the image title of each image in the images section", "coll-rendererror-title": "Error page title for book rendering errors.", "coll-rendererror-no-session": "Error shown when trying to render a book but there is no collection stored in the session.", "coll-rendererror-pdf": "Error shown when the Electron request to render the book into PDF failed. Parameters:\n* $1 - error message", diff --git a/includes/BookRenderer.php b/includes/BookRenderer.php index 1825ca7..a6b602e 100644 --- a/includes/BookRenderer.php +++ b/includes/BookRenderer.php @@ -98,7 +98,8 @@ $final = $this->renderCoverAndToc( $collection, $metadata ) . $final - . $this->renderContributors( $metadata, $headingCounter->incrementAndGetTopLevel() ); + . $this->renderContributors( $metadata, $headingCounter->incrementAndGetTopLevel() ) + . $this->renderImageInfos( $metadata, $headingCounter->incrementAndGetTopLevel() ); return $final; } @@ -172,6 +173,13 @@ 'anchor' => 'mw-book-contributors', 'number' => $headingCounter->incrementAndGetTopLevel(), ]; + $outline[] = [ + 'text' => wfMessage( 'coll-images-title' )->text(), + 'type' => 'images', + 'level' => $contributorsLevel, + 'anchor' => 'mw-book-images', + 'number' => $headingCounter->incrementAndGetTopLevel(), + ]; $metadata['outline'] = $outline; return $this->templateParser->processTemplate( 'toc', $this->fixTemplateData( [ @@ -203,6 +211,25 @@ } /** + * Generate HTML for the images used in the book + * @param array[] $metadata Map of prefixed DB key => metadata, as returned by fetchMetadata(). + * @param string $sectionNumber The section number for the images section, if any. + * @return string HTML to append to the book. + */ + private function renderImageInfos( $metadata, $sectionNumber = null ) { + var_dump($metadata['images'], $sectionNumber ); + return $this->templateParser->processTemplate( 'images', [ + 'sectionNumber' => $sectionNumber, + 'images?' => count( $metadata['images'] ), + 'images' => $metadata['images'], + 'headingMsg' => wfMessage( 'coll-images-title' )->text(), + 'sourceMsg' => wfMessage( 'coll-images-source' )->text(), + 'licenseMsg' => wfMessage( 'coll-images-license' )->text(), + 'artistMsg' => wfMessage( 'coll-images-original-artist' )->text() + ] ); + } + + /** * Get the part inside the <body> from an HTML file. * Not very robust (a <body> tag in a comment or CDATA section could confuse it) but the * <head> section has no user-controlled part so using it with Parsoid HTML should be fine. diff --git a/includes/DataProvider.php b/includes/DataProvider.php index dacef53..487b9ce 100644 --- a/includes/DataProvider.php +++ b/includes/DataProvider.php @@ -61,7 +61,7 @@ $requests = array_map( function ( array $item, Title $title ) { $url = '/restbase/local/v1/page/html/' . wfUrlencode( $title->getPrefixedDBkey() ); if ( isset( $item['revision'] ) ) { - $url .= '/' . $item['revision']; + // $url .= '/' . $item['revision']; } return [ 'method' => 'GET', @@ -133,15 +133,16 @@ 'jsconfigvars' => [], ]; - // get contributors + // get contributors and images $params = [ 'format' => 'json', 'action' => 'query', - 'prop' => 'contributors', + 'prop' => 'contributors|images', 'redirects' => 1, 'pclimit' => 'max', // 500; more titles than that will probably blow up Electron anyway 'titles' => implode( '|', $dbkeys ), ]; + $images = []; do { $data = $this->makeActionApiRequest( $params ); $continue = isset( $data['continue'] ) ? $data['continue'] : []; @@ -150,9 +151,56 @@ foreach ( $page['contributors'] as $key => $contrib ) { $metadata['contributors'][$contrib['name']] = $contrib['userid']; } + foreach ( $page['images'] as $image ) { + $images[] = $image['title']; + } } } while ( $continue ); + // get image infos + if ( $images ) { + $params = [ + 'action' => 'query', + 'titles' => implode( '|', $images ), + 'prop' => 'imageinfo', + 'iiprop' => 'url|extmetadata' + ]; + do { + $data = $this->makeActionApiRequest( $params ); + $continue = isset( $data['continue'] ) ? $data['continue'] : []; + $params = $continue + $params; + foreach ( $data['query']['pages'] as $page ) { + if ( array_key_exists( 'imageinfo', $page ) ) { + $imageinfo = $page['imageinfo'][0]; + + $license = ''; + $credit = ''; + $artist = ''; + if ( array_key_exists( 'extmetadata', $imageinfo ) ) { + $extmetadata = $imageinfo['extmetadata']; + if ( isset( $extmetadata['LicenseShortName']['value'] ) ) { + $license = $extmetadata['LicenseShortName']['value']; + } + if ( isset( $extmetadata['Credit']['value'] ) ) { + $credit = $extmetadata['Credit']['value']; + } + if ( isset( $extmetadata['Artist']['value'] ) ) { + $artist = $extmetadata['Artist']['value']; + } + } + + $metadata['images'][$page['title']] = [ + 'title' => $page['title'], + 'url' => isset( $imageinfo['url'] ) ? $imageinfo['url' ] : '', + 'license' => $license, + 'credit' => $credit, + 'artist' => $artist + ]; + } + } + } while ( $continue ); + } + // get sections & modules foreach ( $dbkeys as $dbkey ) { $data = $this->makeActionApiRequest( [ diff --git a/templates/images.mustache b/templates/images.mustache new file mode 100644 index 0000000..48291fa --- /dev/null +++ b/templates/images.mustache @@ -0,0 +1,13 @@ +{{#images?}} + <h1 id="mw-book-images" data-mw-sectionnumber={{sectionNumber}}>{{headingMsg}}</h1> + <ul> + {{#images}} + <li> + <b>{{title}}</b> + <i>{{sourceMsg}}</i>: {{url}} + <i>{{licenseMsg}}</i>: {{credits}} + <i>{{artistMsg}}</i>: {{{artist}}} {{! artist may contain HTML }} + </li> + {{/images}} + </ul> +{{/images?}} -- To view, visit https://gerrit.wikimedia.org/r/381133 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I7ef0444c474a904a2610c3960ac8524bacad67dd Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Collection Gerrit-Branch: master Gerrit-Owner: Bmansurov <bmansu...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits