[MediaWiki-commits] [Gerrit] mediawiki...CategoryWatch[master]: Add the ability to notify watchers of parent categories
Seanchenldap has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/370774 ) Change subject: Add the ability to notify watchers of parent categories .. Add the ability to notify watchers of parent categories Needs to be tested. Change-Id: Ic7d32294a573fcb95b96e8c00f6792c39abdb5d8 --- M CategoryWatch.php M README.md 2 files changed, 100 insertions(+), 4 deletions(-) Approvals: Seanchenldap: Looks good to me, approved jenkins-bot: Verified diff --git a/CategoryWatch.php b/CategoryWatch.php index cfd3fca..aca8f23 100644 --- a/CategoryWatch.php +++ b/CategoryWatch.php @@ -223,6 +223,97 @@ } } } + + global $wgCategoryWatchNotifyParentWatchers; + if ( $wgCategoryWatchNotifyParentWatchers ) { + self::notifyParentWatchers(); + } + } + + /** +* Notify the watchers of parent categories +*/ + protected static function notifyParentWatchers() { + self::$watcher->allparents = []; + self::$watcher->i = 0; + self::$watcher->findCategoryParents( self::$watcher->after ); + ## For each active parent category, send the mail + if ( self::$watcher->allparents ) { + $page = $article->getTitle(); + $pageurl = $page->getFullUrl(); + foreach ( self::$watcher->allparents as $cat ) { + $title = Title::newFromText( $cat, NS_CATEGORY ); + $message = wfMessage( + 'categorywatch-catchange', $page, + self::$watcher->friendlyCat( $cat ) + ); + self::$watcher->notifyWatchers( + $title, $user, $message, $summary, $medit, $pageurl + ); + } + } + } + + /** +* Recursively find all parents of the given categories +* +* @param array $catarray the categories +*/ + protected function findCategoryParents( array $catarray ) { + $this->i++; + if ( $this->i == 200 ) { + return; + } + + if ( $catarray ) { + foreach ( $catarray as $catname ) { + self::$watcher->allparents[] = $catname; + $id = self::$watcher->getCategoryArticleId( $catname ); + if ( is_numeric( $id ) ) { + $parentCat = self::$watcher->getParentCategories( $id ); + if ( $parentCat ) { + self::$watcher->allparents[] = $parentCat; + self::$watcher->findCategoryParents( [ $parentCat ] ); + } + } + } + self::$watcher->allparents = array_unique( self::$watcher->allparents ); + } + } + + /** +* Return the parent categories +* @param int $id Category Article id +* @return parents +*/ + protected function getParentCategories( $id ) { + $dbr = wfGetDB( DB_SLAVE ); + $cl = $dbr->tableName( 'categorylinks' ); + $res = $dbr->select( + $cl, 'cl_to', "cl_from = $id", __METHOD__, + [ 'ORDER BY' => 'cl_sortkey' ] + ); + $row = $dbr->fetchRow( $res ); + $dbr->freeResult( $res ); + if ( empty( $row[0] ) ) { + return false; + } + return $row[0]; + } + + /** +* Load page ID of one category +* +* @param string $catname name of category +* @return int +*/ + protected function getCategoryArticleId( $catname ) { + $dbr = wfGetDB( DB_SLAVE ); + $cl = $dbr->tableName( 'page' ); + $res = $dbr->select( $cl, 'page_id', "page_title = '$catname'", __METHOD__ ); + $row = $dbr->fetchRow( $res ); + $dbr->freeResult( $res ); + return $row[0]; } /** diff --git a/README.md b/README.md index 1fb356e..6433bdf 100644 --- a/README.md +++ b/README.md @@ -18,14 +18,19 @@ $wgCategoryWatchNotifyEditor = true; ``` -Set this to give every user a unique category that they're automatically watching. The format of the category name is defined on the "categorywatch-autocat" localisation message
[MediaWiki-commits] [Gerrit] mediawiki...CategoryWatch[master]: Add the ability to notify watchers of parent categories
MarkAHershberger has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/370774 ) Change subject: Add the ability to notify watchers of parent categories .. Add the ability to notify watchers of parent categories Needs to be tested. Change-Id: Ic7d32294a573fcb95b96e8c00f6792c39abdb5d8 --- M CategoryWatch.php M README.md 2 files changed, 100 insertions(+), 4 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/CategoryWatch refs/changes/74/370774/1 diff --git a/CategoryWatch.php b/CategoryWatch.php index cfd3fca..aca8f23 100644 --- a/CategoryWatch.php +++ b/CategoryWatch.php @@ -223,6 +223,97 @@ } } } + + global $wgCategoryWatchNotifyParentWatchers; + if ( $wgCategoryWatchNotifyParentWatchers ) { + self::notifyParentWatchers(); + } + } + + /** +* Notify the watchers of parent categories +*/ + protected static function notifyParentWatchers() { + self::$watcher->allparents = []; + self::$watcher->i = 0; + self::$watcher->findCategoryParents( self::$watcher->after ); + ## For each active parent category, send the mail + if ( self::$watcher->allparents ) { + $page = $article->getTitle(); + $pageurl = $page->getFullUrl(); + foreach ( self::$watcher->allparents as $cat ) { + $title = Title::newFromText( $cat, NS_CATEGORY ); + $message = wfMessage( + 'categorywatch-catchange', $page, + self::$watcher->friendlyCat( $cat ) + ); + self::$watcher->notifyWatchers( + $title, $user, $message, $summary, $medit, $pageurl + ); + } + } + } + + /** +* Recursively find all parents of the given categories +* +* @param array $catarray the categories +*/ + protected function findCategoryParents( array $catarray ) { + $this->i++; + if ( $this->i == 200 ) { + return; + } + + if ( $catarray ) { + foreach ( $catarray as $catname ) { + self::$watcher->allparents[] = $catname; + $id = self::$watcher->getCategoryArticleId( $catname ); + if ( is_numeric( $id ) ) { + $parentCat = self::$watcher->getParentCategories( $id ); + if ( $parentCat ) { + self::$watcher->allparents[] = $parentCat; + self::$watcher->findCategoryParents( [ $parentCat ] ); + } + } + } + self::$watcher->allparents = array_unique( self::$watcher->allparents ); + } + } + + /** +* Return the parent categories +* @param int $id Category Article id +* @return parents +*/ + protected function getParentCategories( $id ) { + $dbr = wfGetDB( DB_SLAVE ); + $cl = $dbr->tableName( 'categorylinks' ); + $res = $dbr->select( + $cl, 'cl_to', "cl_from = $id", __METHOD__, + [ 'ORDER BY' => 'cl_sortkey' ] + ); + $row = $dbr->fetchRow( $res ); + $dbr->freeResult( $res ); + if ( empty( $row[0] ) ) { + return false; + } + return $row[0]; + } + + /** +* Load page ID of one category +* +* @param string $catname name of category +* @return int +*/ + protected function getCategoryArticleId( $catname ) { + $dbr = wfGetDB( DB_SLAVE ); + $cl = $dbr->tableName( 'page' ); + $res = $dbr->select( $cl, 'page_id', "page_title = '$catname'", __METHOD__ ); + $row = $dbr->fetchRow( $res ); + $dbr->freeResult( $res ); + return $row[0]; } /** diff --git a/README.md b/README.md index 1fb356e..6433bdf 100644 --- a/README.md +++ b/README.md @@ -18,14 +18,19 @@ $wgCategoryWatchNotifyEditor = true; ``` -Set this to give every user a unique category that they're automatically watching. The format of the category name is defined on the "categorywatch-autocat"