PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wwwroot/mediawiki/maintenance/namespaceDupes.php

https://github.com/spring/spring-website
PHP | 332 lines | 199 code | 31 blank | 102 comment | 29 complexity | a26e3728e2ae901e435efda0b46d10a5 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, LGPL-3.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * Check for articles to fix after adding/deleting namespaces
  4. *
  5. * Copyright Š 2005-2007 Brion Vibber <brion@pobox.com>
  6. * https://www.mediawiki.org/
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. * http://www.gnu.org/copyleft/gpl.html
  22. *
  23. * @file
  24. * @ingroup Maintenance
  25. */
  26. require_once __DIR__ . '/Maintenance.php';
  27. /**
  28. * Maintenance script that checks for articles to fix after
  29. * adding/deleting namespaces.
  30. *
  31. * @ingroup Maintenance
  32. */
  33. class NamespaceConflictChecker extends Maintenance {
  34. /**
  35. * @var DatabaseBase
  36. */
  37. protected $db;
  38. public function __construct() {
  39. parent::__construct();
  40. $this->mDescription = "";
  41. $this->addOption( 'fix', 'Attempt to automatically fix errors' );
  42. $this->addOption( 'suffix', "Dupes will be renamed with correct namespace with " .
  43. "<text> appended after the article name", false, true );
  44. $this->addOption( 'prefix', "Do an explicit check for the given title prefix " .
  45. "appended after the article name", false, true );
  46. }
  47. public function execute() {
  48. $this->db = wfGetDB( DB_MASTER );
  49. $fix = $this->hasOption( 'fix' );
  50. $suffix = $this->getOption( 'suffix', '' );
  51. $prefix = $this->getOption( 'prefix', '' );
  52. $key = intval( $this->getOption( 'key', 0 ) );
  53. if ( $prefix ) {
  54. $retval = $this->checkPrefix( $key, $prefix, $fix, $suffix );
  55. } else {
  56. $retval = $this->checkAll( $fix, $suffix );
  57. }
  58. if ( $retval ) {
  59. $this->output( "\nLooks good!\n" );
  60. } else {
  61. $this->output( "\nOh noeees\n" );
  62. }
  63. }
  64. /**
  65. * @todo Document
  66. * @param $fix Boolean: whether or not to fix broken entries
  67. * @param $suffix String: suffix to append to renamed articles
  68. *
  69. * @return bool
  70. */
  71. private function checkAll( $fix, $suffix = '' ) {
  72. global $wgContLang, $wgNamespaceAliases, $wgCapitalLinks;
  73. $spaces = array();
  74. // List interwikis first, so they'll be overridden
  75. // by any conflicting local namespaces.
  76. foreach ( $this->getInterwikiList() as $prefix ) {
  77. $name = $wgContLang->ucfirst( $prefix );
  78. $spaces[$name] = 0;
  79. }
  80. // Now pull in all canonical and alias namespaces...
  81. foreach ( MWNamespace::getCanonicalNamespaces() as $ns => $name ) {
  82. // This includes $wgExtraNamespaces
  83. if ( $name !== '' ) {
  84. $spaces[$name] = $ns;
  85. }
  86. }
  87. foreach ( $wgContLang->getNamespaces() as $ns => $name ) {
  88. if ( $name !== '' ) {
  89. $spaces[$name] = $ns;
  90. }
  91. }
  92. foreach ( $wgNamespaceAliases as $name => $ns ) {
  93. $spaces[$name] = $ns;
  94. }
  95. foreach ( $wgContLang->getNamespaceAliases() as $name => $ns ) {
  96. $spaces[$name] = $ns;
  97. }
  98. // We'll need to check for lowercase keys as well,
  99. // since we're doing case-sensitive searches in the db.
  100. foreach ( $spaces as $name => $ns ) {
  101. $moreNames = array();
  102. $moreNames[] = $wgContLang->uc( $name );
  103. $moreNames[] = $wgContLang->ucfirst( $wgContLang->lc( $name ) );
  104. $moreNames[] = $wgContLang->ucwords( $name );
  105. $moreNames[] = $wgContLang->ucwords( $wgContLang->lc( $name ) );
  106. $moreNames[] = $wgContLang->ucwordbreaks( $name );
  107. $moreNames[] = $wgContLang->ucwordbreaks( $wgContLang->lc( $name ) );
  108. if ( !$wgCapitalLinks ) {
  109. foreach ( $moreNames as $altName ) {
  110. $moreNames[] = $wgContLang->lcfirst( $altName );
  111. }
  112. $moreNames[] = $wgContLang->lcfirst( $name );
  113. }
  114. foreach ( array_unique( $moreNames ) as $altName ) {
  115. if ( $altName !== $name ) {
  116. $spaces[$altName] = $ns;
  117. }
  118. }
  119. }
  120. ksort( $spaces );
  121. asort( $spaces );
  122. $ok = true;
  123. foreach ( $spaces as $name => $ns ) {
  124. $ok = $this->checkNamespace( $ns, $name, $fix, $suffix ) && $ok;
  125. }
  126. return $ok;
  127. }
  128. /**
  129. * Get the interwiki list
  130. *
  131. * @return Array
  132. */
  133. private function getInterwikiList() {
  134. $result = Interwiki::getAllPrefixes();
  135. $prefixes = array();
  136. foreach ( $result as $row ) {
  137. $prefixes[] = $row['iw_prefix'];
  138. }
  139. return $prefixes;
  140. }
  141. /**
  142. * @todo Document
  143. * @param $ns Integer: a namespace id
  144. * @param $name String
  145. * @param $fix Boolean: whether to fix broken entries
  146. * @param $suffix String: suffix to append to renamed articles
  147. * @return bool
  148. */
  149. private function checkNamespace( $ns, $name, $fix, $suffix = '' ) {
  150. $conflicts = $this->getConflicts( $ns, $name );
  151. $count = count( $conflicts );
  152. if ( $count == 0 ) {
  153. return true;
  154. }
  155. $ok = true;
  156. foreach ( $conflicts as $row ) {
  157. $resolvable = $this->reportConflict( $row, $suffix );
  158. $ok = $ok && $resolvable;
  159. if ( $fix && ( $resolvable || $suffix != '' ) ) {
  160. $ok = $this->resolveConflict( $row, $resolvable, $suffix ) && $ok;
  161. }
  162. }
  163. return $ok;
  164. }
  165. /**
  166. * @todo Do this for real
  167. * @param $key
  168. * @param $prefix
  169. * @param $fix
  170. * @param $suffix string
  171. * @return bool
  172. */
  173. private function checkPrefix( $key, $prefix, $fix, $suffix = '' ) {
  174. $this->output( "Checking prefix \"$prefix\" vs namespace $key\n" );
  175. return $this->checkNamespace( $key, $prefix, $fix, $suffix );
  176. }
  177. /**
  178. * Find pages in mainspace that have a prefix of the new namespace
  179. * so we know titles that will need migrating
  180. *
  181. * @param $ns Integer: namespace id (id for new namespace?)
  182. * @param $name String: prefix that is being made a namespace
  183. *
  184. * @return array
  185. */
  186. private function getConflicts( $ns, $name ) {
  187. $page = 'page';
  188. $table = $this->db->tableName( $page );
  189. $prefix = $this->db->strencode( $name );
  190. $encNamespace = $this->db->addQuotes( $ns );
  191. $titleSql = "TRIM(LEADING '$prefix:' FROM {$page}_title)";
  192. if ( $ns == 0 ) {
  193. // An interwiki; try an alternate encoding with '-' for ':'
  194. $titleSql = $this->db->buildConcat( array( "'$prefix-'", $titleSql ) );
  195. }
  196. $sql = "SELECT {$page}_id AS id,
  197. {$page}_title AS oldtitle,
  198. $encNamespace + {$page}_namespace AS namespace,
  199. $titleSql AS title,
  200. {$page}_namespace AS oldnamespace
  201. FROM {$table}
  202. WHERE ( {$page}_namespace=0 OR {$page}_namespace=1 )
  203. AND {$page}_title " . $this->db->buildLike( $name . ':', $this->db->anyString() );
  204. $result = $this->db->query( $sql, __METHOD__ );
  205. $set = array();
  206. foreach ( $result as $row ) {
  207. $set[] = $row;
  208. }
  209. return $set;
  210. }
  211. /**
  212. * Report any conflicts we find
  213. *
  214. * @return bool
  215. */
  216. private function reportConflict( $row, $suffix ) {
  217. $newTitle = Title::makeTitleSafe( $row->namespace, $row->title );
  218. if ( is_null( $newTitle ) || !$newTitle->canExist() ) {
  219. // Title is also an illegal title...
  220. // For the moment we'll let these slide to cleanupTitles or whoever.
  221. $this->output( sprintf( "... %d (%d,\"%s\")\n",
  222. $row->id,
  223. $row->oldnamespace,
  224. $row->oldtitle ) );
  225. $this->output( "... *** cannot resolve automatically; illegal title ***\n" );
  226. return false;
  227. }
  228. $this->output( sprintf( "... %d (%d,\"%s\") -> (%d,\"%s\") [[%s]]\n",
  229. $row->id,
  230. $row->oldnamespace,
  231. $row->oldtitle,
  232. $newTitle->getNamespace(),
  233. $newTitle->getDBkey(),
  234. $newTitle->getPrefixedText() ) );
  235. $id = $newTitle->getArticleID();
  236. if ( $id ) {
  237. $this->output( "... *** cannot resolve automatically; page exists with ID $id ***\n" );
  238. return false;
  239. } else {
  240. return true;
  241. }
  242. }
  243. /**
  244. * Resolve any conflicts
  245. *
  246. * @param $row Object: row from the page table to fix
  247. * @param $resolvable Boolean
  248. * @param $suffix String: suffix to append to the fixed page
  249. * @return bool
  250. */
  251. private function resolveConflict( $row, $resolvable, $suffix ) {
  252. if ( !$resolvable ) {
  253. $this->output( "... *** old title {$row->title}\n" );
  254. while ( true ) {
  255. $row->title .= $suffix;
  256. $this->output( "... *** new title {$row->title}\n" );
  257. $title = Title::makeTitleSafe( $row->namespace, $row->title );
  258. if ( !$title ) {
  259. $this->output( "... !!! invalid title\n" );
  260. return false;
  261. }
  262. $id = $title->getArticleID();
  263. if ( $id ) {
  264. $this->output( "... *** page exists with ID $id ***\n" );
  265. } else {
  266. break;
  267. }
  268. }
  269. $this->output( "... *** using suffixed form [[" . $title->getPrefixedText() . "]] ***\n" );
  270. }
  271. $this->resolveConflictOn( $row, 'page', 'page' );
  272. return true;
  273. }
  274. /**
  275. * Resolve a given conflict
  276. *
  277. * @param $row Object: row from the old broken entry
  278. * @param $table String: table to update
  279. * @param $prefix String: prefix for column name, like page or ar
  280. * @return bool
  281. */
  282. private function resolveConflictOn( $row, $table, $prefix ) {
  283. $this->output( "... resolving on $table... " );
  284. $newTitle = Title::makeTitleSafe( $row->namespace, $row->title );
  285. $this->db->update( $table,
  286. array(
  287. "{$prefix}_namespace" => $newTitle->getNamespace(),
  288. "{$prefix}_title" => $newTitle->getDBkey(),
  289. ),
  290. array(
  291. // "{$prefix}_namespace" => 0,
  292. // "{$prefix}_title" => $row->oldtitle,
  293. "{$prefix}_id" => $row->id,
  294. ),
  295. __METHOD__ );
  296. $this->output( "ok.\n" );
  297. return true;
  298. }
  299. }
  300. $maintClass = "NamespaceConflictChecker";
  301. require_once RUN_MAINTENANCE_IF_MAIN;