summaryrefslogtreecommitdiff
path: root/bin/wiki/initReevoClass.php
blob: ee67ded76e560c68c4db23d16214a21da68eca76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
# ----
# Copyright (C) 2013-2020 - Reevo Project (http://reevo.org)
# License: Affero GPL version 3 - http://www.gnu.org/licenses/agpl.html
# ES: Script usado para importar páginas de REEVO como formularios, propiedades, etc.
# Creditos: Basado en WikiFab: https://github.com/Wikifab/wikifab-main/tree/master/maintenance
# ----

require_once '../../www/wiki/maintenance/Maintenance.php';

class InitReevo extends Maintenance {

	public function __construct() {
		parent::__construct ();
		$this->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;
	}
}