Index: branches/RL2/extensions/Gadgets/Gadgets.php |
— | — | @@ -92,7 +92,9 @@ |
93 | 93 | 'gadgets-definition-delete' |
94 | 94 | ) ); |
95 | 95 | |
| 96 | +$wgHooks['ArticleDeleteComplete'][] = 'GadgetHooks::articleDeleteComplete'; |
96 | 97 | $wgHooks['ArticleSaveComplete'][] = 'GadgetHooks::articleSaveComplete'; |
| 98 | +$wgHooks['ArticleUndelete'][] = 'GadgetHooks::articleUndelete'; |
97 | 99 | $wgHooks['BeforePageDisplay'][] = 'GadgetHooks::beforePageDisplay'; |
98 | 100 | $wgHooks['CanonicalNamespaces'][] = 'GadgetHooks::canonicalNamespaces'; |
99 | 101 | $wgHooks['GetPreferences'][] = 'GadgetHooks::getPreferences'; |
Index: branches/RL2/extensions/Gadgets/GadgetHooks.php |
— | — | @@ -15,20 +15,103 @@ |
16 | 16 | class GadgetHooks { |
17 | 17 | |
18 | 18 | /** |
| 19 | + * ArticleDeleteComplete hook handler. |
| 20 | + * |
| 21 | + * @param $article Article |
| 22 | + * @param $user User |
| 23 | + * @param $reason String: Deletion summary |
| 24 | + * @param $id Int: Page ID |
| 25 | + */ |
| 26 | + public static function articleDeleteComplete( $article, $user, $reason, $id ) { |
| 27 | + // FIXME: AARGH, duplication, refactor this |
| 28 | + $title = $article->getTitle(); |
| 29 | + $name = $title->getText(); |
| 30 | + // Check that the deletion is in the Gadget definition: namespace and that the name ends in .js |
| 31 | + if ( $title->getNamespace() !== NS_GADGET_DEFINITION || !preg_match( '!\.js$!u', $name ) ) { |
| 32 | + return true; |
| 33 | + } |
| 34 | + // Trim .js from the page name to obtain the gadget name |
| 35 | + $name = substr( $name, 0, -3 ); |
| 36 | + |
| 37 | + $repo = new LocalGadgetRepo( array() ); |
| 38 | + $repo->deleteGadget( $name ); |
| 39 | + // deleteGadget() may return an error if the Gadget didn't exist, but we don't care here |
| 40 | + return true; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
19 | 44 | * ArticleSaveComplete hook handler. |
20 | 45 | * |
21 | 46 | * @param $article Article |
22 | 47 | * @param $user User |
23 | 48 | * @param $text String: New page text |
| 49 | + * @param $summary String: Edit summary |
| 50 | + * @param $isMinor Bool: Whether this was a minor edit |
| 51 | + * @param $isWatch unused |
| 52 | + * @param $section unused |
| 53 | + * @param $flags: Int: Bitmap of flags passed to WikiPage::doEdit() |
| 54 | + * @param $revision: Revision object for the new revision |
24 | 55 | */ |
25 | | - public static function articleSaveComplete( $article, $user, $text ) { |
26 | | - //update cache if MediaWiki:Gadgets-definition was edited |
27 | | - $title = $article->mTitle; |
28 | | - if( $title->getNamespace() == NS_MEDIAWIKI && $title->getText() == 'Gadgets-definition' ) { |
29 | | - Gadget::loadStructuredList( $text ); |
| 56 | + public static function articleSaveComplete( $article, $user, $text, $summary, $isMinor, |
| 57 | + $isWatch, $section, $flags, $revision ) |
| 58 | + { |
| 59 | + $title = $article->getTitle(); |
| 60 | + $name = $title->getText(); |
| 61 | + // Check that the edit is in the Gadget definition: namespace, that the name ends in .js |
| 62 | + // and that $revision isn't null (this happens for a no-op edit) |
| 63 | + if ( $title->getNamespace() !== NS_GADGET_DEFINITION || !preg_match( '!\.js$!u', $name ) || !$revision ) { |
| 64 | + return true; |
30 | 65 | } |
| 66 | + // Trim .js from the page name to obtain the gadget name |
| 67 | + $name = substr( $name, 0, -3 ); |
| 68 | + |
| 69 | + $previousRev = $revision->getPrevious(); |
| 70 | + $prevTs = $previousRev instanceof Revision ? $previousRev->getTimestamp() : wfTimestampNow(); |
| 71 | + |
| 72 | + // Update the database entry for this gadget |
| 73 | + $repo = new LocalGadgetRepo( array() ); |
| 74 | + // TODO: Timestamp in the constructor is ugly |
| 75 | + $gadget = new Gadget( $name, $repo, $text, $prevTs ); |
| 76 | + $repo->modifyGadget( $gadget, $revision->getTimestamp() ); |
| 77 | + |
| 78 | + // modifyGadget() returns a Status object with an error if there was a conflict, |
| 79 | + // but we don't care. If a conflict occurred, that must be because a newer edit's |
| 80 | + // DB update occurred before ours, in which case the right thing to do is to occu |
| 81 | + |
31 | 82 | return true; |
32 | 83 | } |
| 84 | + |
| 85 | + public static function articleUndelete( $title, $created, $comment ) { |
| 86 | + // FIXME: AARGH, duplication, refactor this |
| 87 | + $name = $title->getText(); |
| 88 | + // Check that the deletion is in the Gadget definition: namespace and that the name ends in .js |
| 89 | + if ( $title->getNamespace() !== NS_GADGET_DEFINITION || !preg_match( '!\.js$!u', $name ) ) { |
| 90 | + return true; |
| 91 | + } |
| 92 | + // Trim .js from the page name to obtain the gadget name |
| 93 | + $name = substr( $name, 0, -3 ); |
| 94 | + |
| 95 | + // Check whether this undeletion changed the latest revision of the page, by comparing |
| 96 | + // the timestamp of the latest revision with the timestamp in the DB |
| 97 | + $repo = new LocalGadgetRepo( array() ); |
| 98 | + $gadget = $repo->getGadget( $name ); |
| 99 | + $gadgetTS = $gadget ? $gadget->getTimestamp() : 0; |
| 100 | + |
| 101 | + $rev = Revision::newFromTitle( $title ); |
| 102 | + if ( wfTimestamp( TS_MW, $rev->getTimestamp() ) === |
| 103 | + wfTimestamp( TS_MW, $gadgetTS ) ) { |
| 104 | + // The latest rev didn't change. Someone must've undeleted an older revision |
| 105 | + return true; |
| 106 | + } |
| 107 | + |
| 108 | + // Update the database entry for this gadget |
| 109 | + $newGadget = new Gadget( $name, $repo, $rev->getRawText(), $gadgetTS ); |
| 110 | + $repo->modifyGadget( $newGadget, $rev->getTimestamp() ); |
| 111 | + |
| 112 | + // modifyGadget() returns a Status object with an error if there was a conflict, |
| 113 | + // but we do't care, see similar comment in articleSaveComplete() |
| 114 | + return true; |
| 115 | + } |
33 | 116 | |
34 | 117 | /** |
35 | 118 | * GetPreferences hook handler. |
— | — | @@ -162,7 +245,9 @@ |
163 | 246 | } |
164 | 247 | |
165 | 248 | public static function titleIsCssOrJsPage( $title, &$result ) { |
166 | | - if ( $title->getNamespace() == NS_GADGET || $title->getNamespace() == NS_GADGET_DEFINITION ) { |
| 249 | + if ( ( $title->getNamespace() == NS_GADGET || $title->getNamespace() == NS_GADGET_DEFINITION ) && |
| 250 | + preg_match( '!\.(css|js)$!u', $title->getText() ) ) |
| 251 | + { |
167 | 252 | $result = true; |
168 | 253 | } |
169 | 254 | return true; |