From fc7369835258467bf97eb64f184b93691f9a9fd5 Mon Sep 17 00:00:00 2001 From: Yaco Date: Thu, 4 Jun 2020 11:01:00 -0300 Subject: first commit --- bin/wiki/initReevoClass.php | 210 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 210 insertions(+) create mode 100644 bin/wiki/initReevoClass.php (limited to 'bin/wiki/initReevoClass.php') diff --git a/bin/wiki/initReevoClass.php b/bin/wiki/initReevoClass.php new file mode 100644 index 00000000..ee67ded7 --- /dev/null +++ b/bin/wiki/initReevoClass.php @@ -0,0 +1,210 @@ +mDescription = "Init Reevo pages"; + $this->addArg( 'namespace', 'El namspace especifico del cual generar las pagínas. Tambien puede pasarse un nombre de página especifico, ej: Portada.mw', false ); + $this->addOption ( 'folder', 'Importa las paginas de las carpeta especificada', false , false); + $this->addOption ( 'setReevoHomePage', "Cambia la portada", false, false ); + $this->addOption ( 'force', "Fuerza la edición cuando las páginas ya existan", false, false ); + $this->addOption ( 'int', "Usar solo páginas internacionales, de la carpeta 'int'", false, false ); + } + + protected function getUpdateKey() { + return 'initialise_Reevo_Pages'; + } + + protected function updateSkippedMessage() { + return 'Reevo pages are allready setup'; + } + + public function execute() { + global $wgContLang; + + $setReevoHomePage = $this->getOption ( 'setReevoHomePage' ); + $force = $this->getOption ( 'force' ) ? true : false; + + if ($this->getOption ( 'folder' )) { + echo 'Voy a importar de la carpeta: '. $this->getOption ( 'folder' ); + $lang = $this->getOption ( 'folder' ); + } else { + $lang = $this->getOption ( 'int' ) ? 'int' : $wgContLang->getCode(); + } + + + $homePageFile = [ + 'es' => 'Portada.md', + 'en' => 'Main_Page.md', + 'int' => 'Portada.md' + ]; + + $pagelist = $this->getPageListToCreate ($lang); + + echo "Setting Up Reevo pages ...\n"; + + + if ($setReevoHomePage) { + echo "Setting wiki home page $setReevoHomePage\n"; + + $ret = Title::newMainPage(); + $pageTitle = $ret->getText(); + $title = $this->getPageName ( $pageTitle ); + $content = $this->getPageContent ( $homePageFile[$lang], $lang); + $this->createPage ( $title, $content, true); + + } else { + echo "No Setting wiki home page\n"; + } + + foreach ( $pagelist as $page ) { + if ($page == $homePageFile) { + continue; + } + $title = $this->getPageName ( $page ); + $content = $this->getPageContent ($page, $lang); + $this->createPage ( $title, $content , $force); + + $t = Title::newFromText( $title ); + $protection = "sysop"; + $user = User::newSystemUser( 'Admin', [ 'steal' => true ] ); + $cascade = ''; + $reason = 'Se protege por seguridad'; + + $restrictions = []; + foreach ( $t->getRestrictionTypes() as $type ) { + $restrictions[$type] = $protection; + } + + $page = WikiPage::factory( $t ); + $status = $page->doUpdateRestrictions( $restrictions, [], $cascade, $reason, $user ); + } + } + + /** + * Get a WikiPage object from a title string, if possible. + * + * @param string $titleName + * @param bool|string $load + * Whether load the object's state from the database: + * - false: don't load (if the pageid is given, it will still be loaded) + * - 'fromdb': load from a slave database + * - 'fromdbmaster': load from the master database + * @return WikiPage + */ + protected function getPage($titleName) { + $titleObj = Title::newFromText ( $titleName ); + if (! $titleObj || $titleObj->isExternal ()) { + trigger_error ( 'Fail to get title ' . $titleName, E_USER_WARNI ); + return false; + } + if (! $titleObj->canExist ()) { + trigger_error ( 'Title cannot be created ' . $titleName, E_USER_WARNING ); + return false; + } + $pageObj = WikiPage::factory ( $titleObj ); + + return $pageObj; + } + protected function getAdminUser() { + // get Admin user : (take the first user created) + $dbr = wfGetDB ( DB_SLAVE ); + $res = $dbr->select ( 'user', User::selectFields (), array (), __METHOD__, array ( + 'LIMIT' => 1, + 'ORDER BY' => 'user_id' + ) ); + $users = UserArray::newFromResult ( $res ); + $user = $users->current (); + + return $user; + } + protected function createPage($pageName, $text, $force = false) { + $wikipage = $this->getPage ( $pageName ); + + if ($wikipage->exists () && ! $force) { + echo "page $pageName allready exists.\n"; + return false; + } + + $user = $this->getAdminUser (); + + $content = ContentHandler::makeContent( $text, $wikipage->getTitle() ); + $result = $wikipage->doEditContent( $content, 'Actualizada automaticamente con versión de repositorio', $flags = 0, $baseRevId = false, $user ); + + if ($result->isOK ()) { + echo "page $pageName successfully created.\n"; + return true; + } else { + echo $result->getWikiText (); + } + + return false; + } + protected function getPageName($page) { + // $page = str_replace ( 'Formulario_', 'Formulario:', $page ); + // $page = str_replace ( 'Propiedad_', 'Propiedad:', $page ); + // if (strpos($page,'Template_') == 0) { + // $page = str_replace ( 'Plantilla_', 'Plantilla:', $page ); + // } + // $page = str_replace ( 'Modulo_', 'Modulo:', $page ); + // $page = str_replace ( 'Categoría_', 'Categoría:', $page ); + // $page = str_replace ( 'MediaWiki_', 'Mediawiki:', $page ); + // $page = str_replace ( 'Widget_', 'Widget:', $page ); + // $page = str_replace ( 'Ayuda_', 'Ayuda:', $page ); + // $page = str_replace ( 'Reevo_', 'Reevo:', $page ); + $page = str_replace ( '_', ' ', $page ); + $page = str_replace ( '~', '/', $page ); + $page = str_replace ( '.mw', '', $page ); + + return $page; + } + protected function getPagesDirs($lang) { + return [ + __DIR__ . '/reevoPages/' . $lang + ]; + } + protected function getPageContent($page, $lang = 'int') { + $dirs = $this->getPagesDirs($lang); + + foreach ($dirs as $dir) { + if (file_exists($dir . '/' . $page)) { + return file_get_contents ( $dir . '/' . $page ); + } + } + + throw new Exception('File not found : ' . $page); + } + protected function getPageListToCreate( $lang) { + + $result = [ ]; + + $dirs = $this->getPagesDirs($lang); + foreach ($dirs as $dir) { + $files = scandir ( $dir ); + foreach ( $files as $file ) { + if (preg_match ( '/^([a-zA-Z_0-9\-áéíóúñÁÉÍÓÚÑ~:’()])+\.mw$/', $file )) { + $namespace = $this->getArg( 0 ); + if ($namespace) { + if(strpos($file, $namespace) === 0) { + // echo "$file\n"; + $result[$file] = $file; + } + } else { + $result[$file] = $file; + } + } + } + } + print_r($result); + return $result; + } +} -- cgit v1.2.1