From a4afffd2ba2f19413712e6165b3f5585907e879b Mon Sep 17 00:00:00 2001 From: Ori Livneh Date: Tue, 5 Mar 2013 14:43:03 -0800 Subject: [PATCH] Add CategoryAfterPageAdded / CategoryAfterPageRemoved hooks 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 | 1 + docs/hooks.txt | 8 ++++++++ includes/WikiPage.php | 9 +++++++++ 3 files changed, 18 insertions(+) diff --git a/RELEASE-NOTES-1.21 b/RELEASE-NOTES-1.21 index 303e0b0fe9..f7dcd2a120 100644 --- a/RELEASE-NOTES-1.21 +++ b/RELEASE-NOTES-1.21 @@ -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. diff --git a/docs/hooks.txt b/docs/hooks.txt index 28eedf4725..03aa246ffc 100644 --- a/docs/hooks.txt +++ b/docs/hooks.txt @@ -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 diff --git a/includes/WikiPage.php b/includes/WikiPage.php index 604829415b..10b3bd86ae 100644 --- a/includes/WikiPage.php +++ b/includes/WikiPage.php @@ -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 ) ); + } } /** -- 2.20.1