Sumit has uploaded a new change for review.

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

Change subject: WikidataPageBanner: PAGEBANNER magic word added
......................................................................

WikidataPageBanner: PAGEBANNER magic word added

'PAGEBANNER' magic word added to WikidataPageBanner extension, which accepts an
image file url on the wiki and renders an alternative pagebanner with that image
as the background. Allows the user to control banner image.

Bug: T97455
Change-Id: I37d02267b61fc070b85a9974eefc5cb66b81083e
---
M WikidataPageBanner.i18n.alias.php
M WikidataPageBanner.php
M includes/WikidataPageBanner.hooks.php
3 files changed, 88 insertions(+), 24 deletions(-)


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

diff --git a/WikidataPageBanner.i18n.alias.php 
b/WikidataPageBanner.i18n.alias.php
index 4258562..c253604 100644
--- a/WikidataPageBanner.i18n.alias.php
+++ b/WikidataPageBanner.i18n.alias.php
@@ -7,3 +7,8 @@
  */
 
 $specialPageAliases['en'] = array();
+$magicWords = array();
+$magicWords['en'] = array(
+       'PAGEBANNER' => array( 0, 'PAGEBANNER' ),
+);
+
diff --git a/WikidataPageBanner.php b/WikidataPageBanner.php
index 6ecfd0b..ca14bf7 100644
--- a/WikidataPageBanner.php
+++ b/WikidataPageBanner.php
@@ -44,6 +44,12 @@
 $wgHooks['ArticleViewHeader'][] = 'WikidataPageBanner::viewBanner';
 // Load Banner modules, styles
 $wgHooks['BeforePageDisplay'][] = 'WikidataPageBanner::loadModules';
+$wgHooks['ParserFirstCallInit'][] = 'WikidataPageBannerSetupParserFunction';
+
+function WikidataPageBannerSetupParserFunction( $parser ) {
+       $parser->setFunctionHook( 'PAGEBANNER', 
'WikidataPageBanner::loadBanner', SFH_NO_HASH );
+       return true;
+}
 
 // include WikidataPageBanner class file
 require_once __DIR__ . "/includes/WikidataPageBanner.hooks.php";
diff --git a/includes/WikidataPageBanner.hooks.php 
b/includes/WikidataPageBanner.hooks.php
index 0df9704..26bedca 100644
--- a/includes/WikidataPageBanner.hooks.php
+++ b/includes/WikidataPageBanner.hooks.php
@@ -1,5 +1,6 @@
 <?php
 class WikidataPageBanner {
+
        /**
         * WikidataPageBanner::viewBanner
         *
@@ -7,30 +8,19 @@
         * @return bool
         */
        public static function viewBanner( $article ) {
-               global $wgPBImageUrl, $wgBannerNamespaces;
-               $bannerurl = $wgPBImageUrl;
-               $title = $article->getTitle();
-               $ns = $title->getNamespace();
-               // banner only on specified namespaces, and not Main Page of 
wiki
-               if ( in_array( $ns, $wgBannerNamespaces ) && 
!$title->isMainPage() ) {
-                       $banner = Html::openElement( 'div', array( 'class' => 
'noprint' ) ) .
-                       Html::openElement( 'div', array( 'class' => 
'ext-wpb-pagebanner',
-                                                                       'style' 
=> "background-image:url($bannerurl);"
-                                                               )
-                                                       ) .
-                       Html::openElement( 'div', array( 'class' => 'topbanner' 
) ) .
-                       Html::element( 'div',
-                               array( 'class' => 'name' ),
-                               $title
-                       ) .
-                       Html::element( 'div',
-                               array( 'class' => 'iconbox' )
-                       ) .
-                       Html::closeElement( 'div' ) .
-                       Html::closeElement( 'div' ) .
-                       Html::closeElement( 'div' ) . "\n";
-                       $out = $article->getContext()->getOutput();
-                       $out->addHtml( $banner );
+               // if the page uses no #PAGEBANNER invocation, insert default 
banner
+               $text = $article->getPage()->getRawText();
+               if ( !MagicWord::get( 'PAGEBANNER' )->match( $text ) ) {
+                       global $wgPBImageUrl, $wgBannerNamespaces;
+                       $bannerurl = $wgPBImageUrl;
+                       $title = $article->getTitle();
+                       $ns = $title->getNamespace();
+                       // banner only on specified namespaces, and not Main 
Page of wiki
+                       if ( in_array( $ns, $wgBannerNamespaces ) && 
!$title->isMainPage() ) {
+                               $banner = self::createBanner( $bannerurl, 
$title );
+                               $out = $article->getContext()->getOutput();
+                               $out->addHtml( $banner );
+                       }
                }
                return true;
        }
@@ -50,4 +40,67 @@
                        $out->addModuleStyles( 'ext.WikidataPageBanner' );
                }
        }
+
+       /**
+        * WikidataPageBanner::loadBanner
+        * Parser function hooked to 'PAGEBANNER' magic word, to expand and 
load banner.
+        *
+        * @param  $parser Parser
+        * @param  $bannerurl Url of custom banner
+        * @return output
+        */
+       public static function loadBanner( $parser, $bannerurl ) {
+               global $wgBannerNamespaces;
+               $fileurl = self::getBannerUrl( $bannerurl );
+               $banner='';
+               $title = $parser->getTitle();
+               $ns = $title->getNamespace();
+               if ( in_array( $ns, $wgBannerNamespaces ) && 
!$title->isMainPage() ) {
+                       $banner = self::createBanner( $fileurl, $title );
+               }
+               return array( $banner, 'noparse' => true, 'isHTML' => true );
+       }
+
+       /**
+        * WikidataPageBanner::getBannerUrl Return the full url of the banner 
image,
+        * stored on the wiki, given the image name.
+        *
+        * @param  string $filename Filename of the banner image
+        * @return string Full url of the banner image on the wiki
+        */
+       public static function getBannerUrl( $filename ) {
+               // make title object from image name
+               $title = Title::makeTitleSafe( NS_IMAGE, $filename );
+               $image = wfFindFile( $title );
+               return $image->getFullUrl();
+       }
+
+       /**
+        * WikidataPageBanner::createBanner
+        * Returns the html code for the pagebanner
+        *
+        * @param string $bannerurl Url of the image in the banner
+        * @param string $title Title to display on banner
+        * @return string Html code of the banner
+        * TODO:Move this banner html code to a template.
+        */
+       public static function createBanner( $bannerurl, $title ) {
+               $banner = Html::openElement( 'div', array( 'class' => 'noprint' 
) ) .
+                       Html::openElement( 'div', array( 'class' => 
'ext-wpb-pagebanner',
+                                                                       'style' 
=> "background-image:url($bannerurl);"
+                                                               )
+                                                       ) .
+                       Html::openElement( 'div', array( 'class' => 'topbanner' 
) ) .
+                       Html::element( 'div',
+                               array( 'class' => 'name' ),
+                               $title
+                       ) .
+                       Html::element( 'div',
+                               array( 'class' => 'iconbox' )
+                       ) .
+                       Html::closeElement( 'div' ) .
+                       Html::closeElement( 'div' ) .
+                       Html::closeElement( 'div' ) . "\n";
+               return $banner;
+       }
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I37d02267b61fc070b85a9974eefc5cb66b81083e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikidataPageBanner
Gerrit-Branch: master
Gerrit-Owner: Sumit <asthana.sumi...@gmail.com>

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

Reply via email to