From 774ddc10769082698fc5ee192704459b9fb6838b Mon Sep 17 00:00:00 2001 From: James Montalvo Date: Mon, 24 Apr 2017 09:22:27 -0500 Subject: [PATCH] Make work in MW 1.27 (#3) --- Code Considerations.md | 23 --- CopyWatchers.i18n.php | 18 --- CopyWatchers.php | 78 ++------- Magic.php | 7 + NOTES.md | 9 -- README.md | 33 ++++ extension.json | 21 +++ i18n/en.json | 4 + .../CopyWatchers.php | 148 +++++++----------- includes/Setup.php | 30 ++++ 10 files changed, 162 insertions(+), 209 deletions(-) delete mode 100644 Code Considerations.md delete mode 100644 CopyWatchers.i18n.php create mode 100644 Magic.php delete mode 100644 NOTES.md create mode 100644 extension.json create mode 100644 i18n/en.json rename CopyWatchers.body.php => includes/CopyWatchers.php (50%) create mode 100644 includes/Setup.php diff --git a/Code Considerations.md b/Code Considerations.md deleted file mode 100644 index cbd780d..0000000 --- a/Code Considerations.md +++ /dev/null @@ -1,23 +0,0 @@ -## Manual:Hooks/ArticleSave -Use this for MW 1.20 and earlier - -####Define Function: -public static function onArticleSave( &$article, &$user, &$text, &$summary, -$minor, $watchthis, $sectionanchor, &$flags, &$status ) { ... } - -####Attach hook: -$wgHooks['ArticleSave'][] = 'MyExtensionHooks::onArticleSave'; - - -## Manual:Hooks/PageContentSave - -Use this for MW 1.21 and later - -####Define function: -public static function onPageContentSave( $wikiPage, $user, $content, $summary, -$isMinor, $isWatch, $section ) { ... } - - -####Attach Hook: -$wgHooks['PageContentSave'][] = 'MyExtensionHooks::onPageContentSave'; - diff --git a/CopyWatchers.i18n.php b/CopyWatchers.i18n.php deleted file mode 100644 index fb32b64..0000000 --- a/CopyWatchers.i18n.php +++ /dev/null @@ -1,18 +0,0 @@ - 'Copy Watchers', - 'copywatchers-desc' => 'Allows editors to copy watchers from other pages using the parser function #copywatchers', -); -$magicWords['en'] = array( - 'copywatchers' => array( 0, 'copywatchers' ), -); diff --git a/CopyWatchers.php b/CopyWatchers.php index bc6ed9f..7543962 100644 --- a/CopyWatchers.php +++ b/CopyWatchers.php @@ -1,67 +1,15 @@ __FILE__, - 'name' => 'Copy Watchers', - 'url' => 'http://www.mediawiki.org/wiki/Extension:CopyWatchers', - 'author' => 'James Montalvo', - 'descriptionmsg' => 'copywatchers-desc', - 'version' => '0.1.0' -); - -$dir = dirname( __FILE__ ) . '/'; -$wgExtensionMessagesFiles['CopyWatchers'] = $dir . 'CopyWatchers.i18n.php'; -$wgAutoloadClasses['CopyWatchers'] = $dir . 'CopyWatchers.body.php'; - -// Specify the function that will initialize the parser function. -$wgHooks['ParserFirstCallInit'][] = 'CopyWatchers::setup'; - - -/* -$wgAvailableRights[] = 'semanticwatch'; -$wgAvailableRights[] = 'semanticwatchgroups'; - -$wgHooks['LoadExtensionSchemaUpdates'][] = 'SWLHooks::onSchemaUpdate'; -$wgHooks['SMWStore::updateDataBefore'][] = 'SWLHooks::onDataUpdate'; -$wgHooks['GetPreferences'][] = 'SWLHooks::onGetPreferences'; -$wgHooks['UserSaveOptions'][] = 'SWLHooks::onUserSaveOptions'; -$wgHooks['AdminLinks'][] = 'SWLHooks::addToAdminLinks'; -$wgHooks['PersonalUrls'][] = 'SWLHooks::onPersonalUrls'; -$wgHooks['SWLGroupNotify'][] = 'SWLHooks::onGroupNotify'; - -$wgExtensionFunctions[] = 'wfSetupCatHook'; -$wgHooks['LanguageGetMagic'][] = 'wfCatHookLanguageGetMagic'; - -$wgHooks['ParserAfterTidy'][] = 'wfUserPageViewTracker'; - -class CategoryWatch { - function __construct() { - global $wgHooks; - $wgHooks['ArticleSave'][] = $this; - $wgHooks['ArticleSaveComplete'][] = $this; - } - ... -} - - - -*/ diff --git a/Magic.php b/Magic.php new file mode 100644 index 0000000..af4ccd5 --- /dev/null +++ b/Magic.php @@ -0,0 +1,7 @@ + array( 0, 'copywatchers' ), +); diff --git a/NOTES.md b/NOTES.md deleted file mode 100644 index 52256fa..0000000 --- a/NOTES.md +++ /dev/null @@ -1,9 +0,0 @@ -NOTES -===== - -To-dos, random thoughts, things to consider... - -## Possible considerations - -* When saving a page with no (or few) watchers, promt with "would you -like to add watchers?" and give suggestions basd on content. diff --git a/README.md b/README.md index d71fcb9..bbfc7b0 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,41 @@ A MediaWiki extension which allows a pages editor to add the watchers of one page to the page being edited using the parser function #copywatchers +## Notes + +To-dos, random thoughts, things to consider... + +### Possible considerations + +* When saving a page with no (or few) watchers, promt with "would you +like to add watchers?" and give suggestions basd on content. + ## Code Considerations ### Possible Hooks to use: * Manual:Hooks/AlternateUserMailer (MW 1.19+) * Manual:Hooks/AbortEmailNotification (MW 1.20+) + +### Manual:Hooks/ArticleSave +Use this for MW 1.20 and earlier + +####Define Function: +public static function onArticleSave( &$article, &$user, &$text, &$summary, +$minor, $watchthis, $sectionanchor, &$flags, &$status ) { ... } + +####Attach hook: +$wgHooks['ArticleSave'][] = 'MyExtensionHooks::onArticleSave'; + + +### Manual:Hooks/PageContentSave + +Use this for MW 1.21 and later + +####Define function: +public static function onPageContentSave( $wikiPage, $user, $content, $summary, +$isMinor, $isWatch, $section ) { ... } + + +####Attach Hook: +$wgHooks['PageContentSave'][] = 'MyExtensionHooks::onPageContentSave'; + diff --git a/extension.json b/extension.json new file mode 100644 index 0000000..976661c --- /dev/null +++ b/extension.json @@ -0,0 +1,21 @@ +{ + "name": "Copy Watchers", + "version": "0.10.0", + "author": "James Montalvo", + "url": "http://www.mediawiki.org/wiki/Extension:CopyWatchers", + "descriptionmsg": "copywatchers-desc", + "type": "specialpage", + "ExtensionMessagesFiles": { + "CopyWatchersMagic": "Magic.php" + }, + "AutoloadClasses": { + "CopyWatchers\\Setup": "includes/Setup.php", + "CopyWatchers\\CopyWatchers": "includes/CopyWatchers.php" + }, + "Hooks": { + "ParserFirstCallInit": [ + "CopyWatchers\\Setup::setupParserFunctions" + ] + }, + "manifest_version": 1 +} diff --git a/i18n/en.json b/i18n/en.json new file mode 100644 index 0000000..e87bcba --- /dev/null +++ b/i18n/en.json @@ -0,0 +1,4 @@ +{ + "copywatchers": "Copy Watchers", + "copywatchers-desc": "Allows editors to copy watchers from other pages using the parser function #copywatchers" +} diff --git a/CopyWatchers.body.php b/includes/CopyWatchers.php similarity index 50% rename from CopyWatchers.body.php rename to includes/CopyWatchers.php index 5142d7a..872c1e2 100644 --- a/CopyWatchers.body.php +++ b/includes/CopyWatchers.php @@ -1,103 +1,62 @@ setFunctionHook( 'copywatchers', array( 'CopyWatchers', 'renderCopyWatchersObj' ), SFH_OBJECT_ARGS ); - } else { - $parser->setFunctionHook( 'copywatchers', array( 'CopyWatchers', 'renderCopyWatchersNonObj' ) ); - } +namespace CopyWatchers; +use ParserFunctionHelper\ParserFunctionHelper; - return true; - - } +class CopyWatchers extends ParserFunctionHelper { + + + public function __construct ( \Parser &$parser ) { + + parent::__construct( + $parser, + 'copywatchers', + array( 'pages' => '', 'showoutput' => false ), + array( ) + ); - static function renderCopyWatchersNonObj (&$parser, $pagesToCopyWatchers='', $showOutput=false) { - - $pagesToCopyWatchers = explode(',', $pagesToCopyWatchers); - - if ( $showOutput == 'true' ) - $showOutput = true; - - return self::renderCopyWatchers( $parser, $pagesToCopyWatchers, $showOutput ); - - } - - static function renderCopyWatchersObj ( &$parser, $frame, $args ) { - - $pagesToCopyWatchers = explode(',', $frame->expand( $args[0] ) ); - - if ( isset( $args[1] ) && trim( $frame->expand( $args[1] ) ) == 'true' ) - $showOutput = true; - else - $showOutput = false; - - return self::renderCopyWatchers( $parser, $pagesToCopyWatchers, $showOutput ); - } - - static function renderCopyWatchers ( &$parser, $pagesToCopyArray, $showOutput ) { + + public function render ( \Parser &$parser, $params ) { global $wgCanonicalNamespaceNames; + $pagesToCopyArray = explode( ',', $params['pages'] ); + $showOutput = $params['showoutput']; + + $newWatchers = array(); - + $output = "Copied watchers from:\n\n"; - + foreach( $pagesToCopyArray as $page ) { - + $output .= "* $page"; // returns Title object $titleObj = self::getNamespaceAndTitle( trim($page) ); - + if ( $titleObj->isRedirect() ) { - $redirectArticle = new Article( $titleObj ); - - // FIXME: thought newFromRedirectRecurse() would find the ultimate page - // but it doesn't appear to be doing that - $titleObj = Title::newFromRedirectRecurse( $redirectArticle->getContent() ); - $output .= " (redirects to " . $titleObj->getFullText() . ")"; - - // FIXME: Do this for MW 1.19+ ??? - // $wp = new WikiPage( $titleObj ); - // $titleObj = $wp->followRedirect(); - - // FIXME: Do one of these for MW 1.21+ ??? - // WikiPage::followRedirect() - // Content::getUltimateRedirectTarget() + $redirectArticle = new \Article( $titleObj ); + $titleObj = $redirectArticle->getRedirectTarget(); + $output .= " (redirects to " . $titleObj->getFullText() . ")"; } - + $ns_num = $titleObj->getNamespace(); - $title = $titleObj->getDBkey(); + $title = $titleObj->getDBkey(); unset( $titleObj ); // prob not necessary since it will be reset shortly. - + $watchers = self::getPageWatchers( $ns_num, $title ); $num_watchers = count($watchers); - + if ($num_watchers == 1) $output .= " (" . count($watchers) . " watcher)\n"; else @@ -108,69 +67,69 @@ static function renderCopyWatchers ( &$parser, $pagesToCopyArray, $showOutput ) } } - + // add list of usernames as watchers to this Title foreach ($newWatchers as $userID => $dummy) { - $u = User::newFromId($userID); + $u = \User::newFromId($userID); $u->addWatch( $parser->getTitle() ); } - + if ( $showOutput ) return $output; else return ""; - + } - + static function getNamespaceAndTitle ( $pageName ) { - + // defaults $ns_num = NS_MAIN; $title = $pageName; $colonPosition = strpos( $pageName, ':' ); // location of colon if exists - + // this won't test for a leading colon...but shouldn't use parser function that way anyway... if ( $colonPosition ) { - $test_ns = self::getNamespaceNumber( + $test_ns = self::getNamespaceNumber( substr( $pageName, 0, $colonPosition ) ); - + // only reset $ns and $title if has colon, and pre-colon text actually is a namespace if ( $test_ns !== false ) { $ns_num = $test_ns; $title = substr( $pageName, $colonPosition+1 ); } } - - return Title::makeTitle( $ns_num, $title ); + + return \Title::makeTitle( $ns_num, $title ); //return (object)array("ns_num"=>$ns_num, "title"=>$title); - + } - + // returns number of namespace (can be zero) or false. Use ===. static function getNamespaceNumber ( $ns ) { global $wgCanonicalNamespaceNames; - + foreach ( $wgCanonicalNamespaceNames as $i => $text ) { if (preg_match("/$ns/i", $text)) { return $i; } } - + return false; // if $ns not found above, does not exist } - + static function getPageWatchers ($ns, $title) { - + // code adapted from Extension:WhoIsWatching $dbr = wfGetDB( DB_SLAVE ); $watchingUserIDs = array(); - - + + $res = $dbr->select( 'watchlist', - 'wl_user', + 'wl_user', array('wl_namespace'=>$ns, 'wl_title'=>$title), __METHOD__ ); @@ -179,6 +138,7 @@ static function getPageWatchers ($ns, $title) { } return $watchingUserIDs; - + } -} \ No newline at end of file + +} diff --git a/includes/Setup.php b/includes/Setup.php new file mode 100644 index 0000000..7bd700b --- /dev/null +++ b/includes/Setup.php @@ -0,0 +1,30 @@ +setupParserFunction(); + + // always return true + return true; + + } + +}