Add CategoryAfterPageAdded / CategoryAfterPageRemoved hooks
authorOri Livneh <ori@wikimedia.org>
Tue, 5 Mar 2013 22:43:03 +0000 (14:43 -0800)
committerGerrit Code Review <gerrit@wikimedia.org>
Wed, 6 Mar 2013 04:01:54 +0000 (04:01 +0000)
My first instinct was to derive the hook signature from the implementation and
have a generic 'PageCategoriesUpdated' hook that receives the WikiPage object
and two arrays ($added, $deleted). After some reflection I decided to iterate
through each array and call the hook once per category. I like this way of
doing things because a change in page membership could be thought of from two
directions: it is either the category that is gaining or losing a member, or
the page that is gaining or losing a category.

The category name is sufficient for instantiating a Category object without
additional queries being needed, so my preference was to pass objects rather
than string identifiers.

If implementors of handlers happen to be interested in the set of categories or
pages being updated, it is still the case that they only need to write one
function, and track state using a class property.

Change-Id: Ica4c5fb3acdea6c00678ec0794808fa50a1dd39a

RELEASE-NOTES-1.21
docs/hooks.txt
includes/WikiPage.php

index 303e0b0..f7dcd2a 100644 (file)
@@ -113,6 +113,7 @@ production.
   gl, hr, hsb, hu, is, it, kk, kl, ku, ky, la, lb, lt, lv, mk, mo, mt, nl, no,
   oc, pl, pt, rm, ro, ru, rup, sco, sk, sl, smn, sq, sr, sv, tk, tl, tr, tt, uk,
   uz, vi.
+* Added 'CategoryAfterPageAdded' and 'CategoryAfterPageRemoved' hooks.
 
 === Bug fixes in 1.21 ===
 * (bug 40353) SpecialDoubleRedirect should support interwiki redirects.
index 28eedf4..03aa246 100644 (file)
@@ -745,6 +745,14 @@ $output: OutputPage object in use
 the defaults.
 &$namespaces: Array of namespace numbers with corresponding canonical names
 
+'CategoryAfterPageAdded': After a page is added to a category.
+$category: Category that page was added to
+$wikiPage: WikiPage that was added
+
+'CategoryAfterPageRemoved': After a page is removed from a category.
+$category: Category that page was removed from
+$wikiPage: WikiPage that was removed
+
 'CategoryPageView': Before viewing a categorypage in CategoryPage::view.
 $catpage: CategoryPage instance
 
index 6048294..10b3bd8 100644 (file)
@@ -3058,6 +3058,15 @@ class WikiPage implements Page, IDBAccessObject {
                                __METHOD__
                        );
                }
+
+               foreach( $added as $catName ) {
+                       $cat = Category::newFromName( $catName );
+                       wfRunHooks( 'CategoryAfterPageAdded', array( $cat, $this ) );
+               }
+               foreach( $deleted as $catName ) {
+                       $cat = Category::newFromName( $catName );
+                       wfRunHooks( 'CategoryAfterPageRemoved', array( $cat, $this ) );
+               }
        }
 
        /**