Allow override of page disply within CategoryViewer
authorErik Bernhardson <ebernhardson@wikimedia.org>
Tue, 3 Feb 2015 06:08:12 +0000 (22:08 -0800)
committerMatthias Mullie <git@mullie.eu>
Fri, 20 Feb 2015 12:46:48 +0000 (13:46 +0100)
Patch adds two hook which are described in hooks.txt.  This
is being used to allow Flow to offer two links instead of just
one that are relevant to the page that was categorized.

The default output without these hooks is:

   <a href="...">Topic:Soiasdf90f09</a>

This patch allows flow to provide context as to where this topic came
from, by replacing that with:

   <a href="...">Topic:Soiasdf90f09</a> on <a href="...">Talk:Help</a>

(Note that the names of pages within the Topic namespace will also
become more friendly soonish, but outside the scope of this patch).

Bug: T87793
Related-Flow-Change: Ia4f2953bcd807ba3990e762a2efcaab428c40147
Change-Id: I182e6e35fcc3a2a298e928e088579bdb22e145ff

RELEASE-NOTES-1.25
docs/hooks.txt
includes/CategoryViewer.php

index 67a32ec..df13daa 100644 (file)
@@ -103,6 +103,8 @@ production.
   dynamically-compiled Mustache templates (currently uses lightncandy library).
 * Clickable anchors for each section heading in the content are now generated
   and appear in the gutter on hovering over the heading.
+* Added 'CategoryViewer::doCategoryQuery' and 'CategoryViewer::generateLink' hooks
+  to allow extensions to override how links to pages are rendered within NS_CATEGORY
 
 ==== External libraries ====
 * MediaWiki now requires certain external libraries to be installed. In the past
index 78ac2ff..a88803b 100644 (file)
@@ -870,6 +870,20 @@ $wikiPage: WikiPage that was removed
 'CategoryPageView': Before viewing a categorypage in CategoryPage::view.
 $catpage: CategoryPage instance
 
+'CategoryViewer::doCategoryQuery': After querying for pages to be displayed
+in a Category page. Gives extensions the opportunity to batch load any
+related data about the pages.
+$type: The category type. Either 'page', 'file' or 'subcat'
+$res: Query result from DatabaseBase::select()
+
+'CategoryViewer::generateLink': Before generating an output link allow
+extensions opportunity to generate a more specific or relevant link.
+$type: The category type. Either 'page', 'img' or 'subcat'
+$title: Title object for the categorized page
+$html: Requested html content of anchor
+&$link: Returned value. When set to a non-null value by a hook subscriber
+this value will be used as the anchor instead of Linker::link
+
 'ChangePasswordForm': For extensions that need to add a field to the
 ChangePassword form via the Preferences form.
 &$extraFields: An array of arrays that hold fields like would be passed to the
index 6b86853..1e0bf16 100644 (file)
@@ -174,19 +174,30 @@ class CategoryViewer extends ContextSource {
                // Subcategory; strip the 'Category' namespace from the link text.
                $title = $cat->getTitle();
 
-               $link = Linker::link( $title, htmlspecialchars( $title->getText() ) );
-               if ( $title->isRedirect() ) {
-                       // This didn't used to add redirect-in-category, but might
-                       // as well be consistent with the rest of the sections
-                       // on a category page.
-                       $link = '<span class="redirect-in-category">' . $link . '</span>';
-               }
-               $this->children[] = $link;
+               $this->children[] = $this->generateLink(
+                       'subcat',
+                       $title,
+                       $title->isRedirect(),
+                       htmlspecialchars( $title->getText() )
+               );
 
                $this->children_start_char[] =
                        $this->getSubcategorySortChar( $cat->getTitle(), $sortkey );
        }
 
+       function generateLink( $type, Title $title, $isRedirect, $html = null ) {
+               $link = null;
+               Hooks::run( 'CategoryViewer::generateLink', array( $type, $title, $html, &$link ) );
+               if ( $link === null ) {
+                       $link = Linker::link( $title, $html );
+               }
+               if ( $isRedirect ) {
+                       $link = '<span class="redirect-in-category">' . $link . '</span>';
+               }
+
+               return $link;
+       }
+
        /**
         * Get the character to be used for sorting subcategories.
         * If there's a link from Category:A to Category:B, the sortkey of the resulting
@@ -229,13 +240,7 @@ class CategoryViewer extends ContextSource {
                                $this->gallery->add( $title );
                        }
                } else {
-                       $link = Linker::link( $title );
-                       if ( $isRedirect ) {
-                               // This seems kind of pointless given 'mw-redirect' class,
-                               // but keeping for back-compatibility with user css.
-                               $link = '<span class="redirect-in-category">' . $link . '</span>';
-                       }
-                       $this->imgsNoGallery[] = $link;
+                       $this->imgsNoGallery[] = $this->generateLink( 'image', $title, $isRedirect );
 
                        $this->imgsNoGallery_start_char[] = $wgContLang->convert(
                                $this->collation->getFirstLetter( $sortkey ) );
@@ -252,13 +257,7 @@ class CategoryViewer extends ContextSource {
        function addPage( $title, $sortkey, $pageLength, $isRedirect = false ) {
                global $wgContLang;
 
-               $link = Linker::link( $title );
-               if ( $isRedirect ) {
-                       // This seems kind of pointless given 'mw-redirect' class,
-                       // but keeping for back-compatibility with user css.
-                       $link = '<span class="redirect-in-category">' . $link . '</span>';
-               }
-               $this->articles[] = $link;
+               $this->articles[] = $this->generateLink( 'page', $title, $isRedirect );
 
                $this->articles_start_char[] = $wgContLang->convert(
                        $this->collation->getFirstLetter( $sortkey ) );
@@ -331,6 +330,8 @@ class CategoryViewer extends ContextSource {
                                )
                        );
 
+                       Hooks::run( 'CategoryViewer::doCategoryQuery', array( $type, $res ) );
+
                        $count = 0;
                        foreach ( $res as $row ) {
                                $title = Title::newFromRow( $row );