Legoktm has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/154441

Change subject: [WIP] Script to clean up manual user global.js/css pages
......................................................................

[WIP] Script to clean up manual user global.js/css pages

Bug: 68933
Change-Id: I4b3218ccbbc7754a89846f5fec4d288d86ed46bf
---
A removeOldManualUserPages.php
1 file changed, 129 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/GlobalCssJs 
refs/changes/41/154441/1

diff --git a/removeOldManualUserPages.php b/removeOldManualUserPages.php
new file mode 100644
index 0000000..d8e0504
--- /dev/null
+++ b/removeOldManualUserPages.php
@@ -0,0 +1,129 @@
+<?php
+
+$IP = getenv( 'MW_INSTALL_PATH' );
+if ( $IP === false ) {
+       $IP = __DIR__ . '/../../..';
+}
+require_once( "$IP/maintenance/Maintenance.php" );
+
+/**
+ * Script to remove manually created common.js and common.css pages
+ * by users. You should run this script on every wiki where the user
+ * has an account.
+ */
+class RemoveOldManualUserPages extends Maintenance {
+
+       public function execute() {
+               $userName = $this->getOption( 'user' );
+               $user = User::newFromName( $userName );
+               if ( !class_exists( 'GlobalCssJsHooks' ) ) {
+                       $this->error( 'The GlobalCssJs extension is not enabled 
on this wiki.', 1 );
+               }
+
+               if ( !GlobalCssJsHooks::loadForUser( $user ) ) {
+                       $this->output( "$userName does not load global modules 
on this wiki." );
+                       return;
+               }
+
+               $skins = array_keys( Skin::getSkinNames() );
+               $skins[] = 'common';
+               foreach ( $skins as $name ) {
+                       $this->removeJS( $user, $name );
+                       $this->removeCSS( $user, $name );
+               }
+       }
+
+       /**
+        * Generic checks to see if we should work on a title.
+        *
+        * @param Title $title
+        * @param Revision $rev
+        * @return bool
+        */
+       private function checkTitle( Title $title, Revision $rev ) {
+               if ( !$title->exists() ) {
+                       $this->output( "{$title->getPrefixedText()} does not 
exist on this wiki." );
+                       return false;
+               }
+
+
+               if ( $title->getPreviousRevisionID( $rev->getId() ) !== false ) 
{
+                       $this->output( "{$title->getPrefixedText()} has more 
than one revision, skipping." );
+                       return false;
+               }
+
+               return true;
+       }
+
+       /**
+        * Returns the domain name of the central wiki
+        * escaped to use in a regex.
+        *
+        * @return string
+        */
+       private function getCentralWikiDomain() {
+               global $wgGlobalCssJsConfig;
+               $rl = new ResourceLoader;
+               $sources = $rl->getSources();
+               // Use api.php instead of load.php because it's more likely to 
be on the same domain
+               $api = $sources[$wgGlobalCssJsConfig['source']]['apiScript'];
+               $parsed = wfParseUrl( $api );
+               return preg_quote( $parsed['host'] );
+       }
+
+       private function deletePage( Title $title, $reason ) {
+               $page = WikiPage::factory( $title );
+               $user = User::newFromName( 'GlobalCssJs migration script' );
+               $errors = array();
+               $page->doDeleteArticleReal( wfMessage( 
$reason)->inContentLanguage()->text(), false, 0, true, $errors, $user );
+               $this->output( "{$title->getPrefixedText()} was deleted." );
+       }
+
+       private function removeCSS( User $user, $skin ) {
+               $userName = $user->getName();
+               $title = $user->getUserPage()->getSubpage( $skin . '.js' );
+               $rev = Revision::newFromTitle( $title );
+               if ( !$this->checkTitle( $title, $rev ) ) {
+                       return;
+               }
+
+               /** @var CssContent $content */
+               $content = $rev->getContent();
+               $text = trim( $content->getNativeData() );
+               $domain = $this->getCentralWikiDomain();
+               preg_match( "/@import 
url\('(https?:)?\/\/$domain\/w\/index\/php?title=User:$userName\/global\.css&action=raw&ctype=text\/css'\);/",
 $text, $matches );
+               if ( $matches[0] !== $text ) {
+                       $this->output( "{$title->getPrefixedText()} did not 
match the specified regular expression. Skipping." );
+                       return;
+               }
+
+               // Delete!
+               $this->deletePage( $title, 'globalcssjs-delete-css' );
+       }
+
+       private function removeJS( User $user, $skin ) {
+               $userName = $user->getName();
+               $title = $user->getUserPage()->getSubpage( $skin . '.css' );
+               $rev = Revision::newFromTitle( $title );
+               if ( !$this->checkTitle( $title, $rev ) ) {
+                       return;
+               }
+
+               /** @var JavaScriptContent $content */
+               $content = $rev->getContent();
+               $text = trim( $content->getNativeData() );
+               $domain = $this->getCentralWikiDomain();
+               preg_match( 
"/(mw\.loader\.load|importScriptURI)\('(https?:)?\/\/$domain\/w\/index\.php\?title=User:$userName\/global\.js&action=raw&ctype=text\/javascript'\);/",
 $text, $matches );
+               if ( $matches[0] !== $text ) {
+                       $this->output( "{$title->getPrefixedText()} did not 
match the specified regular expression. Skipping." );
+                       return;
+               }
+
+               // Delete!
+               $this->deletePage( $title, 'globalcssjs-delete-css' );
+       }
+
+}
+
+$maintClass = "RemoveOldManualUserPages";
+require_once( RUN_MAINTENANCE_IF_MAIN );

-- 
To view, visit https://gerrit.wikimedia.org/r/154441
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I4b3218ccbbc7754a89846f5fec4d288d86ed46bf
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/GlobalCssJs
Gerrit-Branch: master
Gerrit-Owner: Legoktm <legoktm.wikipe...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to