You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.6 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
require_once dirname(__DIR__, 3) . '/maintenance/Maintenance.php';
require_once dirname(__DIR__) . '/includes/Hanzi2Pinyin.php';
require_once dirname(__DIR__) . '/includes/Utils.php';
use LatinizeUrl\Hanzi2Pinyin;
use LatinizeUrl\Utils;
/**
* Maintenance script to normalize double-byte Latin UTF-8 characters.
*
* @ingroup Maintenance
*/
class UpdateLatinizeUrl extends Maintenance {
public function __construct() {
parent::__construct();
$this->addDescription( '更新所有页面的拼音url' );
$this->addArg( 'pairfile', '生成新旧网址对', false );
$this->addOption( 'force', '强制更新所有页面的url除了自定义url的页面' );
}
public function getDbType() {
return Maintenance::DB_ADMIN;
}
public function execute() {
global $wgLatinizeUrlConfig;
$force = $this->hasOption( 'force' );
$outputFileName = $this->getArg( 0 );
$outputFile = false;
if($outputFileName){
$outputFile = fopen($outputFileName, 'w');
}
$dbw = $this->getDB( DB_MASTER );
if ( $dbw->getType() !== 'mysql' ) {
$this->fatalError( "This change is only needed on MySQL, quitting.\n" );
}
$convertor = new Hanzi2Pinyin($wgLatinizeUrlConfig);
$res = $this->findRows( $dbw );
foreach($res as $one){
$title = $one->page_title;
$isCustom = boolval(intval($dbw->selectField('url_slug', 'is_custom', ['title' => $title], __METHOD__)));
if(!$force && !$isCustom && Utils::titleSlugExists($title)) continue;
$pinyin = $convertor->parse($title);
$slug = $convertor->pinyin2String($pinyin);
echo $title . ' -> ' . $slug . PHP_EOL;
if($outputFile){
$pair = [$this->getFullUrl($title), $this->getFullUrl($slug)];
fwrite($outputFile, implode(' ', $pair) . "\r\n");
}
Utils::replaceTitleSlugMap($title, $slug, $pinyin);
}
fclose($outputFile);
$this->output( "Done\n" );
}
public function searchIndexUpdateCallback( $dbw, $row ) {
return $this->updateSearchIndexForPage( $dbw, $row->si_page );
}
private function getFullUrl($pageName){
global $wgServer, $wgArticlePath, $wgUsePathInfo;
$pageName = implode("/", array_map("urlencode", explode("/", $pageName)));
if($wgUsePathInfo){
return $wgServer . str_replace('$1', $pageName, $wgArticlePath);
} else {
return $wgServer . '/index.php?title=' . $pageName;
}
}
private function findRows( $dbi ) {
return $dbi->select('page', ['page_title'], [
'page_namespace' => NS_MAIN,
], __METHOD__, []);
}
}
$maintClass = UpdateLatinizeUrl::class;
require_once RUN_MAINTENANCE_IF_MAIN;